Teamcenter_NX集成开发:使用NX、SOA连接Teamcenter
最近工作中经常使用Teamcenter、NX集成开发的情况,因此在这里记录使用NX、SOA连接到Teamcenter的连接方式。
主要操作:
1-初始化UGMGR环境成功后就可以连接到Teamcenter。
2-获取指定ID编码零组件标识和Latest Working(没记错的话应该是最新版本)版本ID。
3-获取版本下相关数据集文件(UGMASTER、UGPART等)。
4-ID、版本、文件类型Encode组成集成环境下NX可以打开的NX部件文件名称,并打开。
5-获取Session,打印Teamcenter当前登录方式(二层或者四层等)及其他信息。
6-关闭打开的零组件并释放内存。
1 // Mandatory UF Includes 2 #include <uf.h> 3 #include <uf_object_types.h> 4 #include <uf_ugmgr.h> 5 #include <uf_part.h> 6 #include <uf_disp.h> 7 #include <uf_modl.h> 8 #include <uf_obj.h> 9 #include <uf_assem.h> 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_PdmSession.hxx> 26 #include <NXOpen/PDM_SoaConnectionHandle.hxx> 27 #include <teamcenter/soa/client/Connection.hxx> 28 #include <teamcenter/services/core/DatamanagementService.hxx> 29 #include <teamcenter/soa/common/Version.hxx> 30 31 // Std C++ Includes 32 #include <iostream> 33 #include <sstream> 34 35 using namespace NXOpen; 36 using namespace Teamcenter::Soa::Client; 37 using namespace Teamcenter::Services::Core; 38 using std::string; 39 using std::exception; 40 using std::stringstream; 41 using std::endl; 42 using std::cout; 43 using std::cerr; 44 45 void do_it() 46 { 47 int err = 0; 48 UF_UGMGR_tag_t database_part_tag = NULL_TAG; 49 err = UF_UGMGR_ask_part_tag("000000000AA000082", &database_part_tag); 50 err = UF_UGMGR_set_config_rule("Latest Working"); 51 52 UF_UGMGR_tag_t part_revision = NULL_TAG; 53 err = UF_UGMGR_ask_configured_rev(database_part_tag, &part_revision); 54 55 char revision_id[UF_UGMGR_NAME_BUFSIZE] = { 0 }; 56 err = UF_UGMGR_ask_part_revision_id(part_revision, revision_id); 57 58 int file_count = 0; 59 char** file_types = NULL; 60 char** file_names = NULL; 61 err = UF_UGMGR_list_part_rev_files(part_revision, &file_count, &file_types, &file_names); 62 if (file_count <= 0) return; 63 64 for (int idx = 0; idx < file_count; idx++){ 65 if (file_names[idx] == NULL) 66 continue; 67 68 char encoded_name[MAX_FSPEC_BUFSIZE] = { 0 }; 69 err = UF_UGMGR_encode_part_filename("000000000AA000082", revision_id, file_types[idx], file_names[idx], encoded_name); 70 71 tag_t partTag = NULL_TAG; 72 UF_PART_load_status_t error_status; 73 err = UF_PART_open(encoded_name, &partTag, &error_status); 74 75 //=================== 76 // 打开零组件后获取Session,不然会报错。也可以获取零组件属性,打开的是图纸还可以导出PDF。 77 //=================== 78 NXOpen::Session *theSession = NXOpen::Session::GetSession(); 79 NXOpen::Part *displayPart(theSession->Parts()->Display()); 80 NXOpen::PDM::PdmSession *thePdmSession = NULL; 81 NXOpen::PDM::SoaConnectionHandle *connectHandle = NULL; 82 Teamcenter::Soa::Client::Connection *tcSoaConnect = NULL; 83 84 thePdmSession = theSession->PdmSession(); 85 connectHandle = thePdmSession->GetSoaConnectionHandle(); 86 tcSoaConnect = reinterpret_cast<Teamcenter::Soa::Client::Connection*>(connectHandle->GetHandle()); 87 Teamcenter::Services::Core::DatamanagementService *dmService = Teamcenter::Services::Core::DatamanagementService::getService(tcSoaConnect); 88 89 Teamcenter::Soa::Common::Version clientVersion = tcSoaConnect->getClientVersion(); 90 Teamcenter::Soa::Common::Version serviceClient = tcSoaConnect->getServerVersion(); 91 Teamcenter::Soa::Client::Connection::Protocol protocol = tcSoaConnect->getProtocol(); 92 Teamcenter::Soa::Client::Connection::Binding binding = tcSoaConnect->getBinding(); 93 94 std::cout << "\nclientVersion.getTeamcenterVersion = " << clientVersion.getTeamcenterVersion() << std::endl; 95 std::cout << "clientVersion.getTeamcenterVersionLabel = " << clientVersion.getTeamcenterVersionLabel() << std::endl; 96 std::cout << "serviceClient.getTeamcenterVersion = " << serviceClient.getTeamcenterVersion() << std::endl; 97 std::cout << "serviceClient.getTeamcenterVersionLabel = " << serviceClient.getTeamcenterVersionLabel() << std::endl; 98 99 std::cout << "\ntcSoaConnect->getLocale = " << tcSoaConnect->getLocale() << std::endl; 100 std::cout << "tcSoaConnect->getServerAddress = " << tcSoaConnect->getServerAddress() << std::endl; 101 std::cout << "tcSoaConnect->getCurrentObjectPropertyPolicy = " << tcSoaConnect->getCurrentObjectPropertyPolicy() << std::endl; 102 103 switch (protocol){ 104 case Teamcenter::Soa::Client::Connection::HTTP: 105 std::cout << "\n\nTeamcenter::Soa::Client::Connection::HTTP" << std::endl; 106 break; 107 case Teamcenter::Soa::Client::Connection::TCCS: 108 std::cout << "\nTeamcenter::Soa::Client::Connection::TCCS" << std::endl; 109 break; 110 case Teamcenter::Soa::Client::Connection::IIOP: 111 std::cout << "\nTeamcenter::Soa::Client::Connection::IIOP" << std::endl; 112 break; 113 case Teamcenter::Soa::Client::Connection::TCTP: 114 std::cout << "\nTeamcenter::Soa::Client::Connection::TCTP" << std::endl; 115 break; 116 default: 117 break; 118 } 119 switch (binding){ 120 case Teamcenter::Soa::Client::Connection::REST: 121 std::cout << "\n\nTeamcenter::Soa::Client::Connection::REST" << std::endl; 122 break; 123 case Teamcenter::Soa::Client::Connection::SOAP: 124 std::cout << "\nTeamcenter::Soa::Client::Connection::SOAP" << std::endl; 125 break; 126 default: 127 break; 128 } 129 130 err = UF_PART_free_load_status(&error_status); 131 err = UF_PART_close(partTag, 1, 1); 132 } 133 134 UF_free(file_names); 135 UF_free(file_types); 136 file_types = NULL; 137 file_names = NULL; 138 } 139 140 //=================== 141 // Entry Point 142 //=================== 143 int main(int argc, char* argv[]) 144 { 145 try{ 146 const char** dd = (const char**)(argv); 147 int err = UF_UGMGR_initialize(argc, dd); 148 do_it(); 149 err = UF_UGMGR_terminate(); 150 return err; 151 } 152 catch (const NXException& e1){ 153 cerr << "NXException: " << e1.ErrorCode() << endl; 154 cerr << e1.Message() << endl; 155 } 156 catch (const exception& e2){ 157 cerr << "Exception: " << e2.what() << endl; 158 } 159 catch (...){ 160 cerr << "Unknown Exception: " << endl; 161 } 162 }
释放应该使用以下函数:
1 UF_free_string_array(file_count, file_names); 2 UF_free_string_array(file_count, file_types);
encode:把ID、版本、文件类型编码成NX可以打开的名称。
控制台输出日志:
注意事项:
1-要以bat文件启动VS进行调试运行,bat文件中设置Teamcenter相关的环境变量,不然初始化函数UF_UGMGR_Initlization()会报错。
2-连接两层Teamcenter客户端时:Tao窗口要成功启动。
3-bat文件内容如下:
1 cd /d %~dp0 2 call "D:\PLM\Siemens\Teamcenter12\install\tem_init.bat" 3 call "D:\PLM\Siemens\Teamcenter12\tc_menu\tc_config1.bat" 4 5 set UGII_UGMGR_COMMUNICATION=IIOP 6 set TPR=D:\PLM\Siemens\Teamcenter12\portal 7 set UGII_BASE_DIR=D:\PLM\Siemens\NX 8 set TC_ROOT=D:\PLM\Siemens\Teamcenter12 9 set TC_DATA=D:\PLM\Siemens\tcdata 10 set FMS_HOME=D:\PLM\Siemens\Teamcenter12\tccs 11 12 set JAVA_HOME=%TC_JRE_HOME% 13 set JRE_HOME=%TC_JRE_HOME% 14 15 set PATH=%JAVA_HOME%\bin;%FME_HOME%\bin;%FMS_HOME%\lib;%TPR%;%UGII_BASE_DIR%\NXBIN;%UGII_BASE_DIR%\UGII;%PATH%; 16 17 start "TAO ImR" /min cmd /c "D:\PLM\Siemens\Teamcenter12\iiopservers\start_imr.bat" 18 cd "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE" 19 C: 20 devenv.exe
零组件在Teamcenter中截图: