青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

青华模具培训学院
查看: 7361|回复: 5

[教程] 一个ug api二次开发写的关于CAM的例子

  [复制链接]
发表于 2014-3-3 11:51 | 显示全部楼层 |阅读模式

这个例子里面的思路是比较值得参考的。


7 F  g. r/ S9 E, T9 T! y

原帖地址:http://www.ugufun.com/?p=80


+ b6 H8 C* _" ]9 h4 X" Q7 f: A

#include <stdlib.h>2 [0 F, y# a. p0 @) C. G: Z
#include <stdio.h>

#include <uf.h>2 e7 v1 l; ^+ Q8 f0 |7 H  k2 W
#include <uf_object_types.h>" w4 C5 Z6 ]% ^6 O0 h
#include <uf_ui.h># Y9 V. E0 I$ D# ?! I$ M) s9 W
#include <uf_disp.h>: E/ Z( V! S9 I; v! [2 S9 @
#include <uf_obj.h>) ?$ s! l7 p4 ?2 e- s8 d
#include <uf_cam.h>/ M) I; G5 Y+ e5 ~
#include <uf_camgeom.h>

#include <ufd_camgeom_ugroup.h>

#define MAX_CAMGEOM 20
1 u) c2 o' c% J#define EXIT_SUCCESS 0

/*#define DEBUG*/

static char *title1 = "Select an API routine...";

static int geoption;7 o1 e3 a% C8 P# Q$ V' _7 r
static char *title3 = "Select Geometry Type(s) for operation then Selection Complete";//创建几何
- `6 w  q3 b1 C- Bstatic char menu3[][38] = { "1) Part Geometry",
; C, W" E: C3 P& X0 b  U"2) Blank Geometry",
/ U+ O7 G8 G3 N: l"3) Drive Geometry",
) w7 y+ F) E. u1 z6 ?7 Q6 Q"4) Cut Area Geometry",
: {; k6 {' n0 C: A7 c; O3 o$ x"5) Trim Geometry",
) J: U/ V. r# j; W/ Y6 \6 O"6) Selection Complete"};

static int init_proc(UF_UI_selection_p_t select, void *user_data);//初始化程序

static void init_camgeom_app_data(UF_CAMGEOM_app_data_p_t app_data);

int ufd_camgeom_ugroup( tag_t objTag, int obj_count )

{
. R/ y. L5 Z, ^( F* Q- MUF_CAMGEOM_app_data_p_t app_data_list[MAX_CAMGEOM];. T: f1 r4 K2 f  @; z& U
tag_t *objects, entity_list[MAX_CAMGEOM];7 X+ Z) T, i5 k6 A
int i, entity_count, object_count, err_code, type, subtype;

char atitle[] = "Geometry Selection.";8 r( R9 n5 D5 G# Q5 H2 _# t* d, \, j
char *cue = "Identify Geometry - Wait for the Filter!";: p. I; k4 R' ^) _' W# Q/ J  f4 I
char errorstrg[133];7 N, x8 v+ P9 H/ q7 Y# L1 ~, |
int response, irc;
  U+ Z$ s. ~. w$ H1 u+ h( Wtag_t eid, view;
/ j" x+ C$ V# r/ f7 vdouble cursor[3];

#ifdef DEBUG- l. H2 y: ^1 Z3 Q" W
printf( "Object tag passed into camgeom_ugroup is %d \n", objTag );
' o: S0 a) z# q#endif

geoption = 0;

while ( geoption < 10 )//为什么是10呢????
6 h% n8 ^5 O# w. ]{: q  z" @: ^/ j& U# R6 i
/* Statements go here for each geometry selection type. 声明起作用 对于选择类型的每个几何体*/

geoption = uc1603(title3, 1, menu3, 6);

if (geoption == 1 || geoption == 2 || geoption == 10 )! f) I/ B4 u7 c& r7 U
{
2 P& u; [- E' N( M: l. H: @break;
, ~! o& d( z9 k! C( {, q}
1 V( s% R5 k0 _. ?, U#ifdef DEBUG8 J8 C) U7 j! s4 h( i
printf("\n Geometry option returns %d\n", geoption);
4 }; ~7 ]5 F7 l/ Y8 c4 U7 o#endif$ A1 O* v0 [# Q: b8 F6 ^& m4 d% T. }
/* Allow the user to select faces or bodies. 选取面和实体*/

/* Pass in this function the object to be assigned geometry 通过函数,对象被分配到几何体 */

if (obj_count > 0 )
: F, r4 u+ d9 Q4 ~* b1 }( f: N. Z{/ c9 f- p  w+ ?( `
entity_count = 0;

