PLM之家PLMHome-国产软件践行者

[二次开发源码] NX二次开发源码分享:动态画圆,指定屏幕点和向量相关用法

  [复制链接]

2016-9-17 10:11:08 6492 2

admin 发表于 2015-2-9 13:45:19 |阅读模式

admin 楼主

2015-2-9 13:45:19

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x

7 a& _, b: E  S/ E) X) rNX二次开发源码分享:动态画圆,指定屏幕点和向量相关用法
! e, }$ D( @8 x9 x5 M$ }
+ e( G+ n/ i% s3 Z

$ A" J, j+ ?. ?8 \, X8 I: a4 _! I这个案例很不错的,分享下,程序运行的结果是指定屏幕,拖拉即可动态绘制圆圈!: R5 K3 w- C, S( ~2 W, _% Z
3 S4 Y4 C0 f4 U" @  r/ K
#include <stdio.h>8 I$ d, H2 @6 ^1 G. P* X+ I
#include <uf_defs.h>, ~5 i# q  y9 f* P" ~
#include <uf.h>
3 u  w# @- I) Z% `#include <uf_ui.h>! T- v0 d( S& x
#include <uf_csys.h>
( w! e: b3 ~0 T4 I/ A#include <uf_vec.h>0 k& b8 K) h% Y* @: _0 o* i
#include <uf_disp.h>% F! i% a) t4 \. a
#include <uf_curve.h>
7 l- }1 [; T' y3 n#include <uf_mtx.h>
3 o5 q! W; a8 l* V#include <uf_obj.h>
' c) J- ^, |+ B; A; c. E- btypedef struct
$ ~5 ^* Q0 m6 ~& G5 ~# E        { UF_CURVE_arc_t  *arc;- N2 }# B9 _3 V, M) f: C, Y
          double          abs_ctr[3];. m" A4 |0 {+ }1 C" Z# n/ o& \) e, j
          double          matrix[9];
5 h: m! F! q% l& k7 E          double          x_axis[3];( c1 x3 g! i2 u7 o6 R
          double          y_axis[3];
, z6 B0 P3 u. i4 d& Z2 n: p        } my_motion_data_t;" `9 S, T8 ]9 K% R# O! Y5 i2 b
/* Define a motion callback to render a visual representation of7 K8 j" A+ q; w1 ^6 y
* the circle to be created, along with a bounding box around the
1 G9 M# P1 D6 C * circle, an arc of radius 1.0, and a "rubberband" line from the1 C* ^1 ]" X7 @9 b1 F
* center to the cursor position.% g4 z$ C0 W- `! g& [
*/
# I( e4 Q& B3 C; K" kstatic void motion_cb( double                   *screen_pos,5 m( I6 ?$ E% y
                       UF_UI_motion_cb_data_p_t  motion_cb_data,
' x# \% z& z- H7 c9 Y                       my_motion_data_t        *my_data ). M. n3 x( W5 ~
{
. t7 v8 g' f0 h2 Q$ s9 ~    double radius, pos_array[5][3];5 L  s9 s! }9 i0 w# U' V
    double xrad[3], yrad[3], x_y_vec[3], x_ny_vec[3];3 \  `7 A* p) C( u4 D5 Q
    /* Calculate the arc radius:  the distance from the arc center! w& O$ [% v. B' v; ?) X& c6 y* G
     * to the current screen position.
' ~! |8 ~7 D) g7 }% J     */
; |" W' ?1 `9 W+ W( r6 y& b    UF_VEC3_distance( my_data->abs_ctr, screen_pos, &radius );
, F" N7 \% {! ]) N& G    /* Map the arc center to the "csys of the arc".
  _6 J3 t/ G8 b, f' F     */: d$ m3 r5 B. `3 n1 L! G0 _
    UF_MTX3_vec_multiply( my_data->abs_ctr,
6 C) ]( J* ?% ~/ j( X                       my_data->matrix,
+ w8 j5 X  v  Y/ B* F                       my_data->arc->arc_center );
7 K, t- J2 }/ c* R6 S    /* Draw a circle and an arc in the view of the cursor.
" |7 Q; H. j& y: L: {9 n     */. m' x/ W9 X6 {) B2 y
    UF_DISP_display_ogp_circle( motion_cb_data->view_tag,
% G0 Q' O6 ^! b! r  b; K4 E                            my_data->matrix,+ i$ J6 t+ L# F- U
                            my_data->arc->arc_center," m$ Y* y( q9 h' y% O5 N: Y( u
                            radius );9 Y0 P) G: J; O
    UF_DISP_display_ogp_arc(    motion_cb_data->view_tag,
0 @, D* m2 k0 X/ C                             my_data->matrix,
8 ?* a! c8 v$ |4 L5 g0 `                             15.0*DEGRA, 345.0*DEGRA,. S" d8 E( O& E$ V* ^
                             my_data->arc->arc_center,
% B/ o6 N9 U! j7 j% D) H                             1.0 );; ]. Y7 f0 z/ V; |8 d
    /* Draw a bounding box around the circle.
# F. |3 q1 w' O/ h( V4 M     */
" C7 h# z& J. K4 j" ^# W    UF_VEC3_scale( radius, my_data->x_axis, xrad );
) l/ V9 s/ [% `  ^: |" h    UF_VEC3_scale( radius, my_data->y_axis, yrad );
2 D2 E3 D2 t1 V% y; T    UF_VEC3_add( xrad, yrad, x_y_vec  );. r/ L& _0 M4 n8 P/ ]: k4 A
    UF_VEC3_sub( xrad, yrad, x_ny_vec );
9 n7 T4 F+ T5 y9 R7 q4 D    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[0] );
0 U0 u2 Q& f& k) j; U4 o    UF_VEC3_sub( my_data->abs_ctr, x_ny_vec, pos_array[1] );
1 Y, o- Y- l' j7 D    UF_VEC3_sub( my_data->abs_ctr, x_y_vec,  pos_array[2] );8 _7 ^1 R' {+ e  `' B4 w# l% l% P8 ~" K
    UF_VEC3_add( my_data->abs_ctr, x_ny_vec, pos_array[3] );
7 o: ?# b" d5 x, x/ d    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[4] );
& Q/ C2 |3 ]; h% ^: Q' l/ T    UF_DISP_display_ogp_polyline( motion_cb_data->view_tag,, {7 i2 a" K  U4 m& d" I; @
                              pos_array, 5 );- H4 O' H" G" `) c2 z
    /* Draw a "rubberband" line from the circle center to the
9 p. p' F6 T5 F+ d, W     * cursor position.* x. u  j! T* k1 S/ M) v0 d8 L
     */
