打开装配体,逐一遍历部件测量体积,最后计算出装配体的总的体积和质量 NX1953+VS2019
//1、模板文件添加头文件*
#include <NXOpen/UnitCollection.hxx>
#include <NXOpen/ScCollectorCollection.hxx>
#include <NXOpen/Unit.hxx>
#include <NXOpen/BodyDumbRule.hxx>
#include <NXOpen/MeasureBodies.hxx>
#include <NXOpen/ScCollectorCollection.hxx>
#include <NXOpen/ScRuleFactory.hxx>
//**2、主程序**
NXOpen::Session* theSession = NXOpen::Session::GetSession();
theSession->ListingWindow()->Open();
NXOpen::BasePart* basePart1;
NXOpen::PartLoadStatus* partLoadStatus1;
basePart1 = theSession->Parts()->OpenActiveDisplay("D:\\CSDN\\CreatFile\\_asm1.prt", NXOpen::DisplayPartOptionAllowAdditional, &partLoadStatus1);//打开装配体模型
NXOpen::Part* AssemPart(theSession->Parts()->Work());
NXOpen::Part* displayPart(theSession->Parts()->Display());
delete partLoadStatus1;
theSession->ApplicationSwitchImmediate("UG_APP_MODELING");
Assemblies::ComponentAssembly* compAssy = AssemPart->ComponentAssembly();
Assemblies::Component* rootcomponent = compAssy->RootComponent();//获取根部件
std::vector< NXOpen::Assemblies::Component* > childcomponents;
childcomponents = rootcomponent->GetChildren();//获取子部件
char msg[256];
double TotalMass = 0.0;
double TotalVolume = 0.0;
for (int i = 0; i < childcomponents.size(); i++)
{
NXOpen::Assemblies::Component* componenti = childcomponents[i];
if (componenti->IsOccurrence() == 1)//判断是否为事例
{
NXOpen::INXObject* Parti = componenti->Prototype();//通过事例获取相应零件几何模型part
NXOpen::Part* part1(dynamic_cast<NXOpen::Part*>(Parti));
NXOpen::PartLoadStatus* partLoadStatus2;
partLoadStatus2 = part1->LoadThisPartFully();//将几何模型载入,相当于变换到零件模型并设置其为工作部件
//必须载入,否则装配环境下无法测量机和实体
NXOpen::BodyCollection* bodys = part1->Bodies();//遍历零件part中几何实体
std::vector<NXOpen::Body*> BodyVecotor;
for (NXOpen::BodyCollection::iterator itebody = bodys->begin(); itebody != bodys->end(); itebody++)
{
Body* bodyi = (*itebody);
BodyVecotor.push_back(bodyi);
}
std::vector<Unit*> massUnits1(5);//定义质量属性的测量单位
Unit* unit1(dynamic_cast<Unit*>(part1->UnitCollection()->FindObject("SquareMilliMeter")));
massUnits1[0] = unit1;
Unit* unit2(dynamic_cast<Unit*>(part1->UnitCollection()->FindObject("CubicMilliMeter")));
massUnits1[1] = unit2;
Unit* unit3(dynamic_cast<Unit*>(part1->UnitCollection()->FindObject("Kilogram")));
massUnits1[2] = unit3;
Unit* unit4(dynamic_cast<Unit*>(part1->UnitCollection()->FindObject("MilliMeter")));
massUnits1[3] = unit4;
Unit* unit5(dynamic_cast<Unit*>(part1->UnitCollection()->FindObject("Newton")));
massUnits1[4] = unit5;
NXOpen::ScCollector* scCollector1;//定义测量体收集器,存放测量体
scCollector1 = part1->ScCollectors()->CreateCollector();
std::vector<NXOpen::Body*> bodies1(1);
bodies1[0] = BodyVecotor[0];
NXOpen::BodyDumbRule* bodyDumbRule1;
bodyDumbRule1 = part1->ScRuleFactory()->CreateRuleBodyDumb(bodies1);
std::vector<NXOpen::SelectionIntentRule*> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector1->ReplaceRules(rules1, false);
NXOpen::MeasureBodies* MeasureBodies1;
MeasureBodies1 = part1->MeasureManager()->NewMassProperties(massUnits1, 0.99, scCollector1);
double BlockVolume = MeasureBodies1->Volume();//获取体积
double BlockMass = MeasureBodies1->Mass();//获取质量
TotalVolume = TotalVolume + BlockVolume;
TotalMass = TotalMass + BlockMass;
}
}
sprintf(msg, "装配体总的体积:%f", TotalVolume);
theSession->ListingWindow()->WriteLine(msg);
sprintf(msg, "装配体总的质量:%f", TotalMass);
theSession->ListingWindow()->WriteLine(msg);