青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

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


) ~! x3 N/ c" `+ M; U0 H1 u

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

3 B* j$ T  `6 l8 m

#include <stdlib.h>
/ n( y, O: T2 y4 l#include <stdio.h>

#include <uf.h>1 F$ L+ b' v6 Q, l# k) t
#include <uf_object_types.h>& H9 ^5 `. l0 \2 R" ]' V* T6 F
#include <uf_ui.h>
" ^; ^; L* z/ g; T  S  r2 e#include <uf_disp.h>
) g+ S; p* e; v#include <uf_obj.h>
  f/ g- b  ^# J3 J#include <uf_cam.h>
& ~0 V; q6 E$ I- _6 d5 _, ]#include <uf_camgeom.h>

#include <ufd_camgeom_ugroup.h>

#define MAX_CAMGEOM 204 x3 Y) g2 J2 h+ }  `' D) e. w
#define EXIT_SUCCESS 0

/*#define DEBUG*/

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

static int geoption;
9 ~1 \# t. o$ i6 ustatic char *title3 = "Select Geometry Type(s) for operation then Selection Complete";//创建几何! U& n: z9 a6 Y8 S1 a
static char menu3[][38] = { "1) Part Geometry",, [. _6 Y$ j( {: x- H  T
"2) Blank Geometry",
$ A( z+ F6 R, d3 |, M: Q"3) Drive Geometry",
" s: S1 Q- a1 j  A* i, j"4) Cut Area Geometry",' f5 N/ z$ R7 G! b
"5) Trim Geometry",
4 f0 E5 V7 v' b( M% M: c# p"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, M( b; d* Z2 Z
UF_CAMGEOM_app_data_p_t app_data_list[MAX_CAMGEOM];! N3 D' a' D& X' v9 }: a
tag_t *objects, entity_list[MAX_CAMGEOM];
& G# X6 E" K0 z2 Tint i, entity_count, object_count, err_code, type, subtype;

char atitle[] = "Geometry Selection.";, _3 n! A# e  u( l7 d1 l0 e! b& ]
char *cue = "Identify Geometry - Wait for the Filter!";
7 v" J7 P2 q1 Z! Q: X  rchar errorstrg[133];
1 {% N# u2 p. j& pint response, irc;
. B9 b( {! v6 x& w# e& Stag_t eid, view;4 |/ I% @% c( ^: u: P# V. Y1 ~; w! m
double cursor[3];

#ifdef DEBUG
; ~& @7 e# e8 }- zprintf( "Object tag passed into camgeom_ugroup is %d \n", objTag );0 z9 k1 p4 @/ c8 m# [' `2 y
#endif

geoption = 0;

while ( geoption < 10 )//为什么是10呢????. a. |" A: t# O0 [5 A9 W. G: w
{& S3 k8 p1 {3 R' b4 T7 E6 c( X
/* Statements go here for each geometry selection type. 声明起作用 对于选择类型的每个几何体*/

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

if (geoption == 1 || geoption == 2 || geoption == 10 )  c, L4 i2 p0 H: E1 _
{5 V* H2 a* B6 f4 W6 ]
break;2 ~  @) L+ _! g4 k8 h9 q7 f
}3 w! v7 ^1 [4 C% m- w7 K
#ifdef DEBUG
3 v1 n: H7 m6 Q" h( N7 p( Y4 sprintf("\n Geometry option returns %d\n", geoption);
7 y! {/ w$ o9 T1 |. k& }9 C#endif
. d' E  p' O0 ~/* Allow the user to select faces or bodies. 选取面和实体*/

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

if (obj_count > 0 )
8 Z- \/ a$ K' c, N6 y9 P{
' l' v/ f( y8 Y" c" O, Xentity_count = 0;

while (TRUE)

{

UF_UI_select_with_single_dialog(cue,atitle,
7 ^0 i% Y: h7 t# i  fUF_UI_SEL_SCOPE_NO_CHANGE, init_proc, NULL,5 o' H5 E( G9 d7 I. q7 c5 F3 ~
&response, &eid, cursor, &view);//选择单个对象,可输名也可打进去。选择范围在UF_UI中定义/ q* `1 K8 g( o6 e4 B) \, v: v2 W
//cue 提示显示的信息,atitle 对话框头标题,UF选取范围,init初始化程序,NULL初始化程序,cursor光标位置坐标

if (response == UF_UI_OBJECT_SELECTED)
& \8 ~* `0 D+ a$ y' p* I8 j& F{$ J2 o) n+ L0 D1 D: `5 G$ F! s
entity_list[entity_count] = eid;
0 z/ e7 _, O+ Z3 Y* x3 q7 X#ifdef DEBUG- F! _) L. v# m3 t# y. S
printf(" EID number returns %d\n", eid );) p# k! h- |# A: D. D  ~, h- g! e
#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),
) d) `( c. P- _3 P&err_code);

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

init_camgeom_app_data(app_data_list[entity_count]);

entity_count++;
9 Z1 L7 m+ y2 L$ m) p}
4 ?/ m) e! q. d4 U: z& h. Gelse if (response == UF_UI_OK || UF_UI_BACK || UF_UI_CANCEL )
( g" X. \% U5 E6 v{. N8 @6 Y% M/ Y7 b
/* Geometry selection is finished.几何体选取完成 */: ?* O$ r' j7 z: n6 Z
break;
9 L  m: T; Y- I+ x& r5 t/ Z}2 X' O- _! j. _+ y$ y1 A
}
5 U0 e2 i8 @3 f* S/ i, K3 O#ifdef DEBUG
; E/ |* L/ l5 d9 \printf(" Entity count this selection is %d\n",entity_count);
/ z. E) ^% ?+ K- R# l( \! Q#endif

if (entity_count > 0)
, {- q, |6 C- e{
" [# i3 w) L# W: t/* UF_CAMGEOM_item_t *items;1 G+ Z" y& I  S+ F' V8 K
int item_count;*/

for( i = 0; i < obj_count; i++ ). e$ k+ T3 N# ~$ O; J8 n
{
/ {3 l- I0 l/ S$ Zirc = UF_OBJ_ask_type_and_subtype( objTag, &type, &subtype );//返回对象类型与子类型TAG值

if( type == UF_machining_operation_type ||
! s8 M! h3 \5 q" ^8 F  f( ttype == UF_machining_geometry_grp_type)

#ifdef DEBUG* s! Z) y) d5 v- }) }% i
printf("\n Current entity count is %d", entity_count);
  r3 d, d2 o! y" j9 S' Pprintf("\n Current object count is %d", obj_count);
1 G  N* s5 M4 }) ]. [printf("\n Geometry option is %d \n", geoption );
7 K7 |4 }1 {. g6 c( X) ?. D9 y#endif

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

{! U/ ?. s3 p! [2 o0 [0 t
switch (geoption)
: z& G* H0 X8 ]$ j# O/ i{
/ V; a) ]5 \4 V/ p! L% t/ Gcase 1:
' F0 l& ]& @% M* ~case 2:. ?( T( q+ y5 `- U/ l3 h) Z
case 3:% U1 f/ e. Z' y9 B
case 4:
3 c+ ^2 y# a- c9 M) zcase 5: /* Part Geometry 部件几何体*/' C0 t) A$ T/ S, H6 y
{
" d2 u9 F0 y' i9 _$ A#ifdef DEBUG  c0 y* B- |) ~, y% B, J
printf("\n Type returned is %d", type);
0 V1 O2 M- j* s8 ~6 hprintf("\n Subtype returned is %d", subtype);
; l: l& T/ s2 L+ ?8 X#endif) A$ ^4 \- ?- W# I) g
{
9 x  Z# E! S1 e5 nirc = UF_CAMGEOM_append_items( objTag, UF_CAM_part, entity_count,
5 S" _- u9 V5 @9 Y0 J& z1 y6 Fentity_list, app_data_list );2 `+ U# |2 B. N( ?$ i
//附加一个几何实体链表给对象 几何体包括实体等 切削区域仅薄壁实体和面 修剪的几何体不允许。
9 C2 J; n/ e% E* _% f//objtag几何体父级组,UF几何体类型,en_list链表指针,app_data_list链表数据。  q1 u& ~6 i; k/ |$ Z
}* ~9 `) k; ?/ d7 }
if ( irc != 0 )
- f" k. \( g" q% ~' F+ V{! U) ~3 k  Y* e& j; s
UF_get_fail_message( irc, errorstrg );6 M5 }; s3 Y2 R$ f  c3 @
printf("\n The return code is %d\n", irc);
' V+ r6 {8 W3 Z& x4 {printf("\n Error code translates to %s\n", errorstrg);
5 n; ]8 R5 \5 b3 D) y$ v* v; kreturn objTag;* \, T: L. u) ~2 |+ r
}4 `! \* I8 W! U' M6 d& T
break;
. ^9 {# G( w* F6 V- W}

case 6: /* Blank Geometry 空白几何体*/3 G! _, e- S( f: }6 c! V
{
: H' b) C) y; Z( K2 ?5 R#ifdef DEBUG6 b6 q1 k1 K% E% |% E$ g) d  |
printf("\n Type returned is %d", type);4 p7 w2 i. h5 o% p0 a$ g
printf("\n Subtype returned is %d", subtype);0 X' s# f2 d  _& [2 \
#endif
! L" A. G' U9 b$ O{
2 G) T% H' F, C, m5 ?+ {- Firc = UF_CAMGEOM_append_items( objTag, UF_CAM_blank, entity_count,
: O  t7 \  A  b. ~6 E" F4 Y  Gentity_list, app_data_list );% R1 s% N% _0 u& l' K
}$ M1 ~3 c  }' ~& v; x$ s% b+ R
if ( irc != 0 )
4 @) d/ x* k# W' @{; T. o5 r9 ]) i
UF_get_fail_message( irc, errorstrg );1 Q! h  v: y# C3 n5 X
printf("\n The return code is %d\n", irc);6 \) Z6 J2 h9 @9 `, V
printf("\n Error code translates to %s\n", errorstrg);
. `$ L: ]$ t$ L5 N1 B$ b+ @return objTag;% I3 V$ M; |4 S: J' f: G
}
4 S1 Y; P% v" ~0 Jbreak;

}

case 7: /* Check Geometry 检查几何体 驱动*/
# H' Z& i, ?" h+ k4 f{
! R! \7 H* e8 J# o4 v* h#ifdef DEBUG
4 L& M- z+ W+ C# M8 z. }printf("\n Type returned is %d", type);1 d' m4 y& m" y
printf("\n Subtype returned is %d", subtype);
3 y% }; i' T/ G1 k! F8 A5 i# R) U' U& L#endif4 J1 ~0 i4 L7 |) L! U
{
& i1 V# l7 O1 W5 ?+ I6 birc=UF_CAMGEOM_append_items(objTag,UF_CAM_drive,entity_count,
9 d8 u: h" n/ a2 u- N. ]& q; Nentity_list,app_data_list);( }% H) j. n" `; [
}- n* y# W: [$ E0 V/ [/ @
if ( irc != 0 )6 |+ g' @$ i6 I, q* w+ ~" \5 {, F3 g
{
  ~) a+ ~) |. G2 pUF_get_fail_message( irc, errorstrg );
) v, X% k' M' e& A+ @  u$ dprintf("\n The return code is %d\n", irc);
! a  A5 _4 E$ K5 k( cprintf("\n Error code translates to %s\n", errorstrg);! [5 ^: I4 M9 C! i
return objTag;
. i% Z; q  \9 Y2 m}# [& ]! H9 _( `* y$ J
break;

}

