PLM之家精品课程培训,联系电话:18301858168 QQ: 939801026

  • NX二次开培训

    NX二次开培训

    适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术对于老鸟也值得借鉴!.

    NX CAM二次开发培训报名 NX二次开发基础培训报名
  • PLM之家Catia CAA二次开发培训

    Catia二次开发培训

    Catia二次开发的市场大,这方面开发人才少,难度大。所以只要你掌握了开发,那么潜力巨大,随着时间的积累,你必将有所用武之地!

  • PLM之Teamcenter最佳学习方案

    Teamcenter培训

    用户应用基础培训,管理员基础培训,管理员高级培训,二次开发培训应有尽有,只要你感兴趣肯学习,专业多年经验大师级打造!

  • PLM之Tecnomatix制造领域培训

    Tecnomatix培训

    想了解制造领域数字化吗?想了解工厂,生产线设计吗?数字化双胞胎,工业4.0吗?我们的课程虚位以待!

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

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

  [复制链接]

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

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

admin 楼主

2015-2-9 13:45:19

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

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

x
5 t/ x6 B- E: a& v
NX二次开发源码分享:动态画圆,指定屏幕点和向量相关用法
& B) D( E1 ]# [
% \+ K) E7 {: x- c8 C, C* E% G
: l7 r3 g+ D! D/ o; |8 @& `
这个案例很不错的,分享下,程序运行的结果是指定屏幕,拖拉即可动态绘制圆圈!
% ~* T" \# V% e$ I3 c
. n9 s/ F' ]  ?5 `- v9 G#include <stdio.h>
$ O2 u: b; L7 u2 z" F0 {8 l#include <uf_defs.h>
6 t) U$ l2 {1 [! a8 W#include <uf.h>8 ?; R$ Y1 o, p9 A! c% ]  j
#include <uf_ui.h>& J( W  R+ j/ X: V8 t" ^/ [% j5 q
#include <uf_csys.h>8 `) L/ v! E3 p& H' K
#include <uf_vec.h>
6 O5 K) C4 j+ m' y3 ~#include <uf_disp.h>2 R5 L* X: H& J1 R3 V/ ^
#include <uf_curve.h>
. x% H; E! Z% J( Z#include <uf_mtx.h>" U5 q! j6 y! R5 c$ ^9 e% L
#include <uf_obj.h>
6 {& {- o* ?8 Z0 P+ Ltypedef struct9 m# A/ L+ Z8 p7 {9 E
        { UF_CURVE_arc_t  *arc;
0 D+ t; q% ]# F; k1 ^          double          abs_ctr[3];2 `2 z$ _, N1 D6 }" H! h
          double          matrix[9];
+ m; i: z( r' k2 _6 J( O          double          x_axis[3];
9 B% ~& y; O; C          double          y_axis[3];
1 T, \, K' n. e' Q, {  X' T        } my_motion_data_t;
& [" Q" Y3 [8 \% T7 P/* Define a motion callback to render a visual representation of
$ A6 V# s4 V" y$ I* K * the circle to be created, along with a bounding box around the
2 I' G, w9 F0 i0 g" [# G * circle, an arc of radius 1.0, and a "rubberband" line from the+ h* u. R7 l" b; [+ J5 K6 f# l- a" D+ Q
* center to the cursor position.0 N8 s* [' x# Y
*/
6 g% s1 n1 P2 F8 |* j1 f- Rstatic void motion_cb( double                   *screen_pos,
9 M& [. U) h) `  L                       UF_UI_motion_cb_data_p_t  motion_cb_data,6 S( O: e! R8 R
                       my_motion_data_t        *my_data ). d8 n. \, s/ {! f: }
{, [  e; F( x* w2 D% ^5 N6 H7 f
    double radius, pos_array[5][3];
9 v4 I, N- G4 f7 T" a    double xrad[3], yrad[3], x_y_vec[3], x_ny_vec[3];5 M/ ]5 }/ u6 O
    /* Calculate the arc radius:  the distance from the arc center6 v- P0 E+ _! C3 h
     * to the current screen position.
( Q" `  l0 X9 n     */9 H# u" s% V- n: G# v. O
    UF_VEC3_distance( my_data->abs_ctr, screen_pos, &radius );
$ c3 @* N. V, _+ h; Z, ~  `% Y' s    /* Map the arc center to the "csys of the arc".5 Y) j5 q1 Z. F' {" y6 J
     */
; D% J; z2 I! Q$ G( N( y    UF_MTX3_vec_multiply( my_data->abs_ctr,* j3 \3 q7 w; w3 j( s0 J1 z8 B
                       my_data->matrix,5 E3 z2 T0 K; x5 `) J+ R$ U+ W
                       my_data->arc->arc_center );
& |2 e% J2 P# C4 I1 b6 y8 k0 v    /* Draw a circle and an arc in the view of the cursor.
# ]1 x) @+ h4 A5 p     */% a+ E( C7 F6 \6 u5 [9 o) l
    UF_DISP_display_ogp_circle( motion_cb_data->view_tag,
) o* A, A( |$ `                            my_data->matrix,
6 `6 k) G" N' p3 _+ [  ~+ e2 D3 l) Q: I! n                            my_data->arc->arc_center,
$ S9 T0 T9 b  ]                            radius );
  S& e! Y" |* ?2 p; E7 X    UF_DISP_display_ogp_arc(    motion_cb_data->view_tag,3 K' b; `% o* U% x  Q! z
                             my_data->matrix,, p+ U1 X2 m" E5 m$ _
                             15.0*DEGRA, 345.0*DEGRA,
' O/ Q; D1 i+ Q. c3 e# a9 a                             my_data->arc->arc_center,8 V# F0 z: R6 {0 W+ m$ G, |. _
                             1.0 );" ~0 N5 A. `. j# V
    /* Draw a bounding box around the circle.: _- I2 Z! b( w- n. s
     */
7 u- |5 K( E3 f+ [    UF_VEC3_scale( radius, my_data->x_axis, xrad );# x- E9 o( F# T
    UF_VEC3_scale( radius, my_data->y_axis, yrad );
- l1 R4 ]" L/ q0 o" ~; j  _( @    UF_VEC3_add( xrad, yrad, x_y_vec  );; Y7 q' x* Z$ d) Z4 \) R0 H  ]: v7 g
    UF_VEC3_sub( xrad, yrad, x_ny_vec );
1 o) t$ X: o: t6 }2 _    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[0] );% i5 n/ g9 G$ o
    UF_VEC3_sub( my_data->abs_ctr, x_ny_vec, pos_array[1] );
3 u4 K# g& u$ N  }1 \1 K) \    UF_VEC3_sub( my_data->abs_ctr, x_y_vec,  pos_array[2] );
8 V" _* G% Q5 J4 j, A; b$ M    UF_VEC3_add( my_data->abs_ctr, x_ny_vec, pos_array[3] );2 J( r. v- ^, y, V
    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[4] );, t/ s, N- z8 o! a1 [7 n& J
    UF_DISP_display_ogp_polyline( motion_cb_data->view_tag,
1 u: @! o* [7 l# k                              pos_array, 5 );8 F6 Y* Z8 J$ E$ r, o4 X+ H" {
    /* Draw a "rubberband" line from the circle center to the
- h' W- @5 [# `! `     * cursor position.
0 \" V8 N$ e1 R" W     */
  G7 F3 Q  S, r2 A/ U- r& U7 B8 a4 _    UF_DISP_display_ogp_line( motion_cb_data->view_tag,, X5 B: r! G7 t& U
                          my_data->abs_ctr,5 ~1 y* l" o* \* c' Y
                          screen_pos );
; J, T& v7 l7 `! l6 @  M}+ m5 [$ P# P8 H. _( {4 j( X
#define UF_CALL(X) (report( __FILE__, __LINE__, #X, (X)))1 C* f+ t% K. Y! }
static int report( char *file, int line, char *call, int irc)
5 ^! x, \4 [$ f7 q5 E: s  U{
2 l4 ^" C6 m1 Q0 f/ e$ ]$ C) I6 `  if (irc)
+ f% g! g/ H! C1 x  P  {
/ w. O# T7 o- r: u& k     char    messg[133];  g9 C' `$ A$ k0 i, W6 R; k# s- @
     printf("%s, line %d:  %s\n", file, line, call);+ t3 v' x9 W7 a. @' N/ v
     (UF_get_fail_message(irc, messg)) ?
, v! y9 ~7 }3 e# k$ Z7 h) T! h       printf("    returned a %d\n", irc) :
8 a6 l4 J4 _4 o8 |* _* U       printf("    returned error %d:  %s\n", irc, messg);
5 l; v! {1 ?: U$ _0 W$ A& r. `' w  }
$ R- V" v2 S( F. Q% K0 L  return(irc);
# j0 |5 V) i+ c" _8 F}" \" G7 ]+ L5 v) ?
static void do_UGopen_api(void)9 H, f, A3 p  z0 w/ B* ~
{
, z1 V; X, t8 U. y    int               default_plane;' z: E1 X$ L7 q4 c7 }
    int               plane_resp, ctr_resp, pos_resp;* d! S& N7 A2 K0 z( V, R
    tag_t             plane_tag, saved_wcs, mtx_id, csys_id,
+ ?1 g% \# C0 M; i5 Z! {                      arc_id, view_tag;: s: x0 ~& G2 E- ~$ a' }
    double            plane_matrix[9], plane_origin[3],
7 p$ z# [8 b5 X+ o1 F# K- I                      x_vec[3], y_vec[3], arc_edge_pos[3];
: a! [. H7 q" L( H# I/ j    double           root_origin[3] = {0.0, 0.0, 0.0};
3 E1 \0 K; f$ P" v    double           work_origin[3];
" {  G9 X7 U' l# M6 |/ S    UF_CURVE_arc_t    arc;8 ^! S5 f6 [# d5 J# y% C) y# F
    my_motion_data_t  my_data;
8 ]( m" c4 w( N3 V" c* O& D) x' e  X2 M+ v
    my_data.arc    = &arc;
9 @3 W( i7 G6 S9 [. s    arc.start_angle =   0.0;* k# p  F  ~7 d
    arc.end_angle   = TWOPI;    /* Create a full circle. */1 I7 _* A# `1 U0 ~) O2 q4 V
    default_plane   = 5;       /* Default plane = WCS */
: }. w+ o& e3 F0 ~+ \% B$ f    do* Q! I( h9 n/ }, |, f4 v; \
    {
! c2 N) M7 k; |' {( Y        /* Specify the plane on which the circle is to be created.
' X5 Q+ c$ A' o$ i. d         */
6 T7 M+ f! a# J" C        UF_CALL(UF_UI_specify_plane(
* p4 _. I8 v3 u                         "Specify plane for circle creation",: [, m7 ~5 }9 P& U7 U* D7 p# k
                         &default_plane,
; f6 \5 p# a$ H8 i9 i+ x3 w% D                         1,4 B* T4 j; [2 }$ ^
                         &plane_resp,, D0 ~- P% @) M# R
                         plane_matrix,
! y+ B* t' Q2 R8 i2 c5 a  K                         plane_origin,! v, [# L  @- n- N9 N
                         &plane_tag ));
