版本
NX9+VS2012
1.怎么往VS軟件里添加VC,C#,VB向導模板
先到NX安裝目錄下UGOPEN文件夾里找到這三個文件夾
拷貝到VS的安裝目錄下
這里有幾個注意事項,VS2017,VS2019以下的版本這樣操作沒問題,
VS2017和VS2019微軟調整了VC文件夾的目錄,換位置了,所以你加過去發現打開VS里面沒有C++的向導模板。
去你的VS2017或者VS2019安裝目錄下找到\Common7\IDE文件夾目錄,把VC向導模板放進去。
下面我們新建項目,使用NX向導模板
兩個向導模板使用哪個都可以的,
向導模板是為我們提供的一個現成的NX二次開發模板框架,有C++模板,C模板,C#模板,VB模板,開發環境都是自動配置好的,可直接調用UFUN和NXOPEN,省去了我們自己配置開發環境的時間,
我還發現從NX1980開始,UGOPEN目錄里還加入了開發向導的差勁,不需要手動在考文件了,直接雙擊插件,就可直接安裝開發向導。
插件就是和這種VS外掛插件一樣
放一張我之前安裝NX1980的時候截的圖,現在已經卸載掉了。
一組新舊對比圖片
在放一張西門子官網的介紹說明
有點類似微軟基礎類庫MFC,除了是一個C++類庫外,還提供一個應用程序框架。給開發者使用。
這里有幾個要注意的事項,在NX8.5和NX9中,不安裝大補丁小布丁,默認應該是沒有NXOPENCPP向導模板的,
一定要安裝補丁才會有NXOPENCPP的向導模板,NX8.5以下的版本,我就不知道有沒有NXOPENCPP模板了,沒用過。
不過沒用的話,自己手動也能改個出來的,向導模板里的內容也可以自己更改。https://www.cnblogs.com/nxopen2018/p/15221264.html
回到正題,我們選擇向導模板,進入項目
先選NXOPENCPP項目模板,這是一個用C++類面向對象方式的一個模板
默認下一步
編譯成功,這就是一個C++的模板框架
//NX9_NXOpenCPP_Wizard7 // 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> // 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 } //------------------------------------------------------------------------------ // 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; }
自己的代碼寫到void MyClass::do_it()里,
演示一下,怎么加代碼,怎么ctrl+u執行。
在來看看OPEN C的向導模板。
這里還是選C++吧,我覺得沒必要選C了,選C沒辦法用NXOPEN C++那些,只能用純C的UFUN。
編譯成功
/***************************************************************************** ** ** NX9_Open_Wizard1.cpp ** ** Description: ** Contains Unigraphics entry points for the application. ** *****************************************************************************/ /* Include files */ #include <stdarg.h> #include <strstream> #include <iostream> using std::ostrstream; using std::endl; using std::ends; using std::cerr; #include <uf.h> #include <uf_ui.h> #include <uf_exit.h> static void ECHO(char *format, ...) { char msg[1025]; va_list args; va_start(args, format); vsnprintf_s(msg, sizeof(msg), 1024, format, args); va_end(args); UF_UI_open_listing_window(); UF_UI_write_listing_window(msg); UF_print_syslog(msg, FALSE); } #define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X))) static int report_error( char *file, int line, char *call, int irc) { if (irc) { char err[133]; UF_get_fail_message(irc, err); ECHO("*** ERROR code %d at line %d in %s:\n", irc, line, file); ECHO("+++ %s\n", err); ECHO("%s;\n", call); } return(irc); } /***************************************************************************** ** Activation Methods *****************************************************************************/ /* Explicit Activation ** This entry point is used to activate the application explicitly, as in ** "File->Execute UG/Open->User Function..." */ extern DllExport void ufusr( char *parm, int *returnCode, int rlen ) { /* Initialize the API environment */ if( UF_CALL(UF_initialize()) ) { /* Failed to initialize */ return; } /* TODO: Add your application code here */ /* Terminate the API environment */ UF_CALL(UF_terminate()); } /***************************************************************************** ** Utilities *****************************************************************************/ /* Unload Handler ** This function specifies when to unload your application from Unigraphics. ** If your application registers a callback (from a MenuScript item or a ** User Defined Object for example), this function MUST return ** "UF_UNLOAD_UG_TERMINATE". */ extern int ufusr_ask_unload( void ) { return( UF_UNLOAD_IMMEDIATELY ); }
我們來看看模板的代碼,也是可以用C++語言,但是是基於面向過程的。沒按面向對象去生成模板。
這個代碼寫到/* TODO: Add your application code here */就行了,我就不演示了。
至於附加依賴性,附加庫目錄,預處理,那些就都不用管了,向導自動給設置好了。我們只要專注寫代碼開發就行了。
結尾到最后,無論哪種項目,建議設置字符集為多字節。
本來還想在寫一下怎么查看幫助手冊,找UFUN函數和NXOPEN的,但是一看23.26了,有點晚了。
該准備睡覺了,要不然一會又該失眠焦慮症了。一晚上睡不着。怎么查看幫助那部分內容就放到后面單獨在開一篇
博客文章。
阿飛
2021年9月2日