NXopen NewMassProperties 测量体质量属性,包括面积、体积、质心坐标等

创建一个块,获取其面积、体积、质心坐标等

//1、模板文件添加头文件*
#include <NXOpen/Features_BlockFeatureBuilder.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/PointCollection.hxx>
#include <NXOpen/MeasureBodies.hxx>
#include <NXOpen/UnitCollection.hxx>
#include <NXOpen/Unit.hxx>
#include <NXOpen/ScCollectorCollection.hxx>
#include <NXOpen/MeasureManager.hxx>
#include <NXOpen/ScCollector.hxx>
#include <NXOpen/ScCollectorCollection.hxx>
#include <NXOpen/ScRuleFactory.hxx>
#include <NXOpen/BodyDumbRule.hxx>
//**2、函数声明**
NXOpen::Features::Feature* CreateBlockFeature(NXOpen::Point3d originPoint, double Length, double Width, double Height, NXOpen::Part* workPart);//创建块函数定义
//**3、函数定义**
NXOpen::Features::Feature* MyClass::CreateBlockFeature(NXOpen::Point3d originPoint, double Length, double Width, double Height, NXOpen::Part* workPart)
{
	NXOpen::Features::Feature* nullNXOpen_Features_Feature(NULL);
	NXOpen::Features::BlockFeatureBuilder* blockFeatureBuilder1;
	blockFeatureBuilder1 = workPart->Features()->CreateBlockFeatureBuilder(nullNXOpen_Features_Feature);
	blockFeatureBuilder1->BooleanOption()->SetType(NXOpen::GeometricUtilities::BooleanOperation::BooleanTypeCreate);
	blockFeatureBuilder1->SetType(NXOpen::Features::BlockFeatureBuilder::TypesOriginAndEdgeLengths);
	char Lengthchar[256];
	sprintf(Lengthchar, "%f", Length);

	char Widthchar[256];
	sprintf(Widthchar, "%f", Width);

	char Heightchar[256];
	sprintf(Heightchar, "%f", Height);

	blockFeatureBuilder1->SetOriginAndLengths(originPoint, Lengthchar, Widthchar, Heightchar);
	NXOpen::Features::Feature* blockfeature;
	blockfeature = blockFeatureBuilder1->CommitFeature();
	return blockfeature;
}
//**4、主程序**
NXOpen::Session* theSession = NXOpen::Session::GetSession();
NXOpen::Part* workPart(theSession->Parts()->Work());
NXOpen::Part* displayPart(theSession->Parts()->Display());

double originX = 0.0;
double originY = 0.0;
double originZ = 0.0;
NXOpen::Point3d originPoint(originX, originY, originZ);//定义块原点
double Length = 200;
double Width = 100;
double Height = 100;//定义块边长
NXOpen::Features::Feature* blockfeature;
blockfeature = CreateBlockFeature(originPoint, Length, Width, Height, workPart);

NXOpen::Body* Blockbody(dynamic_cast<NXOpen::Body*>(workPart->Bodies()->FindObject(blockfeature->JournalIdentifier())));//获取块特征所在体作为体测量对象

std::vector<Unit*> massUnits1(5);//定义质量属性的测量单位
Unit* unit1(dynamic_cast<Unit*>(workPart->UnitCollection()->FindObject("SquareMilliMeter")));
massUnits1[0] = unit1;
Unit* unit2(dynamic_cast<Unit*>(workPart->UnitCollection()->FindObject("CubicMilliMeter")));
massUnits1[1] = unit2;
Unit* unit3(dynamic_cast<Unit*>(workPart->UnitCollection()->FindObject("Kilogram")));
massUnits1[2] = unit3;
Unit* unit4(dynamic_cast<Unit*>(workPart->UnitCollection()->FindObject("MilliMeter")));
massUnits1[3] = unit4;
Unit* unit5(dynamic_cast<Unit*>(workPart->UnitCollection()->FindObject("Newton")));
massUnits1[4] = unit5;

NXOpen::ScCollector* scCollector1;//定义测量体收集器,存放测量体
scCollector1 = workPart->ScCollectors()->CreateCollector();
std::vector<NXOpen::Body*> bodies1(1);
bodies1[0] = Blockbody;
NXOpen::BodyDumbRule* bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies1);

std::vector<NXOpen::SelectionIntentRule*> rules1(1);
rules1[0] = bodyDumbRule1;
scCollector1->ReplaceRules(rules1, false);

NXOpen::MeasureBodies* MeasureBodies1;
MeasureBodies1 = workPart->MeasureManager()->NewMassProperties(massUnits1, 0.99, scCollector1);
char msg[256];
double BlockArea = MeasureBodies1->Area();//获取面积
double BlockVolume = MeasureBodies1->Volume();//获取体积

NXOpen::Point3d Centroidcoordinate;
Centroidcoordinate = MeasureBodies1->Centroid();//获取质心坐标
double BlockMass=MeasureBodies1->Mass();//获取质量
//列表输出各质量属性
sprintf(msg, "Block面积:%f", BlockArea);
theSession->ListingWindow()->WriteLine(msg);

sprintf(msg, "Block体积:%f", BlockVolume);
theSession->ListingWindow()->WriteLine(msg);

sprintf(msg, "Block质心坐标:X %f Y %f Z %f", Centroidcoordinate.X, Centroidcoordinate.Y,Centroidcoordinate.Z);
theSession->ListingWindow()->WriteLine(msg);

sprintf(msg, "Block质量:%f", BlockMass);
theSession->ListingWindow()->WriteLine(msg);

结果如下图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值