& u5 h/ R5 O" P0 x, u7 b6 C        if (plane_resp ==3), O2 r, Y' F% b
        {4 c, l$ G8 o9 S  Y2 K* x( E' D# o
            /* Save the current WCS for future restoration.1 h, N8 _0 l% F$ |
             *// ~" t9 |6 o' o# `
            UF_CSYS_ask_wcs( &saved_wcs );
( I4 r4 s( c) i, ]- Y' [0 Q            /* Move the WCS to the specified plane. This is
* `. k+ x9 v% ~+ P9 D3 B             * necessary because the position passed to the motion
2 U/ |3 B$ E4 K! Z# N4 v             * callback, and the position returned by, y: S! n6 W3 n" M6 f' `  D9 H
             * UF_UI_specify_screen_position, is the screen/ ]# A& V& R; G& _/ b0 ]/ I9 F% ?
             * position projected onto the WCS XY plane.
9 Q% K" ^$ s* ], L7 E% Y+ B             */6 H7 y7 D$ G7 ^5 _/ z! d4 m
            if (default_plane != 5)
1 G1 p, c, J) D3 I& E. ?            {# v$ w2 R/ O* F: G" R( B1 E
                UF_CSYS_create_matrix( plane_matrix, &mtx_id );
2 P* D7 y$ C$ _7 _7 A                UF_CSYS_create_csys(  plane_origin, mtx_id,
4 l7 {, n& H6 B  b6 U9 ^                                   &csys_id );
& N* p5 s( w2 S6 \. v" c                UF_CSYS_set_wcs( csys_id );3 |6 z6 s+ Y3 l; H3 j7 ?. [0 R! a, b
            }) d( K( x% W+ k* o0 `/ ^. D
            /* Obtain unit vectors and the arc matrix relative to  T" A( k) a: h( e4 f& o* {
             * the Work Part coordinate system.
3 @6 d/ T1 b+ z             */
8 s& ?; S: t; X            UF_MTX3_x_vec( plane_matrix, x_vec );
! v1 K* Q  K, r            UF_MTX3_y_vec( plane_matrix, y_vec );
  x5 \; ~. d2 Q! v6 X# E9 w            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, x_vec,% w" M6 r! Z# D6 o6 R' e2 R
                            UF_CSYS_WORK_COORDS, x_vec );
1 m5 ~$ l4 ~0 Y1 i            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, y_vec,; `7 z- e# x# y1 @/ G
                            UF_CSYS_WORK_COORDS, y_vec );# ^% N; Q2 K6 q
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, root_origin,+ N+ Z5 q& q" q: x+ |
                            UF_CSYS_WORK_COORDS, work_origin );  Q6 w+ d/ [. v( H0 u1 d
            UF_VEC3_sub( x_vec, work_origin, my_data.x_axis );
. y( Q& [& a. V8 _9 C7 V            UF_VEC3_sub( y_vec, work_origin, my_data.y_axis );8 V& W* S; A) e1 U. r
            UF_MTX3_initialize( my_data.x_axis, my_data.y_axis,8 U0 m; l+ Y% I! b% J
                             my_data.matrix );; U8 }) O* a; J
            UF_CSYS_create_matrix( my_data.matrix,
/ D+ X6 Y, ?# F& x0 K7 `                                &arc.matrix_tag );
6 i  t5 j- Z* @; S9 O            do3 b) C: N4 Q0 i$ T% n
            {& @& n/ }% X$ k8 T" x+ G2 J
                UF_CALL(UF_UI_specify_screen_position(: C7 N& e' |# P, ?. b8 W, H, Q
                                "Specify arc center",
5 d* P+ [% g5 X8 U- e                                NULL,
2 [' T8 ?* w) c6 g/ f) b8 g                                NULL,
3 B2 [( q) s% O3 S8 Q                                my_data.abs_ctr,
& b  B8 L8 Q3 Y: r& Z7 L! m                                &view_tag,
- T% F; o  B- Y- ~0 d) H1 n                                &ctr_resp ));
- e6 x4 K( G) l8 J  C                if (ctr_resp == UF_UI_PICK_RESPONSE)
3 c! L: v: P% w0 {* r, n: j" U                {6 f7 o: Z& {% O- R2 |7 l8 _; L
                    /* Map the arc center to the arc csys.
& k0 G; P! e# J6 Z0 _                     */
" `' t$ E6 L. P, j                    UF_MTX3_vec_multiply( my_data.abs_ctr," Y9 ~2 K4 y9 ?8 W2 |
                                          my_data.matrix,
) w" N# T& [! S- t                                          arc.arc_center );
; C2 p( L+ `* l* e                    UF_CALL(UF_UI_specify_screen_position(  F  o% k& m1 Z7 e6 a- f
                                    "Indicate arc radius",5 Z4 e' i$ t1 s# m+ F
                                    (UF_UI_motion_fn_t)motion_cb,2 I7 [/ ^5 O2 n( j' i
                                    (void *)&my_data,2 H- r- b: A6 a$ s
                                    arc_edge_pos,
; A4 R! K, Y" Z                                    &view_tag,7 D) a* d4 c5 l$ r
                                    &pos_resp ));! z5 @/ U: {9 W6 E- q. N6 O
                    /* If a position was obtained, create the
6 Z, Z. f, Y& p: W. ^2 B                     * circle.4 s! @. d0 T1 S) ?4 \$ |; j* X  z8 }
                     */
7 l- h  i9 y+ H5 |: R; Z: b. F7 V                    if (pos_resp == UF_UI_PICK_RESPONSE)  k0 ]; C! k0 ~4 ^; e
                    {5 e6 n  Z' G" v7 g/ i9 X3 X
                        UF_VEC3_distance( my_data.abs_ctr,, M* q/ o3 L. {) |8 v4 m+ f# s
                                        arc_edge_pos,' o1 U0 K7 ]# T2 U
                                        &arc.radius );4 v1 l$ J3 ]) v+ y: A+ |9 m
                        UF_CURVE_create_arc( &arc, &arc_id );# z. R' `4 _+ ^' Y
                    }
0 B/ W, m2 H+ b1 p                }( c. S6 r* ?. c7 k) ]8 v
            } while ( (ctr_resp == UF_UI_PICK_RESPONSE) &&* X. M0 @8 S! A2 ~" `
                     (pos_resp != UF_UI_CANCEL)           );; I9 t" g( C4 L( n) }3 r" z
            /* Restore the WCS if it was altered above.
) h! G. C  Y+ a- G             */
8 l$ R, a8 O. p4 T3 {+ ~  n# S& r            if (default_plane != 5)8 y  t* a3 x! Q# m6 c; Y1 ~( i
            {
/ s  c  ^* W0 l1 n) D                UF_CSYS_set_wcs( saved_wcs );- F: s' o8 w1 C. T
                UF_OBJ_delete_object( csys_id );
/ g6 a% t. ~6 u            }
" c# K9 b" V. G+ O        }
6 Q. o+ q6 I8 Y# K. X9 _    } while (ctr_resp == UF_UI_BACK && plane_resp == 3);
. p+ j$ i6 j, T" z/ e}
- |. t( k! I: r- @- ?% {/*ARGSUSED*/" b* z# r# o, n" c$ n+ N5 L" `
void ufusr(char *param, int *reTCode, int param_len)
% _# ?/ t9 o6 _& M4 z8 o3 d  }{% g: C9 R4 |) P0 o* b1 K9 \( _- U: V
  if (!UF_CALL(UF_initialize()))6 X+ u: t/ s+ A& {& z  F1 i0 L
  {
& a) s% \3 b. k" a: z5 g' P    do_ugopen_api();( Z7 h7 L7 i3 Z. l% x2 T& z2 Q
    UF_CALL(UF_terminate());
5 r* R# [9 P+ M' ^  }
' F! |5 f0 N4 U  v}
2 w2 I6 E4 M/ O# I: [% I& I6 m1 O' zint ufusr_ask_unload(void)" q- f8 l, V4 H1 \9 L
{
8 }  H  b) ]$ `8 R; W/ A1 G  return (UF_UNLOAD_IMMEDIATELY);0 B& k/ i) x& @
}
1 v7 I4 [- a. B4 d/ Q& N! b6 u/ e# e3 F* n3 D, a! H
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.diantuankj.com/ 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二次开发专题模块培训报名开始啦

    我知道了