while (TRUE)

{

UF_UI_select_with_single_dialog(cue,atitle,
$ ?9 F/ T; E4 {9 D: xUF_UI_SEL_SCOPE_NO_CHANGE, init_proc, NULL,
, \( O9 F3 P8 o6 R&response, &eid, cursor, &view);//选择单个对象,可输名也可打进去。选择范围在UF_UI中定义
5 ~' {4 Z* Y5 e//cue 提示显示的信息,atitle 对话框头标题,UF选取范围,init初始化程序,NULL初始化程序,cursor光标位置坐标

if (response == UF_UI_OBJECT_SELECTED)
' f1 J# N( u$ o( f  }{4 g2 u& v3 Y: g- n9 N( O+ G
entity_list[entity_count] = eid;
2 c) f7 D% Y- Y9 L#ifdef DEBUG
' r/ A% e! S; a! ^- x  U4 O& \2 tprintf(" EID number returns %d\n", eid );1 h+ Y: X3 n* C- T/ M
#endif

/* Allocate the memory for the application data of an item. 为当前的应用数据分配内存*/

app_data_list[entity_count] = (UF_CAMGEOM_app_data_p_t)UF_allocate_memory(sizeof(UF_CAMGEOM_app_data_t),' Q3 H6 G9 H  x- H! }
&err_code);

/* Initialize the application data.初始化应用数据 */

init_camgeom_app_data(app_data_list[entity_count]);

entity_count++;9 }& K0 U4 K8 Q+ k! L
}
5 r" s: \+ L9 {( R4 l& helse if (response == UF_UI_OK || UF_UI_BACK || UF_UI_CANCEL )
+ O2 J5 |' E6 W' S  z{+ S  z* e/ i3 K: c
/* Geometry selection is finished.几何体选取完成 */
' j  O" C# I* y3 R7 l7 Nbreak;/ k  B$ ]5 w& P
}
5 n! `$ e0 O; s$ g) J6 M}
1 S! M/ c* b% i( f+ p6 V% @/ s) U  D1 H#ifdef DEBUG
% k. \# M' c. W6 y+ U' f) Dprintf(" Entity count this selection is %d\n",entity_count);' \  ]* V' I  f2 x% R
#endif

if (entity_count > 0): M' }% K: \6 Q4 z' @8 i  ^' d
{* Q# |5 d: t8 s, k9 L& N& k' n
/* UF_CAMGEOM_item_t *items;
4 `+ y( O9 b- C3 c+ aint item_count;*/

for( i = 0; i < obj_count; i++ )
/ V$ n$ A5 G$ m1 ]1 ^; {) ]{
) P, U7 R" O" n. _2 g% wirc = UF_OBJ_ask_type_and_subtype( objTag, &type, &subtype );//返回对象类型与子类型TAG值

if( type == UF_machining_operation_type ||: @/ Q0 O# R4 {3 Q
type == UF_machining_geometry_grp_type)

