Teamcenter_NX集成开发:通过NXOpen查询零组件是否存在
之前用过NXOpen PDM的命名空间下的类,现在记录一下通过PDM命名空间下的类查询Teamcenter零组件的信息,也可以用来判断该零组件是否存在。
1-该工程为DLL工程,直接在NX界面调用,所以直接获取NXSession。
2-查询函数advanced用到的查询为:__NX_STD_ANY_ITEM_QUERY,可以在Teamcenter查询构建器模块中看到该查询。
1 // Mandatory UF Includes 2 #include <uf.h> 3 #include <uf_object_types.h> 4 5 // Internal Includes 6 #include <NXOpen/ListingWindow.hxx> 7 #include <NXOpen/NXMessageBox.hxx> 8 #include <NXOpen/UI.hxx> 9 #include <NXOpen/LogFile.hxx> 10 11 // Internal+External Includes 12 #include <NXOpen/Annotations.hxx> 13 #include <NXOpen/Assemblies_Component.hxx> 14 #include <NXOpen/Assemblies_ComponentAssembly.hxx> 15 #include <NXOpen/Body.hxx> 16 #include <NXOpen/BodyCollection.hxx> 17 #include <NXOpen/Face.hxx> 18 #include <NXOpen/Line.hxx> 19 #include <NXOpen/NXException.hxx> 20 #include <NXOpen/NXObject.hxx> 21 #include <NXOpen/Part.hxx> 22 #include <NXOpen/PartCollection.hxx> 23 #include <NXOpen/Session.hxx> 24 25 #include <NXOpen/PDM_SoaConnectionHandle.hxx> 26 #include <NXOpen/PDM_PdmSession.hxx> 27 #include <NXOpen/PDM_PdmSearch.hxx> 28 #include <NXOpen/PDM_PdmFile.hxx> 29 #include <NXOpen/PDM_PdmNavigatorNode.hxx> 30 #include <NXOpen/PDM_PdmPart.hxx> 31 #include <NXOpen/PDM_PdmSearchManager.hxx> 32 #include <NXOpen/PDM_SoaQuery.hxx> 33 #include <NXOpen/PDM_SearchResult.hxx> 34 35 // Std C++ Includes 36 #include <iostream> 37 #include <sstream> 38 39 using namespace NXOpen; 40 using std::string; 41 using std::exception; 42 using std::stringstream; 43 using std::endl; 44 using std::cout; 45 using std::cerr; 46 47 NXOpen::ListingWindow *lw = NULL; 48 NXOpen::NXMessageBox *mb = NULL; 49 50 void do_it(); 51 void print(const NXString &); 52 void print(const string &); 53 void print(const char*); 54 void showClickMessage(int &iRet); 55 56 //------------------------------------------------------------------------------ 57 // Entry point(s) for unmanaged internal NXOpen C/C++ programs 58 //------------------------------------------------------------------------------ 59 extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen ) 60 { 61 try 62 { 63 // 获取NXSession并初始化 64 NXOpen::Session *theSession = NXOpen::Session::GetSession(); 65 NXOpen::UI *theUI = NXOpen::UI::GetUI(); 66 NXOpen::Part *displayPart(theSession->Parts()->Display()); 67 NXOpen::PDM::PdmSession * pdmSession = theSession->PdmSession(); 68 NXOpen::LogFile *lf = theSession->LogFile(); 69 lw = theSession->ListingWindow(); 70 mb = theUI->NXMessageBox(); 71 72 // 获取设置数据 73 bool isSsoEnabled; 74 NXOpen::NXString ssoServerUrl; 75 NXOpen::NXString ssoAppID; 76 NXOpen::NXString connectString; 77 NXOpen::NXString discriminator; 78 pdmSession->GetSsoSettings(&isSsoEnabled, &ssoServerUrl, &ssoAppID); 79 pdmSession->GetTcserverSettings(&connectString, &discriminator); 80 81 // 查询Teamcenter中零组件 000015200AA000000 82 NXOpen::PDM::PdmSearchManager *pdmSearchManager = theSession->PdmSearchManager(); 83 NXOpen::PDM::PdmSearch *pdmSearch = pdmSearchManager->NewPdmSearch(); 84 std::vector<NXOpen::NXString> entries; 85 std::vector<NXOpen::NXString> values; 86 entries.push_back("item_id"); 87 values.push_back("000015200AA000000"); 88 NXOpen::PDM::SearchResult * advancedSearchResults = pdmSearch->Advanced(entries, values); 89 if (advancedSearchResults == NULL) 90 return; 91 std::vector<NXString> resultMfkIds = advancedSearchResults->GetResultMfkIds(); 92 std::vector<NXString> resultObjectNames = advancedSearchResults->GetResultObjectNames(); 93 std::vector<NXString> resultObjectTypes = advancedSearchResults->GetResultObjectTypes(); 94 95 // 输出查询到零组件的信息到信息框 96 print("输出查询到零组件的信息到信息框"); 97 for (int idx = 0; idx < (int)resultMfkIds.size(); idx++){ 98 print("resultMfkIds = " + resultMfkIds[idx]); 99 lf->WriteLine("resultMfkIds = " + resultMfkIds[idx]); 100 } 101 for (int idx = 0; idx < (int)resultObjectNames.size(); idx++){ 102 print("resultObjectNames = " + resultObjectNames[idx]); 103 lf->WriteLine("resultObjectNames = " + resultObjectNames[idx]); 104 } 105 for (int idx = 0; idx < (int)resultObjectTypes.size(); idx++){ 106 print("resultObjectTypes = " + resultObjectTypes[idx]); 107 lf->WriteLine("resultObjectTypes = " + resultObjectTypes[idx]); 108 } 109 110 print("输出信息到信息框"); 111 print("ssoAppID = " + ssoAppID); 112 print("ssoServerUrl = " + ssoServerUrl); 113 print("connectString = " + connectString); 114 print("discriminator = " + discriminator); 115 116 // 输出信息到日志文件 117 print("输出信息到日志文件"); 118 lf->WriteLine("LogFileName = " + lf->FileName()); 119 lf->WriteLine("ssoServerUrl = " + ssoServerUrl); 120 lf->WriteLine("ssoAppID = " + ssoAppID); 121 lf->WriteLine("connectString = " + connectString); 122 lf->WriteLine("discriminator = " + discriminator); 123 124 // 输出查询到零组件的信息到提示框 125 int iRet = mb->Show("resultMfkIds", NXOpen::NXMessageBox::DialogTypeError, resultMfkIds); 126 showClickMessage(iRet); 127 iRet = mb->Show("resultObjectNames", NXOpen::NXMessageBox::DialogTypeWarning, resultObjectNames); 128 showClickMessage(iRet); 129 iRet = mb->Show("resultObjectTypes", NXOpen::NXMessageBox::DialogTypeInformation, resultObjectTypes); 130 showClickMessage(iRet); 131 iRet = mb->Show("resultObjectTypes", NXOpen::NXMessageBox::DialogTypeQuestion, resultObjectTypes); 132 showClickMessage(iRet); 133 } 134 catch (const NXException& e1) 135 { 136 UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message()); 137 } 138 catch (const exception& e2) 139 { 140 UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what()); 141 } 142 catch (...) 143 { 144 UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception."); 145 } 146 } 147 148 149 //------------------------------------------------------------------------------ 150 // Unload Handler 151 //------------------------------------------------------------------------------ 152 extern "C" DllExport int ufusr_ask_unload() 153 { 154 return (int)NXOpen::Session::LibraryUnloadOptionImmediately;// 调试用 155 //return (int)NXOpen::Session::LibraryUnloadOptionAtTermination;// 程序发布用 156 //return (int)NXOpen::Session::LibraryUnloadOptionExplicitly; 157 } 158 159 void showClickMessage(int &iRet){ 160 if (iRet == 1){ 161 mb->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "你点击了是"); 162 } 163 else if (iRet == 2){ 164 mb->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "你点击了否"); 165 } 166 else if (iRet == -2){ 167 mb->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "你点击了确定"); 168 } 169 } 170 171 void print(const NXString &msg) 172 { 173 if (!lw->IsOpen()) lw->Open(); 174 lw->WriteLine(msg); 175 } 176 void print(const string &msg) 177 { 178 if (!lw->IsOpen()) lw->Open(); 179 lw->WriteLine(msg); 180 } 181 void print(const char * msg) 182 { 183 if (!lw->IsOpen()) lw->Open(); 184 lw->WriteLine(msg); 185 }
程序执行后截图:
GIF动图: