Cpp_9
- 格式:ppt
- 大小:95.50 KB
- 文档页数:30


NX⼆次开发-获得NXOpen录制的点集⾥⾯的点坐标XYZ 刚才看到有群友问,就写了⼀下,做个笔记。
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 memberspublic: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 environmentMyClass::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)));//直线tagcurves1[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++){//打印tagchar 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 Executionextern"C" DllExport void ufusr( char *parm, int *returnCode, int rlen ){try{// Create NXOpen C++ class instanceMyClass *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⽇2021年9⽉11⽇补充NX11以下的低版本⾥是没有pointSet1->GetPoints()这个⽅法,要⽤别的⽅法去做。
一、C++基础、变量、常量部分试题:(一)单选题8.由C++语言编写的代码程序( )A。
可直接执行(。
exe才可以直接执行)B。
是一个源程序C。
经过编译即可执行(经过编译链接才可以执行)D。
经过编译解释才能执行9。
按照C++语言规定的用户标识符命名规则,不能出现在标识符中的是()A。
大写字母B。
中划线C。
数字字符D。
下划线10。
下列选项中,不能用作标识符的是( )A。
_1234_ B。
_1_2 C。
int_2_ D。
2_int11.以下选项中合法的用户标识符是()A。
long(关键字) B. _2Test C。
3Dmax D. A.dat 12。
以下不能定义为用户标识符的是()A. Main B。
_0 C。
_int D。
sizeof(运算符)13。
列定义变量的语句错误的是( )A。
int _int; B。
double int_; C。
char For; D。
float USA;14.在C++语言中,非法的字符常量是( )A。
‘\t’ B。
‘\017’ C。
"\n” D.‘\xaa’29。
以下程序的输出结果是()void main(){int k=11;cout<〈dec〈<"k=”〈〈k<〈“,”;cout〈〈oct〈〈”k=”〈〈k〈〈“,”;cout〈〈hex〈〈"k="<〈k<〈endl;}A。
k=11,k=12,k=11 B. k=11,k=13,k=13C. k=11,k=013,k=0xb D。
k=11,k=13,k=b41.不属于C++语言关键字的是().A.int B.break C.while D.character42。
是C++语言提供的合法关键字的是().A.Float B.signed C.integer D.Char43。
下列说法中,错误的是( )。
A.每个语句必须独占一行,语句的最后可以是一个分号,也可以是一个回车换行符号B.每个函数都有一个函数头和一个函数体,主函数也不例外C.主函数只能调用用户函数或系统函数,用户函数可以相互调用D.程序是由若干个函数组成的,但是必须有、而且只能有一个主函数(二)填空题1.一个C++源程序至少有并且只能有一个main(主)函数。