青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

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

7 v- @8 Q7 E  Y/ Y; [- a9 `  y

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


8 G7 u) x, P' a. r7 Z6 |9 Z

#include <stdlib.h>1 l1 L0 ]# Q8 T4 m) p* v- X
#include <stdio.h>

#include <uf.h>
: R$ F3 _3 S2 Q' r, B; O#include <uf_object_types.h>) T$ B! S3 [0 P9 O  d+ _
#include <uf_ui.h>) N# p5 K* V9 ]+ _6 Q( n( M7 S
#include <uf_disp.h>/ X0 s" l, p9 m, }* c! j3 m2 }7 m1 M
#include <uf_obj.h>  e% h4 m0 E; P/ P- {3 N  A8 Y
#include <uf_cam.h>, T' h3 ?# e1 H. u* H: V
#include <uf_camgeom.h>

#include <ufd_camgeom_ugroup.h>

#define MAX_CAMGEOM 20% J$ P7 Q1 x# O3 U; q! O+ Q6 A0 N* l
#define EXIT_SUCCESS 0

/*#define DEBUG*/

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

static int geoption;
7 E: d- O1 R% J! |static char *title3 = "Select Geometry Type(s) for operation then Selection Complete";//创建几何
9 M" x# |* A. }static char menu3[][38] = { "1) Part Geometry",
$ @# g- a3 y) x- \4 y6 a, c"2) Blank Geometry",
. R0 \' X6 E6 d' X4 ]"3) Drive Geometry",: d, a, A! }$ [* F8 e, W# }
"4) Cut Area Geometry",; z9 R4 x0 {# C' m
"5) Trim Geometry",
$ ~( P& f: A; Q  ?+ r: C"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 )

{% ~/ e, V! N4 W1 ^$ G1 p; L
UF_CAMGEOM_app_data_p_t app_data_list[MAX_CAMGEOM];
6 d( k% C" X+ `. M9 F- htag_t *objects, entity_list[MAX_CAMGEOM];" [, v2 O) V: P2 Y% B4 s5 }5 Y2 K
int i, entity_count, object_count, err_code, type, subtype;

char atitle[] = "Geometry Selection.";
: L. `& C7 j( Echar *cue = "Identify Geometry - Wait for the Filter!";
7 q( C& `5 w. J$ p5 ~char errorstrg[133];
8 A- i; f5 l/ T3 Q. `! |4 X9 b( Qint response, irc;4 T4 Z! ]4 g3 t0 B
tag_t eid, view;
" Q6 k; m- W, c- ~1 W+ H( `* gdouble cursor[3];

#ifdef DEBUG/ U3 e( n+ y2 E* t9 k& S) {, J
printf( "Object tag passed into camgeom_ugroup is %d \n", objTag );
+ h* p% N1 r7 {$ x+ }#endif

geoption = 0;

while ( geoption < 10 )//为什么是10呢????1 V7 Q3 a& w! n" e2 @' Q' s7 {
{8 u7 _" G4 t5 b" u% W
/* Statements go here for each geometry selection type. 声明起作用 对于选择类型的每个几何体*/

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

if (geoption == 1 || geoption == 2 || geoption == 10 )
3 f9 q8 \1 u+ y+ q$ r' _4 {3 D) S' p{7 p2 e0 W# u+ }2 r3 Y& n; d
break;
* L' V7 U7 }1 h2 B! Y}
$ P1 C$ t7 C9 b' P+ ?#ifdef DEBUG& a7 K# x: c- l- f: }( l! r- }
printf("\n Geometry option returns %d\n", geoption);2 h  v+ ~- ]7 M- r  j
#endif3 K3 L7 R. M2 s5 G5 M6 ~
/* Allow the user to select faces or bodies. 选取面和实体*/

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

if (obj_count > 0 )! a# F( f) Y) M' I- l
{
! g& b6 M9 O! {0 r. R# Xentity_count = 0;

while (TRUE)

{

UF_UI_select_with_single_dialog(cue,atitle,$ j# u) F! @) |
UF_UI_SEL_SCOPE_NO_CHANGE, init_proc, NULL,  e- k9 E  t$ }" c' q' m+ K; {
&response, &eid, cursor, &view);//选择单个对象,可输名也可打进去。选择范围在UF_UI中定义
- [0 F1 Z) q$ x1 z//cue 提示显示的信息,atitle 对话框头标题,UF选取范围,init初始化程序,NULL初始化程序,cursor光标位置坐标

if (response == UF_UI_OBJECT_SELECTED)
+ Y6 }5 S4 k+ Q7 {5 I( }0 @( g{
6 w, D! V2 t3 ~8 I5 jentity_list[entity_count] = eid;$ I8 u6 ], x9 Y5 p! G7 V& h% U! v
#ifdef DEBUG
6 {, `- `& {; T4 u; y2 P' pprintf(" EID number returns %d\n", eid );( e8 ]$ v+ U0 m$ [6 l% l/ Z) {
#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),7 |) Y* ~8 i: {$ Z# F5 z* n
&err_code);

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

init_camgeom_app_data(app_data_list[entity_count]);

entity_count++;( A' v. r( |+ W+ L9 l( T
}0 I3 w. \8 L6 D9 ]/ ?8 K
else if (response == UF_UI_OK || UF_UI_BACK || UF_UI_CANCEL ): a2 _: w( s( S; T/ [% k
{
  H2 o: n& J8 g) y  g/* Geometry selection is finished.几何体选取完成 */
# v0 t0 O0 [( Z$ [break;
; J1 u  k  V0 m; z}( D5 d, c/ K" c
}
8 [# v. H- L$ f) F#ifdef DEBUG
- V, ]6 w( W( D% v+ j# gprintf(" Entity count this selection is %d\n",entity_count);1 p. E- n5 Q) @8 `9 k
#endif

if (entity_count > 0)4 w7 d; o2 t* I) [' n% _0 L
{
4 s0 A' }4 D" I7 J! s/* UF_CAMGEOM_item_t *items;
7 e& K( {5 Q3 n2 R, I9 mint item_count;*/

for( i = 0; i < obj_count; i++ )3 P% G/ r9 D+ E" C
{
3 V4 r7 \! [  f/ d% Z* k" d  c8 a, mirc = UF_OBJ_ask_type_and_subtype( objTag, &type, &subtype );//返回对象类型与子类型TAG值

if( type == UF_machining_operation_type ||1 S5 q) K7 n3 {0 L: S9 C6 i
type == UF_machining_geometry_grp_type)

#ifdef DEBUG7 J9 A) j2 w1 S9 ?# x4 \
printf("\n Current entity count is %d", entity_count);
& r9 x5 W$ F' }( x1 z- Oprintf("\n Current object count is %d", obj_count);  }' @& S" E4 d; U2 Q, h
printf("\n Geometry option is %d \n", geoption );. N  \/ q5 N$ }3 u
#endif

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

{3 b) c4 \+ n1 C! e+ I9 I9 T( ?
switch (geoption)
9 E6 |+ n- D4 N8 K  V3 U" E{
+ U1 L' Q3 T5 X# |9 h6 F& I) N6 g3 Ycase 1:
. u4 m- b) a1 C- X, g& |case 2:  F5 |) z2 F9 n9 T
case 3:
+ q+ a3 k; }# Z  L5 lcase 4:# x7 J, r! ~( ^
case 5: /* Part Geometry 部件几何体*/6 q8 w) B+ Z& @8 C4 G
{& i) j0 C1 t( E4 S/ r3 `+ {
#ifdef DEBUG
& w9 o  j! n# w- {0 H: F2 xprintf("\n Type returned is %d", type);
0 e7 ?- Y5 c7 E, V  g9 E; bprintf("\n Subtype returned is %d", subtype);) y6 ^. t+ V4 N3 \+ w* y. J& j
#endif8 _- ~2 {+ h, L; V7 j2 J* G
{
; f5 A2 P3 W6 X3 E; T7 }: b% {irc = UF_CAMGEOM_append_items( objTag, UF_CAM_part, entity_count,
" {  f! o! ^- {6 a8 _entity_list, app_data_list );
" \1 z" i, O$ ?- b0 R//附加一个几何实体链表给对象 几何体包括实体等 切削区域仅薄壁实体和面 修剪的几何体不允许。! G4 }8 o( v5 j: M- m& j6 W
//objtag几何体父级组,UF几何体类型,en_list链表指针,app_data_list链表数据。
3 R3 p2 N) J8 s& T}
0 Q5 u  F0 U$ j8 Yif ( irc != 0 )* z+ ^' G! P: M7 h% x
{
# C7 w* N; k" N6 b( H" W0 VUF_get_fail_message( irc, errorstrg );7 M) T  r: O: T8 P
printf("\n The return code is %d\n", irc);' X- R- e! n, F& D9 D' [
printf("\n Error code translates to %s\n", errorstrg);
+ x0 s1 \; u4 n7 m+ F7 Creturn objTag;8 I6 f% S, W+ b/ c3 Z  O! _; I
}
9 F+ w# l# P6 [; Ubreak;
9 W8 @: f$ i6 V9 P* s# }}

case 6: /* Blank Geometry 空白几何体*/& A: d  P5 E; N1 b6 |
{. R: V/ m+ y! b4 K
#ifdef DEBUG; P: y8 G9 t9 P! L! j% \6 Z) x# p
printf("\n Type returned is %d", type);
% n$ \9 x. Y$ i; u4 Jprintf("\n Subtype returned is %d", subtype);
- m5 D* ?. D: ]/ p: D#endif
* H' n7 i. H7 p{# p+ \6 y0 z" G* {6 w
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_blank, entity_count,
& Z2 m, j0 g4 x5 ^% aentity_list, app_data_list );% E0 l. G2 @0 a6 T5 C/ S! f
}
2 d) n. V. v" u/ u+ sif ( irc != 0 )
( m; ^" o6 r7 i0 [{3 h+ I6 V& p3 V$ P$ [
UF_get_fail_message( irc, errorstrg );1 S# b: W% v+ e- m; O8 U! ^
printf("\n The return code is %d\n", irc);
/ P# I1 k* l6 O4 _printf("\n Error code translates to %s\n", errorstrg);
9 ~  d5 ]' ]) qreturn objTag;
6 z; \  `! K6 u, r}6 Z$ G0 w( x* s9 ~/ J, c+ D) |
break;

}

case 7: /* Check Geometry 检查几何体 驱动*/
4 U: d4 H4 @. s, C{
  H0 X3 c1 ^. F) ]#ifdef DEBUG0 M! c* V( P- S. u$ @
printf("\n Type returned is %d", type);8 |: f4 |1 E9 ?
printf("\n Subtype returned is %d", subtype);, D2 e/ w% j' B9 X% I
#endif- y! l) R( j# b  o- v
{
8 H6 b; v+ x0 S+ pirc=UF_CAMGEOM_append_items(objTag,UF_CAM_drive,entity_count,
) `; l+ W) M) s; R# wentity_list,app_data_list);
( M. X- p: H7 q/ i9 q$ E}6 c! U7 w/ x- ?% w/ P( Z, K
if ( irc != 0 )7 d" J. @- \$ ~  W1 k1 d3 f
{. o) @) Z! G4 ~' l0 W$ q
UF_get_fail_message( irc, errorstrg );2 a. J: v! ?; P! e5 j) ~: V
printf("\n The return code is %d\n", irc);
) d) [9 t+ S2 u0 e6 }printf("\n Error code translates to %s\n", errorstrg);
' J2 s% M: N& t$ ^5 Areturn objTag;* A! i6 L/ \  v+ i2 o( \$ \
}- e$ i1 X, R2 d0 O( e
break;

}

case 8: /* Cut Area Geometry 切削区域几何体*/
$ {& V! ?( n% f3 c( y' k{6 R6 C: x9 B0 N5 m6 ?! z
#ifdef DEBUG
, k6 [- @3 @( U/ ]6 |printf("\n Type returned is %d", type);) g9 F' N) v$ F% R. S* y  W
printf("\n Subtype returned is %d", subtype);- m# Y) O$ R- F& s
#endif6 E7 }. {2 u  Z# e
{
: ]( O' f8 K3 P4 ]# airc = UF_CAMGEOM_append_items( objTag, UF_CAM_cut_area, entity_count,
1 I2 K1 s2 X1 Z/ C5 e. U' Gentity_list, app_data_list );0 x$ W! t5 O" C0 ^9 R0 m
}: l  D0 Y  s/ i+ Q
if ( irc != 0 )
0 [" o- r% g2 A2 w9 M- g, q# o: H{
* k. P2 t, A8 t2 I# A/ Z( \6 m4 tUF_get_fail_message( irc, errorstrg );
1 h) @' q8 C: x; |printf("\n The return code is %d\n", irc);
. v; Q7 f4 B" Qprintf("\n Error code translates to %s\n", errorstrg);* o; d* j" H& S' a
return objTag;% ?( w, R* o& m+ ]* m6 T2 l& h- S
}
; d* w2 H4 p7 [& ~( ]break;4 V% ?; _0 n* ^, v* R! |- ]
}  J$ _6 t7 c  n7 d- Y& c! G- j' x
case 9: /* Trim Geometry 修剪几何体 */2 ]( `- ^* e0 K4 D. L  h9 v/ N  C
{
2 T$ v$ {6 [1 B" L- x4 W; L, @% {& hprintf("This case not implemented.\n");6 s+ H- Z6 N2 h2 C; P% s3 h/ l; M
break;' o$ a9 I8 z$ \5 |, V
}- V( N7 |* \. p& Z/ s% k
/*Drive Geometry 驱动几何体*/

default: /* Selection Complete 完成选取 表达式没有相应的则走这条语句*/
" @( t& n* O9 [+ ]& F{: f( X- S1 {3 a: l2 G4 A
break;
' c! u- P6 x0 x% r" `/ u8 t}
  B. c% B$ ?2 h- j4 e( l% P4 x7 U6 G}
$ y3 X1 y: {9 x4 ~  o: H}. s; L+ R' \4 l5 F
}" \0 a5 e8 R8 A
}
9 d: z" d: Q8 q1 v; Y/ Q8 C}8 I* b+ z! G; G" V
}
4 E# x( }8 p5 M  ~$ `/* Free the Allocated Memory. 释放内存*/
" f- v) a) I+ g- R7 r1 N& F+ u/ }3 Wfor (i=0; i<entity_count; i++)
; L& @% b- `1 T  r$ G{0 ~" B& B7 R" p7 A
UF_free (app_data_list);
9 `" E8 h0 q9 J9 c0 h5 d0 w" _* g}

/* UF_free (objects);*/

return(0);
6 ~% @+ ^0 Z& _$ K) }}
0 o9 U8 A( ^- Q+ ~' {* [% ]/* Selection initialization procedure 选择初始化程序*/, b6 w. w* Z  h% p! ?
static int init_proc
5 b5 s5 x: D6 z* N3 M(
0 S. H/ ?( Y9 {0 n- ?& H/ `! _UF_UI_selection_p_t select,//指针 pointer
8 j# |7 r0 N9 O7 G+ j3 _void* user_data' z- b, H. c) M. ~; L4 v# \! F2 D
)3 n5 @0 ]  |8 e; q' A
{
( b. y+ a5 R0 {8 dint num_triples = 3;6 }! h; Z" I& y6 ~# j$ F
//最终指定的特征类型) T9 b: y$ m/ W: A  |# y; l
UF_UI_mask_t mask_triples[] = {. A6 s# D' Y) |% o6 S2 }. g( U
UF_line_type, 0, 0,
) c. m" V0 n: O) R" {1 S6 HUF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE,
) e. S  c3 \; e- A+ O  I. T2 GUF_solid_type, 0, UF_UI_SEL_FEATURE_BODY};

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

if((UF_UI_set_sel_mask(select,1 Y& b& i1 T8 G* M+ g& ~5 c7 x
UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,: ]0 o$ Y1 l$ D3 R8 }
num_triples, mask_triples)) == 0)//3倍 3*3=9

{8 B- F) V  k' X) Q
return (UF_UI_SEL_SUCCESS);% ]) A) w8 z/ d
}0 W' M# O( l/ X/ p5 T  @/ |" h
else4 G8 l9 C4 F7 ^4 c$ |) o
{! I; U/ Q* j! s+ s% D* Z. k( G3 \
return (UF_UI_SEL_FAILURE);
1 H- X; S* ~$ l- Z* o3 K% J' z}
$ }. W. n" h& \' m9 e& a}
* D1 r4 o! F$ p. o) a, Astatic void init_camgeom_app_data
8 d: j+ u7 \/ Q+ ^7 w(
# c2 t9 L% e# ?# |9 i9 cUF_CAMGEOM_app_data_p_t app_data4 U$ y' @( z! K7 [
)
/ J# L$ v  R2 ^$ H3 N{
9 ^- f- h% m; O3 G" S' ~. W9 `if (app_data)
# h' N4 a7 ?" E. b{
% `: R2 A- Q1 G- k* n/* Set flags. 设置标识*/

app_data->has_stock = 0; /* Used 1 - Else 0 */; m( |  G. G. Y: T- }
app_data->has_cut_stock = 0; /* Used 1 - Else 0 */
6 e  H8 O, o3 M6 K& Wapp_data->has_tolerances = 0 ; /* Used 1 - Else 0 */
) r0 F* W5 E6 h9 j$ [$ z  t- happ_data->has_feedrate = 0 ; /* Used 1 - Else 0 */
8 m+ h) I1 j$ W- j. Japp_data->has_offset = 0; /* Used 1 - Else 0 */
+ [5 ]7 Q% J- |9 ^app_data->has_avoidance_type = 0 ; /* Used 1 - Else 0 */

/* Set values.设置参数值 */' D' D# `6 E& n: u2 p6 w0 x. o2 n9 w
app_data->stock = 0.033 ;
# @/ C& _  c5 ?app_data->cut_stock[0] = 0.2 ;
$ Z% Q& F8 `( ?5 [9 tapp_data->cut_stock[1] = 0.1 ;
6 R; P1 Q" B3 j( _1 ]% Wapp_data->cut_stock[2] = 0.5 ;. F) y( r8 A# b5 k$ q4 p
app_data->tolerances[0] = 0.003 ;7 a3 x- w7 ]) [, Q. O. P
app_data->tolerances[1] = 0.003 ;
* V) Q7 A9 B& t1 z) r# i- rapp_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;
" F. E1 a8 W) p  h+ Papp_data->feedrate_value = 33.0 ;
' s8 A% b% D, Capp_data->offset = 0.055 ;
$ c7 b- \6 ?  a3 ]) o, ^) xapp_data->avoidance_type = UF_CAM_avoidance_type_warning;+ p0 `8 v* Z: f3 K! g- D. z! O5 |9 u
}
+ O% Q8 I7 O: U9 p}

# V2 P- d; {1 w* }% a
发表于 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 | 显示全部楼层

7 W$ K3 F, T. a6 d/ @3 c  r, J求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-19 07:07 , Processed in 0.057635 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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