青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

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


7 H3 V$ j- }; D( y  N" _" R: `6 y

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

5 q7 _6 {  c: M. q6 g

#include <stdlib.h>
2 p) |+ ^9 U; P9 j* i) ?#include <stdio.h>

#include <uf.h>/ T2 a6 o! g$ `8 [4 ]
#include <uf_object_types.h>
& j- V: G% Z4 C9 G#include <uf_ui.h>! f. w9 f2 ^4 v! W
#include <uf_disp.h>5 i( x4 m8 w) ?! I5 D8 l
#include <uf_obj.h>0 T2 C& z# u7 ]% G
#include <uf_cam.h>
4 k# C$ \  u( x: _8 Y) S/ d" W: K#include <uf_camgeom.h>

#include <ufd_camgeom_ugroup.h>

#define MAX_CAMGEOM 20
$ F. I# j5 _# U- R6 U* W- A#define EXIT_SUCCESS 0

/*#define DEBUG*/

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

static int geoption;9 j* H7 {- ~5 `+ W# @( k( f, [$ e
static char *title3 = "Select Geometry Type(s) for operation then Selection Complete";//创建几何, I( h. M6 C1 u- O: W% C7 L
static char menu3[][38] = { "1) Part Geometry",
" q7 z* s) c( v" F( q"2) Blank Geometry",
% R% K. m4 g" K9 s$ Y"3) Drive Geometry",5 r% l  Z3 a2 t, S5 m6 Q
"4) Cut Area Geometry",
. z% R& T3 ~# I4 \"5) Trim Geometry",$ B& R7 b3 K% d
"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 )

{) @7 s; b6 s* Y2 q3 ^
UF_CAMGEOM_app_data_p_t app_data_list[MAX_CAMGEOM];& J9 s0 _# P/ c* u6 j' C4 g
tag_t *objects, entity_list[MAX_CAMGEOM];
0 h! Y. v6 s2 |  kint i, entity_count, object_count, err_code, type, subtype;

char atitle[] = "Geometry Selection.";
9 H5 i% O% B" Ochar *cue = "Identify Geometry - Wait for the Filter!";
, [4 p& S4 F  p8 kchar errorstrg[133];
3 ?! ^0 K$ Z  T0 zint response, irc;
5 x3 B$ t: L" E' o2 Ptag_t eid, view;/ ^! A1 ]7 Y& u% l! F
double cursor[3];

#ifdef DEBUG5 y9 N3 H" x, F$ }5 T! v) Y4 }
printf( "Object tag passed into camgeom_ugroup is %d \n", objTag );) S: R! p4 s( {- g1 m; R1 W
#endif

geoption = 0;

while ( geoption < 10 )//为什么是10呢????2 L( D. [" v+ B, @0 P6 |
{) z- _5 w& L6 z7 ^# |/ _) b. x
/* Statements go here for each geometry selection type. 声明起作用 对于选择类型的每个几何体*/

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

if (geoption == 1 || geoption == 2 || geoption == 10 )+ ?2 g' b1 u8 M0 m# \+ j# U
{$ F  x& S5 R3 j/ A
break;) \, B: X- D7 j6 l9 {
}
  Q6 U0 ^! g7 i4 G2 B) {#ifdef DEBUG
; }) W' ?8 q# E# |; [3 R  Tprintf("\n Geometry option returns %d\n", geoption);
3 \5 a8 K! A$ v( m0 ?#endif
  m  D8 Z8 |/ Z% W1 `& _3 D/* Allow the user to select faces or bodies. 选取面和实体*/

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

if (obj_count > 0 )
) q; ^, J* b) }5 g. D. d{
5 n% C9 N6 _# D& p1 f+ p" \entity_count = 0;

while (TRUE)

{

UF_UI_select_with_single_dialog(cue,atitle," f# H$ ~3 L; B" h: m
UF_UI_SEL_SCOPE_NO_CHANGE, init_proc, NULL,
2 e+ Z- o8 O3 D3 r/ @- H1 `&response, &eid, cursor, &view);//选择单个对象,可输名也可打进去。选择范围在UF_UI中定义
2 Z# ]6 F9 U. O+ ~//cue 提示显示的信息,atitle 对话框头标题,UF选取范围,init初始化程序,NULL初始化程序,cursor光标位置坐标

if (response == UF_UI_OBJECT_SELECTED)
/ s% n6 b  t( l! ?5 W+ R/ u{% t, `6 N& {" }2 o' P; e% k+ y
entity_list[entity_count] = eid;
8 E- G. ~, x% v#ifdef DEBUG: O1 }  J! ~" |8 \! b8 a
printf(" EID number returns %d\n", eid );
1 w$ k& c& J! d#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),
6 @: w7 H0 E" t- r&err_code);

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

init_camgeom_app_data(app_data_list[entity_count]);

entity_count++;6 M8 f/ s1 Z0 Z0 i( E( @0 J
}
* Z( s. c  N6 |+ Belse if (response == UF_UI_OK || UF_UI_BACK || UF_UI_CANCEL )
$ K7 D& {: H/ a) D6 O* I{+ }) Z) e# s& Y: ?$ x& }: B8 Y
/* Geometry selection is finished.几何体选取完成 */3 R! o% R& N( I/ A. g% G
break;
' A5 G; X/ H" [  e  d: w1 \}* z7 Z' a1 V: G. v
}$ i  m' ]7 V+ i; h; E# O; y
#ifdef DEBUG% D! }- N- }* I) Y  A
printf(" Entity count this selection is %d\n",entity_count);
9 B7 I& p  }, ?& c! C. [+ d* B2 I#endif

if (entity_count > 0)2 V5 W- }) k  n
{
9 Q8 |" W4 c, `& S/* UF_CAMGEOM_item_t *items;
5 E6 A5 D% n% D- \1 Cint item_count;*/

for( i = 0; i < obj_count; i++ )
' [. @8 M9 V" j9 E{& I- I  ^; v3 J7 l& J
irc = UF_OBJ_ask_type_and_subtype( objTag, &type, &subtype );//返回对象类型与子类型TAG值

if( type == UF_machining_operation_type ||
3 H/ O' i, U8 _9 e+ ?. ltype == UF_machining_geometry_grp_type)

#ifdef DEBUG
' ]# d+ c  [" V+ [printf("\n Current entity count is %d", entity_count);
4 g" b" i4 }; Q  g) Q7 d/ h5 uprintf("\n Current object count is %d", obj_count);1 q& \- o/ q4 O
printf("\n Geometry option is %d \n", geoption );
, {# p& y+ j" w0 G#endif

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

{
2 N, n; j& Y' w) y  U3 D& s+ cswitch (geoption)% q0 z" K! F4 c- l% e# }7 m
{
5 W2 C3 o' T) d0 u" `6 r& V: ?) Gcase 1:, b* m9 Y1 C/ j% R' o
case 2:
! B% ^0 ^9 Y0 W1 j8 l$ [case 3:6 c9 z; L/ W( ~! h- a- K% h
case 4:
; P5 T' @  Z0 T" }/ Rcase 5: /* Part Geometry 部件几何体*/6 W1 G5 @1 F: H
{
7 ]" R5 P- a# r#ifdef DEBUG. v2 J$ R$ `" ~3 D: p
printf("\n Type returned is %d", type);+ o9 k! i9 I4 J" @; {. S
printf("\n Subtype returned is %d", subtype);" c, p/ v9 g4 E  I) G
#endif
3 q, L- ^* @) j1 X) o{/ z* c& J, N4 Q: Y* m; ^8 R9 Z
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_part, entity_count,
: A8 c. f" `1 Ventity_list, app_data_list );  B' N& |5 E  x( m. N
//附加一个几何实体链表给对象 几何体包括实体等 切削区域仅薄壁实体和面 修剪的几何体不允许。
4 d" k) d' P9 |- y/ g3 F//objtag几何体父级组,UF几何体类型,en_list链表指针,app_data_list链表数据。! @5 O7 Z' {$ o4 ]: t& z  {
}/ z1 u8 e: _" @
if ( irc != 0 )
0 j( W. h* {) \) Z( K{
" k: g9 ?' f+ R7 WUF_get_fail_message( irc, errorstrg );8 S% w8 }3 U% `2 k+ x0 [
printf("\n The return code is %d\n", irc);
8 ~2 e1 O- F8 b  Oprintf("\n Error code translates to %s\n", errorstrg);1 A4 x: U( c8 L( @1 v  o- O  w
return objTag;) o. O( W1 \" o, f# L& a6 b  q
}5 D2 ?) C: p6 ?, u
break;
6 u! u; U+ I+ E) y7 B: z}

case 6: /* Blank Geometry 空白几何体*/& j  w# _* {$ p. M( `+ G- {
{) [/ k9 n  `" d3 G# `
#ifdef DEBUG2 l- Y, f& m' m& {
printf("\n Type returned is %d", type);
1 V! Z" r0 a! q; ^) a' t/ ]( N& Tprintf("\n Subtype returned is %d", subtype);
6 y& {5 ?* l8 C, D#endif
5 o- E! ]+ S# T4 W; e1 e- J+ w{
2 q+ M" p" n; i* nirc = UF_CAMGEOM_append_items( objTag, UF_CAM_blank, entity_count," U! b1 W# [* ?
entity_list, app_data_list );
! f$ H% g( m' h, r& t}
$ ~$ a8 L* m+ x( yif ( irc != 0 )- |, U( c& ?0 x3 r
{
. }6 p7 E0 q( O7 Q$ |% nUF_get_fail_message( irc, errorstrg );
% P6 v$ f- Y0 P" Sprintf("\n The return code is %d\n", irc);1 W/ j7 e/ T7 H& ?# h) w# w( ^8 Y
printf("\n Error code translates to %s\n", errorstrg);
3 O3 Z$ A; g4 }return objTag;
4 G3 P7 K* J( b- N* U}7 V( t: U) v  e6 x
break;

}

case 7: /* Check Geometry 检查几何体 驱动*/2 G# X5 z, f% G2 l* L  ~
{
' Y9 Q8 F$ r& P" f7 @. G#ifdef DEBUG
  A, g5 w. }$ q, U. [printf("\n Type returned is %d", type);1 @" K5 G% u' A: K, H6 o' y0 x5 V8 l
printf("\n Subtype returned is %d", subtype);
% ]3 v3 Q4 q7 B5 G! X#endif
5 n+ P* f* [. Z4 ?4 I8 T  x; A{' p  k9 g1 |/ }  w0 \1 k8 E: r
irc=UF_CAMGEOM_append_items(objTag,UF_CAM_drive,entity_count,. k- h6 Q% x& r8 P9 R
entity_list,app_data_list);
4 T% C% `  m2 Z- |+ y" m/ y}
, ~  r+ z( p5 f6 H5 P9 Jif ( irc != 0 )" T& |( u+ G  Y6 V0 K7 q
{2 I( T2 m% I$ A6 O( J9 t& n
UF_get_fail_message( irc, errorstrg );: J/ N; ^- b( P. s5 M
printf("\n The return code is %d\n", irc);
- J6 z% [% L1 rprintf("\n Error code translates to %s\n", errorstrg);9 n+ S1 o7 F5 ~, j# C; d" `& U& s7 O
return objTag;
# C9 d9 I; u# N& V, d# s) ]}
1 H9 f, I8 _. n2 T3 n: [& Abreak;

}

case 8: /* Cut Area Geometry 切削区域几何体*/7 ?, S3 g8 R4 x: d) R
{
1 }* f# l" H  F$ E* |9 r9 U#ifdef DEBUG
4 P# L" M( R) M& {7 O% tprintf("\n Type returned is %d", type);: b) [7 c# w* s$ C; a0 t3 b
printf("\n Subtype returned is %d", subtype);  g! ^5 P9 M: ?. D, P0 K
#endif( u& K9 Z7 o, q  c, g7 o# }: N
{1 z& y) v- w% _0 n3 I/ J0 G5 w
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_cut_area, entity_count,8 t3 h9 P9 J' Q& l9 o! k
entity_list, app_data_list );7 j; ~! H  b4 O' E3 b. ?
}4 I: }: Q3 u# o* h9 h. G% i1 N+ x
if ( irc != 0 ): h0 f9 i: ~" j
{  R: I: Y4 Z% c7 A% x
UF_get_fail_message( irc, errorstrg );9 m: u; P! y! U, B% g1 t/ x
printf("\n The return code is %d\n", irc);9 O1 X0 N2 F' D( [
printf("\n Error code translates to %s\n", errorstrg);, K+ J/ F, K5 }! I2 ?# a. T
return objTag;
/ ^& K+ R/ K( I' }}
: g6 F6 Y% N; v3 u& q4 Abreak;6 R& n! r% s! \- V* F: R+ T/ @
}
: M( Y  \  I5 rcase 9: /* Trim Geometry 修剪几何体 */
# P8 {& u$ U4 r! V9 W+ f+ \. z* X  ]{
% C# X0 j. G# a0 ?+ s$ Wprintf("This case not implemented.\n");) l9 t* {& J4 V* ~
break;
8 }/ z) U2 V0 p  q( |6 q}2 m$ z7 ?/ q6 }
/*Drive Geometry 驱动几何体*/

default: /* Selection Complete 完成选取 表达式没有相应的则走这条语句*/
$ b9 g" B- i7 K2 O0 |  q1 R- J{
; B' X( U1 \; Gbreak;
# {/ K' p# B2 _& V/ X6 s/ ]4 {5 s}* y* v8 s7 }) s. \9 K/ _2 ]1 N
}
# J/ R" K7 o+ {$ A! x}
$ U4 _) u3 e0 Z% a1 V; |2 S}
5 O% K" ]2 G: Q$ W}8 K- c8 h; F$ F' @- D5 j7 \
}
- C0 P( f, c* F' g* N6 e. C+ {}! A' T: t4 Y% U' Y
/* Free the Allocated Memory. 释放内存*/
" e4 q! d& |) M% p' tfor (i=0; i<entity_count; i++)% E, g, x7 l  z5 D  m6 n/ J+ Y2 M2 v
{, P: Q% I. j% d8 k
UF_free (app_data_list);: W& i6 p& g! ]% q
}

/* UF_free (objects);*/

return(0);
' d9 _- `! `  c# \}+ o4 h! }- [+ L% u  k0 S
/* Selection initialization procedure 选择初始化程序*/- m/ a! J# Y1 K- U1 K( X: I
static int init_proc
7 W! Q" n6 n. Q(
! Y# v" e( ?% _* X  Q* S. Z5 N" NUF_UI_selection_p_t select,//指针 pointer* y* L! n! s  z2 R. @# U
void* user_data
; ~4 \+ S3 N1 W* t  Q7 Q8 L2 k)
( T( S7 q7 X8 E& m$ f* l; `1 r8 j& L{
; b. v9 N( @) z6 R' w9 ^int num_triples = 3;  V3 e" v% ?- K. F
//最终指定的特征类型; n" t4 I2 g# ^& w9 U) o  p
UF_UI_mask_t mask_triples[] = {
# w  ~# K. x) Y4 G7 vUF_line_type, 0, 0,% b6 Y# W( g3 \2 ^5 H' k0 a
UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE,
% b* k$ u9 s' d0 e6 yUF_solid_type, 0, UF_UI_SEL_FEATURE_BODY};

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

if((UF_UI_set_sel_mask(select,5 q: R% F  T! p! b8 [9 b# _6 C0 g
UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,
- l9 x. `) ~5 y) O4 C( Q5 K1 q! anum_triples, mask_triples)) == 0)//3倍 3*3=9

