- 文章讨论主题
NXOpen CAM中的Operation类和OperationBuilder类
- 类-方法简单介绍
CAMOperationCollection()->CreateBuilder这个是从NX1926才有的方法,但是其他的方法 我看低版本上面还是有的。
- 方法说明
- 代码例子
NX2008+VS2019 //NXOpenCPP_Wizard1 // 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.h> #include <uf_ui.h> #include <uf_ui_ont.h> #include <uf_obj.h> #include <NXOpen/NXObjectManager.hxx> #include <NXOpen/CAM_Operation.hxx> #include <NXOpen/CAM_OperationBuilder.hxx> #include <NXOpen/CAM_OperationCollection.hxx> #include <NXOpen/CAM_CAMSetup.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: BasePart *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()->BaseWork(); displayPart = theSession->Parts()->BaseDisplay(); } //------------------------------------------------------------------------------ // 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 UF_initialize(); NXOpen::Session* theSession = NXOpen::Session::GetSession(); NXOpen::Part* workPart(theSession->Parts()->Work()); NXOpen::Part* displayPart(theSession->Parts()->Display()); //获取当前加工导航器选中的对象数量和TAG int count = 0; tag_t* objects = NULL_TAG; UF_UI_ONT_ask_selected_nodes(&count, &objects); for (int i = 0; i < count; i++) { //使用OperationBuilder来更改操作的描述 NXOpen::CAM::OperationBuilder* operationBuilder1; operationBuilder1 = workPart->CAMSetup()->CAMOperationCollection()->CreateBuilder(dynamic_cast<NXOpen::CAM::Operation*>(NXOpen::NXObjectManager::Get(objects[i]))); operationBuilder1->SetDescription("SB");//更改描述 operationBuilder1->Commit(); operationBuilder1->Destroy(); //使用Operation来更改 进给率这些 通用参数 NXOpen::CAM::Operation* oper = (NXOpen::CAM::Operation*)(NXOpen::NXObjectManager::Get(objects[i])); //这个"Feed Rapid"只能靠猜,帮助上也没有说明这些字符串参数应该输什么,而且Operation里好像只能设置一些通用的参数,其他的更多具体参数还是要单独录制 oper->SetFeedRate("Feed Rapid", 232.5, NXOpen::CAM::CAMObject::FeedRateUnit::FeedRateUnitNone); } //释放 UF_free(objects); UF_terminate(); } //------------------------------------------------------------------------------ // 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() { // Unloads the image when the application completes return (int)Session::LibraryUnloadOptionImmediately; } 阿飞 2022年4月17日
- 演示
更多内容
一位网友分享在西门子支持中心上面下载的代码例子,可以参考学习一下。
//report_rapid_feed_of_machine_and_operations // 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/CAM_CAMObject.hxx> #include <NXOpen/CAM_CAMSetup.hxx> #include <NXOpen/CAM_InheritableDoubleBuilder.hxx> #include <NXOpen/CAM_InheritableFeedBuilder.hxx> #include <NXOpen/CAM_NCGroup.hxx> #include <NXOpen/CAM_NCGroupBuilder.hxx> #include <NXOpen/CAM_NCGroupCollection.hxx> #include <NXOpen/CAM_Operation.hxx> #include <NXOpen/CAM_OperationCollection.hxx> #include <NXOpen/CAM_MachineGroupBuilder.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/NXException.hxx> // Std C++ Includes #include <iostream> #include <sstream> //------------------------------------------------------------------------------ // NXOpen c++ test class //------------------------------------------------------------------------------ class MyClass { // class members public: static NXOpen::Session *theSession; static NXOpen::UI *theUI; MyClass(); ~MyClass(); void do_it(); void print(const NXOpen::NXString &); void print(const std::string &); void print(const char*); private: NXOpen::Part *workPart, *displayPart; NXOpen::NXMessageBox *mb; NXOpen::ListingWindow *lw; }; //------------------------------------------------------------------------------ // Initialize static variables //------------------------------------------------------------------------------ NXOpen::Session *(MyClass::theSession) = NULL; NXOpen::UI *(MyClass::theUI) = NULL; //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ MyClass::MyClass() { // Initialize the NX Open C++ API environment MyClass::theSession = NXOpen::Session::GetSession(); MyClass::theUI = NXOpen::UI::GetUI(); mb = theUI->NXMessageBox(); lw = theSession->ListingWindow(); workPart = theSession->Parts()->Work(); displayPart = theSession->Parts()->Display(); } //------------------------------------------------------------------------------ // Destructor //------------------------------------------------------------------------------ MyClass::~MyClass() { } //------------------------------------------------------------------------------ // Print string to listing window or stdout //------------------------------------------------------------------------------ void MyClass::print(const NXOpen::NXString &msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } void MyClass::print(const std::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() { std::ostringstream out; double f8Value; NXOpen::CAM::CAMSetup *camSetup = displayPart->CAMSetup(); NXOpen::CAM::OperationCollection *operColl = camSetup->CAMOperationCollection(); NXOpen::CAM::OperationCollection::iterator it = operColl->begin(); while( it != operColl->end() ) { NXOpen::CAM::Operation *oper = (NXOpen::CAM::Operation*)(*it); out.str(""); out << "Operation: " << oper->Name().GetText() << std::endl; oper->GetFeedRate("Feed Rapid", &f8Value); out << " Rapid Feed Rate: " << f8Value << std::endl; print(out.str().c_str()); it++; } NXOpen::CAM::NCGroup *mctRoot(camSetup->GetRoot(NXOpen::CAM::CAMSetup::ViewMachineTool)); NXOpen::CAM::MachineGroupBuilder *mctGrpBuilder; mctGrpBuilder = camSetup->CAMGroupCollection()->CreateMachineGroupBuilder(mctRoot); out.str(""); out << "Machine: " << mctGrpBuilder->GetObject()->Name().GetText() << std::endl; out << " Rapid Feed Rate: " << mctGrpBuilder->RapidFeed()->Value() << std::endl; print(out.str().c_str()); mctGrpBuilder->Destroy(); } //------------------------------------------------------------------------------ // 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 NXOpen::NXException& e1) { NXOpen::UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message()); } catch (const std::exception& e2) { NXOpen::UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what()); } catch (...) { NXOpen::UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception."); } } //------------------------------------------------------------------------------ // Unload Handler //------------------------------------------------------------------------------ extern "C" DllExport int ufusr_ask_unload() { return (int)NXOpen::Session::LibraryUnloadOptionImmediately; }
阿飞
2022年4月17日