在GC工具里面是有一個重命名裝配組件的命令的,除了這個外,好像沒看到NX里還有其他可以重命名裝配組件的命令,本來以為在UFUN ASSEM裝配的頭文件里會有更改裝配部件名字的函數,但是沒有找到,可能沒有。本來以為UF_ASSEM_rename_instance這個可以,后來發現還不行,這個只能改右鍵屬性名字。
但是我找到了替換組件的函數,我覺得這個是可以實現功能的。后來我按照自己想到的思路,對照着GC工具就寫了一下。
我的思路是這樣的:一個單選對話框->選擇裝配組件->得到tag->由tag得到裝配組件的part名字->把名字顯示到一個字符串窗口->用戶更改名字->copy本地那個裝配組件.prt文件->把新名字命名給copy后的那個.prt文件->
然后用替換組件函數替換copy后的那個.prt文件->最后在把copy前那個舊的.prt文件刪掉
1 //RenameComponent 2 3 // Mandatory UF Includes 4 #include <uf.h> 5 #include <uf_object_types.h> 6 7 // Internal Includes 8 #include <NXOpen/ListingWindow.hxx> 9 #include <NXOpen/NXMessageBox.hxx> 10 #include <NXOpen/UI.hxx> 11 12 // Internal+External Includes 13 #include <NXOpen/Annotations.hxx> 14 #include <NXOpen/Assemblies_Component.hxx> 15 #include <NXOpen/Assemblies_ComponentAssembly.hxx> 16 #include <NXOpen/Body.hxx> 17 #include <NXOpen/BodyCollection.hxx> 18 #include <NXOpen/Face.hxx> 19 #include <NXOpen/Line.hxx> 20 #include <NXOpen/NXException.hxx> 21 #include <NXOpen/NXObject.hxx> 22 #include <NXOpen/Part.hxx> 23 #include <NXOpen/PartCollection.hxx> 24 #include <NXOpen/Session.hxx> 25 26 #include <uf.h> 27 #include <uf_ui.h> 28 #include <uf_modl.h> 29 #include <uf_part.h> 30 #include <uf_curve.h> 31 #include <uf_assem.h> 32 #include <uf_obj.h> 33 #include <windows.h> 34 #include <stdarg.h> 35 #include <strstream> 36 #include <iostream> 37 #include <string> 38 #include <io.h> 39 40 // Std C++ Includes 41 #include <iostream> 42 #include <sstream> 43 44 using namespace NXOpen; 45 using std::string; 46 using std::exception; 47 using std::stringstream; 48 using std::endl; 49 using std::cout; 50 using std::cerr; 51 52 53 //------------------------------------------------------------------------------ 54 // NXOpen c++ test class 55 //------------------------------------------------------------------------------ 56 class MyClass 57 { 58 // class members 59 public: 60 static Session *theSession; 61 static UI *theUI; 62 63 MyClass(); 64 ~MyClass(); 65 66 void do_it(); 67 void print(const NXString &); 68 void print(const string &); 69 void print(const char*); 70 71 int DelectValue;//對話框返回值,決定是否刪除原組件 72 string NameOrPath(const char* Path, int Type);//返回.prt文件名字和所在文件夾路徑 73 void filesearch(string path, int layer);//遍歷文件夾所有.prt文件 74 char part_name[MAX_FSPEC_BUFSIZE];//當前顯示部件.prt文件所在路徑 75 char str[133];//字符串輸入對話框值 76 77 private: 78 Part *workPart, *displayPart; 79 NXMessageBox *mb; 80 ListingWindow *lw; 81 LogFile *lf; 82 }; 83 84 //------------------------------------------------------------------------------ 85 // Initialize static variables 86 //------------------------------------------------------------------------------ 87 Session *(MyClass::theSession) = NULL; 88 UI *(MyClass::theUI) = NULL; 89 90 //------------------------------------------------------------------------------ 91 // Constructor 92 //------------------------------------------------------------------------------ 93 MyClass::MyClass() 94 { 95 96 // Initialize the NX Open C++ API environment 97 MyClass::theSession = NXOpen::Session::GetSession(); 98 MyClass::theUI = UI::GetUI(); 99 mb = theUI->NXMessageBox(); 100 lw = theSession->ListingWindow(); 101 lf = theSession->LogFile(); 102 103 workPart = theSession->Parts()->Work(); 104 displayPart = theSession->Parts()->Display(); 105 106 } 107 108 //------------------------------------------------------------------------------ 109 // Destructor 110 //------------------------------------------------------------------------------ 111 MyClass::~MyClass() 112 { 113 } 114 115 //------------------------------------------------------------------------------ 116 // Print string to listing window or stdout 117 //------------------------------------------------------------------------------ 118 void MyClass::print(const NXString &msg) 119 { 120 if(! lw->IsOpen() ) lw->Open(); 121 lw->WriteLine(msg); 122 } 123 void MyClass::print(const string &msg) 124 { 125 if(! lw->IsOpen() ) lw->Open(); 126 lw->WriteLine(msg); 127 } 128 void MyClass::print(const char * msg) 129 { 130 if(! lw->IsOpen() ) lw->Open(); 131 lw->WriteLine(msg); 132 } 133 134 135 136 static int select_filter_proc_fn(tag_t object, int type[3], void* user_data, UF_UI_selection_p_t select) 137 { 138 if (object == NULL) 139 { 140 return UF_UI_SEL_REJECT; 141 } 142 else 143 { 144 return UF_UI_SEL_ACCEPT; 145 } 146 } 147 148 static int init_proc(UF_UI_selection_p_t select, void* user_data) 149 { 150 int num_triples = 1;//可選類型的數量 151 UF_UI_mask_t mask_triples[] = 152 { UF_component_type, UF_UI_SEL_NOT_A_FEATURE, 153 };//可選對象類型 154 UF_UI_set_sel_mask(select, UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, num_triples, mask_triples); 155 if ((UF_UI_set_sel_procs(select, select_filter_proc_fn, NULL, user_data)) == 0) 156 { 157 return UF_UI_SEL_SUCCESS; 158 } 159 else 160 { 161 return UF_UI_SEL_FAILURE; 162 } 163 } 164 165 166 string MyClass::NameOrPath(const char* Path, int Type) 167 { 168 //反向找位置,分割字符串(只讀取文件夾路徑) 169 string strPath = Path; 170 string strDir; 171 int nPos = strPath.find_last_of('\\'); 172 if (string::npos != nPos) 173 { 174 strDir = strPath.substr(0, nPos); 175 } 176 177 //分割字符串(只讀取part名字+后綴) 178 //方法2 179 int pos = strPath.find_last_of('\\'); 180 string s1(strPath.substr(pos + 1)); 181 182 //分割字符串(只讀取part名) 183 string PartName(s1.substr(0, s1.find(".prt"))); 184 185 if (Type == 1) 186 { 187 return PartName;//返回名字 188 } 189 else if (Type == 2) 190 { 191 return strDir;//返回文件夾路徑 192 } 193 } 194 195 196 void MyClass::filesearch(string path, int layer) 197 { 198 char msg[256] = ""; 199 char msgg[256] = ""; 200 struct _finddata_t filefind; 201 string f; 202 string curr = path + "\\*.*"; 203 int done = 0, i, handle; 204 if ((handle = _findfirst(curr.c_str(), &filefind)) != -1) 205 { 206 while (!(done = _findnext(handle, &filefind))) 207 { 208 if (strcmp(filefind.name, "..") == 0) 209 continue; 210 for (i = 0; i < layer; i++) 211 printf("\t"); 212 if ((_A_SUBDIR == filefind.attrib)) // 是目錄 213 { 214 curr = path + "\\" + filefind.name; 215 sprintf(msg, "%s\n", curr.c_str()); 216 //UF_UI_write_listing_window(msg); 217 filesearch(curr, layer + 1); // 遞歸遍歷子目錄 218 } 219 else // 是文件 220 { 221 222 sprintf(msg, "%s", filefind.name); 223 if (strlen(msg) > 4) 224 { 225 strncpy(msgg, msg + strlen(msg) - 4, 4); 226 if (strcmp(msgg, ".prt") == 0) // 文件格式 227 { 228 f = path + "\\" + filefind.name; 229 sprintf(msg, "%s\n", f.c_str()); 230 //UF_UI_write_listing_window(msg); 231 232 //分割字符串(只讀取part名) 233 string PartName = NameOrPath(f.c_str(), 1); 234 string PartPath = NameOrPath(f.c_str(), 2); 235 236 //分割字符串(只讀取part名) 237 string PartName1 = NameOrPath(part_name, 1); 238 239 //找到名字相同的.prt文件 240 if (PartName == PartName1) 241 { 242 //轉換 243 char NewName1[256]; 244 sprintf(NewName1, "\\%s.prt", str); 245 string NewName2 = NewName1; 246 //字符串拼接 247 string NewName = PartPath + NewName2; 248 249 //copy出一個新的.prt文件 250 CopyFile(f.c_str(), NewName.c_str(), FALSE); 251 252 //由名字得到裝配部件實例的TAG 253 tag_t instanceTAG = UF_ASSEM_ask_instance_of_name(UF_PART_ask_display_part(), part_name); 254 255 //重新命名裝配部件的part名稱 256 UF_PART_load_status_t load_status; 257 UF_ASSEM_use_alternate(&instanceTAG, NewName.c_str(), str, str, &load_status); 258 259 //由對話框值判斷是否刪除原組件 260 if (DelectValue == 5) 261 { 262 //刪除舊名字的.prt文件 263 DeleteFile(f.c_str()); 264 } 265 266 267 } 268 269 } 270 } 271 } 272 } 273 _findclose(handle); 274 } 275 } 276 277 278 //------------------------------------------------------------------------------ 279 // Do something 280 //------------------------------------------------------------------------------ 281 void MyClass::do_it() 282 { 283 284 // TODO: add your code here 285 286 UF_initialize(); 287 288 //單對象選擇對話框 289 char sCue[] = "單對象選擇對話框"; 290 char sTitle[] = "選擇一個裝配組件"; 291 int iScope = UF_UI_SEL_SCOPE_NO_CHANGE; 292 int iResponse; 293 tag_t tView; 294 double adCursor[3]; 295 tag_t ComponentTag = NULL_TAG;//單選控件獲得的tag 296 UF_UI_select_with_single_dialog(sCue, sTitle, iScope, init_proc, NULL, &iResponse, &ComponentTag, adCursor, &tView); 297 298 //獲取裝配部件的part名稱 299 char refset_name[UF_OBJ_NAME_BUFSIZE]; 300 char instance_name[UF_CFI_MAX_FILE_NAME_BUFSIZE]; 301 double origin[3]; 302 double csys_matrix[9]; 303 double transform[4][4]; 304 UF_ASSEM_ask_component_data(ComponentTag, part_name, refset_name, instance_name, origin, csys_matrix, transform); 305 306 //輸入字符串控件 307 char * cue = "輸入框"; 308 //轉換 309 char str1[133]; 310 sprintf(str1, "%s", part_name); 311 //分割字符串(只讀取part名) 312 string PartName1 = NameOrPath(str1, 1); 313 sprintf(str, "%s", PartName1.c_str()); 314 int length = 0; 315 uc1600(cue, str, &length); 316 317 //彈出對話框 318 char sPromptStr1[] = "單選菜單對話框"; 319 int iDefault1 = 0; 320 char asOptions1[][38] = { "刪除原組件", "保留原組件" }; 321 int iNumOfOptions1 = 2; 322 DelectValue = uc1603(sPromptStr1, iDefault1, asOptions1, iNumOfOptions1); 323 324 //獲得當前顯示部件.prt文件所在路徑 325 char part_fspec[MAX_FSPEC_BUFSIZE]; 326 UF_PART_ask_part_name(UF_PART_ask_display_part(), part_fspec); 327 328 //獲取文件夾路徑 329 string strDir = NameOrPath(part_fspec, 2); 330 331 //遍歷文件夾 332 filesearch(strDir, 0); 333 334 UF_terminate(); 335 336 } 337 338 //------------------------------------------------------------------------------ 339 // Entry point(s) for unmanaged internal NXOpen C/C++ programs 340 //------------------------------------------------------------------------------ 341 // Explicit Execution 342 extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen ) 343 { 344 try 345 { 346 // Create NXOpen C++ class instance 347 MyClass *theMyClass; 348 theMyClass = new MyClass(); 349 theMyClass->do_it(); 350 delete theMyClass; 351 } 352 catch (const NXException& e1) 353 { 354 UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message()); 355 } 356 catch (const exception& e2) 357 { 358 UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what()); 359 } 360 catch (...) 361 { 362 UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception."); 363 } 364 } 365 366 367 //------------------------------------------------------------------------------ 368 // Unload Handler 369 //------------------------------------------------------------------------------ 370 extern "C" DllExport int ufusr_ask_unload() 371 { 372 return (int)NXOpen::Session::LibraryUnloadOptionImmediately; 373 } 374 375 Caesar盧尚宇 376 2019年8月12日
補充:
今天又寫了第二種方法(2019年8月13日)
1 //NX9_NXOpenCPP_Wizard2 2 3 // Mandatory UF Includes 4 #include <uf.h> 5 #include <uf_object_types.h> 6 7 // Internal Includes 8 #include <NXOpen/ListingWindow.hxx> 9 #include <NXOpen/NXMessageBox.hxx> 10 #include <NXOpen/UI.hxx> 11 12 // Internal+External Includes 13 #include <NXOpen/Annotations.hxx> 14 #include <NXOpen/Assemblies_Component.hxx> 15 #include <NXOpen/Assemblies_ComponentAssembly.hxx> 16 #include <NXOpen/Body.hxx> 17 #include <NXOpen/BodyCollection.hxx> 18 #include <NXOpen/Face.hxx> 19 #include <NXOpen/Line.hxx> 20 #include <NXOpen/NXException.hxx> 21 #include <NXOpen/NXObject.hxx> 22 #include <NXOpen/Part.hxx> 23 #include <NXOpen/PartCollection.hxx> 24 #include <NXOpen/Session.hxx> 25 26 27 #include <uf.h> 28 #include <uf_assem.h> 29 #include <uf_obj.h> 30 #include <uf_part.h> 31 #include <uf_modl.h> 32 #include <uf_ui.h> 33 #include <windows.h> 34 #include <uf_disp.h> 35 36 37 // Std C++ Includes 38 #include <iostream> 39 #include <sstream> 40 41 using namespace NXOpen; 42 using std::string; 43 using std::exception; 44 using std::stringstream; 45 using std::endl; 46 using std::cout; 47 using std::cerr; 48 49 50 //------------------------------------------------------------------------------ 51 // NXOpen c++ test class 52 //------------------------------------------------------------------------------ 53 class MyClass 54 { 55 // class members 56 public: 57 static Session *theSession; 58 static UI *theUI; 59 60 MyClass(); 61 ~MyClass(); 62 63 void do_it(); 64 void print(const NXString &); 65 void print(const string &); 66 void print(const char*); 67 68 //單選 69 int DelectValue;//對話框返回值,決定是否刪除原組件 70 string NameOrPath(const char* Path, int Type);//返回.prt文件名字和所在文件夾路徑 71 72 private: 73 Part *workPart, *displayPart; 74 NXMessageBox *mb; 75 ListingWindow *lw; 76 LogFile *lf; 77 }; 78 79 //------------------------------------------------------------------------------ 80 // Initialize static variables 81 //------------------------------------------------------------------------------ 82 Session *(MyClass::theSession) = NULL; 83 UI *(MyClass::theUI) = NULL; 84 85 //------------------------------------------------------------------------------ 86 // Constructor 87 //------------------------------------------------------------------------------ 88 MyClass::MyClass() 89 { 90 91 // Initialize the NX Open C++ API environment 92 MyClass::theSession = NXOpen::Session::GetSession(); 93 MyClass::theUI = UI::GetUI(); 94 mb = theUI->NXMessageBox(); 95 lw = theSession->ListingWindow(); 96 lf = theSession->LogFile(); 97 98 workPart = theSession->Parts()->Work(); 99 displayPart = theSession->Parts()->Display(); 100 101 } 102 103 //------------------------------------------------------------------------------ 104 // Destructor 105 //------------------------------------------------------------------------------ 106 MyClass::~MyClass() 107 { 108 } 109 110 //------------------------------------------------------------------------------ 111 // Print string to listing window or stdout 112 //------------------------------------------------------------------------------ 113 void MyClass::print(const NXString &msg) 114 { 115 if(! lw->IsOpen() ) lw->Open(); 116 lw->WriteLine(msg); 117 } 118 void MyClass::print(const string &msg) 119 { 120 if(! lw->IsOpen() ) lw->Open(); 121 lw->WriteLine(msg); 122 } 123 void MyClass::print(const char * msg) 124 { 125 if(! lw->IsOpen() ) lw->Open(); 126 lw->WriteLine(msg); 127 } 128 129 130 131 static int select_filter_proc_fn(tag_t object, int type[3], void* user_data, UF_UI_selection_p_t select) 132 { 133 if (object == NULL) 134 { 135 return UF_UI_SEL_REJECT; 136 } 137 else 138 { 139 return UF_UI_SEL_ACCEPT; 140 } 141 } 142 143 static int init_proc(UF_UI_selection_p_t select, void* user_data) 144 { 145 int num_triples = 1;//可選類型的數量 146 UF_UI_mask_t mask_triples[] = 147 { UF_component_type, UF_UI_SEL_NOT_A_FEATURE, 148 };//可選對象類型 149 UF_UI_set_sel_mask(select, UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, num_triples, mask_triples); 150 if ((UF_UI_set_sel_procs(select, select_filter_proc_fn, NULL, user_data)) == 0) 151 { 152 return UF_UI_SEL_SUCCESS; 153 } 154 else 155 { 156 return UF_UI_SEL_FAILURE; 157 } 158 } 159 160 string MyClass::NameOrPath(const char* Path, int Type) 161 { 162 //反向找位置,分割字符串(只讀取文件夾路徑) 163 string strPath = Path; 164 string strDir; 165 int nPos = strPath.find_last_of('\\'); 166 if (string::npos != nPos) 167 { 168 strDir = strPath.substr(0, nPos); 169 } 170 171 //分割字符串(只讀取part名字+后綴) 172 //方法2 173 int pos = strPath.find_last_of('\\'); 174 string s1(strPath.substr(pos + 1)); 175 176 //分割字符串(只讀取part名) 177 string PartName(s1.substr(0, s1.find(".prt"))); 178 179 if (Type == 1) 180 { 181 return PartName;//返回名字 182 } 183 else if (Type == 2) 184 { 185 return strDir;//返回文件夾路徑 186 } 187 } 188 189 //------------------------------------------------------------------------------ 190 // Do something 191 //------------------------------------------------------------------------------ 192 void MyClass::do_it() 193 { 194 195 // TODO: add your code here 196 197 UF_initialize(); 198 199 //單對象選擇對話框 200 char sCue[] = "單對象選擇對話框"; 201 char sTitle[] = "選擇一個裝配組件"; 202 int iScope = UF_UI_SEL_SCOPE_NO_CHANGE; 203 int iResponse; 204 tag_t tView; 205 double adCursor[3]; 206 tag_t ComponentTag = NULL_TAG;//單選控件獲得的tag 207 UF_UI_select_with_single_dialog(sCue, sTitle, iScope, init_proc, NULL, &iResponse, &ComponentTag, adCursor, &tView); 208 209 //獲取裝配部件的part名稱 210 char part_name[MAX_FSPEC_BUFSIZE];//當前顯示部件.prt文件所在路徑 211 char refset_name[UF_OBJ_NAME_BUFSIZE]; 212 char instance_name[UF_CFI_MAX_FILE_NAME_BUFSIZE]; 213 double origin[3]; 214 double csys_matrix[9]; 215 double transform[4][4]; 216 UF_ASSEM_ask_component_data(ComponentTag, part_name, refset_name, instance_name, origin, csys_matrix, transform); 217 218 //輸入字符串控件 219 char * cue = "輸入框"; 220 //轉換 221 char str1[133]; 222 sprintf(str1, "%s", part_name); 223 //分割字符串(只讀取part名) 224 string PartName1 = NameOrPath(str1, 1); 225 char str[133];//字符串輸入對話框值 226 sprintf(str, "%s", PartName1.c_str()); 227 int length = 0; 228 uc1600(cue, str, &length); 229 230 //彈出對話框 231 char sPromptStr1[] = "單選菜單對話框"; 232 int iDefault1 = 0; 233 char asOptions1[][38] = { "刪除原組件", "保留原組件" }; 234 int iNumOfOptions1 = 2; 235 DelectValue = uc1603(sPromptStr1, iDefault1, asOptions1, iNumOfOptions1); 236 237 //返回原型標識 238 tag_t ComponentPart = UF_ASSEM_ask_prototype_of_occ(ComponentTag); 239 240 //設置為工作部件 241 UF_ASSEM_set_work_part(ComponentPart); 242 243 //獲得當前工作部件.prt文件所在路徑 244 char part_fspec1[MAX_FSPEC_BUFSIZE]; 245 UF_PART_ask_part_name(UF_ASSEM_ask_work_part(), part_fspec1); 246 247 //重命名實例名稱 248 UF_PART_rename(ComponentPart, str); 249 250 //獲得當前顯示部件.prt文件所在路徑 251 char part_fspec[MAX_FSPEC_BUFSIZE]; 252 UF_PART_ask_part_name(UF_PART_ask_display_part(), part_fspec); 253 254 //獲取文件夾路徑 255 string strDir = NameOrPath(part_fspec, 2); 256 257 //轉換 258 char msg[256]; 259 sprintf_s(msg, "\\%s.prt", str); 260 string strDir1 = msg; 261 262 //字符串拼接 263 string strDir2 = strDir + strDir1; 264 265 //另存為改名后的.prt 266 UF_PART_save_as(strDir2.c_str()); 267 268 //把顯示部件設置為工作部件 269 UF_ASSEM_set_work_part(UF_PART_ask_display_part()); 270 271 //部件清理,移除高亮 272 NXOpen::PartCleanup *partCleanup1; 273 partCleanup1 = theSession->NewPartCleanup(); 274 partCleanup1->SetTurnOffHighlighting(true); 275 partCleanup1->DoCleanup(); 276 277 //刪除舊的.prt文件 278 if (DelectValue == 5) 279 { 280 DeleteFile(part_fspec1); 281 } 282 283 UF_terminate(); 284 } 285 286 //------------------------------------------------------------------------------ 287 // Entry point(s) for unmanaged internal NXOpen C/C++ programs 288 //------------------------------------------------------------------------------ 289 // Explicit Execution 290 extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen ) 291 { 292 try 293 { 294 // Create NXOpen C++ class instance 295 MyClass *theMyClass; 296 theMyClass = new MyClass(); 297 theMyClass->do_it(); 298 delete theMyClass; 299 } 300 catch (const NXException& e1) 301 { 302 UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message()); 303 } 304 catch (const exception& e2) 305 { 306 UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what()); 307 } 308 catch (...) 309 { 310 UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception."); 311 } 312 } 313 314 315 //------------------------------------------------------------------------------ 316 // Unload Handler 317 //------------------------------------------------------------------------------ 318 extern "C" DllExport int ufusr_ask_unload() 319 { 320 return (int)NXOpen::Session::LibraryUnloadOptionImmediately; 321 } 322 323 324 Caesar盧尚宇 325 2019年8月13日
補充2019年11月18日
今天有網友合工大-wzl給我留言,糾正了一個bug,在這個例子中,這個函數tag_t instanceTAG = UF_ASSEM_ask_instance_of_name(UF_PART_ask_display_part(), part_name);
里面的這個輸入參數,part_name,不應該是part的名字,應該是裝配部件的實例名稱instance_name。這個地方是我的一個疏忽,因為默認情況下應該part名字和實例名字是一個名字。
感謝網友糾正的錯誤。