#ifdef DEBUG
+ Q, a( ]8 X8 k% j( g4 jprintf("\n Current entity count is %d", entity_count);  b+ r4 O4 S( l& U. ?8 b
printf("\n Current object count is %d", obj_count);8 A4 ~; V9 _; u/ U. ^1 t
printf("\n Geometry option is %d \n", geoption );
; f0 M1 n9 T% r# J, s#endif

/* Above prints to help in debugging if "DEBUG" defined. */

{( x" D" y( q0 r8 T9 Z0 f
switch (geoption)7 o, L# _7 t- @; Y$ P
{
( Z1 p9 o  }7 B9 J! A8 rcase 1:$ g0 z  V# p' R4 V
case 2:
" f  d$ C9 l5 g( U. }6 c2 Z! jcase 3:, G, h1 y2 z% I+ P! w+ c6 R
case 4:8 R, |* G4 l* k3 Y$ h. r
case 5: /* Part Geometry 部件几何体*/
6 D5 J+ P+ S4 o9 O) k* O  R{
+ \0 q5 e* J! n6 M8 D* d6 {# L- X#ifdef DEBUG
5 P& b: h$ n+ g6 h' L: @printf("\n Type returned is %d", type);
. T, V/ W. B; |4 B" T7 L5 Eprintf("\n Subtype returned is %d", subtype);
6 X- T8 \9 G% y( C+ q  Y' p' q#endif
: ~+ _( }, _+ ^. O) _( S: f  a{7 G; B  Q. e9 V! X; T$ h
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_part, entity_count,
) ^3 B) A& _' kentity_list, app_data_list );
7 j8 Q/ A2 ?) R//附加一个几何实体链表给对象 几何体包括实体等 切削区域仅薄壁实体和面 修剪的几何体不允许。
  o) {( y* q% l( h& P& @5 a; d% d//objtag几何体父级组,UF几何体类型,en_list链表指针,app_data_list链表数据。
$ y, G4 A! u9 w& k" e}
) N, |) F4 z% Z$ `  }. x; Q8 \if ( irc != 0 )
6 M: X. F3 a% Y' R5 t, ~% ]{
. ]. C/ p1 H  l+ c  X: OUF_get_fail_message( irc, errorstrg );
. i8 M* X/ b' z* t* Aprintf("\n The return code is %d\n", irc);# Q& h; \$ k; D/ Q) j5 D1 F
printf("\n Error code translates to %s\n", errorstrg);
4 r& `" V. {: ?! g0 i/ ^4 breturn objTag;
4 x2 ^5 B; ^4 e  W: J3 P}
4 b. d/ |+ J9 e( i2 D& s% Hbreak;
' e/ F- G/ w/ ]}

case 6: /* Blank Geometry 空白几何体*/
4 i  |( [* Y' @8 V: \- w* X! K{
3 I/ m3 {  u9 X8 g7 F' o/ q#ifdef DEBUG
5 L# [; S& z4 Vprintf("\n Type returned is %d", type);/ I% U- Z4 I6 w. T0 T
printf("\n Subtype returned is %d", subtype);! z- l) I1 x" h% O
#endif6 Q4 w5 t7 f) K$ x2 O+ S
{7 a8 C4 Q, g% @
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_blank, entity_count,7 a: }1 C- Q3 y7 O% E  L$ x( w$ y
entity_list, app_data_list );3 s! V9 Q- s$ L& c' W( ^4 G& ]; S0 b
}
6 T1 ]1 W) A" O$ q: [if ( irc != 0 )) z. x5 ^1 R1 `
{7 S3 m* }7 P$ X! B2 M1 v
UF_get_fail_message( irc, errorstrg );
  _2 X* y. y0 q- @8 g+ k: d6 Fprintf("\n The return code is %d\n", irc);0 s3 c; n# _+ U: H
printf("\n Error code translates to %s\n", errorstrg);' n1 T9 h' R/ F& K2 b
return objTag;9 e# j1 h. P! L. J
}) x; \' Z7 a& y' r3 @
break;

}

case 7: /* Check Geometry 检查几何体 驱动*/
1 J# Y2 a" _8 e% S; i, r7 o{
, Q: F* }$ ~/ D. E#ifdef DEBUG, N) Z/ D- p8 i
printf("\n Type returned is %d", type);
: h! b  e2 X  ~" ^" Oprintf("\n Subtype returned is %d", subtype);
5 _! y4 O" m. L, `2 G, Z#endif
  I* r9 t9 `. H5 Z; p$ |. E{
: n/ G" f1 e# Y/ I  a: F% Qirc=UF_CAMGEOM_append_items(objTag,UF_CAM_drive,entity_count,1 h3 P8 p3 n; N$ e2 _, a' y
entity_list,app_data_list);( D& X4 R1 H8 ]- P, I# y
}
+ P: R* z) K) r8 {8 pif ( irc != 0 )
% j/ M0 i. k  C  ^{" u5 M* q6 v' }' J. i/ o5 R
UF_get_fail_message( irc, errorstrg );
) @1 t0 m& {% Y4 nprintf("\n The return code is %d\n", irc);$ L+ o0 ~$ r; w6 t7 k1 e
printf("\n Error code translates to %s\n", errorstrg);( w6 s" d: Z/ Y. `5 e3 f. ]
return objTag;& R5 p8 o+ V3 F! D- Y
}
5 E5 ?9 F& ]8 U% D3 N/ e, ^break;

}

