Teamcenter_NX集成开发:UF_UGMGR_invoke_pdm_server函数的使用

打印 上一主题 下一主题

主题 956|帖子 956|积分 2868

之前了解到通过UFUN函数UF_UGMGR_invoke_pdm_server可以调用Teamcenter ITK函数,从而可以获取及编辑Teamcenter对象。UFUN中有样例代码,但是就是不知道怎么使用,今天下午看了帮助文档,想到需要把ITK的USER_invoke_pdm_server函数进行注册,就进行测试,没想到给写通了。在此记录代码调试过程,转载请注明出处。
注意事项:
  1-需要了解Teamcenter Handler注册过程。
  2-Teamcenter开发方面可以参考微信公众号:PLMCODE
 
NX工程代码:
  1.   1 //================================
  2.   2 // UF_UGMGR_invoke_pdm_server
  3.   3 //================================
  4.   4 //tcnx_project
  5.   5
  6.   6 // Mandatory UF Includes
  7.   7 #include <uf.h>
  8.   8 #include <uf_object_types.h>
  9.   9 #include <uf_draw.h>
  10. 10 #include <uf_part.h>
  11. 11 #include <uf_ugmgr.h>
  12. 12
  13. 13 #include <NXSigningResource.cpp>
  14. 14
  15. 15 // Internal Includes
  16. 16 #include <NXOpen/ListingWindow.hxx>
  17. 17 #include <NXOpen/NXMessageBox.hxx>
  18. 18 #include <NXOpen/UI.hxx>
  19. 19 #include <NXOpen/LogFile.hxx>
  20. 20
  21. 21 // Internal+External Includes\
  22. 22 #include <NXOpen/Annotations.hxx>
  23. 23 #include <NXOpen/Assemblies_Component.hxx>
  24. 24 #include <NXOpen/Assemblies_ComponentAssembly.hxx>
  25. 25 #include <NXOpen/Body.hxx>
  26. 26 #include <NXOpen/BodyCollection.hxx>
  27. 27 #include <NXOpen/Face.hxx>
  28. 28 #include <NXOpen/Line.hxx>
  29. 29 #include <NXOpen/NXException.hxx>
  30. 30 #include <NXOpen/NXObject.hxx>
  31. 31 #include <NXOpen/Part.hxx>
  32. 32 #include <NXOpen/PartCollection.hxx>
  33. 33 #include <NXOpen/Session.hxx>
  34. 34
  35. 35 #include <NXOpen/PDM_SoaConnectionHandle.hxx>
  36. 36 #include <NXOpen/PDM_PdmSession.hxx>
  37. 37 #include <NXOpen/PDM_PdmSearch.hxx>
  38. 38 #include <NXOpen/PDM_PdmFile.hxx>
  39. 39 #include <NXOpen/PDM_PdmNavigatorNode.hxx>
  40. 40 #include <NXOpen/PDM_PdmPart.hxx>
  41. 41 #include <NXOpen/PDM_PdmSearchManager.hxx>
  42. 42 #include <NXOpen/PDM_SoaQuery.hxx>
  43. 43 #include <NXOpen/PDM_SearchResult.hxx>
  44. 44
  45. 45 #include <NXOpen/PrintPDFBuilder.hxx>
  46. 46 #include <NXOpen/PlotManager.hxx>
  47. 47 #include <NXOpen/Drawings_DrawingSheet.hxx>
  48. 48 #include <NXOpen/NXObjectManager.hxx>
  49. 49
  50. 50 // Std C++ Includes
  51. 51 #include <iostream>
  52. 52 #include <sstream>
  53. 53 #include <vector>
  54. 54 #include <string>
  55. 55 #include <algorithm>
  56. 56 #include <tchar.h>
  57. 57 #include <atlconv.h>
  58. 58 #include <shellapi.h>
  59. 59
  60. 60 #include <windows.h>
  61. 61 #undef CreateDialog
  62. 62 #pragma comment(lib,"shell32.lib")
  63. 63
  64. 64 #define CREATION_DATE       1
  65. 65 #define MODIFICATION_DATE   2
  66. 66 #define MAX_UGMGR_NAME_LEN 1024
  67. 67
  68. 68 using namespace NXOpen;
  69. 69 using std::string;
  70. 70 using std::exception;
  71. 71 using std::stringstream;
  72. 72 using std::endl;
  73. 73 using std::cout;
  74. 74 using std::cerr;
  75. 75
  76. 76 NXOpen::ListingWindow *lw = NULL;
  77. 77 NXOpen::NXMessageBox *mb = NULL;
  78. 78
  79. 79 #define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
  80. 80 int report_error(char *file, int line, char *call, int code)
  81. 81 {
  82. 82     if (code){
  83. 83         stringstream errmsg;
  84. 84         errmsg << "Error " << code << " in " << file << " at line " << line << endl;
  85. 85         errmsg << call << endl;
  86. 86         cerr << errmsg.str();
  87. 87         throw NXOpen::NXException::Create(code);
  88. 88     }
  89. 89     return(code);
  90. 90 }
  91. 91
  92. 92 void print(const NXString &);
  93. 93 void print(const string &);
  94. 94 void print(const char*);
  95. 95 int invokePdmServer();
  96. 96 int invokePdmServer()
  97. 97 {
  98. 98     int _errCode = 0;
  99. 99     _errCode = UF_CALL(UF_UGMGR_initialize(0, NULL));
  100. 100
  101. 101     char        part_name[MAX_UGMGR_NAME_LEN + 1] = "000015200AA000000";
  102. 102     int         output_code = -1;
  103. 103     char*       date = NULL;
  104. 104
  105. 105     _errCode = UF_CALL(UF_UGMGR_invoke_pdm_server(CREATION_DATE, part_name, &output_code, &date));
  106. 106     print("\nAfter calling UF_UGMGR_invoke_pdm_server()\n");
  107. 107     print("Part          : " + string(part_name));
  108. 108     print("creation date : " + string(date));
  109. 109     print("output_code   : " + std::to_string(output_code) + "\n\n");
  110. 110     UF_free(date);
  111. 111     date = NULL;
  112. 112     
  113. 113     UF_UGMGR_terminate();
  114. 114     return _errCode;
  115. 115 }
  116. 116
  117. 117 extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
  118. 118 {
  119. 119     try
  120. 120     {
  121. 121         invokePdmServer();
  122. 122         return;
  123. 123     }
  124. 124     catch (const NXException& e1)
  125. 125     {
  126. 126         UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
  127. 127     }
  128. 128     catch (const exception& e2)
  129. 129     {
  130. 130         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
  131. 131     }
  132. 132     catch (...)
  133. 133     {
  134. 134         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
  135. 135     }
  136. 136 }
  137. 137
  138. 138 extern "C" DllExport int ufusr_ask_unload()
  139. 139 {
  140. 140     return (int)NXOpen::Session::LibraryUnloadOptionImmediately;// 调试用
  141. 141     //return (int)NXOpen::Session::LibraryUnloadOptionAtTermination;// 程序发布用
  142. 142     //return (int)NXOpen::Session::LibraryUnloadOptionExplicitly;
  143. 143 }
  144. 144
  145. 145 void print(const NXString &msg)
  146. 146 {
  147. 147     if (!lw->IsOpen()) lw->Open();
  148. 148     lw->WriteLine(msg);
  149. 149 }
  150. 150 void print(const string &msg)
  151. 151 {
  152. 152     if (!lw->IsOpen()) lw->Open();
  153. 153     lw->WriteLine(msg);
  154. 154 }
  155. 155 void print(const char * msg)
  156. 156 {
  157. 157     if (!lw->IsOpen()) lw->Open();
  158. 158     lw->WriteLine(msg);
  159. 159 }
