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

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

  [复制链接]

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

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

admin 楼主

2015-2-9 13:45:19

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

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

x
. B) y& v( F" f( @$ w
NX二次开发源码分享:动态画圆,指定屏幕点和向量相关用法
$ n/ k$ C" J7 }: p
' E/ d+ @& U: f+ C# h
0 N- c1 u% L, a+ g- N% h% o
这个案例很不错的,分享下,程序运行的结果是指定屏幕,拖拉即可动态绘制圆圈!6 n0 C6 `: \+ }; S' M0 C) a
- k# g- Z4 ~0 @* s  ]
#include <stdio.h>
5 k  J7 a8 ]( }#include <uf_defs.h>0 |4 T7 A3 I* u$ N# r9 T
#include <uf.h>
. \' e. c, O) h7 G6 q% u( @#include <uf_ui.h>
  c+ `/ C7 S, Z4 Z' u#include <uf_csys.h>: p- z0 a8 J! o: x
#include <uf_vec.h>
' h6 C$ |! V: i% b" r+ ^' Y4 e#include <uf_disp.h>
$ P/ V1 X( {0 S( z( }8 A" A#include <uf_curve.h>6 H- I" S7 ^! d2 @$ J7 t4 ~, G- Q
#include <uf_mtx.h>
0 q! q7 P% \3 I( N#include <uf_obj.h>6 z: E+ ^; Z4 K8 S6 r3 M6 n
typedef struct
/ r1 n" C: {7 H( Y+ c2 S( t        { UF_CURVE_arc_t  *arc;$ @9 H3 P1 d) {9 d% I
          double          abs_ctr[3];
( G. L: q& |+ f          double          matrix[9];) a9 J3 d2 e- x$ m( ~
          double          x_axis[3];
7 ]  P% W5 n' q, }: ]! b4 a          double          y_axis[3];
$ v& r" [/ q, k/ r& b: f* X        } my_motion_data_t;  u7 k# g  s. s
/* Define a motion callback to render a visual representation of9 ~1 I' T( u! ~) \+ l& l" }
* the circle to be created, along with a bounding box around the
9 r+ T3 V6 w) q( L. W1 _  ?3 x * circle, an arc of radius 1.0, and a "rubberband" line from the( [( L) c3 f$ I$ j, D
* center to the cursor position.0 s; a5 B* T8 V! [. u# {. p! z. F
*/2 V$ p! n3 _+ K) b' @) T6 |8 M
static void motion_cb( double                   *screen_pos,8 X: V, T7 e- q
                       UF_UI_motion_cb_data_p_t  motion_cb_data,
( q& R- C* l4 {. g# E; L# L: B0 g                       my_motion_data_t        *my_data )
( k0 G$ l" ~( _) }8 ~0 Y! I{
8 |% P7 N6 [% u7 D1 O! p8 ^6 F+ c% m    double radius, pos_array[5][3];1 T5 D" h/ ?; A0 |0 p! N
    double xrad[3], yrad[3], x_y_vec[3], x_ny_vec[3];
" F8 N1 R, g  b, ?    /* Calculate the arc radius:  the distance from the arc center% O! d, ^' W$ N- w% l
     * to the current screen position.
5 W  K) L, d8 p$ v  r' S$ |/ O& |     */. j% @6 b$ v: `7 L) W2 ?: I
    UF_VEC3_distance( my_data->abs_ctr, screen_pos, &radius );
! Z, Z2 Y  h/ g7 }    /* Map the arc center to the "csys of the arc".) `: a/ G, i: p' x0 G
     */+ P$ N; x1 W4 o) h+ g
    UF_MTX3_vec_multiply( my_data->abs_ctr,
# [  a+ a2 h; O( L1 p                       my_data->matrix,
4 w7 \$ q8 K. o4 X% A                       my_data->arc->arc_center );
  Q2 D- i& f- X2 l    /* Draw a circle and an arc in the view of the cursor.
7 h2 A% B* h  b# {- H     */) V1 v$ `: X2 j# S5 w  ^" [! P
    UF_DISP_display_ogp_circle( motion_cb_data->view_tag,5 a) z7 F0 v  Q  Y+ j
                            my_data->matrix,
/ |7 `/ M& I4 [1 W# T  A                            my_data->arc->arc_center,% M5 }6 I2 p' m4 h
                            radius );' A3 l2 S8 s8 H' d# x& m! G3 Z
    UF_DISP_display_ogp_arc(    motion_cb_data->view_tag,  [- x4 _, c' H9 {( K( o. T6 y
                             my_data->matrix,
$ d! }9 ]5 J% i( _. {( E$ @0 D. t                             15.0*DEGRA, 345.0*DEGRA,
; ~, t/ P2 U$ R( l  \9 s0 V/ H0 S                             my_data->arc->arc_center,: {5 G2 V( w1 q2 v) a  [, B8 s0 q
                             1.0 );, E! I" W+ b/ p7 G) E0 r& x! c4 {
    /* Draw a bounding box around the circle.' t/ ?0 l6 b' I% d; A1 J% U
     */+ x; i5 a  W  _1 A) n$ |$ _7 Z
    UF_VEC3_scale( radius, my_data->x_axis, xrad );
* G: F9 R/ j# V1 B$ x% |: \: D( ^    UF_VEC3_scale( radius, my_data->y_axis, yrad );
. Y9 c6 r/ x" H) `' C    UF_VEC3_add( xrad, yrad, x_y_vec  );
; J  J: H. e/ X8 V    UF_VEC3_sub( xrad, yrad, x_ny_vec );' `8 _7 ^( R4 @* S8 _* h& \
    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[0] );
. V; w- Y% @/ U! V4 K' V    UF_VEC3_sub( my_data->abs_ctr, x_ny_vec, pos_array[1] );; {# h1 C% P! l0 Y  |" w6 O
    UF_VEC3_sub( my_data->abs_ctr, x_y_vec,  pos_array[2] );
2 i6 f; _4 c0 K' c    UF_VEC3_add( my_data->abs_ctr, x_ny_vec, pos_array[3] );
2 q" C3 q! B( B- M    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[4] );
3 a; G6 q0 v) }; X3 v    UF_DISP_display_ogp_polyline( motion_cb_data->view_tag,) ]! k# U- e: D) M" z! [
                              pos_array, 5 );
% r, k& V1 }" x4 M4 H& N( Y    /* Draw a "rubberband" line from the circle center to the
! T, {2 S0 L# ?, I2 H2 u( v     * cursor position.
6 y, s1 I- E9 P- k2 z* t     */% _( N* U: b; S5 s! p/ d
    UF_DISP_display_ogp_line( motion_cb_data->view_tag,$ m+ V' @  _. m2 e# L
                          my_data->abs_ctr,7 n* ?3 h% ^1 u2 w
                          screen_pos );
# |/ D  Y  P1 [5 f6 O}- @8 p1 \. d* G; ]8 y+ y7 k
#define UF_CALL(X) (report( __FILE__, __LINE__, #X, (X)))5 y: c$ \, a: Y* k, ^
static int report( char *file, int line, char *call, int irc)) J5 p) Q% z  f2 i( r/ h
{  H2 _" U6 B, R
  if (irc)# v* W" Y6 e) ?/ r7 `, D
  {9 g6 s' p) J& l* P' H
     char    messg[133];7 |6 k9 h, Z2 p6 [9 U/ i# i$ \
     printf("%s, line %d:  %s\n", file, line, call);
, |5 k, ?9 \! G     (UF_get_fail_message(irc, messg)) ?
; ~! l& R' N8 V. x0 {0 G       printf("    returned a %d\n", irc) :( M" R0 J8 w0 W; J
       printf("    returned error %d:  %s\n", irc, messg);
% U: Q9 [- E% ?6 H3 i, X  }" O6 r( h7 h# w. j1 t: I& K/ Y* t4 |5 S
  return(irc);4 e5 D# p$ ?/ }8 k
}" L/ R) G' U% b; y; E- G
static void do_UGopen_api(void)
6 v6 i) j0 \/ V8 [{  _' Q9 c; a/ a
    int               default_plane;
8 D& Y) J0 i( h6 g    int               plane_resp, ctr_resp, pos_resp;
' U# l5 D- ~+ y2 l; x% s    tag_t             plane_tag, saved_wcs, mtx_id, csys_id,) u8 W' i2 H* j# M" ]$ l
                      arc_id, view_tag;
9 t7 B+ q6 m+ ~6 p7 b1 {! K    double            plane_matrix[9], plane_origin[3]," o" b7 @5 Z7 k' b  M  Q" I
                      x_vec[3], y_vec[3], arc_edge_pos[3];
" H; v9 D, ~2 V( q* \6 ?    double           root_origin[3] = {0.0, 0.0, 0.0};1 b- J' ?$ }* r0 L6 w& e% J
    double           work_origin[3];
3 z$ I+ H4 }9 k5 q/ h    UF_CURVE_arc_t    arc;
  ]# K9 w" y5 R* `    my_motion_data_t  my_data;; T& |/ E& E& \9 G
% ]9 S/ r5 C) h. Y' S
    my_data.arc    = &arc;
" V/ d9 s# l, H+ z0 R    arc.start_angle =   0.0;
- E1 E* @# S8 a9 H& s    arc.end_angle   = TWOPI;    /* Create a full circle. */
1 m9 ]* u+ W; r7 z/ w    default_plane   = 5;       /* Default plane = WCS */# E! }9 P* m! T/ i* l
    do8 S6 q  ?2 e2 S' N) ]+ _
    {2 P- A: E( M, i+ o
        /* Specify the plane on which the circle is to be created.0 U$ K5 H- i" R8 w) t7 G/ {
         */; ]9 I; X' t, k: |9 T8 \
        UF_CALL(UF_UI_specify_plane(& P  ^) P; v: @* p/ J7 x
                         "Specify plane for circle creation",2 S( E- e9 s( }
                         &default_plane,
5 P! k% F3 |; c- O" i: b# \                         1,, P- @. D  J; ?: Z( |
                         &plane_resp,. N  k+ p2 z$ h6 J& q
                         plane_matrix,
( ~* |, J; F2 G0 Z1 D( b                         plane_origin,- K0 V8 r7 z2 S1 d) o
                         &plane_tag ));
3 }7 V0 c, F3 e8 O        if (plane_resp ==3)
, @/ r* X. O+ U! c        {( r" _# E# i/ H1 t0 m: N$ p
            /* Save the current WCS for future restoration.0 d. f0 y* k. @
             */
2 ]9 _2 ^/ d( S& u            UF_CSYS_ask_wcs( &saved_wcs );
/ v5 U* q. s0 f* g# `/ h            /* Move the WCS to the specified plane. This is
2 S% g! R& a' `, H3 H             * necessary because the position passed to the motion
& |  A9 h6 N& R3 e2 o. S) r& R             * callback, and the position returned by
* F) N8 b: ]) U5 h5 R             * UF_UI_specify_screen_position, is the screen- q8 e" b* B* G7 F& u' U
             * position projected onto the WCS XY plane.
* v2 _2 {  T5 K* K: G, j6 O             */
0 L1 N6 _: C* j  X& E9 r/ n6 ^1 ~8 j            if (default_plane != 5)
+ a' p% Q6 N9 A            {7 ~+ n2 @! m1 ~+ I
                UF_CSYS_create_matrix( plane_matrix, &mtx_id );
2 ?' f& q) D2 f4 E                UF_CSYS_create_csys(  plane_origin, mtx_id,
* [0 F# D& H& g9 N                                   &csys_id );
. e% F3 U) q8 H+ e                UF_CSYS_set_wcs( csys_id );
& @1 g( n9 Q# u' ~            }9 `" ?5 E7 P  p/ {( S) X
            /* Obtain unit vectors and the arc matrix relative to! [  }3 q, r+ L4 d2 u2 T" T- M
             * the Work Part coordinate system.
4 g: H# F, X; W# ]; _0 P             */
+ z0 i  I: p0 k. H7 e7 d            UF_MTX3_x_vec( plane_matrix, x_vec );
! E# t* t0 j5 `$ Z            UF_MTX3_y_vec( plane_matrix, y_vec );
3 _) }- B" F7 H# J) ~/ V3 k4 Y( o0 |            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, x_vec,
1 T( ]) I& _8 @3 b                            UF_CSYS_WORK_COORDS, x_vec );' K; @. q2 \' Z* B
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, y_vec,# B8 v! K1 y; `) B
                            UF_CSYS_WORK_COORDS, y_vec );" V; g. H5 K" m6 I
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, root_origin,
0 o) ?, m( d5 H8 Z) s3 u1 t. F3 G: Q                            UF_CSYS_WORK_COORDS, work_origin );# Y1 r& G. t1 k& V5 j
            UF_VEC3_sub( x_vec, work_origin, my_data.x_axis );8 l" F; l2 J4 R3 X6 Q" b8 o
            UF_VEC3_sub( y_vec, work_origin, my_data.y_axis );