{
% ?( K4 ?- }& g7 Y/ c, e4 `4 Areturn (UF_UI_SEL_SUCCESS);. z$ M# Z9 E* Z2 ~2 o9 h, [: o
}
& Z- s. {9 q! L: W% d. K8 aelse
0 p# J" e: c' h! A% [0 P{
: e# B! F; N9 Y8 z1 jreturn (UF_UI_SEL_FAILURE);
; Y/ h& h" @2 r, [0 x9 S* {8 m}- I1 l8 c* X$ y; I" L' u
}3 g* q) D: A4 y% Y& @# h  o: r" i* k
static void init_camgeom_app_data
' _9 y' W: ?- n4 C& B4 j(
$ y' v% J* a0 G) oUF_CAMGEOM_app_data_p_t app_data
/ ?0 _0 G; }6 t* ^, t" ~- `): ~$ P- F! @( V* b, F1 m4 |
{  l% o0 }. c3 X+ m/ g8 @
if (app_data)
* X) s% O' K" B{
* y% Z. D5 [1 X) h9 t/* Set flags. 设置标识*/

app_data->has_stock = 0; /* Used 1 - Else 0 */
- K* [2 O" b" bapp_data->has_cut_stock = 0; /* Used 1 - Else 0 */
+ {" L* h; m; Qapp_data->has_tolerances = 0 ; /* Used 1 - Else 0 */
5 N" |( Z4 d- Y) xapp_data->has_feedrate = 0 ; /* Used 1 - Else 0 */
7 r5 D# W* W. v2 i. K6 Oapp_data->has_offset = 0; /* Used 1 - Else 0 */
( d$ U, H! M7 s' m* e. W% ?. japp_data->has_avoidance_type = 0 ; /* Used 1 - Else 0 */

/* Set values.设置参数值 */, M8 W: A8 [. p8 O4 [- M7 n2 u
app_data->stock = 0.033 ;
5 R- @% I  L9 }" q3 {9 |app_data->cut_stock[0] = 0.2 ;1 z+ F5 p7 C. i2 X0 ^4 B
app_data->cut_stock[1] = 0.1 ;
8 P/ d+ ^! t' i% Xapp_data->cut_stock[2] = 0.5 ;
' ~5 a( l. u! j9 W4 B$ Eapp_data->tolerances[0] = 0.003 ;
6 |& ]5 K& Z) M& m" V1 Q2 f8 Qapp_data->tolerances[1] = 0.003 ;/ z# S4 |8 Y/ m- E( c
app_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;5 L1 Q! F# A5 \0 r' ]/ [$ h7 N
app_data->feedrate_value = 33.0 ;
; \. D- Y4 q! C0 {app_data->offset = 0.055 ;
9 N& f7 I% G" d! sapp_data->avoidance_type = UF_CAM_avoidance_type_warning;) I: N+ W) F. ]0 V$ t
}
+ a  j, s8 k- e( w' S}

% E/ M. F, ]8 v1 M
发表于 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 | 显示全部楼层
# _0 {' ?  I2 v. ^8 j
求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-11 21:21 , Processed in 0.073828 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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