Исходники для Unigraphics NX копирование подсборки в координаты точек поверхности


Добавил:DMT
Дата создания:5 апреля 2008, 21:39
Дата обновления:5 апреля 2008, 21:54
Просмотров:6100 последний позавчера, 0:48
Комментариев: 0

Копирование подсборки в координаты точек поверхности

Копирование подсборки в координаты точек поверхности. Порядок действий:
  • Выбор компоненты для тиражирования
  • Выбор базовой систему координат CSYS
  • Выбор поверхности
  • Запрос точек на поверхности
  • Копирование компоненты
Код на C++
  1. /* Include files */
  2. #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
  3. # include <strstream>
  4. # include <iostream>
  5. using std::ostrstream;
  6. using std::endl;
  7. using std::ends;
  8. using std::cerr;
  9. #else
  10. # include <strstream.h>
  11. # include <iostream.h>
  12. #endif
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <math.h>
  19.  
  20. #include <uf.h>
  21. #include <uf_ui.h>
  22. #include <uf_exit.h>
  23. #include <uf_obj.h>
  24. #include <uf_object_types.h>
  25. #include <uf_disp.h>
  26. #include <uf_modl.h>
  27. #include <uf_part.h>
  28. #include <uf_vec.h>
  29. #include <uf_mtx.h>
  30. #include <uf_assem.h>
  31. #include <uf_csys.h>
  32. #include <uf_vec.h>
  33.  
  34. static FILE* prterr;
  35. static int errorCode;
  36. static tag_t part;
  37.  
  38. #define PRTERR if(errorCode)\
  39. {PrintErrorMessage(errorCode,__LINE__);goto term;}
  40.  
  41. //процедура расшифровки кода ошибки
  42. static void PrintErrorMessage(int errorCode, int line)
  43. {
  44. if(errorCode)
  45. {
  46. char message[133];
  47.  
  48. UF_get_fail_message(errorCode,message);
  49. UF_UI_set_status(message);
  50. fprintf(prterr,"errorCode=%d\tmessage=%s\tline=%d\n",errorCode,message,line);
  51. fflush(prterr);
  52. errorCode=0;
  53. }
  54. }
  55.  
  56. int component_init_proc(UF_UI_selection_p_t select, void* user_data)
  57. {
  58. int ret_code;
  59. int num_triples=1;
  60. UF_UI_mask_t mask_triples[]={UF_component_type,0,0};
  61.  
  62. ret_code=UF_UI_set_sel_mask
  63. (select,UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,num_triples,mask_triples);
  64.  
  65. if(ret_code==0)
  66. return UF_UI_SEL_SUCCESS;
  67.  
  68. return UF_UI_SEL_FAILURE;
  69.  
  70. }
  71.  
  72. int csys_init_proc(UF_UI_selection_p_t select, void* user_data)
  73. {
  74. int ret_code;
  75. int num_triples=1;
  76. UF_UI_mask_t mask_triples[]={UF_coordinate_system_type,0,0};
  77.  
  78. ret_code=UF_UI_set_sel_mask
  79. (select,UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,num_triples,mask_triples);
  80.  
  81. if(ret_code==0)
  82. return UF_UI_SEL_SUCCESS;
  83.  
  84. return UF_UI_SEL_FAILURE;
  85.  
  86. }
  87.  
  88. int face_init_proc(UF_UI_selection_p_t select, void* user_data)
  89. {
  90. int ret_code;
  91. int num_triples=1;
  92. UF_UI_mask_t mask_triples[]={UF_face_type,0,0};
  93.  
  94. ret_code=UF_UI_set_sel_mask
  95. (select,UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,num_triples,mask_triples);
  96.  
  97. if(ret_code==0)
  98. return UF_UI_SEL_SUCCESS;
  99.  
  100. return UF_UI_SEL_FAILURE;
  101.  
  102. }
  103.  
  104. int point_init_proc(UF_UI_selection_p_t select, void* user_data)
  105. {
  106. int ret_code;
  107. int num_triples=1;
  108. UF_UI_mask_t mask_triples[]={UF_point_type,0,0};
  109.  
  110. ret_code=UF_UI_set_sel_mask
  111. (select,UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,num_triples,mask_triples);
  112.  
  113. if(ret_code==0)
  114. return UF_UI_SEL_SUCCESS;
  115.  
  116. return UF_UI_SEL_FAILURE;
  117.  
  118. }
  119.  
  120. void print_mtx3(double mtx[3],char* mes,int line)
  121. {
  122. fprintf(prterr,"\n %s line=%d\n",mes,line);
  123. fprintf(prterr,"\n\t%12.4f\t%12.4f\t%12.4f\n",
  124. mtx[0],mtx[1],m
  125. 2471
  126. tx[2]);
  127. }
  128.  
  129. void print_mtx9(double mtx[9],char* mes,int line)
  130. {
  131. fprintf(prterr,"\n %s line=%d\n",mes,line);
  132. fprintf(prterr,"\t%12.4f\t%12.4f\t%12.4f\n",
  133. mtx[0],mtx[1],mtx[2]);
  134. fprintf(prterr,"\t%12.4f\t%12.4f\t%12.4f\n",
  135. mtx[3],mtx[4],mtx[5]);
  136. fprintf(prterr,"\t%12.4f\t%12.4f\t%12.4f\n",
  137. mtx[6],mtx[7],mtx[8]);
  138. }
  139.  
  140. void do_it()
  141. {
  142. tag_t* component_group;
  143. int component_count;
  144. //выбор компонент
  145. label_get_components:
  146. {
  147. char* message="Выбери компоненты";
  148. char* title="Выбери компоненты";
  149. int scope=UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY;
  150. int response;
  151. int count;
  152. tag_p_t object;
  153.  
  154. errorCode=UF_UI_select_with_class_dialog
  155. (message,title,scope,component_init_proc,NULL,&response,&count,&object);PRTERR;
  156.  
  157. switch(response)
  158. {
  159.  
  160. case UF_UI_BACK:
  161. goto label_get_components;
  162.  
  163. case UF_UI_CANCEL:
  164. goto term;
  165.  
  166. case UF_UI_OK:
  167. break;
  168.  
  169. }
  170. if(count==0)
  171. {
  172. uc1601("необходимо выбрать компоненты!",1);
  173. goto label_get_components;
  174. }
  175.  
  176. int i;
  177.  
  178. for(i=0;i<count;i++)
  179. {
  180. //set highlight off
  181. errorCode=UF_DISP_set_highlight(object[i],0);PRTERR;
  182. }
  183. component_group=object;
  184. component_count=count;
  185. }
  186. //выбор базовой системы координат
  187. tag_t csys_tag;
  188. double csys_origin[3];
  189. double matrix_values[9];
  190.  
  191. label_get_csys:
  192. //
  193. {
  194. char* message="Выбери базовую csys";
  195. char* title="Выбери базовую csys";
  196. int scope=UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY;
  197. int response;
  198. tag_t object;
  199. double cursor[3];
  200. tag_t view;
  201. tag_t matrix_id;
  202.  
  203.  
  204. errorCode=UF_UI_select_with_single_dialog
  205. (message,title,scope,csys_init_proc,NULL,&response,&object,cursor,&view);PRTERR;
  206. switch(response)
  207. {
  208. case UF_UI_BACK:
  209. goto label_get_components;
  210.  
  211. case UF_UI_CANCEL:
  212. goto term;
  213.  
  214. case UF_UI_OK:
  215. break;
  216.  
  217. case UF_UI_OBJECT_SELECTED:
  218. break;
  219.  
  220. case UF_UI_OBJECT_SELECTED_BY_NAME:
  221. break;
  222. }
  223.  
  224.  
  225. if(object==NULL_TAG)
  226. {
  227. uc1601("Необходимо выбрать базовую csys",1);
  228. goto label_get_csys;
  229. }
  230. //set highlight off
  231. errorCode=UF_DISP_set_highlight(object,0);PRTERR;
  232.  
  233. csys_tag=object;
  234.  
  235. errorCode=UF_CSYS_ask_csys_info (csys_tag,&matrix_id,csys_origin);PRTERR;
  236.  
  237. errorCode=UF_CSYS_ask_matrix_values (matrix_id,matrix_values);PRTERR;
  238. }
  239. //выбор грани
  240. tag_t face_tag;
  241. label_get_face:
  242. //
  243. {
  244. char* message="Выбери базовую face";
  245. char* title="Выбери базовую face";
  246. int scope=UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY;
  247. int response;
  248. tag_t object;
  249. double cursor[3];
  250. tag_t view;
  251.  
  252. errorCode=UF_UI_select_with_single_dialog
  253. (message,title,scope,face_init_proc,NULL,&response,&object,cursor,&view);PRTERR;
  254. switch(response)
  255. {
  256. case UF_UI_BACK:
  257. goto label_get_csys;
  258.  
  259. case UF_UI_CANCEL:
  260. goto term;
  261.  
  262. case UF_UI_OK:
  263. break;
  264.  
  265. case UF_UI_OBJECT_SELECTED:
  266. break;
  267.  
  268. case UF_UI_OBJECT_SELECTED_BY_NAME:
  269. break;
  270. }
  271.  
  272. if(object==NULL_TAG)
  273. {
  274. uc1601("Будьте любезны выбрать базовую face",1);
  275. goto label_get_face;
  276. }
  277.  
  278. //set highlight off
  279. errorCode=UF_DISP_set_highlight(object,0);PRTERR;
  280.  
  281. face_tag=object;
  282.  
  283.  
  284. }
  285.  
  286. tag_t* point_group;
  287. int point_count;
  288. //выбор точек
  289. label_get_points:
  290. {
  291. char* message="Выбери точки";
  292. char* title="Выбери точки";
  293. int scope=UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY;
  294. int response;
  295. int count;
  296. tag_p_t object;
  297.  
  298. errorCode=UF_UI_select_with_class_dialog
  299. (message,title,scope,point_init_proc,NULL,&response,&count,&object);PRTERR;
  300.  
  301. switch(response)
  302. {
  303.  
  304. case UF_UI_BACK:
  305. goto label_get_face;
  306.  
  307. case UF_UI_CANCEL:
  308. goto term;
  309.  
  310. case UF_UI_OK:
  311. break;
  312.  
  313. }
  314.  
  315. if(count==0)
  316. {
  317. uc1601("будьте любезны выбрать точки!",1);
  318. goto label_get_points;
  319. }
  320. int i;
  321. for(i=0;i<count;i++)
  322. {
  323. //set highlight off
  324. errorCode=UF_DISP_set_highlight(object[i],0);PRTERR;
  325. }
  326. point_group=object;
  327. point_count=count;
  328. }
  329. //тиражирование компонент
  330. {
  331. int i,j;
  332. double point_coords[3];
  333. double parm[2];
  334. double face_pnt[3];
  335. double u1[3],v1[3],u2[3],v2[3],unit_norm[3],radii[2];
  336. double mtx9[9];
  337.  
  338. char part_name[ 256 + 1 ];
  339. char refset_name[ 30 + 1 ];
  340. char instance_name[ ( 128 + 1 ) ];
  341. double origin[ 3 ];
  342. double csys_matrix[ 9 ];
  343. double transform[ 4 ][ 4 ];
  344. tag_t instance;
  345. UF_PART_load_status_t error_status;
  346. double mtx16[16];
  347.  
  348. //цикл по точкам поверхности
  349. for(i=0;i<point_count;i++)
  350. {
  351. //получить координаты точки <point_coords>
  352. errorCode=UF_CURVE_ask_point_data(point_group[i],point_coords);PRTERR;
  353.  
  354. //получить параметры в точке поверхности <parm>
  355. errorCode=UF_MODL_ask_face_parm(face_tag,point_coords,parm,face_pnt);PRTERR;
  356.  
  357. //получить производные по u v
  358. errorCode=UF_MODL_ask_face_props(face_tag,parm,face_pnt,u1,v1,u2,v2,unit_norm,radii);PRTERR;
  359. //нормализовать u и v
  360. errorCode=UF_MTX3_initialize (u1,v1,mtx9);
  361. if(errorCode)
  362. {
  363. uc1601("Matrix cannot be defined",1);
  364. fprintf(prterr,"\n 1 = Matrix cannot be defined line=%d\n",__LINE__);
  365. }
  366.  
  367. //построить преобразование от базовой системы координат в систему координат точки поверхности
  368. errorCode=UF_MTX4_csys_to_csys
  369. (csys_origin,matrix_values+0,matrix_values+3,point_coords,mtx9+0,mtx9+3,mtx16);PRTERR;
  370. //print_mtx9(mtx9,"mtx9",__LINE__);
  371.  
  372.  
  373. //цикл по компонентам агрегата
  374. for(j=0;j<component_count;j++)
  375. {
  376. //получить данные компонента
  377. errorCode=UF_ASSEM_ask_component_data (component_group[j],part_name,refset_name,
  378. instance_name,origin,csys_matrix,transform);PRTERR;
  379. //print_mtx9(csys_matrix,"csys_matrix до преобразования",__LINE__);
  380. //print_mtx3(origin,"origin",__LINE__);
  381. //превратить направляющие косинусы в точки
  382. UF_VEC3_add(origin,csys_matrix+0,csys_matrix+0);
  383. UF_VEC3_add(origin,csys_matrix+3,csys_matrix+3);
  384. //print_mtx9(csys_matrix,"csys_matrix направляющие косинусы перешли в точки",__LINE__);
  385. //перевести точки в новую систему координат
  386. UF_MTX4_vec3_multiply (origin,mtx16,origin);
  387. //print_mtx3(origin,"origin новый",__LINE__);
  388. UF_MTX4_vec3_multiply (csys_matrix+0,mtx16,csys_matrix+0);
  389. UF_MTX4_vec3_multiply (csys_matrix+3,mtx16,csys_matrix+3);
  390. //print_mtx9(csys_matrix,"csys_matrix направляющие косинусы перешли в новые точки",__LINE__);
  391. UF_VEC3_sub (csys_matrix+0,origin,csys_matrix+0);
  392. UF_VEC3_sub (csys_matrix+3,origin,csys_matrix+3);
  393. //print_mtx9(csys_matrix,"csys_matrix новые направляющие косинусы ",__LINE__);
  394. errorCode=UF_ASSEM_add_part_to_assembly
  395. (part,part_name,refset_name,instance_name,origin,csys_matrix,0,&instance,&error_status);PRTERR;
  396. }
  397.  
  398. }
  399.  
  400. }
  401.  
  402. goto term;
  403. term:
  404. return;
  405. }
  406.  
  407. /*****************************************************************************
  408. ** Activation Methods
  409. *****************************************************************************/
  410. /* Explicit Activation
  411. ** This entry point is used to activate the application explicitly, as in
  412. ** "File->Execute UG/Open->User Function..." */
  413. extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
  414. {
  415. //переменные обработки времени
  416. struct tm* newtime;
  417. time_t aclock,start_time,finish_time;
  418. double work_time;
  419. int min,sec;
  420.  
  421. time(&start_time);
  422.  
  423. //попытка открыть файл ошибок на D:
  424. if((prterr=fopen("D:\\prterr.txt","w"))!=NULL)
  425. {
  426. //uc1601("жми ОК и работай дальше",1);
  427. goto label_clock;
  428. }
  429.  
  430.  
  431. //попытка открыть файл ошибок на C:\TEMP
  432. if((prterr=fopen("C:\\temp\\prterr.txt","w"))!=NULL)
  433. {
  434. //uc1601("жми ОК и работай дальше",1);
  435. goto label_clock;
  436. }
  437.  
  438. //файл ошибок разместить не удалось
  439. uc1601("не могу разместить файл ошибок",1);
  440. return;
  441.  
  442.  
  443. label_clock:
  444. time(&aclock);
  445. newtime=localtime(&aclock);
  446.  
  447. fprintf(prterr,"\n<bolt_gaika_shaiba> HAS STARTED line=%d\n\n",__LINE__);
  448.  
  449. fprintf(prterr,"время начала работы программы: %s\n\n",asctime(newtime));
  450.  
  451. errorCode=UF_initialize();PRTERR;
  452. //проверь открытие файла модели или сборки
  453. part=UF_PART_ask_display_part();
  454. if(part==NULL_TAG)
  455. {
  456. uc1601("открой непустой файл!",1);
  457. fprintf(prterr,"\n открой непустой файл line=%d\n",__LINE__);
  458. goto term;
  459. }
  460. /* TODO: Add your application code here */
  461. do_it();
  462. errorCode=UF_terminate();PRTERR;
  463.  
  464. term:
  465. time(&finish_time);
  466. work_time=difftime(finish_time,start_time);
  467. min=(int)work_time/60;
  468. sec=(int)(work_time-min*60);
  469.  
  470. fprintf(prterr,"\n<bolt_gaika_shaiba> HAS ENDED line=%d\n\n",__LINE__);
  471. fprintf(prterr,"working time is %u min %u sec\n",min,sec);
  472.  
  473. fflush(prterr);
  474. fclose(prterr);
  475.  
  476. return;
  477. }
  478.  
  479. /*****************************************************************************
  480. ** Utilities
  481. *****************************************************************************/
  482.  
  483. /* Unload Handler
  484. ** This function specifies when to unload your application from Unigraphics.
  485. ** If your application registers a callback (from a MenuScript item or a
  486. ** User Defined Object for example), this function MUST return
  487. ** "UF_UNLOAD_UG_TERMINATE". */
  488. extern int ufusr_ask_unload( void )
  489. {
  490. return( UF_UNLOAD_IMMEDIATELY );
  491. }
При использовании обязательна ссылка на http://DMTSoft.ru
up