# x) ^  `1 P( i) o; g/ X            UF_MTX3_initialize( my_data.x_axis, my_data.y_axis,
3 o# H& j5 s/ y                             my_data.matrix );$ ?3 x; z- v$ q; I! A  \' [: ]8 q3 Q
            UF_CSYS_create_matrix( my_data.matrix,4 U. }$ L8 Y' b: I
                                &arc.matrix_tag );# [3 _; z( v, N5 j+ f0 T0 V
            do8 [% P& T7 J) {6 m* l  l
            {. c8 |& r1 U( [  V2 O. r$ X: E6 s
                UF_CALL(UF_UI_specify_screen_position(
) i; O7 Q$ |$ I. ]/ W% {* b                                "Specify arc center",. X8 R# |, X, F  N* M0 i( W( h
                                NULL,
* P2 {3 L6 g4 z                                NULL,
% ~; V; i5 K# N+ D7 c: t                                my_data.abs_ctr,' {1 r% \% _- o* K
                                &view_tag,
2 J7 M0 b# |) Y0 z" g                                &ctr_resp ));! ]( f- v) V. f' |# \# Z* S1 s
                if (ctr_resp == UF_UI_PICK_RESPONSE)
$ N$ W& U5 ]2 n0 o                {
8 |: q& y( [% A! q! M' {                    /* Map the arc center to the arc csys.
% [" O7 [6 F6 Z% B0 K, D                     */
% q9 ^) v! N) N+ {: ~* R                    UF_MTX3_vec_multiply( my_data.abs_ctr,. K2 K8 h& R  |- s& O
                                          my_data.matrix,2 z; T+ z0 X( P0 k
                                          arc.arc_center );
/ I; D8 L1 A/ ?  G* G                    UF_CALL(UF_UI_specify_screen_position(7 K8 ]; Q( v# ?
                                    "Indicate arc radius",4 C$ }5 }8 ?1 P8 q
                                    (UF_UI_motion_fn_t)motion_cb,: i) w* k3 L7 k8 ^6 m5 q
                                    (void *)&my_data,4 `) t. t! T3 L, F
                                    arc_edge_pos,8 I+ ~% V- |* n. x# d2 ~
                                    &view_tag,; h! K+ c3 H9 ^2 o
                                    &pos_resp ));  n9 c3 l( ^, N* x' k. d
                    /* If a position was obtained, create the
8 y0 z* v  a/ W( k                     * circle.' s( p6 y# B6 ^& [6 O
                     */
- e& f: A7 Y) `& n% k3 j                    if (pos_resp == UF_UI_PICK_RESPONSE)
) n- `& Y0 D* P3 B# W3 C                    {6 j' \5 }# v/ N/ ]4 ]
                        UF_VEC3_distance( my_data.abs_ctr,+ }7 v  x# C- f5 ], [9 w
                                        arc_edge_pos,1 u! P& f- ]2 S; ^4 c2 I4 f3 o
                                        &arc.radius );
9 K2 t* v2 M. l* x; s# F                        UF_CURVE_create_arc( &arc, &arc_id );# d. G! x  [( t/ U' Q2 W- O+ J
                    }
) A$ p1 S* p, _4 w: J. H' d                }* q( ]/ h" h7 I# M& ~& |+ R
            } while ( (ctr_resp == UF_UI_PICK_RESPONSE) &&+ q, Z0 [# E* P# S' d, y8 Z
                     (pos_resp != UF_UI_CANCEL)           );
/ @* i( Z! v- r9 c8 {8 u            /* Restore the WCS if it was altered above.
9 Y- `- R# ^( B* ]" `9 k             */
6 j0 }3 C' v# s% v$ S            if (default_plane != 5)
  V, U7 y$ ~! x! z5 Z            {- R+ F7 n* K, W
                UF_CSYS_set_wcs( saved_wcs );
+ y! A* u/ h& [+ ]                UF_OBJ_delete_object( csys_id );( j  a- V* `5 |4 z
            }
) ?  @5 L6 y" U& ^        }
" G& S8 I# b' y8 f- p; Z  D6 x/ N    } while (ctr_resp == UF_UI_BACK && plane_resp == 3);
, _$ [: ]( p! c) h& u  k}, L8 e, e/ L* T" j4 H) I
/*ARGSUSED*/
$ p4 _. Q. Q  V& G, a1 _: Wvoid ufusr(char *param, int *reTCode, int param_len)
+ p$ l5 p$ U0 R6 h{5 L- t" m8 T/ m, u
  if (!UF_CALL(UF_initialize()))
8 ?5 ~( Z9 M4 @3 m& _  {  S+ X$ s8 a- n6 Y4 p
    do_ugopen_api();
8 Y8 c! q6 b  R" X3 a  P    UF_CALL(UF_terminate());
: t5 n$ L7 z  b& b  F- Z  }
1 `6 g! \; b1 K+ l: i% W: P}1 W; V5 {" p% s/ d" L2 n/ K* r5 u
int ufusr_ask_unload(void)' w) f! D% U* N- Q: c) N
{3 a, r0 L1 [2 |  Y# R) Q& E
  return (UF_UNLOAD_IMMEDIATELY);- i/ s! S' p0 E6 }8 d0 P
}- i2 k; w( T2 n( _# x
+ Z8 a1 v5 L" H% ]6 v- f! `: |
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了