: W& K! \5 r9 I* t# ]    UF_DISP_display_ogp_line( motion_cb_data->view_tag,$ B) ~# i% g5 Q5 A7 g8 _% j) d
                          my_data->abs_ctr,* ~( V4 r1 K2 _
                          screen_pos );7 b; x) I( N7 g, F$ }$ e# Z
}4 q$ z/ E* \. w0 O
#define UF_CALL(X) (report( __FILE__, __LINE__, #X, (X)))1 Z2 X8 d! H/ P/ I! K) [
static int report( char *file, int line, char *call, int irc)" x4 p3 |. A! w
{
7 W$ b9 h% J) n5 R+ D0 X. M  if (irc)( H6 u( R" _& P% ^# [0 e
  {9 r7 a' s0 n% r4 v/ t
     char    messg[133];+ Q" b7 k; e% H- `3 G4 q* x
     printf("%s, line %d:  %s\n", file, line, call);1 V7 F7 f% r. |7 ]# |( O; O
     (UF_get_fail_message(irc, messg)) ?
+ q, x8 g8 }2 Z( d( ?8 Y: o& B% X       printf("    returned a %d\n", irc) :
8 x; U; ]- [- P7 L) x       printf("    returned error %d:  %s\n", irc, messg);6 c5 v2 v, O; a, c9 |$ t/ r
  }
  [8 H, t& d) I7 h  return(irc);% n- M( U' ~1 y1 C7 e
}
/ v. n; W2 e; K& q" n3 Q" ystatic void do_UGopen_api(void)
" j) {) f/ v. A& S; w{9 K  k$ P- c, m
    int               default_plane;$ W- ?+ o& P1 `
    int               plane_resp, ctr_resp, pos_resp;' H% I2 J& z& g1 ]  m7 K
    tag_t             plane_tag, saved_wcs, mtx_id, csys_id,! W  `( h' ^) F7 [8 Y- p
                      arc_id, view_tag;
  w) L$ l! ^1 h! ^; l0 `  Y    double            plane_matrix[9], plane_origin[3],
$ c% U7 P+ q! ^+ h! h8 T                      x_vec[3], y_vec[3], arc_edge_pos[3];
. J) V3 t; {! e3 O+ c( B    double           root_origin[3] = {0.0, 0.0, 0.0};
7 L6 {1 A5 T5 K    double           work_origin[3];
  [; ?. J4 F7 G: T: T7 c2 f5 y) {. }) u) P    UF_CURVE_arc_t    arc;
7 }& ]  q! T0 B' W    my_motion_data_t  my_data;
8 Z  |; V4 t- s, @/ ?
" A$ k0 c4 U' i; x3 Z: O    my_data.arc    = &arc;2 h) m- p1 I5 C; n/ T& e2 ]3 ^; q
    arc.start_angle =   0.0;
: w- C; C! K$ S1 u    arc.end_angle   = TWOPI;    /* Create a full circle. */* E" v, G$ c$ a; y/ b- L
    default_plane   = 5;       /* Default plane = WCS */7 `8 n) N& m4 |$ t5 f
    do7 L5 P. }, f) S
    {7 g5 T- F2 l" c: y% m. [
        /* Specify the plane on which the circle is to be created.
3 s; U3 i. s5 G! E8 e/ r& @# E$ m8 R/ j         */% p% ]5 k3 w1 A1 t" S
        UF_CALL(UF_UI_specify_plane(
0 @! i) W7 j) _2 g# w0 ~4 V' b% C                         "Specify plane for circle creation",
! o+ M5 u$ W- @1 i6 z* y  T+ z                         &default_plane,
) g/ m  k' F6 T' F1 D4 ~3 j: _                         1,) {, c2 e! r! x( P/ u  [1 E
                         &plane_resp,
* g. b! _: d% f- g4 Y$ Z# a                         plane_matrix,
( m+ f3 z) d5 ^! v2 v                         plane_origin,
' D- S; V6 C6 K  L8 d                         &plane_tag ));
& V2 l8 m0 V" k7 h, J8 P        if (plane_resp ==3)$ K" z& b; w' {5 c' I
        {: n$ {% U1 i1 f
            /* Save the current WCS for future restoration.; P* ^2 ?: g5 q8 s/ w) `
             */; Z! y; y" t; m9 y& }0 e3 W
            UF_CSYS_ask_wcs( &saved_wcs );
, q( _. B, W; K: ]* x! {& Z4 H            /* Move the WCS to the specified plane. This is
" o4 M* }. g# I9 g/ ~  T" R. l             * necessary because the position passed to the motion
3 d6 }  M2 k) k) q& P             * callback, and the position returned by
7 L6 W  O' W& s% R2 z8 u* @             * UF_UI_specify_screen_position, is the screen& y; \8 w4 u; U4 Y  U
             * position projected onto the WCS XY plane.
8 Z! j% B# h: O" c7 v             */
* V  [& f+ [% R2 }+ o$ t            if (default_plane != 5)
0 v6 k& V' F0 R% |, u9 [            {/ V& T2 G. {' k
                UF_CSYS_create_matrix( plane_matrix, &mtx_id );
$ ~; W' V$ o" d* W                UF_CSYS_create_csys(  plane_origin, mtx_id,% i1 B; m- j7 Y% A9 Y, @$ z% O
                                   &csys_id );
/ u  w: T0 N7 h2 C                UF_CSYS_set_wcs( csys_id );
* D2 ?" H) o! J% e7 _            }
6 V# x7 S9 F9 a' P) f2 [: V1 d            /* Obtain unit vectors and the arc matrix relative to0 X1 L# t5 C0 J4 P
             * the Work Part coordinate system.& J8 l4 [9 N# m( o) ~" W1 b( I
             */
( Y; E3 ?( G1 L+ U: W7 G            UF_MTX3_x_vec( plane_matrix, x_vec );" c9 I, ~/ |1 K6 O
            UF_MTX3_y_vec( plane_matrix, y_vec );
( O$ z) ?8 Q" v0 D& m% \            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, x_vec,
( k4 b. {9 F* [                            UF_CSYS_WORK_COORDS, x_vec );7 [5 {2 n+ w, p* _( u
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, y_vec,
5 O" H" c9 h" I4 K* c; R                            UF_CSYS_WORK_COORDS, y_vec );7 p$ B. P. n/ F
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, root_origin," f, g/ L* w7 R' C& t( ?
                            UF_CSYS_WORK_COORDS, work_origin );
; h$ L$ c7 ]( `7 N            UF_VEC3_sub( x_vec, work_origin, my_data.x_axis );
, U) f) ^1 f0 k3 L3 W# j            UF_VEC3_sub( y_vec, work_origin, my_data.y_axis );! J" I7 E/ q$ d
            UF_MTX3_initialize( my_data.x_axis, my_data.y_axis," Z) y4 f- [2 O) E( E2 K- N$ \& Q
                             my_data.matrix );
& q7 ^3 A  Q1 Y- U' k            UF_CSYS_create_matrix( my_data.matrix,2 Q7 P0 l- Z0 ~) K" z4 N0 f0 e
                                &arc.matrix_tag );
- T$ a+ P( {  ]9 `) ]            do
8 [  Z6 n% V2 l. K: |, ]* f4 s: V            {
) t2 o; G% _: e6 Q/ K* M1 q# A$ m                UF_CALL(UF_UI_specify_screen_position(* O; t* g0 _6 u4 d- N3 K6 p' H* b
                                "Specify arc center",4 d! o; m7 m* ~6 p) Y$ w
                                NULL,6 Q0 v2 j) L! B* X
                                NULL,
6 i: T2 z, v" x) w1 j                                my_data.abs_ctr,
8 _$ x" K$ @1 }9 x  {" v& y                                &view_tag,
" n/ ^/ @) w/ T$ u                                &ctr_resp ));# e4 T. f, v# y: b- I
                if (ctr_resp == UF_UI_PICK_RESPONSE)
7 m6 m1 D* x, t- a0 c* G. }$ Q  ~                {+ c, t5 e0 B7 p* i% \
                    /* Map the arc center to the arc csys./ \/ @, ~* b5 P% L' r, o
                     */
% ^/ b1 e1 K1 W8 b" a+ z. y                    UF_MTX3_vec_multiply( my_data.abs_ctr,. t% w" ?, @" m+ }2 s6 ]2 s
                                          my_data.matrix,; I6 f) B. R( _; m% D
                                          arc.arc_center );
% q& f7 P7 E% C8 k                    UF_CALL(UF_UI_specify_screen_position(
6 M' G" E$ `2 E$ X% e                                    "Indicate arc radius",' m5 G9 `, q' T/ K& `2 |1 {
                                    (UF_UI_motion_fn_t)motion_cb,
5 x4 o  q" Q8 X5 E1 w9 _3 r2 y7 b                                    (void *)&my_data,
5 ~7 Q3 z! z# ]% }( v  `                                    arc_edge_pos,( |8 }9 I4 b9 v# w6 c5 M( l
                                    &view_tag,+ |" d# {; _! |( [2 _
                                    &pos_resp ));
' \2 w* e5 Y# i0 d: L, k/ P                    /* If a position was obtained, create the9 W& T+ n4 u# a% j0 ^
                     * circle.! ]) p3 J: u, P: B$ j, u3 ^6 l" P
                     */
! |2 k7 S% x# d( R; U                    if (pos_resp == UF_UI_PICK_RESPONSE)- T; G1 x4 o2 M$ p( t
                    {: ]& z- V! w2 r6 m" z2 g
                        UF_VEC3_distance( my_data.abs_ctr,9 ~: [# n. G& T% ]6 C. o/ a
                                        arc_edge_pos,3 E* u3 B0 R# V# Y( H  b2 p+ C
                                        &arc.radius );
$ W! T" v6 E1 K: d1 T3 w# T; G                        UF_CURVE_create_arc( &arc, &arc_id );
# ~% O$ P- A! K9 c5 E5 Y- @0 \/ T$ q4 F9 i                    }
- Q$ g2 f2 s% w6 ^3 a( l" u8 ?/ N                }
1 W7 Z  M" i$ \# V9 N+ j7 q            } while ( (ctr_resp == UF_UI_PICK_RESPONSE) &&
! [# _: ]4 z5 E# k7 s" E; \8 k                     (pos_resp != UF_UI_CANCEL)           );1 q& i1 J' x; Q! }/ j$ @
            /* Restore the WCS if it was altered above.
; }2 z) H0 j6 N, ]( F             */5 t# }; b* F& @; p- w& t
            if (default_plane != 5)  m3 R$ s, m3 B- \; I
            {
; x: V, w& D* M/ ~/ }% N$ V                UF_CSYS_set_wcs( saved_wcs );
& p  E, C% ~' I: @% D( b                UF_OBJ_delete_object( csys_id );
" d- J9 z# h9 j            }
& y9 ?  n/ @; z; h3 _4 b+ w        }
4 Q, S2 }0 c3 i8 \) H% c    } while (ctr_resp == UF_UI_BACK && plane_resp == 3);
& R: L; i+ `- B$ h4 J}" z* k7 D) L* ^0 i4 d5 O' B) b6 x
/*ARGSUSED*/
: W2 {: i5 q  ^2 L  w$ Qvoid ufusr(char *param, int *reTCode, int param_len)6 {0 q& P  X$ K# V
{$ M+ M/ Q2 n7 H& b
  if (!UF_CALL(UF_initialize()))# ~% B( Y% X; Y# _. t
  {6 _; w- w+ H( S! F& Q) h
    do_ugopen_api();
4 n; c, P8 i6 J6 z* F# q5 o, c    UF_CALL(UF_terminate());% C- a. Z, z" ?' i
  }* g/ Q+ B& N, @+ ~' M$ j
}
6 g3 d4 r! v1 d. x! V3 p( i% eint ufusr_ask_unload(void)
  q. _$ I. f4 h8 [- B+ s{5 p+ N1 h1 B2 D' r* |0 z
  return (UF_UNLOAD_IMMEDIATELY);
1 Y8 X1 w/ w% R1 Z}0 G! s1 s( [( ~, s" P" E' i' |# s5 ^
! o8 _8 a  _9 g+ `
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

全部回复2

yuleihz 发表于 2016-1-8 22:21:57

yuleihz 沙发

2016-1-8 22:21:57

老师来个NXOPEN的耍耍
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ doTeam.tech
回复 支持 反对

使用道具 举报

wu150060 发表于 2016-9-17 10:11:08

wu150060 板凳

2016-9-17 10:11:08

学习一下,顶( }
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ doTeam.tech
回复 支持 反对

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了