通过建立三条关联直线创建有界平面,并测量其周长和面积
//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>
//**2、函数声明**
NXOpen::Point* pointcreat(double PTcoordinates[3], NXOpen::Part* part);
//**3、函数定义**
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);//创建点Point1
double P2coordinates[3] = {2.0,2.0,0.0 };
NXOpen::Point* Point2;
Point2 = pointcreat(P2coordinates, workPart);//创建点Point2
double P3coordinates[3] = { 4.0,0.0,0.0 };
NXOpen::Point* Point3;
Point3 = pointcreat(P3coordinates, workPart);//创建点Point3
NXOpen::Line* Line1 = workPart->Curves()->CreateLine(Point1, Point2);//创建直线Line1 通过点Point1和点Point2
NXOpen::Line* Line2 = workPart->Curves()->CreateLine(Point2, Point3);//创建直线Line2通过点Point2和点Point3
NXOpen::Line* Line3 = workPart->Curves()->CreateLine(Point3, Point1);//创建直线Line3 通过点Point3和点Point1
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();//获取有界平面特征的face对象
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);
//NewFaceProperties方法仅可返回面的面积和周长
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);