版本

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日