NXopen C++ 装配体部件遍历 体积质量计算 NewMassProperties

打开装配体,逐一遍历部件测量体积,最后计算出装配体的总的体积和质量 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);

在这里插入图片描述

  • 40
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值