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

博客 分享
0 231
优雅殿下
优雅殿下 2023-03-25 17:26:41
悬赏:0 积分 收藏

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

之前了解到通过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 <uf.h>
  8 include <uf_object_types.h>
  9 include <uf_draw.h>
 10 include <uf_part.h>
 11 include <uf_ugmgr.h>
 12 
 13 include <NXSigningResource.cpp>
 14 
 15 // Internal Includes
 16 include <NXOpen/ListingWindow.hxx>
 17 include <NXOpen/NXMessageBox.hxx>
 18 include <NXOpen/UI.hxx>
 19 include <NXOpen/LogFile.hxx>
 20 
 21 // Internal+External Includes\
 22 include <NXOpen/Annotations.hxx>
 23 include <NXOpen/Assemblies_Component.hxx>
 24 include <NXOpen/Assemblies_ComponentAssembly.hxx>
 25 include <NXOpen/Body.hxx>
 26 include <NXOpen/BodyCollection.hxx>
 27 include <NXOpen/Face.hxx>
 28 include <NXOpen/Line.hxx>
 29 include <NXOpen/NXException.hxx>
 30 include <NXOpen/NXObject.hxx>
 31 include <NXOpen/Part.hxx>
 32 include <NXOpen/PartCollection.hxx>
 33 include <NXOpen/Session.hxx>
 34 
 35 include <NXOpen/PDM_SoaConnectionHandle.hxx>
 36 include <NXOpen/PDM_PdmSession.hxx>
 37 include <NXOpen/PDM_PdmSearch.hxx>
 38 include <NXOpen/PDM_PdmFile.hxx>
 39 include <NXOpen/PDM_PdmNavigatorNode.hxx>
 40 include <NXOpen/PDM_PdmPart.hxx>
 41 include <NXOpen/PDM_PdmSearchManager.hxx>
 42 include <NXOpen/PDM_SoaQuery.hxx>
 43 include <NXOpen/PDM_SearchResult.hxx>
 44 
 45 include <NXOpen/PrintPDFBuilder.hxx>
 46 include <NXOpen/PlotManager.hxx>
 47 include <NXOpen/Drawings_DrawingSheet.hxx>
 48 include <NXOpen/NXObjectManager.hxx>
 49 
 50 // Std C++ Includes
 51 include <iostream>
 52 include <sstream>
 53 include <vector>
 54 include <string>
 55 include <algorithm>
 56 include <tchar.h>
 57 include <atlconv.h>
 58 include <shellapi.h>
 59 
 60 include <windows.h>
 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     try
120     {
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_H
10 
11 pragma once
12 include <bom/bom.h>
13 include <ict/ict_userservice.h>
14 include <epm\epm_access_control.h>
15 include <epm\epm_task_template_itk.h>
16 include <epm\epm.h>
17 include <epm\epm_toolkit_iman_utils.h>
18 include <epm/epm_toolkit_tc_utils.h>
19 include <pie/sample_err.h>
20 include <tc/tc.h>
21 include <tc/iman.h>
22 include <tccore/custom.h>
23 include <tccore/aom_prop.h>
24 include <user_exits/user_exits.h>
25 include <tccore/workspaceobject.h>
26 
27 include <tccore/aom.h>
28 include <tccore/item.h>
29 include <base_utils/Mem.h>
30 
31 include "register_custom_user_service.h"
32 
33 
34 ifdef __cplusplus
35 extern "C"
36 {
37 endif
38 
39     extern DLLAPI int libA2CustStudy_register_callbacks();
40     int libA2CustStudy_invoke_pdm_server(int *decision, va_list args);
41 
42 ifdef __cplusplus
43 }
44 endif
45 
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 __cplusplus
10 extern "C"
11 {
12 endif
13 
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 __cplusplus
85 }
86 endif

 

Teamcenter零组件属性截图:

 

 

工程调试运行GIF动图:

 

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

posted @ 2023-03-25 16:53  huangym1  阅读(3)  评论(0编辑  收藏  举报
回帖
    优雅殿下

    优雅殿下 (王者 段位)

    2017 积分 (2)粉丝 (47)源码

    小小码农,大大世界

     

    温馨提示

    亦奇源码

    最新会员