青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

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


! t- L8 L' m3 ]

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


% P: Q, X/ i+ C. J

#include <stdlib.h>/ t4 x/ O5 t: w" X0 ~
#include <stdio.h>

#include <uf.h>$ ?  U7 Y5 G$ q1 B; q$ ?
#include <uf_object_types.h>( G. {6 v7 R6 n# I  J' L0 F  M
#include <uf_ui.h>" S, N: A# B9 m2 {, v* t
#include <uf_disp.h>3 M% n4 M9 G9 F8 o# F: T# w4 s! j
#include <uf_obj.h>
3 V4 _8 S" i9 k( G' E+ N! C#include <uf_cam.h>
: u1 U5 K/ ^. X#include <uf_camgeom.h>

#include <ufd_camgeom_ugroup.h>

#define MAX_CAMGEOM 20# C+ A/ G; Q$ q* ?
#define EXIT_SUCCESS 0

/*#define DEBUG*/

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

static int geoption;( R/ R; ^/ M' F( I
static char *title3 = "Select Geometry Type(s) for operation then Selection Complete";//创建几何/ F" i: ^( r$ h
static char menu3[][38] = { "1) Part Geometry",' S, W' `  N! ]* c" D
"2) Blank Geometry",
8 p6 l0 ]" W. X; ]: Y3 U( Q"3) Drive Geometry",* r3 d$ H* v! v9 f0 f; e
"4) Cut Area Geometry",3 f: E$ F0 N9 b+ \! O- x' _
"5) Trim Geometry",4 o9 w1 L# O6 `. m  w  X
"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 )

{
' I: A/ A, K' XUF_CAMGEOM_app_data_p_t app_data_list[MAX_CAMGEOM];
/ {. S6 ^+ B: b+ Mtag_t *objects, entity_list[MAX_CAMGEOM];; M0 k" d) \0 D/ t5 |+ _3 p; x1 w
int i, entity_count, object_count, err_code, type, subtype;

char atitle[] = "Geometry Selection.";
0 C" ~& j7 J0 `. S; Q. o" m- rchar *cue = "Identify Geometry - Wait for the Filter!";
6 E3 w+ O* w. E" i6 I8 ochar errorstrg[133];4 l6 L. {3 P2 G; B
int response, irc;2 z1 N$ E* Y6 ~( z5 V
tag_t eid, view;
6 g1 I) |; j9 n% {double cursor[3];

#ifdef DEBUG( S/ [# }9 X  J8 Z4 D
printf( "Object tag passed into camgeom_ugroup is %d \n", objTag );7 b4 L& ~9 W" I
#endif

geoption = 0;

while ( geoption < 10 )//为什么是10呢????- k- l. c* d6 v7 b
{
: f" L( |6 ?* o" M7 C' m/* Statements go here for each geometry selection type. 声明起作用 对于选择类型的每个几何体*/

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

if (geoption == 1 || geoption == 2 || geoption == 10 )
/ F( b, e" `1 k7 P9 O8 A+ u( X{. g5 e2 h) E* H# X$ m8 Y/ F3 W
break;
5 e- {( K4 T% ]$ ^0 z0 F+ M' r4 D}$ J! u# v7 Y- C) d
#ifdef DEBUG
+ q6 B, [5 a) m6 iprintf("\n Geometry option returns %d\n", geoption);1 g% o5 f" m4 Y* s( S
#endif2 h8 j- Q$ i  N9 z% w
/* Allow the user to select faces or bodies. 选取面和实体*/

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

if (obj_count > 0 )3 N9 \- I$ \4 I
{. u4 m+ d# g! Y5 X7 r" m
entity_count = 0;

while (TRUE)

{

UF_UI_select_with_single_dialog(cue,atitle,
- n6 B7 N/ k$ U* m3 o+ ~# GUF_UI_SEL_SCOPE_NO_CHANGE, init_proc, NULL,
( ~1 j0 _; d1 [  r9 L& r+ a&response, &eid, cursor, &view);//选择单个对象,可输名也可打进去。选择范围在UF_UI中定义
+ T3 k  Z8 S- R( M8 J//cue 提示显示的信息,atitle 对话框头标题,UF选取范围,init初始化程序,NULL初始化程序,cursor光标位置坐标

if (response == UF_UI_OBJECT_SELECTED)
, n! `  Q- R+ v) t5 I2 w( Z{
6 |* ^7 x! P; ?3 d$ Y! {: A0 u) o3 centity_list[entity_count] = eid;
  w4 ^# b: _1 B% ]#ifdef DEBUG6 \. A1 Y7 ~8 b! f" P7 |( M
printf(" EID number returns %d\n", eid );1 J3 A6 h- @6 U1 x  P
#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),  m2 O( z4 j+ R5 h* l
&err_code);

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

init_camgeom_app_data(app_data_list[entity_count]);

entity_count++;
9 T9 i3 [  Y1 ]- S}
5 o) n. [; l$ d) ^5 g6 F5 c$ I; O; Eelse if (response == UF_UI_OK || UF_UI_BACK || UF_UI_CANCEL )# r) l' O) n+ }
{' z' B4 x3 ?8 x5 [- A) [
/* Geometry selection is finished.几何体选取完成 */
8 s1 ^% H0 ~+ @* Cbreak;
. q* j. `; M9 D6 s0 }/ q4 f. Y}9 D8 ^4 n! u; F! n, N1 J. F
}
+ J! y/ q. l1 w1 Z" r% g' n8 h" H#ifdef DEBUG
# F; k# y- c) o, H5 {) [/ Kprintf(" Entity count this selection is %d\n",entity_count);$ K: D5 J' N# A0 w
#endif

if (entity_count > 0)
1 Q8 K% h9 p( e2 T{
% y7 t' G% c& e2 L. ?; z/ Q/* UF_CAMGEOM_item_t *items;5 r0 y/ C4 t' o* ]# U
int item_count;*/

for( i = 0; i < obj_count; i++ )
$ h+ K2 O( x  y: w9 |{# n# w" b- {! q% ~
irc = UF_OBJ_ask_type_and_subtype( objTag, &type, &subtype );//返回对象类型与子类型TAG值

if( type == UF_machining_operation_type ||8 m) H% U- k# w- ?2 E
type == UF_machining_geometry_grp_type)

#ifdef DEBUG
* o5 ^# T  E/ rprintf("\n Current entity count is %d", entity_count);
( u, e4 w0 h8 h# Nprintf("\n Current object count is %d", obj_count);
5 _1 ?+ I/ d3 m6 \printf("\n Geometry option is %d \n", geoption );
6 H' F& {& `  x8 V#endif

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

{/ a: J8 U+ v$ T# E
switch (geoption)% b* G+ K* K& a6 j' y
{
# g# Y( k$ o$ s$ k: L0 p9 x* Bcase 1:, w8 F" Q- `) H1 T2 Q
case 2:  v2 |! y  P6 p4 a* b
case 3:
: b* w- b" _  i! Ccase 4:0 L; `: z* s$ z/ P" N& g( U, m% Q
case 5: /* Part Geometry 部件几何体*/
7 S5 f6 i- F) L+ c6 B' F2 k{
7 C# d; k* l' E9 }; E0 X#ifdef DEBUG
5 H8 X6 |" n1 r1 K  X. Q- _7 ^printf("\n Type returned is %d", type);
$ r( D$ v+ B* L2 w7 nprintf("\n Subtype returned is %d", subtype);
* _& l, _( @8 w  M#endif
  j* g; h. T' m( H{: l9 g0 n5 h. Q
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_part, entity_count,$ T5 A! z2 L" k
entity_list, app_data_list );
# ~0 Y' Z  h& _0 h//附加一个几何实体链表给对象 几何体包括实体等 切削区域仅薄壁实体和面 修剪的几何体不允许。. R3 ?$ C: x% g# [# @2 t/ `7 v3 X
//objtag几何体父级组,UF几何体类型,en_list链表指针,app_data_list链表数据。6 u4 G" G, d' y& u( g0 B
}
, g3 u$ M8 r, {if ( irc != 0 )
3 \1 z  s& o. ?8 k2 ?{
* W3 O, c# I2 ^2 EUF_get_fail_message( irc, errorstrg );
3 P! P3 ]' ?6 I7 z. _5 s- Zprintf("\n The return code is %d\n", irc);
1 k0 X/ C$ a  g' M7 n9 yprintf("\n Error code translates to %s\n", errorstrg);
4 L; V6 T5 y! \' A* A' t6 q1 Freturn objTag;
2 z+ v6 \9 `  r( s1 O! T}% _6 O( f* m) g# ^( L; S) ^
break;
7 G( y7 i" S& J  P}

case 6: /* Blank Geometry 空白几何体*/
6 U2 S0 l4 p" Q( U) X( f{
7 Z- }4 s8 @: W  A#ifdef DEBUG2 i, ?- Q! R, d2 ]" R( t* q; m
printf("\n Type returned is %d", type);2 E5 d6 u8 ^* ]; G3 \4 t
printf("\n Subtype returned is %d", subtype);- s, l& ~) B* O( Z, x: _
#endif
+ _) |3 h) l% r2 t1 f. n) v{; I. K- \2 P% H
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_blank, entity_count,5 u: A; ~7 f( C9 [- Y
entity_list, app_data_list );! B3 a: u5 P6 o& g
}( }4 b+ p7 q% i
if ( irc != 0 )
" O# I% P6 \; I8 _( ]! L+ M{
( D: B  D. q7 tUF_get_fail_message( irc, errorstrg );
& F2 G. E' c* h" l4 x9 uprintf("\n The return code is %d\n", irc);! `& o0 N/ x4 H  v# _6 k1 Q# [; x0 z
printf("\n Error code translates to %s\n", errorstrg);6 Y! i; }" P- b/ r/ s
return objTag;
6 d' d1 O; e5 H, h0 k/ L- Z* b/ t}
1 S' j% f; W- G4 [  l: }1 Qbreak;

}

case 7: /* Check Geometry 检查几何体 驱动*/0 ?# @# i& V; W5 w8 X# j
{
( M7 e' q1 t& V; V. D0 E. Q#ifdef DEBUG/ W: G! X/ m  @& ?- _
printf("\n Type returned is %d", type);, u9 J1 Z( R2 k8 Q9 _5 ~) B
printf("\n Subtype returned is %d", subtype);
8 a5 x- S. i! ?  w: Z- y: i#endif2 \5 W8 E  x0 ~9 Y3 y, w( l
{
# R& m8 _  P+ J6 wirc=UF_CAMGEOM_append_items(objTag,UF_CAM_drive,entity_count,
0 W2 j; j! r& `) Eentity_list,app_data_list);" G# m3 V% @# p" }
}7 p8 }' f( i( ?$ t& i2 G8 @2 J5 S( E3 J
if ( irc != 0 )
: ^1 H5 H: X/ l4 g& L{
) h$ r3 |- o( |- n3 IUF_get_fail_message( irc, errorstrg );! L# }; q  H7 e8 W, Q
printf("\n The return code is %d\n", irc);  w: z* {( n: W7 v5 o2 I
printf("\n Error code translates to %s\n", errorstrg);' W+ e) O7 g/ q1 d( x1 a' W; ?' ?
return objTag;
- F2 y5 \4 i$ P% @/ _) F}
7 @1 l9 A% A/ p; Wbreak;

}

case 8: /* Cut Area Geometry 切削区域几何体*/
, `1 h* a: r* j* n/ I7 k" Z{# w9 L. y) m- e$ ], b& Y1 u! u% j
#ifdef DEBUG
' C$ j) k1 t& M% Xprintf("\n Type returned is %d", type);
' P' M) x2 Y+ ~3 {printf("\n Subtype returned is %d", subtype);
% b7 W" h/ S' @" m#endif# N3 S% `. ?9 C
{) y3 A9 H' V2 d2 r2 }
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_cut_area, entity_count,3 U4 \' o7 ]# O$ o3 p
entity_list, app_data_list );4 E. l2 J- k. o! [* ]* _  H1 h
}9 u9 N; s& T: \3 G0 U2 @
if ( irc != 0 )$ e( x/ L6 o' y! O+ O& r, X
{
- b  ^9 r9 u( K2 C/ a' Y( _/ DUF_get_fail_message( irc, errorstrg );
) h& i  t" k* U) Tprintf("\n The return code is %d\n", irc);
. ~$ i! W' N+ s7 r: P7 C8 sprintf("\n Error code translates to %s\n", errorstrg);
! A$ @9 r+ V5 ]! ~3 d4 ?. K3 P$ sreturn objTag;
4 e) ^4 Z3 u6 q2 Q+ j}
8 d* n# b- B" k4 y5 Tbreak;
5 j- j- K# |( r5 U" K8 o}3 w* Y+ t1 g8 C$ L* |
case 9: /* Trim Geometry 修剪几何体 */
% ^- W# k" R: ]7 H5 p+ `{+ ~' y3 i% w* x% w6 {, v6 J' J
printf("This case not implemented.\n");, Z! d# z1 ^  j% i
break;$ B: Z; V5 L1 s8 q
}
0 X- f# S2 S7 }/*Drive Geometry 驱动几何体*/

default: /* Selection Complete 完成选取 表达式没有相应的则走这条语句*/
. g7 Y4 }' J& g. d" R5 _2 w{* _# j5 V3 ~% f' d3 m. N
break;: p3 Y+ L1 i: k/ k( H7 I# o
}2 W9 y( g( W9 y) b* _9 H
}
" {7 x+ V; j1 `$ O}
5 S' |6 }, q; M4 G# i. W}
! `1 R4 |" U* x9 _$ N}# X, |' F: h3 ~1 z7 ^6 j
}( f9 z' M2 A3 |1 `6 M
}: G6 m, \# e" s3 j3 k% v
/* Free the Allocated Memory. 释放内存*/
5 L1 T/ H9 W0 _. o9 l/ W1 i' mfor (i=0; i<entity_count; i++)
7 Q! m# {( p) C0 S0 ~* c( r; b- r  t& @{
7 @& a0 n! N) d0 [UF_free (app_data_list);
. l+ V  F1 X* n/ e, |5 x. V}

/* UF_free (objects);*/

return(0);2 e! ]4 E' H" O' `
}1 |2 `. w# D% l" `* m; [7 A3 y
/* Selection initialization procedure 选择初始化程序*/
1 u! c+ }0 k  c9 s! v$ Lstatic int init_proc% r0 f( j2 C0 K4 h, |
(
8 i) h. C6 k! `) K/ \) N4 GUF_UI_selection_p_t select,//指针 pointer
3 G2 t! p% n3 F9 ]: Z$ Ovoid* user_data
8 p1 T, q. C. S1 m& S)- _% }! |# C! b  M8 a
{7 b; z& ]. y/ w4 w4 A' e
int num_triples = 3;0 M2 X9 j+ A5 {9 y
//最终指定的特征类型' s& Q2 |2 x& X# m* N0 ^/ O. A
UF_UI_mask_t mask_triples[] = {# e0 E' H" P3 x- e2 c2 o% v: {
UF_line_type, 0, 0,$ n$ _9 v) J7 I  P, Q3 f
UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE,
" W2 h+ V" E+ x+ C" {. XUF_solid_type, 0, UF_UI_SEL_FEATURE_BODY};

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

if((UF_UI_set_sel_mask(select,7 y5 M" D/ T% Q2 F0 L
UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,* T) q. X# Z- r2 E  a
num_triples, mask_triples)) == 0)//3倍 3*3=9

{' L* X# w6 F6 [3 P* T1 L# k
return (UF_UI_SEL_SUCCESS);
" s9 p2 F* f+ j: C6 C! B6 k}
* ^9 u5 [. \; k6 S% [else2 \" I2 m! x5 j' q4 M
{
* t6 v9 Z& b; }* K. \/ X9 ireturn (UF_UI_SEL_FAILURE);
) |( O9 O: [; N# [}
0 s. b2 q9 S' P" [! R: ?}
5 S* |+ Z* M2 z: Wstatic void init_camgeom_app_data* r+ t& y" x1 F6 p! H
(
7 T6 V( H; s$ p  |  f. SUF_CAMGEOM_app_data_p_t app_data6 J; Z3 z# ?; Y5 @0 D
)
: q' F: K% d  U1 `{% F$ U. ^$ u2 Z  `0 k7 j
if (app_data)
3 {, |6 w4 Y+ ~{( A5 D: ?5 g7 f& A3 A6 l2 ?
/* Set flags. 设置标识*/

app_data->has_stock = 0; /* Used 1 - Else 0 */7 G4 {: m! f3 ]7 X, I. R! c
app_data->has_cut_stock = 0; /* Used 1 - Else 0 */5 I4 c- }& D3 C! @2 j2 S0 D. L
app_data->has_tolerances = 0 ; /* Used 1 - Else 0 */- `$ c0 A* V/ h2 l2 m8 ~( f
app_data->has_feedrate = 0 ; /* Used 1 - Else 0 */
1 c2 f. P, n' R+ }, w& Tapp_data->has_offset = 0; /* Used 1 - Else 0 */
4 b/ Y" ?  l1 S1 lapp_data->has_avoidance_type = 0 ; /* Used 1 - Else 0 */

/* Set values.设置参数值 */
0 o% m( I" ?( b1 \app_data->stock = 0.033 ;
$ {. |( J8 v. o8 B0 sapp_data->cut_stock[0] = 0.2 ;: ^" V# m4 h5 W# j8 D
app_data->cut_stock[1] = 0.1 ;3 C7 Z3 x- g4 s0 F' q- C  l
app_data->cut_stock[2] = 0.5 ;4 Y6 T! Y9 B- q( }
app_data->tolerances[0] = 0.003 ;
, ~, X+ t& f4 p! f, P+ Vapp_data->tolerances[1] = 0.003 ;
2 D3 j( t5 ~  b  X3 [0 n' h) Japp_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;
9 i, n7 ~4 |( F9 H8 p) a0 I  |6 N4 Iapp_data->feedrate_value = 33.0 ;
' v; ?+ s1 O$ E% b9 eapp_data->offset = 0.055 ;: V6 T# s; k4 B5 w1 h
app_data->avoidance_type = UF_CAM_avoidance_type_warning;1 G: v6 v$ c- l: F
}- \- @" F  g: u: D2 d
}


, _! N7 E! z: t7 k* C
发表于 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 | 显示全部楼层

( H' ]& D6 _# `( O3 o1 Z; \求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-19 08:40 , Processed in 0.176000 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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