复制代码
Teamcenter Handler工程代码:
  1. 1 //======================================
  2. 2 // libA2CustStudy_register_callbacks
  3. 3 //======================================
  4. 4
  5. 5 //=======================
  6. 6 // libA2CustStudy.h
  7. 7 //=======================
  8. 8 #ifndef TCHANDLER_STUDY_H
  9. 9 #define TCHANDLER_STUDY_H
  10. 10
  11. 11 #pragma once
  12. 12 #include <bom/bom.h>
  13. 13 #include <ict/ict_userservice.h>
  14. 14 #include <epm\epm_access_control.h>
  15. 15 #include <epm\epm_task_template_itk.h>
  16. 16 #include <epm\epm.h>
  17. 17 #include <epm\epm_toolkit_iman_utils.h>
  18. 18 #include <epm/epm_toolkit_tc_utils.h>
  19. 19 #include <pie/sample_err.h>
  20. 20 #include <tc/tc.h>
  21. 21 #include <tc/iman.h>
  22. 22 #include <tccore/custom.h>
  23. 23 #include <tccore/aom_prop.h>
  24. 24 #include <user_exits/user_exits.h>
  25. 25 #include <tccore/workspaceobject.h>
  26. 26
  27. 27 #include <tccore/aom.h>
  28. 28 #include <tccore/item.h>
  29. 29 #include <base_utils/Mem.h>
  30. 30
  31. 31 #include "register_custom_user_service.h"
  32. 32
  33. 33
  34. 34 #ifdef __cplusplus
  35. 35 extern "C"
  36. 36 {
  37. 37 #endif
  38. 38
  39. 39     extern DLLAPI int libA2CustStudy_register_callbacks();
  40. 40     int libA2CustStudy_invoke_pdm_server(int *decision, va_list args);
  41. 41
  42. 42 #ifdef __cplusplus
  43. 43 }
  44. 44 #endif
  45. 45
  46. 46 #endif
复制代码
  1. 1 //=======================
  2. 2 // libA2CustStudy.cpp
  3. 3 //=======================
  4. 4 #include "libA2CustStudy.h"
  5. 5
  6. 6 #define CUST_TCHANDLER_STUDY "libA2CustStudy"
  7. 7
  8. 8 extern DLLAPI int libA2CustStudy_register_callbacks()
  9. 9 {
  10. 10     int stat = ITK_ok;
  11. 11     stat = CUSTOM_register_exit(CUST_TCHANDLER_STUDY, "USER_invoke_pdm_server", (CUSTOM_EXIT_ftn_t)libA2CustStudy_invoke_pdm_server);
  12. 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. 13     return stat;
  14. 14 }
