青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

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

8 Z& o4 ~0 ^2 {

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

! J' d  U  b. G9 D

#include <stdlib.h>
6 X# d  ]! b5 s& e9 b4 Z#include <stdio.h>

#include <uf.h>6 [+ z5 p& G) ~3 F
#include <uf_object_types.h>  n& h% p! c+ o! u* p
#include <uf_ui.h>
" x( m9 J9 d2 V+ |' ^" K#include <uf_disp.h>
& l5 o8 y: ~; \' k) x6 f#include <uf_obj.h>
1 }. W0 N6 h4 s( N7 ^1 P) B#include <uf_cam.h>3 ^9 V  Q7 z' L6 w6 h+ N& A5 z/ X
#include <uf_camgeom.h>

#include <ufd_camgeom_ugroup.h>

#define MAX_CAMGEOM 20% B& U6 Y" h% n/ |) E
#define EXIT_SUCCESS 0

/*#define DEBUG*/

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

static int geoption;9 M4 U2 ~+ o1 U% u" L
static char *title3 = "Select Geometry Type(s) for operation then Selection Complete";//创建几何
* T& j1 Y0 }+ W( ]6 ?static char menu3[][38] = { "1) Part Geometry",
0 d+ _5 `" l/ E"2) Blank Geometry",* J2 ]) @; s! j& C* _5 F
"3) Drive Geometry",
3 v7 ]. X; D1 C3 H1 q# q/ O: s1 Q"4) Cut Area Geometry",
: ^7 w0 W( z+ F* d"5) Trim Geometry",
! ~" w( r9 s- Z( J/ `"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 )

{1 x- j0 e9 Y. X. x! T  w8 \
UF_CAMGEOM_app_data_p_t app_data_list[MAX_CAMGEOM];7 N1 e" X# l% G: o5 ~( @
tag_t *objects, entity_list[MAX_CAMGEOM];0 A/ s; ?: R$ l  ?7 s, }
int i, entity_count, object_count, err_code, type, subtype;

char atitle[] = "Geometry Selection.";' C6 n* I8 R5 u6 D# l2 [) {
char *cue = "Identify Geometry - Wait for the Filter!";) q, y+ \0 E/ n* u4 D7 e3 s6 k
char errorstrg[133];
: d" e6 m* ?8 v$ J; ~9 cint response, irc;! ]" y" Q6 }1 v! {( A
tag_t eid, view;
# \1 n$ V5 B# j4 l- Kdouble cursor[3];

#ifdef DEBUG
$ q! y- v. ~/ c0 p  Nprintf( "Object tag passed into camgeom_ugroup is %d \n", objTag );$ z* }. B0 g) k. u+ P
#endif

geoption = 0;

while ( geoption < 10 )//为什么是10呢????. B% f( S0 g! i) h
{
9 U% L6 v8 R  _2 ?) ^% Y/* Statements go here for each geometry selection type. 声明起作用 对于选择类型的每个几何体*/

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

