NX二次開發 版本通用方法


對於用C++做NX二次開發,隨着NX的版本變更,二開所用的函數也會有相應的更新調整,生成的DLL不能跨版本運行,報錯率極高,甚至不能加載。折騰了好久找到三種方法:

1.純使用C++的代碼開發或者盡可能使用UF函數(方法不大靠普,能不能跨版使有點碰運氣的成份)

2.同一功能對應不同的NX版本編譯不同版本的DLL,對應不同的版本建立不同的按鈕。(親測是能正常使用,但是一個功能多個按鈕,不專業)

3.同一個功能多個版本共用一個按鈕,建立一個主DLL通過識別NX主版本,再根據NX的版本調用該功能對應版本的DLL(看起來專業多了,目前NX8.5、NX10、NX12 NX1946 做了測試,正常使用。不知道二開高手是怎么做,有更好的方法請指點下)

 

法1 和法 2 都有明顯的缺陷,不適用,下面說明下法3是如何實現的。(自個網上找資料折騰出來的方法,不知道對不對,目前功能實現起來是沒有問題

1)首先把功能對應NX版本編譯不同的DLL

 

 

2)新建工程

 

 3)工程創建以下函數

//獲取運行程序的路徑
HMODULE GetSelfModuleHandle()
{

MEMORY_BASIC_INFORMATION mbi;
return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0)
? (HMODULE)mbi.AllocationBase : NULL);
}
CString GetProgramDir() //獲取運行程序的路徑
{
char exeFullPath[MAX_PATH]; // 全路徑
string strPath = "";
GetModuleFileName(GetSelfModuleHandle(), exeFullPath, MAX_PATH);
strPath = (string)exeFullPath; // 獲取程序路徑
int pos = strPath.find_last_of('\\', strPath.length());
CString path;
path = strPath.substr(0, pos).c_str();
return path;
}

string MyClass::GetPath() //輸出程序路徑
{
CString exe_path = GetProgramDir();
string ProgramDirPath = exe_path;

return string(ProgramDirPath);//返回路徑
}


string MyClass::YiNingToolPath(string DLLDir) //分割程序路徑獲取工具目錄
{
try
{
//反向找位置,分割字符串(只讀取文件夾路徑)
string strPath = DLLDir;
string strDir;
int nPos = strPath.find_last_of('\\');
if (string::npos != nPos)
{
strDir = strPath.substr(0, nPos);
}

return string(strDir);//返回文件夾路徑
}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
MyClass::theUI->NXMessageBox()->Show("分割程序路徑獲取工具目錄", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}


}


void MyClass::UFUN_API_Call_DLL(char* dllPath)
{
//調系統命令
typedef void(*load_ufusr_f_p_t)(char* param, int* retcode, int paramLen);
load_ufusr_f_p_t load_ufusr_ptr = NULL;
int irc = UF_load_library(dllPath, "ufusr", (UF_load_f_p_t*)&load_ufusr_ptr);

//調用DLL
if (load_ufusr_ptr != NULL)
{
int retcode;
load_ufusr_ptr(dllPath, &retcode, 1);
}

if (irc != 0)
{
uc1601("failed load", 1);
UF_unload_library(dllPath); // 注意該語句只能在失敗時被調用,否則UG會退出
return;
}
}


void MyClass::GetNXRev()//獲取NX主要版本
{
try
{
UF_initialize();
theSession->ListingWindow()->Open();//打開信息窗口

//獲取NX主要版本
UF_get_release(&NXrelease); //獲取NX主要版本
//theSession->ListingWindow()->WriteLine(NXrelease);

UF_terminate();

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
MyClass::theUI->NXMessageBox()->Show("幫助", NXOpen::NXMessageBox::DialogTypeError, "程序錯誤,請檢查代碼");
}
}
void MyClass::CallToolingDLL(string CallDllName) //對應版本調用DLL
{
try
{
UF_initialize();
theSession->ListingWindow()->Open();//打開信息窗口

string CurrentDllPath = GetPath();//輸出程序路徑
string CurrentToolPath = YiNingToolPath(CurrentDllPath); //分割程序路徑獲取工具目錄
//theSession->ListingWindow()->WriteLine("當前DLL路徑:" + CurrentDllPath);
//theSession->ListingWindow()->WriteLine("當前外掛路徑:" + CurrentToolPath);

string NX85DllPath = CurrentDllPath + "\\NX85\\" + CallDllName + "_NX85.dll";
string NX10DllPath = CurrentDllPath + "\\NX10\\" + CallDllName + "_NX10.dll";
string NX12DllPath = CurrentDllPath + "\\NX12\\" + CallDllName + "_NX12.dll";
string NX1946DllPath = CurrentDllPath + "\\NX1946\\" + CallDllName + "_NX1946.dll";

char YiNing_tools_NX85[256];
char YiNing_tools_NX10[256];
char YiNing_tools_NX12[256];
char YiNing_tools_NX1946[256];
sprintf(YiNing_tools_NX85, "%s", NX85DllPath.c_str());
sprintf(YiNing_tools_NX10, "%s", NX10DllPath.c_str());
sprintf(YiNing_tools_NX12, "%s", NX12DllPath.c_str());
sprintf(YiNing_tools_NX1946, "%s", NX1946DllPath.c_str());

GetNXRev();//獲取NX主版本

//獲取NX主要版本
if (strcmp(NXrelease, "NX V8.5") == 0)
{
UFUN_API_Call_DLL(YiNing_tools_NX85);
//theSession->ListingWindow()->WriteLine(YiNing_tools_NX85);
}
else if (strcmp(NXrelease, "NX V10.0") == 0)
{
UFUN_API_Call_DLL(YiNing_tools_NX10);
//theSession->ListingWindow()->WriteLine(YiNing_tools_NX10);
}
else if (strcmp(NXrelease, "NX V12.0") == 0)
{
UFUN_API_Call_DLL(YiNing_tools_NX12);
//theSession->ListingWindow()->WriteLine(YiNing_tools_NX12);
}
else if (strcmp(NXrelease, "NX V1926") == 0)
{
UFUN_API_Call_DLL(YiNing_tools_NX1946);
//theSession->ListingWindow()->WriteLine(YiNing_tools_NX1946);
}

UF_terminate();

}
catch (exception& ex)
{
//---- Enter your exception handling code here -----
MyClass::theUI->NXMessageBox()->Show("對應版本調用", NXOpen::NXMessageBox::DialogTypeError, "程序錯誤,請檢查代碼");
}
}

3)入口函數徙通過字符串對比識別菜單按鈕調用DLL

 

4)編譯生成DLL:YiNingToolsSelectMen.dll

 

5)外掛菜單掛上生成的DLL

 

 

 

 6)效果圖如下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM