青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

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


# Z4 d  g% |! r- E! }8 K

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


% }2 z6 i, [% d

#include <stdlib.h># s+ i8 U7 a6 H5 I+ _
#include <stdio.h>

#include <uf.h>1 d9 M/ Q! p8 B' X9 F& U$ F
#include <uf_object_types.h>. N: Y2 a+ h! G! Y  K
#include <uf_ui.h>+ k8 i- M( K6 R- y% W& k0 p4 h
#include <uf_disp.h>
/ {8 z2 E$ f, {- }0 e8 o#include <uf_obj.h># d* |' Y0 x2 x' N; V* S
#include <uf_cam.h>
+ J) q/ D+ E; C1 [& m2 A+ f#include <uf_camgeom.h>

#include <ufd_camgeom_ugroup.h>

#define MAX_CAMGEOM 20
; {! y" H7 b! n/ R  i& {9 V- C#define EXIT_SUCCESS 0

/*#define DEBUG*/

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

static int geoption;
( K- G2 r' R. z# z7 D7 ^static char *title3 = "Select Geometry Type(s) for operation then Selection Complete";//创建几何& ]# u+ ]3 a7 X. n& x6 O. u1 K
static char menu3[][38] = { "1) Part Geometry",
' ]% F  D7 v! U& w"2) Blank Geometry",4 n4 t; j5 {! v2 k8 S. H' f
"3) Drive Geometry",
# w  p8 R6 Z: v7 ^  L6 W1 ["4) Cut Area Geometry",9 w- @, @, I) @3 \$ L
"5) Trim Geometry",  J+ q& o5 O: _0 a: G
"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 )

{! ?3 S" P- W$ _: \8 _/ ?3 ?6 {! G
UF_CAMGEOM_app_data_p_t app_data_list[MAX_CAMGEOM];
! B4 T- U3 p! U. Itag_t *objects, entity_list[MAX_CAMGEOM];+ L5 B! I# g2 Q8 |
int i, entity_count, object_count, err_code, type, subtype;

char atitle[] = "Geometry Selection.";. p( l/ u  C) _! o7 B: ~3 M
char *cue = "Identify Geometry - Wait for the Filter!";0 v, {7 x8 n) N! r
char errorstrg[133];* w8 ~' I8 y8 R* z: ^, I
int response, irc;
1 o9 Z) J# p9 \$ J6 Etag_t eid, view;4 |& m5 U0 U# i2 b  D$ t2 l
double cursor[3];

#ifdef DEBUG! U  l) L& E9 l- }
printf( "Object tag passed into camgeom_ugroup is %d \n", objTag );
4 L5 W; u: b% J" }0 U$ C% `#endif

geoption = 0;

while ( geoption < 10 )//为什么是10呢????' w# C, h' {: X' ^, l/ {' A
{7 R' T) w& \: h  b& b8 f! U
/* Statements go here for each geometry selection type. 声明起作用 对于选择类型的每个几何体*/

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

if (geoption == 1 || geoption == 2 || geoption == 10 )
" B  k7 k$ P, L+ h4 F{4 n* X2 |) ^* g% P7 [- p
break;8 y+ @; z- X4 K( Z
}/ W, m3 K4 y0 c& g" R
#ifdef DEBUG
# w6 d! P3 n& t6 @- [printf("\n Geometry option returns %d\n", geoption);
4 S3 D' W5 e9 y$ @. L7 l#endif
/ |/ h0 d, V4 s) u* v/* Allow the user to select faces or bodies. 选取面和实体*/

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

if (obj_count > 0 )
5 r9 D; h- b, |: D1 {% x{
; a- \* G8 Q/ z" l1 L; Zentity_count = 0;

while (TRUE)

{

UF_UI_select_with_single_dialog(cue,atitle,' k9 L1 d3 i: v- D. D$ O% ~9 ^
UF_UI_SEL_SCOPE_NO_CHANGE, init_proc, NULL,
8 _( e9 L- _& ^( X4 @&response, &eid, cursor, &view);//选择单个对象,可输名也可打进去。选择范围在UF_UI中定义
( N$ |' [- d2 N! m* V  L//cue 提示显示的信息,atitle 对话框头标题,UF选取范围,init初始化程序,NULL初始化程序,cursor光标位置坐标

if (response == UF_UI_OBJECT_SELECTED); i$ o8 n+ i! p4 U3 D0 G
{, x: k/ W- V, e' u1 h
entity_list[entity_count] = eid;  a. G! _+ g; [7 y& p. J% W( l
#ifdef DEBUG
! ~& w  A  x9 k# O& @printf(" EID number returns %d\n", eid );/ J7 Q8 s3 `1 B8 m8 M+ j: t
#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),
9 r* F; G" O3 }1 e2 A&err_code);

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

init_camgeom_app_data(app_data_list[entity_count]);

entity_count++;
: U# L$ B  G* T# @$ {0 \4 ~}
3 {; ^( \9 F6 s. y" ~else if (response == UF_UI_OK || UF_UI_BACK || UF_UI_CANCEL )8 L+ S' G3 \, c' [
{
8 l* P0 O8 I  k' t/* Geometry selection is finished.几何体选取完成 */2 f% k; K+ F6 u  R+ `3 p+ b
break;0 [2 A' ?! R- K$ C0 C1 G: G4 w
}; |, B# U, f) s$ e% l! I, P
}3 g0 U) D1 B" q. ^5 I
#ifdef DEBUG
2 E; L) @: C* u  J2 hprintf(" Entity count this selection is %d\n",entity_count);) P$ s: _/ U" H0 N" _+ _
#endif

if (entity_count > 0), F9 f6 n  D) Y: n" p" |
{
% j# y$ |- ^& C  G/* UF_CAMGEOM_item_t *items;
% A" z9 j. ?: @2 p" kint item_count;*/

for( i = 0; i < obj_count; i++ )
( R) M0 @3 g* _' n9 A{
) x) y. g" v% Z; G: X- K2 L" wirc = UF_OBJ_ask_type_and_subtype( objTag, &type, &subtype );//返回对象类型与子类型TAG值

if( type == UF_machining_operation_type ||" r, s! E# |  E. R8 p8 |
type == UF_machining_geometry_grp_type)

#ifdef DEBUG  h& I# N, D, C. C( T$ P. b: }
printf("\n Current entity count is %d", entity_count);, g1 {$ A5 y: J" K' L! u' I
printf("\n Current object count is %d", obj_count);
9 L/ A% b7 j* U& L% }printf("\n Geometry option is %d \n", geoption );2 g7 U; k# p6 G# x) h9 `% v! }
#endif

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

{
1 d/ _6 I  L1 z" n+ ]+ e# cswitch (geoption)5 c& _" r5 [" A* z9 g; r# K
{
$ F0 l6 O+ z2 ncase 1:2 \, b- B7 `/ }' W- k! I
case 2:
+ U# M9 M& d( ^! i. ^/ V/ Lcase 3:6 v: M$ ~9 n# t( Q" Z2 @
case 4:
( f* S3 v1 e" f6 ]0 h3 w  Q4 ncase 5: /* Part Geometry 部件几何体*/" `  F2 c% U) K/ x/ M% @
{
) f5 B- `# ]5 ~5 ~#ifdef DEBUG- ^  A# j3 @/ A5 V! f
printf("\n Type returned is %d", type);
! z) ?4 V+ M: Eprintf("\n Subtype returned is %d", subtype);
: B1 f. o: N) H) S7 Y#endif7 f- ?: h5 E  ~/ a5 [: ]
{; N( v" w$ C! c
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_part, entity_count,
3 p* f4 j. T+ u2 x: P3 C7 R: Centity_list, app_data_list );
8 k, I  L2 o, n; g1 `//附加一个几何实体链表给对象 几何体包括实体等 切削区域仅薄壁实体和面 修剪的几何体不允许。' Z- C) n/ i8 |1 S3 e
//objtag几何体父级组,UF几何体类型,en_list链表指针,app_data_list链表数据。6 [- z% p- Q5 ^# K* k5 x
}5 a" x; p/ @6 p1 u/ x( a: u
if ( irc != 0 )
  N, r# L# E# l  B) J: Q{
. q4 q' K. ^  f- I$ i0 YUF_get_fail_message( irc, errorstrg );* y7 p7 U* U! w! V
printf("\n The return code is %d\n", irc);, x( y- X2 Z) @5 i. i& h- g5 Z
printf("\n Error code translates to %s\n", errorstrg);3 F  K9 m0 [9 h* r: n
return objTag;; x1 y$ i' N9 u3 i' {& {3 }5 D$ C
}
2 I( O9 C3 @7 j+ Jbreak;- d3 z2 W" w  X% J4 t9 ^
}

case 6: /* Blank Geometry 空白几何体*/( b: _% g5 _' _) L9 j
{
  Q, M# j6 v* k1 a6 r#ifdef DEBUG
  \( _* d! q: F  Q/ Iprintf("\n Type returned is %d", type);3 j0 N. P. \' T1 T. {( ~6 f2 m
printf("\n Subtype returned is %d", subtype);" c) e& J# `  M' K( _# |
#endif
1 z( [0 L6 S" B& }% N{
2 f# U( W& s, Y( }( rirc = UF_CAMGEOM_append_items( objTag, UF_CAM_blank, entity_count,/ e5 I5 X% j  D. ~
entity_list, app_data_list );
% h9 A& ~; F6 E* l}$ ^, t- ]4 Y* O) M
if ( irc != 0 )
  L2 g- @: y0 S4 V{: Q( \+ z- Q8 L; x+ m9 {( b! C! K
UF_get_fail_message( irc, errorstrg );* U3 ]  x7 p$ Q( Q8 t
printf("\n The return code is %d\n", irc);" X2 T; u0 w+ g1 E, t; P
printf("\n Error code translates to %s\n", errorstrg);
8 }" x3 a: E) Q/ [, ~' m3 c- C: w; Jreturn objTag;2 s5 X0 r2 U; X& ~& G
}# `. B/ R# _. z7 e9 ]
break;

}

case 7: /* Check Geometry 检查几何体 驱动*/
. g" X  X; c3 ^. ?+ T{4 C0 {+ b4 I8 H9 T) T2 E9 R
#ifdef DEBUG
* T9 o: p/ Q  ]& X+ Z  a: `7 qprintf("\n Type returned is %d", type);
5 Q. g! p, K. u2 @printf("\n Subtype returned is %d", subtype);
: o) o1 I3 Y# z+ g#endif
8 H: b) T5 _7 A0 n/ @3 [  h& F{
7 `/ F- ]0 V: H$ b' Lirc=UF_CAMGEOM_append_items(objTag,UF_CAM_drive,entity_count,8 m2 s: w% |* D% m0 H: d
entity_list,app_data_list);: p4 x7 w6 ^# V: Y+ b6 ]
}
) u8 m% Q% t0 ~6 U# Z" {if ( irc != 0 )
- N' o  E' W6 T% c7 n- m( m{3 e% W' J" j0 C+ n
UF_get_fail_message( irc, errorstrg );
" n  J+ L  o9 }8 i5 i- A8 gprintf("\n The return code is %d\n", irc);
+ t8 w, t5 c& H& p  f3 ?$ R5 eprintf("\n Error code translates to %s\n", errorstrg);
7 n4 s( P. x# K5 v$ _- Wreturn objTag;
8 r$ |  y: R1 q! x+ U}
. D( H) L  C; t; m1 P% @break;

}

case 8: /* Cut Area Geometry 切削区域几何体*/
! F% @/ \3 I  X% g% y$ H/ Z+ A, m  ?+ {{
/ y. G0 l+ d2 \% ]% B#ifdef DEBUG2 l3 Y; \+ E% _7 y+ [8 \4 X
printf("\n Type returned is %d", type);
. v2 F, G& j) J6 p- O& S" ^& Mprintf("\n Subtype returned is %d", subtype);
# E% S( s& u$ ]/ u# p. Y$ _. t#endif5 e/ |. a8 m+ q- g  j* x7 Z
{9 H8 z) e& A0 n, l) e2 R
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_cut_area, entity_count,
! H+ o6 U5 u) J5 }entity_list, app_data_list );: p. z0 ^! n( {' N& c
}8 @0 [7 c" D/ I
if ( irc != 0 ): J; b) [. x& I1 f* {
{/ }0 V& c$ X7 b# _
UF_get_fail_message( irc, errorstrg );
9 h. h6 J: a4 j5 ~2 \+ u/ ?- n5 Rprintf("\n The return code is %d\n", irc);0 b' p8 H5 [" f& h' L; P
printf("\n Error code translates to %s\n", errorstrg);
3 G8 E& x9 P2 I+ P( ~return objTag;6 ^, H7 W- J% ~& U0 {# u3 v: h
}
$ ?- C+ Y- H3 y! f( Lbreak;
$ g+ b! |/ \# o: M7 t4 L# E9 a}) v# E5 c* x1 L
case 9: /* Trim Geometry 修剪几何体 */0 T  z; e, K. W+ y& [' q
{
! {1 q  w/ e3 c% d# y# [printf("This case not implemented.\n");
( P" {$ L% f6 R* `break;: u! M( l* T1 k
}4 u1 f5 Z7 o$ W5 H
/*Drive Geometry 驱动几何体*/

default: /* Selection Complete 完成选取 表达式没有相应的则走这条语句*/: i' Q  _, _$ X/ _
{7 n# U( l/ o2 X& Y) l+ D& f, K
break;
7 U, I2 G0 S' P}6 ]+ D* Z6 P% k) `% E' {1 P& x3 ~+ w
}
- ^5 N- f% h* w6 }- m}
! w% T' p) n$ C+ A% t* n}! U8 _9 b# Z, B& O$ m
}! r6 ]* B1 P. k; y& S- f
}4 b2 }" e, D2 n, X: W% S6 w" z! [
}: o' \# k- i: o. v6 S7 L' m
/* Free the Allocated Memory. 释放内存*/4 n5 G7 G$ X) u& {) E0 g( I
for (i=0; i<entity_count; i++)
& Z0 G8 Y% }! I' I6 `{6 |3 q( k9 t; Q$ u% ~% s! o6 y3 W
UF_free (app_data_list);
. h/ X9 U9 y' ?3 J}

/* UF_free (objects);*/

return(0);
0 f8 x: i: b8 c1 E' D" ^}
9 T1 [; U' v' r1 ?$ \/* Selection initialization procedure 选择初始化程序*/. r* c. A/ Z3 }9 {0 L. q
static int init_proc% a1 m8 Y6 Y8 j: i" g& x; N
(3 ?& \# Q0 C. p) S! D3 K) f/ y1 z
UF_UI_selection_p_t select,//指针 pointer
/ r6 O) h6 Y: I! G9 h2 B% @void* user_data
% R0 L7 ]6 V/ U) M3 O)
7 i, r5 e- h" G# Q  Y" J0 v{# `) r, h& x, F( S, K3 ]
int num_triples = 3;
$ K+ A  a& V9 W' j! v0 x  e//最终指定的特征类型
3 a9 y) }- H' @% {! sUF_UI_mask_t mask_triples[] = {0 @; M" V) K( l; S4 u
UF_line_type, 0, 0,4 B/ N6 [7 Y# r0 V" I
UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE,
+ A: `- i: n0 C% U8 s% I2 @UF_solid_type, 0, UF_UI_SEL_FEATURE_BODY};

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

if((UF_UI_set_sel_mask(select,3 d  O7 g( {: l1 A% g0 P4 ~
UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,7 B6 P& c4 x4 e  [+ i2 a1 O
num_triples, mask_triples)) == 0)//3倍 3*3=9

{# _6 a& E5 s7 b& \% x0 N
return (UF_UI_SEL_SUCCESS);6 o, D3 T. s8 u* v0 m
}0 X" W3 j# F; y$ v
else! Z* N) g4 S' U9 }# ^: ^, B2 A4 D
{9 \9 }1 T4 l; t, K- R4 i$ L$ z; Y
return (UF_UI_SEL_FAILURE);
' I9 b& Q$ k, Y3 z( ~) x5 m$ ?; T}
7 m/ z5 O( s2 N) {- [}
0 O9 C& R7 _1 d8 f3 o* I- `static void init_camgeom_app_data
' B& B  X$ N* k: {! f: r2 M# V! k  z($ X' b( r( M( J, w" t
UF_CAMGEOM_app_data_p_t app_data
  c; C6 B  X! [: S+ s/ B)0 u  @. A8 r% f1 g* |$ f
{
! Y6 y. r8 c2 B5 u/ Q( k; b; uif (app_data)
* u/ w0 i2 j' n4 N{3 `6 `8 E) P1 I6 \8 B5 ]
/* Set flags. 设置标识*/

app_data->has_stock = 0; /* Used 1 - Else 0 */
; a  Y; a- P" r; Uapp_data->has_cut_stock = 0; /* Used 1 - Else 0 */( B% Z/ [9 i! {7 b8 y
app_data->has_tolerances = 0 ; /* Used 1 - Else 0 */
2 t4 |+ ~0 Z/ [; `  b. A; napp_data->has_feedrate = 0 ; /* Used 1 - Else 0 */
  o3 c1 i# O6 p$ xapp_data->has_offset = 0; /* Used 1 - Else 0 */, m1 R8 f0 d  Z9 N% s
app_data->has_avoidance_type = 0 ; /* Used 1 - Else 0 */

/* Set values.设置参数值 */
; X7 e1 U! p  W" ]app_data->stock = 0.033 ;) K' B. {3 R4 F" z) p. N- O# [# J
app_data->cut_stock[0] = 0.2 ;
1 P8 e7 a$ q3 U4 f+ {1 r- \1 Kapp_data->cut_stock[1] = 0.1 ;
; O( B# Y. ^6 U. a/ A7 m! l6 fapp_data->cut_stock[2] = 0.5 ;
! a" ]+ e7 I5 \  P; ]app_data->tolerances[0] = 0.003 ;, k; F0 }: G, C. v( T. i
app_data->tolerances[1] = 0.003 ;
" d) @$ G2 ?! ^; g2 c! G( Iapp_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;3 T& j$ }9 Y+ V9 }, a
app_data->feedrate_value = 33.0 ;9 R0 u- @1 Z% }$ ]9 ]+ @0 q
app_data->offset = 0.055 ;
# P. z/ @0 d. C1 Y/ oapp_data->avoidance_type = UF_CAM_avoidance_type_warning;
7 l1 s! S( B9 [$ }}7 N$ g0 K* w+ k8 }- e% h; {
}

/ y5 R- X/ W) N8 a& S
发表于 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 | 显示全部楼层

; ]( M4 o  L4 V* z  Q4 i4 e求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 19:39 , Processed in 0.071714 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

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