复制代码
  1. 1 //=======================
  2. 2 // lib_register_custom.cpp
  3. 3 //=======================
  4. 4 #include "libA2CustStudy.h"
  5. 5
  6. 6 #define CREATION_DATE     1
  7. 7 #define MODIFICATION_DATE 2
  8. 8
  9. 9 #ifdef __cplusplus
  10. 10 extern "C"
  11. 11 {
  12. 12 #endif
  13. 13
  14. 14     int libA2CustStudy_invoke_pdm_server(int *decision, va_list args)
  15. 15     {
  16. 16         /***********  va_list for USER_invoke_pdm_server ***********/
  17. 17         int  input_code = va_arg(args, int);          /* args 1 */
  18. 18         char *input_string = va_arg(args, char *);    /* args 2 */
  19. 19         int  *output_code = va_arg(args, int *);      /* args 3 */
  20. 20         char **output_string = va_arg(args, char **); /* args 4 */
  21. 21         /***********************************************************/
  22. 22         printf("\t libA2CustStudy_invoke_pdm_server \n\n");
  23. 23         *decision = ALL_CUSTOMIZATIONS;
  24. 24
  25. 25         tag_t             item_tag;
  26. 26         int               itkfail;
  27. 27         WSO_description_t description;
  28. 28         char              createDate[WSO_date_size_c + 1];
  29. 29         char              modifyDate[WSO_date_size_c + 1];
  30. 30         char              objectName[WSO_date_size_c + 1];
  31. 31         char              objectType[WSO_date_size_c + 1];
  32. 32         char              ownersName[WSO_date_size_c + 1];
  33. 33         char              owning_group_name[WSO_date_size_c + 1];
  34. 34         char              last_modify_user_name[WSO_date_size_c + 1];
  35. 35         char              object_desc[WSO_date_size_c + 1];
  36. 36         *output_code = 0;
  37. 37         char tmp_output_string[1024] = { 0 };
  38. 38
  39. 39         itkfail = ITEM_find_item(input_string, &item_tag);
  40. 40         if (itkfail != ITK_ok){
  41. 41             printf("Failed to find item %s\n", input_string);
  42. 42             *output_code = 1;
  43. 43         }
  44. 44
  45. 45         itkfail = WSOM_describe(item_tag, &description);
  46. 46         if (itkfail != ITK_ok){
  47. 47             printf("Failed to get description of the item\n");
  48. 48             *output_code = 1;
  49. 49         }
  50. 50
  51. 51         printf("ITK USER_invoke_pdm_server() routine :\n");
  52. 52         printf("INPUT:\n");
  53. 53         printf("input_code    : %d\n", input_code);
  54. 54         printf("input_string  : %s\n\n", input_string);
  55. 55
  56. 56         switch (input_code){
  57. 57         case CREATION_DATE:
  58. 58         case MODIFICATION_DATE:
  59. 59             strcpy_s(object_desc, strlen(description.date_created) + 1, description.description);
  60. 60             strcpy_s(createDate, strlen(description.date_created) + 1, description.date_created);
  61. 61             strcpy_s(modifyDate, strlen(description.date_modified) + 1, description.date_modified);
  62. 62             strcpy_s(objectName, strlen(description.object_name) + 1, description.object_name);
  63. 63             strcpy_s(objectType, strlen(description.object_type) + 1, description.object_type);
  64. 64             strcpy_s(ownersName, strlen(description.owners_name) + 1, description.owners_name);
  65. 65             strcpy_s(owning_group_name, strlen(description.owning_group_name) + 1, description.owning_group_name);
  66. 66             strcpy_s(last_modify_user_name, strlen(description.last_modifying_user_name) + 1, description.last_modifying_user_name);
  67. 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. 68                 createDate, modifyDate, objectName, objectType, ownersName, owning_group_name, last_modify_user_name, object_desc);
  69. 69             break;
  70. 70         default:
  71. 71             printf("OUTPUT:该参数 %d 未定义操作!\n", input_code);
  72. 72             break;
  73. 73         }
  74. 74         if (strlen(tmp_output_string) > 0){
  75. 75             *output_string = (char*)malloc(sizeof(tmp_output_string));
  76. 76             strcpy_s(*output_string, strlen(tmp_output_string) + 1, tmp_output_string);
  77. 77             printf("OUTPUT:\n");
  78. 78             printf("output_string : %s (%s)\n", *output_string, (input_code == 1) ? "CREATION DATE" : "MODIFICATION DATE");
  79. 79             printf("output_code   : %d\n", *output_code);
  80. 80         }
  81. 81         return ITK_ok;
  82. 82     }
  83. 83
  84. 84 #ifdef __cplusplus
  85. 85 }
  86. 86 #endif
复制代码
 
Teamcenter零组件属性截图:

 
 
工程调试运行GIF动图:

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

光之使者

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表