Teamcenter_NX集成开发:UF_UGMGR函数的使用
最近工作中经常使用Teamcenter、NX集成开发的情况,因此在这里记录UF_UGMGR函数的使用。使用UF_UGMGR相关函数需要有Teamcenter使用经验,理解Teamcenter中文件夹、伪文件夹、零组件、零组件版本、数据集、关系、表单、命名引用等对象。
相关的可以看帮助中 Teamcenter Integration for NX的内容 及 UF_UGMGR Overview 介绍的数据模型及使用方法、注意事项。
主要操作:
1-初始化UGMGR环境成功后就可以连接到Teamcenter。
2-获取指定ID编码零组件标识和Latest Working(没记错的话应该是最新版本)版本ID。
3-UF_UGMGR相关函数使用
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 include <stdio.h> 35 include <stdlib.h> 36 37 using namespace NXOpen; 38 using namespace Teamcenter::Soa::Client; 39 using namespace Teamcenter::Services::Core; 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 static int indent_level = 0; 48 49 define CHECK( func_ ) \ 50 ifail = (func_); \ 51 if (ifail != 0) {\ 52 printf("ERROR: %s returned %d", func_, ifail); \ 53 return ifail;} 54 55 define PRINT( content_ ) \ 56 { int ii; \ 57 for (ii = 0; ii < indent_level; ii++) \ 58 { printf(" "); } \ 59 printf content_; \ 60 printf("\n"); } 61 62 static int list_in_part(UF_UGMGR_tag_t part) 63 { 64 int _errCode = 0; 65 UF_UGMGR_tag_t* revisions; 66 int i, j, k; 67 int count; 68 int dep_count; 69 int rev_count; 70 char** names; 71 char** file_types; 72 char** file_names; 73 char part_name[MAX_FSPEC_SIZE + 1]; 74 char revision_id[UF_UGMGR_NAME_SIZE + 1]; 75 char part_number[UF_UGMGR_NAME_SIZE + 1]; 76 if (part == UF_UGMGR_null_tag) 77 return -1; 78 79 _errCode = UF_UGMGR_ask_part_number(part, part_number);// 获取零组件ItemID 80 PRINT(("Part: %s", part_number)); 81 indent_level++; 82 _errCode = UF_UGMGR_list_part_revisions(part, &rev_count, &revisions);// 获取零组件所有版本 83 for (j = 0; j < rev_count; j++) 84 { 85 _errCode = UF_UGMGR_ask_part_revision_id(revisions[j], revision_id);// 获取版本ID 86 _errCode = UF_UGMGR_encode_part_filename(part_number, revision_id, "", "", part_name);// encode文件名 87 PRINT(("Rev.: %s - Type: %s", revision_id, "master")); 88 indent_level++; 89 90 _errCode = UF_UGMGR_ask_dependent_files(part_name, &dep_count, &names);// 获取关联文件 91 for (k = 0; k < dep_count; k++) 92 PRINT(("Dependent files: %s", names[k])); 93 94 indent_level--; 95 if (dep_count > 0) 96 UF_free_string_array(dep_count, names); 97 98 _errCode = UF_UGMGR_list_part_rev_files(revisions[j], &count, &file_types, &file_names);// 列出版本下数据集 99 for (i = 0; i < count; i++) 100 { 101 _errCode = UF_UGMGR_encode_part_filename(part_number, revision_id, file_types[i], file_names[i], part_name);// 根据ItemID、版本、文件类型、文件名encode成新的文件名 102 PRINT(("Rev.: %s - Type: %s, Name: %s", revision_id, file_types[i], file_names[i])); 103 indent_level++; 104 _errCode = UF_UGMGR_ask_dependent_files(part_name, &dep_count, &names);// 获取关联文件 105 for (k = 0; k < dep_count; k++) 106 PRINT(("Dependent files: %s", names[k])); 107 108 indent_level--; 109 if (dep_count > 0) 110 UF_free_string_array(dep_count, names); 111 } 112 if (count > 0){ 113 UF_free_string_array(count, file_types); 114 UF_free_string_array(count, file_names); 115 } 116 } 117 indent_level--; 118 UF_free(revisions); 119 revisions = NULL; 120 return _errCode; 121 } 122 123 static int list_in_folder(UF_UGMGR_tag_t folder) 124 { 125 int _errCode = 0; 126 char folder_name[UF_UGMGR_NAME_SIZE + 1]; 127 if (folder == UF_UGMGR_null_tag) return -1; 128 129 _errCode = UF_UGMGR_ask_folder_name(folder, folder_name);// 获取文件夹名称 130 PRINT(("Folder: %s", folder_name)); 131 indent_level++; 132 133 UF_UGMGR_object_type_t object_type; 134 int root_folder_contents_count = 0; 135 UF_UGMGR_tag_t * folder_contents = NULL; 136 _errCode = UF_UGMGR_list_folder_contents(folder, &root_folder_contents_count, &folder_contents);// 列出文件夹下的所有文件 137 for (int idx = 0; idx < root_folder_contents_count; idx++) 138 { 139 _errCode = UF_UGMGR_ask_object_type(folder_contents[idx], &object_type);// 获取文件类型 140 switch (object_type) 141 { 142 case UF_UGMGR_type_folder: 143 list_in_folder(folder_contents[idx]);// 是文件夹 144 break; 145 case UF_UGMGR_type_part: 146 list_in_part(folder_contents[idx]);// 是零组件 147 break; 148 default: 149 printf("Invalid object type found"); 150 break; 151 } 152 } 153 UF_free(folder_contents); 154 folder_contents = NULL; 155 indent_level--; 156 return _errCode; 157 } 158 159 void do_it() 160 { 161 int _errCode = 0; 162 163 tag_t partTag = NULL_TAG; 164 UF_PART_load_status_t error_status; 165 UF_UGMGR_tag_t database_part_tag = NULL_TAG; 166 UF_UGMGR_tag_t part_revision = NULL_TAG; 167 UF_UGMGR_tag_t root_folder_tag = NULL_TAG; 168 UF_UGMGR_tag_t user_folder_tag = NULL_TAG; 169 170 char current_rule[UF_UGMGR_NAME_BUFSIZE] = { 0 }; 171 char revision_id[UF_UGMGR_NAME_BUFSIZE] = { 0 }; 172 char export_dir_name[MAX_FSPEC_BUFSIZE] = { 0 }; 173 char encoded_name[MAX_FSPEC_BUFSIZE] = { 0 }; 174 char user_role[UF_UGMGR_ROLE_BUFSIZE] = { 0 }; 175 176 _errCode = UF_UGMGR_ask_part_tag("000000000AA000082", &database_part_tag);// 获取编码为000000000AA000082的部件标识 177 _errCode = UF_UGMGR_ask_config_rule(current_rule);// 获取默认配置规则 178 std::cout << "默认配置规则:" << current_rule << std::endl; 179 _errCode = UF_UGMGR_set_config_rule("Latest Working");// 设置配置规则 180 _errCode = UF_UGMGR_ask_configured_rev(database_part_tag, &part_revision);// 获取配置版本 181 _errCode = UF_UGMGR_ask_part_revision_id(part_revision, revision_id);// 获取版本ID 182 183 _errCode = UF_UGMGR_ask_root_folder(&root_folder_tag);// 获取用户根文件夹,即HOME文件夹标识 184 _errCode = UF_UGMGR_ask_user_folder("infodba", &user_folder_tag);// 获取用户文件夹 185 _errCode = UF_UGMGR_ask_user_role(user_role);// 获取用户角色 186 187 std::cout << "\n=================用户文件夹下文件=================" << std::endl; 188 _errCode = list_in_folder(root_folder_tag);// 列出该文件夹下地下所有文件夹和零组件 189 std::cout << "=================用户文件夹下文件=================\n" << std::endl; 190 191 int config_rules_count = 0; 192 char ** config_rules = NULL; 193 _errCode = UF_UGMGR_list_config_rules(&config_rules_count, &config_rules);// 列出所有配置规则,并打印到控制台 194 std::cout << "\n=================所有配置规则=================" << std::endl; 195 for (int idx = 0; idx < config_rules_count; idx++) 196 std::cout << config_rules[idx] << std::endl; 197 UF_free(config_rules); 198 config_rules = NULL; 199 std::cout << "=================所有配置规则=================\n" << std::endl; 200 201 int file_count = 0; 202 char** file_types = NULL; 203 char** file_names = NULL; 204 _errCode = UF_UGMGR_list_part_rev_files(part_revision, &file_count, &file_types, &file_names);// 列出版本下所有数据集文件 205 if (file_count <= 0) return; 206 207 for (int idx = 0; idx < file_count; idx++){ 208 if (file_names[idx] == NULL) continue; 209 210 _errCode = UF_UGMGR_encode_part_filename("000000000AA000082", revision_id, file_types[idx], file_names[idx], encoded_name);// encode组成文件名 211 _errCode = UF_PART_open(encoded_name, &partTag, &error_status);// NX后台打开 212 _errCode = UF_UGMGR_ask_export_directory(partTag, export_dir_name);// 获取export文件夹目录 213 std::cout << "临时export文件夹目录:" << export_dir_name << std::endl; 214 _errCode = UF_PART_free_load_status(&error_status); 215 _errCode = UF_PART_close(partTag, 1, 1); 216 } 217 if (file_count > 0){ 218 UF_free_string_array(file_count, file_names); 219 UF_free_string_array(file_count, file_types); 220 } 221 file_types = NULL; 222 file_names = NULL; 223 } 224 225 //=================== 226 // Entry Point 227 //=================== 228 int main(int argc, char* argv[]) 229 { 230 try{ 231 int _errCode = 0; 232 const char** consolePara = (const char**)(argv); 233 logical is_active; 234 _errCode = UF_is_ugmanager_active(&is_active);// 判断ugmanager环境是否已经初始化 235 if (!is_active) 236 _errCode = UF_UGMGR_initialize(argc, consolePara);// 初始化ugmanager环境 237 do_it(); 238 _errCode = UF_UGMGR_terminate(); 239 return _errCode; 240 } 241 catch (const NXException& e1){ 242 cerr << "NXException: " << e1.ErrorCode() << endl; 243 cerr << e1.Message() << endl; 244 } 245 catch (const exception& e2){ 246 cerr << "Exception: " << e2.what() << endl; 247 } 248 catch (...){ 249 cerr << "Unknown Exception: " << endl; 250 } 251 }
调试截图:
NX版本配置规则截图:
版本规则解释:
Teamcenter对象介绍:中文是自动翻译的,不准确。详细请看帮助文档:Siemens 文档: Teamcenter Integration for NX