之前了解到通过UFUN函数UF_UGMGR_invoke_pdm_server可以调用Teamcenter ITK函数,从而可以获取及编辑Teamcenter对象。UFUN中有样例代码,但是就是不知道怎么使用,今天下午看了帮助文档,想到需要把ITK的USER_invoke_pdm_server函数进行注册,就进行测试,没想到给写通了。在此记录代码调试过程,转载请注明出处。

注意事项:

  1-需要了解Teamcenter Handler注册过程。

  2-Teamcenter开发方面可以参考微信公众号:PLMCODE

NX工程代码:

  1 //================================  2 // UF_UGMGR_invoke_pdm_server  3 //================================  4 //tcnx_project  5   6 // Mandatory UF Includes  7 #include   8 #include   9 #include  10 #include  11 #include  12  13 #include  14  15 // Internal Includes 16 #include  17 #include  18 #include  19 #include  20  21 // Internal+External Includes\ 22 #include  23 #include  24 #include  25 #include  26 #include  27 #include  28 #include  29 #include  30 #include  31 #include  32 #include  33 #include  34  35 #include  36 #include  37 #include  38 #include  39 #include  40 #include  41 #include  42 #include  43 #include  44  45 #include  46 #include  47 #include  48 #include  49  50 // Std C++ Includes 51 #include  52 #include  53 #include  54 #include <string> 55 #include  56 #include  57 #include  58 #include  59  60 #include  61 #undef CreateDialog 62 #pragma comment(lib,"shell32.lib") 63  64 #define CREATION_DATE       1 65 #define MODIFICATION_DATE   2 66 #define MAX_UGMGR_NAME_LEN 1024 67  68 using namespace NXOpen; 69 using std::string; 70 using std::exception; 71 using std::stringstream; 72 using std::endl; 73 using std::cout; 74 using std::cerr; 75  76 NXOpen::ListingWindow *lw = NULL; 77 NXOpen::NXMessageBox *mb = NULL; 78  79 #define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X))) 80 int report_error(char *file, int line, char *call, int code) 81 { 82     if (code){ 83         stringstream errmsg; 84         errmsg << "Error " << code << " in " << file << " at line " << line << endl; 85         errmsg << call << endl; 86         cerr << errmsg.str(); 87         throw NXOpen::NXException::Create(code); 88     } 89     return(code); 90 } 91  92 void print(const NXString &); 93 void print(const string &); 94 void print(const char*); 95 int invokePdmServer(); 96 int invokePdmServer() 97 { 98     int _errCode = 0; 99     _errCode = UF_CALL(UF_UGMGR_initialize(0, NULL));100 101     char        part_name[MAX_UGMGR_NAME_LEN + 1] = "000015200AA000000";102     int         output_code = -1;103     char*       date = NULL;104 105     _errCode = UF_CALL(UF_UGMGR_invoke_pdm_server(CREATION_DATE, part_name, &output_code, &date));106     print("\nAfter calling UF_UGMGR_invoke_pdm_server()\n");107     print("Part          : " + string(part_name));108     print("creation date : " + string(date));109     print("output_code   : " + std::to_string(output_code) + "\n\n");110     UF_free(date);111     date = NULL;112     113     UF_UGMGR_terminate();114     return _errCode;115 }116 117 extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )118 {119     try120     {121         invokePdmServer();122         return;123     }124     catch (const NXException& e1)125     {126         UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());127     }128     catch (const exception& e2)129     {130         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());131     }132     catch (...)133     {134         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");135     }136 }137 138 extern "C" DllExport int ufusr_ask_unload()139 {140     return (int)NXOpen::Session::LibraryUnloadOptionImmediately;// 调试用141     //return (int)NXOpen::Session::LibraryUnloadOptionAtTermination;// 程序发布用142     //return (int)NXOpen::Session::LibraryUnloadOptionExplicitly;143 }144 145 void print(const NXString &msg)146 {147     if (!lw->IsOpen()) lw->Open();148     lw->WriteLine(msg);149 }150 void print(const string &msg)151 {152     if (!lw->IsOpen()) lw->Open();153     lw->WriteLine(msg);154 }155 void print(const char * msg)156 {157     if (!lw->IsOpen()) lw->Open();158     lw->WriteLine(msg);159 }