if (geoption == 1 || geoption == 2 || geoption == 10 )2 v. Y* Q- y7 c. x4 Y
{
- M; G/ W* A' n1 O* \* ^break;7 K7 g3 |, S* {5 }9 I
}9 S7 H7 j5 N' W- T
#ifdef DEBUG
7 v# n$ F2 j7 k% vprintf("\n Geometry option returns %d\n", geoption);8 N7 Q6 W* E7 \6 e* f6 \$ k
#endif
, k3 [$ h. t& ~" @/* Allow the user to select faces or bodies. 选取面和实体*/

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

if (obj_count > 0 )' v/ B$ L$ M7 w& X  Z5 J+ W& M
{# }3 ?8 `/ _5 x! s3 t6 w% v) r7 Z
entity_count = 0;

while (TRUE)

{

UF_UI_select_with_single_dialog(cue,atitle,6 f" T2 R" l3 {' e- E
UF_UI_SEL_SCOPE_NO_CHANGE, init_proc, NULL,, O# i& O: E& x$ D( G% S9 y4 }  Z8 I6 [
&response, &eid, cursor, &view);//选择单个对象,可输名也可打进去。选择范围在UF_UI中定义
! `* O9 a5 {/ i0 d( v5 ?2 O3 e//cue 提示显示的信息,atitle 对话框头标题,UF选取范围,init初始化程序,NULL初始化程序,cursor光标位置坐标

if (response == UF_UI_OBJECT_SELECTED)
4 b: m+ f5 \4 x8 ^) h{
" q# E  L4 K5 R0 `( v5 ?3 d" v, Rentity_list[entity_count] = eid;
1 V! S5 P9 W5 C- [#ifdef DEBUG7 K4 b) b( l' \6 j
printf(" EID number returns %d\n", eid );
- g. z' Q) D" Q6 I, L#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),! Q0 {* @1 I" v. O) `% k
&err_code);

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

init_camgeom_app_data(app_data_list[entity_count]);

entity_count++;
8 e& ^  c; E' ]. O; o}
! p. m1 S* D4 Selse if (response == UF_UI_OK || UF_UI_BACK || UF_UI_CANCEL )2 c  X. y' G  O2 d, ^- D) r6 q
{* X* p; ^  Q6 ]: ]0 Y) c6 N
/* Geometry selection is finished.几何体选取完成 */0 H: A) D" E% _
break;
& z& z! X8 t; B}
7 \* w# `4 a( {  T}5 V7 Z1 z  v) t! X
#ifdef DEBUG
2 G' @# N' B6 h6 e( p& c0 Tprintf(" Entity count this selection is %d\n",entity_count);: Q( s2 F9 p$ i: f* \5 F- x
#endif

if (entity_count > 0)
% }1 H* e. u' n* V9 Q{
' e  Z8 \" N* S) K/* UF_CAMGEOM_item_t *items;- c" u6 T2 a* ^2 C- e1 d
int item_count;*/

for( i = 0; i < obj_count; i++ )9 n6 j* B/ D* R8 d: K8 C$ K) ~
{
0 P9 M: z- S/ Mirc = UF_OBJ_ask_type_and_subtype( objTag, &type, &subtype );//返回对象类型与子类型TAG值

if( type == UF_machining_operation_type ||# p( y& m* t0 G$ G% z. Y8 @
type == UF_machining_geometry_grp_type)

#ifdef DEBUG5 c, K5 k+ l# c! H. u+ }( T
printf("\n Current entity count is %d", entity_count);% n# F. y5 `  V, A* Q- G( L
printf("\n Current object count is %d", obj_count);& O. d  e+ e3 S  r8 l8 r4 s; A* c
printf("\n Geometry option is %d \n", geoption );7 F5 A+ W) |+ v  B) P- G. c7 q
#endif

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

