刚才看到有群友问,就写了一下,做个笔记。
NX11+VS2013 //NX11_NXOpenCPP_Wizard9 // Mandatory UF Includes #include <uf.h> #include <uf_object_types.h> // Internal Includes #include <NXOpen/ListingWindow.hxx> #include <NXOpen/NXMessageBox.hxx> #include <NXOpen/UI.hxx> // Internal+External Includes #include <NXOpen/Annotations.hxx> #include <NXOpen/Assemblies_Component.hxx> #include <NXOpen/Assemblies_ComponentAssembly.hxx> #include <NXOpen/Body.hxx> #include <NXOpen/BodyCollection.hxx> #include <NXOpen/Face.hxx> #include <NXOpen/Line.hxx> #include <NXOpen/NXException.hxx> #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/Session.hxx> #include <uf_defs.h> #include <NXOpen/NXException.hxx> #include <NXOpen/Session.hxx> #include <NXOpen/BasePart.hxx> #include <NXOpen/Builder.hxx> #include <NXOpen/CurveDumbRule.hxx> #include <NXOpen/DisplayableObject.hxx> #include <NXOpen/Expression.hxx> #include <NXOpen/ExpressionCollection.hxx> #include <NXOpen/Face.hxx> #include <NXOpen/Features_AssociativeLine.hxx> #include <NXOpen/Features_FeatureCollection.hxx> #include <NXOpen/Features_PointSetBuilder.hxx> #include <NXOpen/Features_PointSetFacePercentageBuilder.hxx> #include <NXOpen/Features_PointSetFacePercentageBuilderList.hxx> #include <NXOpen/IBaseCurve.hxx> #include <NXOpen/Line.hxx> #include <NXOpen/NXObject.hxx> #include <NXOpen/NXObjectList.hxx> #include <NXOpen/ObjectList.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/Point.hxx> #include <NXOpen/PointList.hxx> #include <NXOpen/ScCollector.hxx> #include <NXOpen/ScRuleFactory.hxx> #include <NXOpen/Section.hxx> #include <NXOpen/SelectDisplayableObjectList.hxx> #include <NXOpen/SelectFace.hxx> #include <NXOpen/SelectObject.hxx> #include <NXOpen/SelectionIntentRule.hxx> #include <NXOpen/Session.hxx> #include <NXOpen/TaggedObject.hxx> #include <NXOpen/Unit.hxx> #include <NXOpen/NXObjectManager.hxx> #include <NXOpen/Features_PointSet.hxx> // Std C++ Includes #include <iostream> #include <sstream> using namespace NXOpen; using std::string; using std::exception; using std::stringstream; using std::endl; using std::cout; using std::cerr; //------------------------------------------------------------------------------ // NXOpen c++ test class //------------------------------------------------------------------------------ class MyClass { // class members public: static Session *theSession; static UI *theUI; MyClass(); ~MyClass(); void do_it(); void print(const NXString &); void print(const string &); void print(const char*); private: Part *workPart, *displayPart; NXMessageBox *mb; ListingWindow *lw; LogFile *lf; }; //------------------------------------------------------------------------------ // Initialize static variables //------------------------------------------------------------------------------ Session *(MyClass::theSession) = NULL; UI *(MyClass::theUI) = NULL; //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ MyClass::MyClass() { // Initialize the NX Open C++ API environment MyClass::theSession = NXOpen::Session::GetSession(); MyClass::theUI = UI::GetUI(); mb = theUI->NXMessageBox(); lw = theSession->ListingWindow(); lf = theSession->LogFile(); workPart = theSession->Parts()->Work(); displayPart = theSession->Parts()->Display(); } //------------------------------------------------------------------------------ // Destructor //------------------------------------------------------------------------------ MyClass::~MyClass() { } //------------------------------------------------------------------------------ // Print string to listing window or stdout //------------------------------------------------------------------------------ void MyClass::print(const NXString &msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } void MyClass::print(const string &msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } void MyClass::print(const char * msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } //------------------------------------------------------------------------------ // Do something //------------------------------------------------------------------------------ void MyClass::do_it() { // TODO: add your code here //录制创建点集(随便录得,这个不重要,使用者自己重新录) NXOpen::Features::PointSet *nullNXOpen_Features_PointSet(NULL); NXOpen::Features::PointSetBuilder *pointSetBuilder1; pointSetBuilder1 = workPart->Features()->CreatePointSetBuilder(nullNXOpen_Features_PointSet); std::vector<NXOpen::IBaseCurve *> curves1(1); NXOpen::Line *line1(dynamic_cast<NXOpen::Line *>(NXObjectManager::Get(44473)));//直线tag curves1[0] = line1; NXOpen::CurveDumbRule *curveDumbRule1; curveDumbRule1 = workPart->ScRuleFactory()->CreateRuleBaseCurveDumb(curves1); pointSetBuilder1->SingleCurveOrEdgeCollector()->AllowSelfIntersection(true); std::vector<NXOpen::SelectionIntentRule *> rules1(1); rules1[0] = curveDumbRule1; NXOpen::NXObject *nullNXOpen_NXObject(NULL); NXOpen::Point3d helpPoint1(0, 0, 0); pointSetBuilder1->SingleCurveOrEdgeCollector()->AddToSection(rules1, line1, nullNXOpen_NXObject, nullNXOpen_NXObject, helpPoint1, NXOpen::Section::ModeCreate, false); pointSetBuilder1->NumberOfPointsExpression()->SetRightHandSide("20"); NXOpen::NXObject *nXObject1; nXObject1 = pointSetBuilder1->Commit(); pointSetBuilder1->Destroy(); //转换 NXOpen::Features::PointSet *pointSet1(dynamic_cast<NXOpen::Features::PointSet *>(NXOpen::NXObjectManager::Get(nXObject1->Tag()))); std::vector<NXOpen::Point*> points = pointSet1->GetPoints(); //打印点坐标 theSession->ListingWindow()->Open(); for (int i = 0; i < points.size(); i++) { //打印tag char msg[256]; sprintf(msg, "点坐标:X=%f,Y=%f,Z=%f\n", points[i]->Coordinates().X, points[i]->Coordinates().Y, points[i]->Coordinates().Z); theSession->ListingWindow()->WriteLine(msg); } } //------------------------------------------------------------------------------ // Entry point(s) for unmanaged internal NXOpen C/C++ programs //------------------------------------------------------------------------------ // Explicit Execution extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen ) { try { // Create NXOpen C++ class instance MyClass *theMyClass; theMyClass = new MyClass(); theMyClass->do_it(); delete theMyClass; } catch (const NXException& e1) { UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message()); } catch (const exception& e2) { UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what()); } catch (...) { UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception."); } } //------------------------------------------------------------------------------ // Unload Handler //------------------------------------------------------------------------------ extern "C" DllExport int ufusr_ask_unload() { return (int)NXOpen::Session::LibraryUnloadOptionImmediately; } 阿飞 2021年9月9日
阿飞
2021年9月9日