Teamcenter Handler工程代码:

 1 //====================================== 2 // libA2CustStudy_register_callbacks 3 //====================================== 4  5 //======================= 6 // libA2CustStudy.h 7 //======================= 8 #ifndef TCHANDLER_STUDY_H 9 #define TCHANDLER_STUDY_H10 11 #pragma once12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 #include 25 #include 26 27 #include 28 #include 29 #include 30 31 #include "register_custom_user_service.h"32 33 34 #ifdef __cplusplus35 extern "C"36 {37 #endif38 39     extern DLLAPI int libA2CustStudy_register_callbacks();40     int libA2CustStudy_invoke_pdm_server(int *decision, va_list args);41 42 #ifdef __cplusplus43 }44 #endif45 46 #endif
 1 //======================= 2 // libA2CustStudy.cpp 3 //======================= 4 #include "libA2CustStudy.h" 5  6 #define CUST_TCHANDLER_STUDY "libA2CustStudy" 7  8 extern DLLAPI int libA2CustStudy_register_callbacks() 9 {10     int stat = ITK_ok;11     stat = CUSTOM_register_exit(CUST_TCHANDLER_STUDY, "USER_invoke_pdm_server", (CUSTOM_EXIT_ftn_t)libA2CustStudy_invoke_pdm_server);12     stat == ITK_ok ? printf("(libA2CustStudy.dll)-函数libA2CustStudy_invoke_pdm_server返回值为ITK_ok,注册成功!!!\n") : printf("(libA2CustStudy.dll)-函数libA2CustStudy_invoke_pdm_server返回值不等于ITK_ok,注册失败!!!\n");13     return stat;14 }
 1 //======================= 2 // lib_register_custom.cpp 3 //======================= 4 #include "libA2CustStudy.h" 5  6 #define CREATION_DATE     1 7 #define MODIFICATION_DATE 2 8  9 #ifdef __cplusplus10 extern "C"11 {12 #endif13 14     int libA2CustStudy_invoke_pdm_server(int *decision, va_list args)15     {16         /***********  va_list for USER_invoke_pdm_server ***********/17         int  input_code = va_arg(args, int);          /* args 1 */18         char *input_string = va_arg(args, char *);    /* args 2 */19         int  *output_code = va_arg(args, int *);      /* args 3 */20         char **output_string = va_arg(args, char **); /* args 4 */21         /***********************************************************/22         printf("\t libA2CustStudy_invoke_pdm_server \n\n");23         *decision = ALL_CUSTOMIZATIONS;24 25         tag_t             item_tag;26         int               itkfail;27         WSO_description_t description;28         char              createDate[WSO_date_size_c + 1];29         char              modifyDate[WSO_date_size_c + 1];30         char              objectName[WSO_date_size_c + 1];31         char              objectType[WSO_date_size_c + 1];32         char              ownersName[WSO_date_size_c + 1];33         char              owning_group_name[WSO_date_size_c + 1];34         char              last_modify_user_name[WSO_date_size_c + 1];35         char              object_desc[WSO_date_size_c + 1];36         *output_code = 0;37         char tmp_output_string[1024] = { 0 };38 39         itkfail = ITEM_find_item(input_string, &item_tag);40         if (itkfail != ITK_ok){41             printf("Failed to find item %s\n", input_string);42             *output_code = 1;43         }44 45         itkfail = WSOM_describe(item_tag, &description);46         if (itkfail != ITK_ok){47             printf("Failed to get description of the item\n");48             *output_code = 1;49         }50 51         printf("ITK USER_invoke_pdm_server() routine :\n");52         printf("INPUT:\n");53         printf("input_code    : %d\n", input_code);54         printf("input_string  : %s\n\n", input_string);55 56         switch (input_code){57         case CREATION_DATE:58         case MODIFICATION_DATE:59             strcpy_s(object_desc, strlen(description.date_created) + 1, description.description);60             strcpy_s(createDate, strlen(description.date_created) + 1, description.date_created);61             strcpy_s(modifyDate, strlen(description.date_modified) + 1, description.date_modified);62             strcpy_s(objectName, strlen(description.object_name) + 1, description.object_name);63             strcpy_s(objectType, strlen(description.object_type) + 1, description.object_type);64             strcpy_s(ownersName, strlen(description.owners_name) + 1, description.owners_name);65             strcpy_s(owning_group_name, strlen(description.owning_group_name) + 1, description.owning_group_name);66             strcpy_s(last_modify_user_name, strlen(description.last_modifying_user_name) + 1, description.last_modifying_user_name);67             sprintf_s(tmp_output_string, "date_created=%s\ndate_modified=%s\nobject_name=%s\nobject_type=%s\nowners_name=%s\nowning_group_name=%s\nlast_modify_user_name=%s\nobject_desc=%s\n",68                 createDate, modifyDate, objectName, objectType, ownersName, owning_group_name, last_modify_user_name, object_desc);69             break;70         default:71             printf("OUTPUT:该参数 %d 未定义操作!\n", input_code);72             break;73         }74         if (strlen(tmp_output_string) > 0){75             *output_string = (char*)malloc(sizeof(tmp_output_string));76             strcpy_s(*output_string, strlen(tmp_output_string) + 1, tmp_output_string);77             printf("OUTPUT:\n");78             printf("output_string : %s (%s)\n", *output_string, (input_code == 1) ? "CREATION DATE" : "MODIFICATION DATE");79             printf("output_code   : %d\n", *output_code);80         }81         return ITK_ok;82     }83 84 #ifdef __cplusplus85 }86 #endif

Teamcenter零组件属性截图:

工程调试运行GIF动图:

2023年03月25日 16点52分发布。转载请注明出处!!!