case 8: /* Cut Area Geometry 切削区域几何体*/) v" T8 w; w6 `! Y) S) n8 K4 T
{, n0 y  o% \2 {  e/ A
#ifdef DEBUG- j* h6 q& K) A4 `: w9 g% ?- {
printf("\n Type returned is %d", type);7 A: E( g% R% Y+ p: ^
printf("\n Subtype returned is %d", subtype);
2 g; [% e  y8 k" t. `" X- _* u#endif5 w( }, h* y; h0 i7 t, j# Y: p8 Y2 g
{
$ _7 o3 z/ [1 l' Firc = UF_CAMGEOM_append_items( objTag, UF_CAM_cut_area, entity_count,
/ \8 V' y% Y5 }! ]  _entity_list, app_data_list );) i6 x2 H/ W0 u' `& `5 x
}5 D' L4 C9 X, W' j+ M' {& m2 z
if ( irc != 0 )
7 q. y& Z; P7 M  _+ I{
: y" |* Y7 X2 ?( E& K  {' RUF_get_fail_message( irc, errorstrg );
3 w; C2 y* z) @$ e; q) b1 P6 q0 }printf("\n The return code is %d\n", irc);2 l& }' `3 K' e: F; M
printf("\n Error code translates to %s\n", errorstrg);. P( N  p: P& ~0 U4 o
return objTag;
$ [% W1 h9 j) _+ A/ U- x}8 Z( G) L" O3 {1 ]0 R$ [
break;
% |  k- _0 @' Q# w" I) h3 z2 ~}
! v$ K' F: F/ @6 Y  |case 9: /* Trim Geometry 修剪几何体 */& h; `7 |# x0 F
{) ?7 e+ N; b3 d3 D% {; }* F
printf("This case not implemented.\n");
. h6 B7 h; D& n4 Abreak;
* Z6 k/ g* O* t/ l, k0 u}' @( @& T5 e. H! ~+ v* G
/*Drive Geometry 驱动几何体*/

default: /* Selection Complete 完成选取 表达式没有相应的则走这条语句*/
# ]$ f& F% ~7 Z: ~( v- V6 R{$ r' j- z( [; m6 Y
break;
  ?- `1 B/ }6 ?( j5 }- z}
! a# G+ ?: L6 x0 l5 P0 g& |}: B2 E. W4 u: N/ G/ g% q3 k
}
. n) L& d9 @9 \0 r; n}/ V% ^& S+ t+ T
}0 `, g0 B+ C* E* B
}
/ F8 Z( S$ s" _1 B- q}
9 G' k  E  c) b, b/* Free the Allocated Memory. 释放内存*// r0 P# |6 {% F  A
for (i=0; i<entity_count; i++)
$ R/ w0 |# R, e{1 P3 t) F* h# E
UF_free (app_data_list);
1 V; _4 g9 s, L/ \2 i}

/* UF_free (objects);*/

return(0);
6 W8 i+ D  ]/ J2 M. d& C}  D) ?& E4 ~/ i) N5 s9 y
/* Selection initialization procedure 选择初始化程序*/; W9 b5 H$ l7 E
static int init_proc
  U7 e% O3 Q% T& ?(- o1 b; U* B* t) Q9 C