{0 ~4 ~: Q  G2 O! S
switch (geoption)2 ^9 _6 ^6 D; T7 u' [# n9 U
{
0 f# F3 t' O8 Qcase 1:4 I7 D. Z6 w  e6 p. B* X
case 2:0 f+ f8 {7 p9 q0 _1 D
case 3:
1 q" V6 b4 i" ?: B9 r( ^case 4:
! a4 ]& G. O4 k$ U6 Pcase 5: /* Part Geometry 部件几何体*/5 m0 W3 r, a$ p; D' H+ [1 ]
{8 X! w( k; q1 f  O5 ]* i
#ifdef DEBUG
/ z4 j; D7 I8 x; Eprintf("\n Type returned is %d", type);
+ y* V; @* m* \printf("\n Subtype returned is %d", subtype);& j" S3 U- X% A0 \2 ]
#endif' c! J* m# Y: |# Y2 `" e$ T
{7 F8 j9 C5 _( P( [0 g' K
irc = UF_CAMGEOM_append_items( objTag, UF_CAM_part, entity_count,# r) |" i2 j* {( h( k8 w; e) M
entity_list, app_data_list );& |* P! A% f( k) z+ b$ z$ Q
//附加一个几何实体链表给对象 几何体包括实体等 切削区域仅薄壁实体和面 修剪的几何体不允许。
+ h5 Q( Q8 r2 U* _//objtag几何体父级组,UF几何体类型,en_list链表指针,app_data_list链表数据。
' [: a* r4 _- q. \4 b# m7 @# H}1 s" I3 ^( W& g9 Y5 q/ t" B1 e
if ( irc != 0 )5 H9 P& G" J. B4 L0 a  G) Y% o
{, X7 f5 t, T  A* n9 U  y
UF_get_fail_message( irc, errorstrg );
7 p& F  H) y  Uprintf("\n The return code is %d\n", irc);
/ g+ x- x* R5 W$ Gprintf("\n Error code translates to %s\n", errorstrg);
# ^% N: O# C+ ^! F0 S2 G% `9 }5 [return objTag;9 ^5 P, p* V- T7 [. v0 _
}; J% D' O. j4 T4 c* ^; n
break;
( e, D& F5 o. _# r" n6 G- |- f% y}

case 6: /* Blank Geometry 空白几何体*/3 e9 p6 V" O: _3 A3 L6 x, r
{5 z5 H3 r2 y4 z0 N0 \. `6 L
#ifdef DEBUG2 G$ y. ^. G2 K
printf("\n Type returned is %d", type);
$ p" e6 _; h% x- T) Y6 x' hprintf("\n Subtype returned is %d", subtype);8 R$ H% D) ~# p$ F; }4 X2 |7 G8 |
#endif
; X. g  D1 K4 m" {4 E) |{
1 H) Y' m$ f% f' X0 |8 G/ @1 L# I! Sirc = UF_CAMGEOM_append_items( objTag, UF_CAM_blank, entity_count,
- ?# {$ B1 T" q; T+ c8 l" nentity_list, app_data_list );& M8 [8 J8 R: x; @# b1 b  E7 A
}8 |0 Z. g: o& P9 q5 _; u' {
if ( irc != 0 )& r4 k3 J9 d5 W- P
{
" [+ M5 G' Q/ H! I& YUF_get_fail_message( irc, errorstrg );
6 g  |, U* w! b# D3 [printf("\n The return code is %d\n", irc);0 v9 ?- [3 q1 u, f
printf("\n Error code translates to %s\n", errorstrg);
3 Q& E  ~! o& k: D  ureturn objTag;
2 L0 S! s5 B; B4 a$ T}: v0 ?6 Q9 p. [5 d1 ]* ]" N" V
break;

}

case 7: /* Check Geometry 检查几何体 驱动*/
; {, X- b, w, M' m; V{$ Y' C+ Z& S2 L) K
#ifdef DEBUG
) b7 J" O7 J3 k5 a/ w5 Dprintf("\n Type returned is %d", type);8 n6 d* c* ?7 L. L: L
printf("\n Subtype returned is %d", subtype);6 \/ L- X- \! O4 h
#endif
/ b7 i, {5 M/ Y, C* h& Q# y{( _. f9 g" p0 ?/ X& }
irc=UF_CAMGEOM_append_items(objTag,UF_CAM_drive,entity_count,* D6 |& N4 I$ L0 o; F/ d
entity_list,app_data_list);
) X( L- Q8 B5 A: s* a6 W}
9 ^- K( {% |  ~$ G1 K/ a4 qif ( irc != 0 )! j, M5 k5 f% E
{& w, Y* O9 _+ ^9 o  m2 v$ ^, }' P7 _
UF_get_fail_message( irc, errorstrg );8 ~( Z1 I7 k* l, G5 K' k/ U/ N
printf("\n The return code is %d\n", irc);6 J2 p( u! q* U/ _
printf("\n Error code translates to %s\n", errorstrg);
, u5 d: w+ B  l( }; S& xreturn objTag;% E  s- u' M9 @; q
}. E( y3 G, Z& S5 P( ^+ o( A
break;

}

case 8: /* Cut Area Geometry 切削区域几何体*/
- n) ~, \# b3 c$ u1 d{
. }( m+ R# w0 P) N. R  A#ifdef DEBUG
6 M7 z6 p& y* b2 b7 F1 [9 |$ d7 lprintf("\n Type returned is %d", type);
- O- a5 I' c$ a/ _& Cprintf("\n Subtype returned is %d", subtype);
# S2 s0 h9 N* b( y# y* {* }3 _6 e4 Q#endif: D& `" p# w6 x% d5 Q& B; Q, h
{
" {) {# c% D+ F/ e1 a! jirc = UF_CAMGEOM_append_items( objTag, UF_CAM_cut_area, entity_count,% m3 d0 Z# y( N; {2 o5 K' J
entity_list, app_data_list );
* Q+ _# `4 R" h( C- b* x}" [; L# d, N; L- |) C( h" H7 E
if ( irc != 0 )
8 Y$ {; J( m! w4 a* p{* e; t3 H% {. g3 d8 a6 C" J
UF_get_fail_message( irc, errorstrg );
' P- w% @9 B0 Y& b3 |0 lprintf("\n The return code is %d\n", irc);
9 m  b. E% L3 q7 _* gprintf("\n Error code translates to %s\n", errorstrg);$ B4 E% L: b, V3 h; Y9 b: P- o' c
return objTag;1 O- B, K5 i: r& ?
}; L( F5 n; H& F0 D+ n# {6 A
break;
( V/ X6 E9 q, v3 P# n}
- V8 a) [5 F7 N7 ^0 {case 9: /* Trim Geometry 修剪几何体 */# t! |3 h: d3 a) t, V' z  z' ^
{
# f( h6 r( l3 A4 y% u( Mprintf("This case not implemented.\n");
. r- M9 g3 Y. B2 J2 E$ u# }! N: Zbreak;
3 `: i/ Z$ _. j! ~; e+ M8 q- o6 r}- m4 W2 S0 X0 m& A$ D7 h, _
/*Drive Geometry 驱动几何体*/

default: /* Selection Complete 完成选取 表达式没有相应的则走这条语句*/
2 u4 x2 W( F2 \0 E0 I- X$ t2 F( z{8 `* G# n: B( p* e- U
break;# e- w0 _% g6 N* @, F7 i3 B
}
' ]1 \1 d4 {* V! S2 `. R8 ^}
7 T3 f0 L& Q2 I* A}2 o" {! \2 |$ q# i- J8 M2 h
}
9 c6 |5 e" v: R}
: l+ I+ t1 ]3 B8 {: }3 n}
% u: p" S* f$ |& S0 {) S& N}
5 v. r8 L2 i0 }: w9 h' \6 w/* Free the Allocated Memory. 释放内存*/
9 j$ i8 e% G5 ~for (i=0; i<entity_count; i++)
$ O2 _9 J: m. _& z{
$ P, ?$ n! F  PUF_free (app_data_list);
* [. r7 v' \: {6 }}

/* UF_free (objects);*/

return(0);
- u; q/ H& J+ \0 G9 P}5 X, |5 F* x9 y
/* Selection initialization procedure 选择初始化程序*/% z, h. N  X! u, n
static int init_proc
! J4 i( n9 B0 P& {(
, t8 e) l( G$ i. W5 VUF_UI_selection_p_t select,//指针 pointer
! O8 m9 J- H/ P( ~% A4 ovoid* user_data
" K0 I% |1 s( ~" W, })1 j2 V9 R2 u! I; i1 n  L# y
{5 g' q; P2 X6 M5 W3 x
int num_triples = 3;
, I; u/ [9 c; ]1 m9 C  J//最终指定的特征类型
. V1 J$ m& v- ]$ K, ?* J- B0 CUF_UI_mask_t mask_triples[] = {
% q+ o! G" Y, ]: g2 `' V& y& L; V: rUF_line_type, 0, 0,
6 X& @$ A( ~0 c. `# GUF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE,5 R5 W! |8 R6 R! X/ S
UF_solid_type, 0, UF_UI_SEL_FEATURE_BODY};

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

if((UF_UI_set_sel_mask(select,
  G% j$ P  w2 @; t% h  ]UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC,% f# F; o: h( j
num_triples, mask_triples)) == 0)//3倍 3*3=9

{
) l+ ~% U% M; {& `5 K1 areturn (UF_UI_SEL_SUCCESS);
* k- n# c" B- c- L. a& n}8 l: Q, k+ n2 k9 O* o3 u
else
, u- i6 S- H  v4 |7 D{
$ D  @6 _" B# M8 preturn (UF_UI_SEL_FAILURE);
! B5 s( p, W! l  s9 I9 R}" D5 `) \2 _) F5 {9 n, r
}6 v- S. n2 x1 z# Y
static void init_camgeom_app_data* [# f0 l8 W1 |& y
(
" D3 z& B0 \9 ~4 C" ~- }% d0 l$ n* kUF_CAMGEOM_app_data_p_t app_data
- N$ e& K) U* {% K+ Q)
) i2 G, X+ J) I7 X8 @5 E{, t( y8 I0 U* M/ v
if (app_data)
% [% d+ Q3 W: c, Q: Y. F{
7 _+ k& H/ E$ o+ |: S/* Set flags. 设置标识*/

app_data->has_stock = 0; /* Used 1 - Else 0 */
% r4 j; s9 z' ^4 Q6 happ_data->has_cut_stock = 0; /* Used 1 - Else 0 */
- S* l) Z/ V2 m* dapp_data->has_tolerances = 0 ; /* Used 1 - Else 0 */4 g; M' K, b0 [. j: F3 U! H9 B- R
app_data->has_feedrate = 0 ; /* Used 1 - Else 0 */
5 y& `* t# M" X( @$ \app_data->has_offset = 0; /* Used 1 - Else 0 */
$ f+ U8 l0 |" J3 ~. J4 j- Lapp_data->has_avoidance_type = 0 ; /* Used 1 - Else 0 */

/* Set values.设置参数值 */2 ?# d8 f  F& C# ^* \: D
app_data->stock = 0.033 ;- v  g2 D. b/ j1 v
app_data->cut_stock[0] = 0.2 ;
$ x- |9 H" R# Q: yapp_data->cut_stock[1] = 0.1 ;, o( J' v5 H4 r
app_data->cut_stock[2] = 0.5 ;
$ V4 b# O; q6 P' qapp_data->tolerances[0] = 0.003 ;# K9 O7 W1 D4 ?$ b8 v! t; u
app_data->tolerances[1] = 0.003 ;
3 H; e2 t$ Q$ f( @% U5 q/ [2 C, wapp_data->feedrate_unit = UF_CAM_feedrate_unit_per_minute ;9 a3 _6 e% Q# a
app_data->feedrate_value = 33.0 ;. ]5 W9 K9 ~7 [% O, e  t5 l
app_data->offset = 0.055 ;
8 R3 ?6 H4 d5 L9 ^% sapp_data->avoidance_type = UF_CAM_avoidance_type_warning;* M: c% V8 U2 L, ]
}4 Z7 X$ t3 @7 ^/ t+ V
}


7 b( y! C- {6 E. b4 `
发表于 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 | 显示全部楼层
8 d9 ]' Q( U' E2 T/ T
求高手讲解一下吧。。。。这个实现什么功能的?
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-16 18:05 , Processed in 0.055105 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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