NXopen C++面的质心、面积、周长测量 NewFaceProperties NewMassProperties

以一个有界平面为例,通过NewFaceProperties获得该面的周长、面积属性,对该面进行对称加厚处理生成体,通过体NewMassProperties间接获得该面的质心坐标。

//1、模板文件添加头文件*
#include <NXOpen/Features_BoundedPlane.hxx>
#include <NXOpen/Features_BoundedPlaneBuilder.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/Features_AssociativeLineBuilder.hxx>
#include <NXOpen/SelectPoint.hxx>
#include <NXOpen/Features_BaseFeatureCollection.hxx>
#include <NXOpen/CurveCollection.hxx>
#include <NXOpen/PointCollection.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <uf_curve.h>
#include <NXOpen/CurveDumbRule.hxx>
#include <NXOpen/ScRuleFactory.hxx>
#include <NXOpen/MeasureFaces.hxx>
#include <NXOpen/UnitCollection.hxx>
#include <NXOpen/Unit.hxx>
#include <NXOpen/Features_ThickenBuilder.hxx>
#include <NXOpen/FaceDumbRule.hxx>
#include <NXOpen/MeasureBodies.hxx>
//**2、函数声明**
NXOpen::Point* pointcreat(double PTcoordinates[3], NXOpen::Part* part);//根据坐标创建点对象
NXOpen::NXObject* CreatAssociativeLine(NXOpen::Point* starpoint, NXOpen::Point* endpoint, NXOpen::Part* part1);//通过点对象创建关联直线
//**3、函数定义**
NXOpen::NXObject* MyClass::CreatAssociativeLine(NXOpen::Point* starpoint, NXOpen::Point* endpoint, NXOpen::Part* part1)
{
	NXOpen::Features::AssociativeLine* nullNXOpen_Features_AssociativeLine(NULL);
	NXOpen::Features::AssociativeLineBuilder* associativeLineBuilder;
	associativeLineBuilder = part1->BaseFeatures()->CreateAssociativeLineBuilder(nullNXOpen_Features_AssociativeLine);
	NXOpen::Unit* unit;
	unit = associativeLineBuilder->Limits()->StartLimit()->Distance()->Units();
	associativeLineBuilder->SetStartPointOptions(NXOpen::Features::AssociativeLineBuilder::StartOptionPoint);
	associativeLineBuilder->SetEndPointOptions(NXOpen::Features::AssociativeLineBuilder::EndOptionPoint);
	associativeLineBuilder->Limits()->StartLimit()->SetLimitOption(NXOpen::GeometricUtilities::CurveExtendData::LimitOptionsAtPoint);
	associativeLineBuilder->Limits()->EndLimit()->SetLimitOption(NXOpen::GeometricUtilities::CurveExtendData::LimitOptionsAtPoint);
	associativeLineBuilder->StartPoint()->SetValue(starpoint);
	associativeLineBuilder->EndPoint()->SetValue(endpoint);
	NXOpen::NXObject* nXObject;
	nXObject = associativeLineBuilder->Commit();
	associativeLineBuilder->Destroy();
	return nXObject;
}
NXOpen::Point* MyClass::pointcreat(double PTcoordinates[3], NXOpen::Part* part)
{
	NXOpen::Point3d coordinates(PTcoordinates[0], PTcoordinates[1], PTcoordinates[2]);
	NXOpen::Point* point;
	point = part->Points()->CreatePoint(coordinates);
	return point;
}
//**4、主程序**
    NXOpen::Session* theSession = NXOpen::Session::GetSession();
	NXOpen::Part* workPart(theSession->Parts()->Work());
	NXOpen::Part* displayPart(theSession->Parts()->Display());

	double P1coordinates[3] = { 0.0,0.0,0.0 };
	NXOpen::Point* Point1;
	Point1 = pointcreat(P1coordinates, workPart);

	double P2coordinates[3] = {2.0,2.0,0.0 };
	NXOpen::Point* Point2;
	Point2 = pointcreat(P2coordinates, workPart);

	double P3coordinates[3] = { 4.0,0.0,0.0 };
	NXOpen::Point* Point3;
	Point3 = pointcreat(P3coordinates, workPart);

	NXOpen::Line* Line1 = workPart->Curves()->CreateLine(Point1, Point2);
	NXOpen::NXObject* Line1Object;
	Line1Object = CreatAssociativeLine(Point1, Point2, workPart);


	NXOpen::Line* Line2 = workPart->Curves()->CreateLine(Point2, Point3);
	NXOpen::NXObject* Line2Object;
	Line2Object = CreatAssociativeLine(Point2, Point3, workPart);

	NXOpen::Line* Line3 = workPart->Curves()->CreateLine(Point3, Point1);
	NXOpen::NXObject* Line3Object;
	Line3Object = CreatAssociativeLine(Point3, Point1, workPart);//定义有界平面的三条边界直线

	NXOpen::Features::BoundedPlane* nullNXOpen_Features_BoundedPlane(NULL);
	NXOpen::Features::BoundedPlaneBuilder* boundedPlaneBuilder1;//有界平面构造器
	boundedPlaneBuilder1 = workPart->Features()->CreateBoundedPlaneBuilder(nullNXOpen_Features_BoundedPlane);
	boundedPlaneBuilder1->BoundingCurves()->SetAllowedEntityTypes(NXOpen::Section::AllowTypesOnlyCurves);


	std::vector<NXOpen::IBaseCurve*> curves1;
	curves1.push_back(Line1);
	curves1.push_back(Line2);
	curves1.push_back(Line3);
	NXOpen::CurveDumbRule* curveDumbRule1;
	curveDumbRule1 = workPart->ScRuleFactory()->CreateRuleBaseCurveDumb(curves1);

	std::vector<NXOpen::SelectionIntentRule*> rules1(1);
	rules1[0] = curveDumbRule1;
	NXOpen::NXObject* nullNXOpen_NXObject(NULL);
	NXOpen::Point3d helpPoint1;
	boundedPlaneBuilder1->BoundingCurves()->AddToSection(rules1, Line1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen::Section::ModeCreate, false);

	NXOpen::NXObject* nXObject1;
	nXObject1 = boundedPlaneBuilder1->Commit();
	NXOpen::Features::Feature* boundedPlane1;
	boundedPlane1 = boundedPlaneBuilder1->CommitFeature();
	std::vector< NXOpen::Face* >BoundedFaces = boundedPlane1->GetFaces();

	std::vector< IParameterizedSurface*> objects1(1);
	objects1[0] = BoundedFaces[0];
	MeasureFaces* measureFaces1;
	Unit* unit1 = workPart->UnitCollection()->FindObject("SquareMilliMeter");
	Unit* unit2 = workPart->UnitCollection()->FindObject("MilliMeter");
	measureFaces1 = workPart->MeasureManager()->NewFaceProperties(unit1, unit2, 0.9999, objects1);//测量有界平面的面积和周长

	double areaValue = measureFaces1->Area();
	double PerimeterValue = measureFaces1->Perimeter();

	char msg[256];
	sprintf(msg, "有界平面面积:%f", areaValue);
	theSession->ListingWindow()->WriteLine(msg);

	sprintf(msg, "有界平面周长:%f", PerimeterValue);
	theSession->ListingWindow()->WriteLine(msg);

	NXOpen::Features::Feature* nullNXOpen_Features_Feature(NULL);
	NXOpen::Features::ThickenBuilder* thickenBuilder1;//面加厚成体构造器
	thickenBuilder1 = workPart->Features()->CreateThickenBuilder(nullNXOpen_Features_Feature);
	thickenBuilder1->SetTolerance(0.001);

	thickenBuilder1->FirstOffset()->SetFormula("0.1");
	thickenBuilder1->SecondOffset()->SetFormula("-0.1");//对称加厚

	std::vector< NXOpen::Face* >faces;
	faces.push_back(BoundedFaces[0]);
	NXOpen::FaceDumbRule* faceDumbRule1;
	faceDumbRule1 = workPart->ScRuleFactory()->CreateRuleFaceDumb(faces);

	std::vector<NXOpen::SelectionIntentRule*> rules2(1);
	rules2[0]= faceDumbRule1;
	thickenBuilder1->FaceCollector()->ReplaceRules(rules2, false);

	NXOpen::NXObject* nXObject2;
	nXObject2 = thickenBuilder1->Commit();

	NXOpen::Features::Feature* thickenFeature = thickenBuilder1->CommitFeature();
	std::vector< NXOpen::Body* > thickenBodies;
	thickenBodies = thickenFeature->GetBodies();

	std::vector<Unit*> massUnits1(1);
	Unit* unit11(dynamic_cast<Unit*>(workPart->UnitCollection()->FindObject("MilliMeter")));
	massUnits1[3] = unit11;

	std::vector<NXOpen::IBody*> bodies1(1);
	bodies1[0] = thickenBodies[0];

	NXOpen::MeasureBodies* MeasureBodies1;
	MeasureBodies1 = workPart->MeasureManager()->NewMassProperties(massUnits1, 0.99, bodies1);//测量体的质量属性

	double BlockArea = MeasureBodies1->Area();
	double BlockVolume = MeasureBodies1->Volume();

	NXOpen::Point3d Centroidcoordinate;
	Centroidcoordinate = MeasureBodies1->Centroid();
	double BlockMass = MeasureBodies1->Mass();

	sprintf(msg, "有界平面质心坐标:X %f Y %f Z %f", Centroidcoordinate.X, Centroidcoordinate.Y, Centroidcoordinate.Z);//获取面的质心坐标
	theSession->ListingWindow()->WriteLine(msg);

在这里插入图片描述

相关推荐

  1. 面试技巧:成为令HR程序猿

    2024-04-08 16:52:02       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-08 16:52:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-08 16:52:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-08 16:52:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-08 16:52:02       18 阅读

热门阅读

  1. GO - 标准库

    2024-04-08 16:52:02       15 阅读
  2. Hamilton-Jacobi-Bellman (HJB) 方程

    2024-04-08 16:52:02       18 阅读
  3. 第十四届蓝桥杯省赛大学B组填空题(c++)

    2024-04-08 16:52:02       13 阅读
  4. Android Apk签名算法使用SHA256

    2024-04-08 16:52:02       17 阅读
  5. C++ 动态字符串String的介绍及经典用法展示

    2024-04-08 16:52:02       16 阅读
  6. linux知识点

    2024-04-08 16:52:02       13 阅读
  7. 个人网站开发记录(五)二系统后端nodejs

    2024-04-08 16:52:02       13 阅读