UF_UI_selection_p_t select,//指针 pointer# w# }* y1 L: G, n7 K% J) e
void* user_data+ `- \8 x0 l; m7 J+ l
)
, ~5 r' Y+ c8 C. N; Z& O$ y{9 p* t) I  n, @: p% Y6 m5 l' |
int num_triples = 3;% X) k7 \' E0 I9 m! S: f" n
//最终指定的特征类型
# j3 M0 A+ z$ s( U. k+ o' tUF_UI_mask_t mask_triples[] = {
2 ^  l# i& B* G) [: O- R% n7 VUF_line_type, 0, 0,& G6 [1 H8 t2 ~9 j; n
UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE,
- e/ r7 l# e$ p4 w8 G0 W  rUF_solid_type, 0, UF_UI_SEL_FEATURE_BODY};

/* Enable Faces and Solid Bodies允许选取面和实体 */

if((UF_UI_set_sel_mask(select,
6 h) i& F2 G/ p, LUF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,- a6 Z' z# r! j; I' T! T* o8 H5 L
num_triples, mask_triples)) == 0)//3倍 3*3=9

{
/ J- q( p* d' o0 z9 j) freturn (UF_UI_SEL_SUCCESS);
$ t0 A, S% W: @) C}; n2 c( d& C+ L% x( H2 ]0 w
else' d7 a( c5 t$ {7 d
{2 X' t$ S/ G9 z
return (UF_UI_SEL_FAILURE);
* ~; B( `. f7 t* R0 F# c3 {! I+ O}
$ R" A& p2 W5 Y9 q9 O% F}
8 H3 h% N" f3 a+ i* E1 i, estatic void init_camgeom_app_data' @* F. Z1 I0 x+ G: y4 S, q
(
9 w% B. ~2 `# |, _1 C+ [  V7 rUF_CAMGEOM_app_data_p_t app_data
& M  _2 T( J; }2 w)
( _7 s; j- z, g# K' x! A4 W% G{6 \" d" r$ ]7 F/ U2 g
if (app_data)' w8 k" W1 Y! X' ^, O
{& l- V4 n: G% T
/* Set flags. 设置标识*/

app_data->has_stock = 0; /* Used 1 - Else 0 */$ f% c3 J2 ^- {) C$ f
app_data->has_cut_stock = 0; /* Used 1 - Else 0 */
8 C0 X% y- D0 r, ?8 n" `2 S$ l6 gapp_data->has_tolerances = 0 ; /* Used 1 - Else 0 */' H7 N3 }9 i% U+ g( d1 C; p
app_data->has_feedrate = 0 ; /* Used 1 - Else 0 */$ w! e, H1 h5 t3 u
app_data->has_offset = 0; /* Used 1 - Else 0 */
5 D0 g) P5 z$ g. `# f5 Q% A; rapp_data->has_avoidance_type = 0 ; /* Used 1 - Else 0 */

/* Set values.设置参数值 */$ t) p9 O; K: [4 S
app_data->stock = 0.033 ;+ Z. c4 l9 Q9 v, F% P4 C9 ?
app_data->cut_stock[0] = 0.2 ;
: M4 A, H5 Z$ Y% {  lapp_data->cut_stock[1] = 0.1 ;; G8 j. @4 M  g, V/ B* U( Q
app_data->cut_stock[2] = 0.5 ;
6 {1 k: I- X7 Zapp_data->tolerances[0] = 0.003 ;
7 M$ }4 W# f" O- d1 b$ h  ?& b! qapp_data->tolerances[1] = 0.003 ;- ^$ s' W& d5 E/ S4 \( d
app_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;
( d7 Y. A3 K6 Lapp_data->feedrate_value = 33.0 ;1 x3 G7 V( j. S5 K
app_data->offset = 0.055 ;$ l6 g' `  |4 y! A# q- C# ?- ]
app_data->avoidance_type = UF_CAM_avoidance_type_warning;
6 @5 a( ?' w. I}. l8 E) m; L, |  K' I
}

6 Q7 V% n, s. u/ N7 s0 J
发表于 2014-3-4 20:31 | 显示全部楼层
求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 2 反对 0

使用道具 举报

发表于 2014-6-7 09:56 | 显示全部楼层
例子不错,谢谢分享!
回复 支持 1 反对 0

使用道具 举报

发表于 2016-7-30 08:33 | 显示全部楼层
把UG自带的例子发上来也不多加点中文注释。。
回复 支持 反对

使用道具 举报

发表于 2016-9-9 11:36 | 显示全部楼层

+ T$ n* |8 ~4 Z" q9 ?* d求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|关于我们|sitemap|小黑屋|Archiver|手机版|UG网-UG技术论坛-青华数控模具培训学校 ( 粤ICP备15108561号 )

GMT+8, 2024-5-2 05:41 , Processed in 0.054164 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表