case 8: /* Cut Area Geometry 切削区域几何体*/
5 H  k/ n) f: K& X: A{
3 J) B6 Q* R  {9 n, D; N: s#ifdef DEBUG* I9 z7 ^/ d9 q
printf("\n Type returned is %d", type);0 U' `5 h  x. b
printf("\n Subtype returned is %d", subtype);$ c2 W- O5 z) N' S) m
#endif+ T6 _$ t8 |; Q; U
{
# \) r& w; ~5 T# M8 R  @+ A) uirc = UF_CAMGEOM_append_items( objTag, UF_CAM_cut_area, entity_count,+ v$ j' O$ \% n" T
entity_list, app_data_list );
+ b' D  m; Q* U' b* n5 U6 E}
3 i3 `9 ^; c/ s; [, u) V3 xif ( irc != 0 )* N2 p1 y7 N; T8 K9 ]& d1 ?; v
{, z  H& ?6 i+ r- G. e, L
UF_get_fail_message( irc, errorstrg );
$ M+ m& X1 C% ]printf("\n The return code is %d\n", irc);
* A1 }) j% Y! p5 g0 Rprintf("\n Error code translates to %s\n", errorstrg);
( D4 @' W8 u$ \8 s5 y( M3 areturn objTag;
& G9 o9 X1 @+ d; L+ c}
& v: ^, m  `" {break;+ L! n# P" e9 Z0 z
}! j( K) R4 e' ]( S. [4 D4 M# b
case 9: /* Trim Geometry 修剪几何体 */
# w$ ?" g' ]2 t' @7 n" N  D{
$ h! K2 `  B. z/ D' Qprintf("This case not implemented.\n");, x0 @/ B3 F/ ^- C, ^' I
break;
+ x$ ~1 A/ N- Z' J# n}" @6 p# p8 e7 Z/ t, K" Y6 P
/*Drive Geometry 驱动几何体*/

default: /* Selection Complete 完成选取 表达式没有相应的则走这条语句*/
3 ^4 j3 `/ C' @  P& C6 j{! M$ \, w3 V& d* T0 I! R
break;" N- z4 K, u; f
}; E) v! H( j0 o) W" h
}
6 `" t3 s) `2 a0 ?5 X* x9 ~5 x! x: w}
0 {1 M( j+ u6 l' I, O& G2 \}
7 p3 h3 K" K* Y( W7 u}
/ o: Q; I( F( ^: j}
' t/ u3 d* v( Q2 ~( H) a}( z4 Y- B# A$ s+ y; ^- V6 Q0 j
/* Free the Allocated Memory. 释放内存*/
, X4 H2 C' n, l9 s# Hfor (i=0; i<entity_count; i++), r6 b3 T$ @8 t( Q: b
{
' A" a% c8 k, x& @9 U7 q, vUF_free (app_data_list);
6 C* i3 s% s) Z) J}

/* UF_free (objects);*/

return(0);, J+ S! r4 c" D
}
* v* l- S6 l( [/* Selection initialization procedure 选择初始化程序*/
2 X1 ~1 h* k! N* T( Cstatic int init_proc3 y2 N$ ?6 o0 [
(# x. c8 O8 f0 J
UF_UI_selection_p_t select,//指针 pointer2 H; x: M1 I: r9 }& |
void* user_data, f+ e# a2 T1 w
)
: j/ R# a% U: ^! f{
- z5 q, c, I; X$ [7 d3 aint num_triples = 3;6 l5 [5 Y9 D: B4 Z4 q( e8 j: G
//最终指定的特征类型
$ S: x4 |* J2 e  HUF_UI_mask_t mask_triples[] = {$ h* e  e: B' {9 b( t( E
UF_line_type, 0, 0,& h0 l0 g- U, {2 f
UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE,
. [0 j% E* g- G8 O+ h' MUF_solid_type, 0, UF_UI_SEL_FEATURE_BODY};

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

if((UF_UI_set_sel_mask(select,2 e4 _4 Z: z  t' K
UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,
9 e& m( N( j' z; r7 ^4 j, `num_triples, mask_triples)) == 0)//3倍 3*3=9

{8 P. |' Q" B2 B( O2 P; q$ k
return (UF_UI_SEL_SUCCESS);) S; Z6 L8 i0 S2 m  a
}+ y) z+ [% B$ w. A. j5 y
else5 k& o+ k( G, h/ g
{0 k; V1 [9 B) p: I1 ]7 M
return (UF_UI_SEL_FAILURE);2 Q0 P0 X) M9 S& y( ]
}, K- D4 S$ n( O- B0 R
}6 |2 H, Y* y1 s2 y6 z( w5 s9 d) z
static void init_camgeom_app_data- o3 A& J( i0 u. g
(
1 G; [+ v' t1 `- u' O- ^* ~UF_CAMGEOM_app_data_p_t app_data5 y' \5 c/ Y! T  {; N
)
! Y$ r6 F$ W0 f% q  y9 `% s9 I- G6 d{: M( }& [' b* O4 [$ @' o
if (app_data)
9 r4 S! U( t" T/ _: _{6 }/ F& D( B& T1 T1 r8 V
/* Set flags. 设置标识*/

app_data->has_stock = 0; /* Used 1 - Else 0 */
3 v- \  e( M$ J8 Q' Tapp_data->has_cut_stock = 0; /* Used 1 - Else 0 */
$ T( R, v$ r5 a1 Kapp_data->has_tolerances = 0 ; /* Used 1 - Else 0 */
4 V' \& `6 `  I7 A5 z: {& Uapp_data->has_feedrate = 0 ; /* Used 1 - Else 0 */4 p7 z! d  k+ O# E2 M4 k* ~! y
app_data->has_offset = 0; /* Used 1 - Else 0 */9 ]0 K& b2 W2 X5 I
app_data->has_avoidance_type = 0 ; /* Used 1 - Else 0 */

/* Set values.设置参数值 */7 n8 }% d* Y, A
app_data->stock = 0.033 ;+ K" N0 j3 U% D- K: g& w
app_data->cut_stock[0] = 0.2 ;8 A& \* b" x' S  X: `
app_data->cut_stock[1] = 0.1 ;$ ~9 i2 L, J1 E; \- u7 U( R
app_data->cut_stock[2] = 0.5 ;
" _9 y) r3 u: A/ \% Napp_data->tolerances[0] = 0.003 ;( J5 J* d# {- U  K4 f
app_data->tolerances[1] = 0.003 ;
) u" M6 x& r: g" s) E: {* `2 Zapp_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;. l4 J  \% e( T& Q
app_data->feedrate_value = 33.0 ;, }# j. q, `! M/ _- u
app_data->offset = 0.055 ;$ m! P$ p$ H1 E# @4 E9 N0 ~( |
app_data->avoidance_type = UF_CAM_avoidance_type_warning;* l0 F7 k6 w4 }. [, D% p0 A! @1 D
}
+ p5 Q% w5 u. }4 n3 \6 g}


4 \) u6 r! p+ Q; O3 b% |* f
发表于 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 | 显示全部楼层

4 z; o' k' d* K+ E求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-21 20:01 , Processed in 0.058927 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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