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 6208 2

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

admin 楼主

2015-2-9 13:45:19

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

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

x
5 R  b2 {, R# t; ^( r
NX二次开发源码分享:动态画圆,指定屏幕点和向量相关用法
0 i0 U4 v' z- h0 s
6 n5 |9 a2 n' D" N" e# K3 y% u

* I: t1 i9 I; E7 g这个案例很不错的,分享下,程序运行的结果是指定屏幕,拖拉即可动态绘制圆圈!3 k. ?3 K: X+ l' O6 w' N
) I, j- K  L& d' ]+ e6 V
#include <stdio.h>  \1 y, w4 z1 b5 H, e
#include <uf_defs.h>% ^, e, V2 N. c! v2 j0 J) f
#include <uf.h>& i* _4 P* s6 f( W
#include <uf_ui.h>
" V5 u! A" c+ K  ]! G, @# j- I#include <uf_csys.h>
: F! C- \4 C" z5 k! x#include <uf_vec.h>6 N; @2 c! D5 x3 S. \
#include <uf_disp.h>  N3 g' o$ X- P4 S' B  j3 W
#include <uf_curve.h>
, g3 e# a( ^, {: [% F#include <uf_mtx.h>9 q+ r- N: S3 ]) u! s4 {
#include <uf_obj.h>3 X! H7 K+ w3 [
typedef struct* E: w' j6 X1 A
        { UF_CURVE_arc_t  *arc;1 E; l4 }( N- a$ k+ u
          double          abs_ctr[3];& t- @6 K% J$ B! ^2 Y3 r
          double          matrix[9];# r+ V' Q0 D- H3 w- \
          double          x_axis[3];0 [5 u  q$ ^) M) E
          double          y_axis[3];' c& b* G$ ~2 ^" C
        } my_motion_data_t;# c1 I8 a- Q! y. X& [
/* Define a motion callback to render a visual representation of+ q( C& b& G* w0 b8 g8 r
* the circle to be created, along with a bounding box around the
2 ?6 D, q, D* B3 m * circle, an arc of radius 1.0, and a "rubberband" line from the5 \; k  \7 O( \
* center to the cursor position.
# {% u( d0 z- [# w- x */! z. l$ z; E- f3 q& i3 m
static void motion_cb( double                   *screen_pos,
  o# A( G% X6 i% W& ~2 Y: |                       UF_UI_motion_cb_data_p_t  motion_cb_data,
: W: A0 M' e- O1 d                       my_motion_data_t        *my_data )
: D# |' l& g+ ]" W! W9 B$ K{, L0 e. C0 Y5 O( M
    double radius, pos_array[5][3];. p- Q; ~; I' s; h9 c+ Y$ |+ I
    double xrad[3], yrad[3], x_y_vec[3], x_ny_vec[3];' L8 Z" n  o# n5 ~' {4 C" f
    /* Calculate the arc radius:  the distance from the arc center. a8 i+ j9 w1 ?$ k8 Z
     * to the current screen position.
: _% e1 t+ }' a5 b- q( r     */% L& P9 Y* w2 D" J7 z, o
    UF_VEC3_distance( my_data->abs_ctr, screen_pos, &radius );
) D2 U' G) c: X2 z    /* Map the arc center to the "csys of the arc".. U8 p6 E3 o1 L$ F3 O# P1 a: g
     */3 b8 j0 r& h/ R" t
    UF_MTX3_vec_multiply( my_data->abs_ctr,
/ U9 b" k4 P' ~0 p0 ~, k                       my_data->matrix,
7 Y8 j, j; t, m/ P) C( o+ ]  @                       my_data->arc->arc_center );! n, W5 g9 |# f; R  a
    /* Draw a circle and an arc in the view of the cursor.5 O# C7 l6 ^3 C" {5 u4 C
     */
; Q% B" [$ Z3 F    UF_DISP_display_ogp_circle( motion_cb_data->view_tag,# m9 O  ]3 _9 l* h
                            my_data->matrix,
2 F- \! u  `% P/ T7 S                            my_data->arc->arc_center,
2 E; ?. c: o2 u4 U. e: J! W/ h# C. P                            radius );( b  c3 y; }1 E$ P  ^( J6 y( H: S
    UF_DISP_display_ogp_arc(    motion_cb_data->view_tag,
& M2 B7 |7 T. S; T+ C6 Q                             my_data->matrix,4 ]5 b7 E  a0 P- {5 y
                             15.0*DEGRA, 345.0*DEGRA,
- L  ~% q' J* f* F$ _. f2 ~; H                             my_data->arc->arc_center,
8 {$ K6 X* N5 U! t0 k; y# R5 y                             1.0 );
$ }& u' C, k" r  Q# O5 Z+ l    /* Draw a bounding box around the circle.
8 I$ C" {( y: N' `. s9 C     */4 c( h% X* X* t$ q& |
    UF_VEC3_scale( radius, my_data->x_axis, xrad );- o# r2 g: f: X; J3 ?, d
    UF_VEC3_scale( radius, my_data->y_axis, yrad );' Q2 z) u, }& M( g/ I; W. Q
    UF_VEC3_add( xrad, yrad, x_y_vec  );/ O/ U$ I# K; o( E* j1 V) B
    UF_VEC3_sub( xrad, yrad, x_ny_vec );
8 V+ z  k% T" z  S' h( `  |    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[0] );( h6 g7 C  A4 Z6 \% @$ \
    UF_VEC3_sub( my_data->abs_ctr, x_ny_vec, pos_array[1] );
, ?" @5 g+ a  ^- M' j: c( S* F- k    UF_VEC3_sub( my_data->abs_ctr, x_y_vec,  pos_array[2] );
# U4 r7 w' @2 i9 N    UF_VEC3_add( my_data->abs_ctr, x_ny_vec, pos_array[3] );
2 R6 n$ V6 k7 X! F    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[4] );
; l: u! W0 H# d1 g    UF_DISP_display_ogp_polyline( motion_cb_data->view_tag,
  ]6 `) r) I  a                              pos_array, 5 );
# J6 t: c; x6 m8 F/ ?$ }- o    /* Draw a "rubberband" line from the circle center to the: `, l5 P) q0 Z: l8 D1 x
     * cursor position.5 k0 }" _5 V% d" y7 Y
     */* S2 |8 J! l0 ?/ J2 C. a
    UF_DISP_display_ogp_line( motion_cb_data->view_tag,0 r: {/ |2 j$ \8 y; I6 a1 ^
                          my_data->abs_ctr,
* j/ r1 R& g, i9 b# a5 F                          screen_pos );
, D  q( F0 Q9 u, b3 }" m6 a  G. A/ u}' e+ |" d8 }  m* x$ Q* P( ?9 T
#define UF_CALL(X) (report( __FILE__, __LINE__, #X, (X)))
% K& I$ J7 n3 P1 h7 E9 o& V2 ~static int report( char *file, int line, char *call, int irc)
6 [7 x2 x4 J  E) ]( e{
. f+ e- C! y) P, T7 k2 w: |  if (irc)
; W+ @; U. F: z8 v' d  {
' k4 r& l! J% H& \- h- R     char    messg[133];4 i1 D1 A' D" h8 P
     printf("%s, line %d:  %s\n", file, line, call);% \, x! i' {' ]2 x7 I& W+ j
     (UF_get_fail_message(irc, messg)) ?
- l" U, H" y' O, |7 [       printf("    returned a %d\n", irc) :  Z9 G2 T: D: n8 H" X( i2 U
       printf("    returned error %d:  %s\n", irc, messg);
( Q6 {, r8 @3 C! x9 s! F  }5 h" ?/ C2 }  p
  return(irc);" z  O, v9 t' i9 h7 C
}6 D+ b! ?' c& O  ^1 J' o- g
static void do_UGopen_api(void)% r8 D6 A2 Q7 ]) _8 Q5 c
{7 L0 c2 U' D, v
    int               default_plane;9 i2 Z% E# O& @) C% f. w0 O
    int               plane_resp, ctr_resp, pos_resp;+ t1 r1 ~( Z# [% J6 P( F7 D5 U
    tag_t             plane_tag, saved_wcs, mtx_id, csys_id,. \, K0 e4 `4 J, L* e! ]# j
                      arc_id, view_tag;4 }5 {4 W6 p( \5 Y% H
    double            plane_matrix[9], plane_origin[3],
& D5 S8 M3 E1 j( @/ B8 ^4 l                      x_vec[3], y_vec[3], arc_edge_pos[3];* p( i) S5 B% L
    double           root_origin[3] = {0.0, 0.0, 0.0};% ?$ F. T0 |5 D( U" j% A
    double           work_origin[3];. H: |1 u* j: g' j- Y8 `' V
    UF_CURVE_arc_t    arc;6 K0 @  d8 {# c- {3 }
    my_motion_data_t  my_data;
7 H: `; r) H! F) t; h
6 w4 k" K6 r) \* k9 h    my_data.arc    = &arc;
7 g! Q' J1 p) C- ~% K0 D$ A" R    arc.start_angle =   0.0;
: L0 t7 `. k) i; e    arc.end_angle   = TWOPI;    /* Create a full circle. */3 Z! {8 H7 \! b
    default_plane   = 5;       /* Default plane = WCS */
% O! I) ^; u- @* _" k    do, t4 K: }/ ^$ }+ h0 L9 X
    {6 _- V( @' Q3 I+ O- E
        /* Specify the plane on which the circle is to be created.
2 E) z8 n* r# V8 v% A/ V         */2 J! r( V5 y* H, V8 t$ m( s7 G
        UF_CALL(UF_UI_specify_plane(4 h' J6 S& [$ C% d8 t
                         "Specify plane for circle creation",% }5 S- D( [) s% R
                         &default_plane,+ N3 D# V& H# c, H1 _, a
                         1,, A( I2 J1 |# `& }
                         &plane_resp,7 z5 X, k8 C9 b; B, W  x
                         plane_matrix,6 n$ w; x* `9 |4 C1 V, T. R  @3 ?. G" P
                         plane_origin,6 n1 ?( ?0 c( X6 ^, G" k- `$ k" S
                         &plane_tag ));5 D8 D4 k. U( W1 F
        if (plane_resp ==3)1 v; _0 C# Q/ i, o
        {
9 a6 z, h6 t  i8 u/ G) X- P            /* Save the current WCS for future restoration.7 `  h" ]0 b% b/ S
             */
* R, y( r9 W+ J% b8 X            UF_CSYS_ask_wcs( &saved_wcs );4 m8 R# g% y7 D- Z) s- w
            /* Move the WCS to the specified plane. This is
, s, o8 K5 O0 P# _* \             * necessary because the position passed to the motion
  m5 C' G2 g' E( b" s! n- M! |& A             * callback, and the position returned by
' s/ c# M; ]0 j2 X. q3 c             * UF_UI_specify_screen_position, is the screen( z  d1 l* T: V7 _% _
             * position projected onto the WCS XY plane.
4 F# Z5 B7 H9 ^             */, ?/ Y( i0 l$ K5 o3 `2 ]
            if (default_plane != 5)
& C! m, m% a  k; d7 h2 L            {
) D9 e% b- d9 l9 g/ r                UF_CSYS_create_matrix( plane_matrix, &mtx_id );5 Y  z9 m$ ]  p& \
                UF_CSYS_create_csys(  plane_origin, mtx_id,
+ G" F2 m9 \) z8 X4 L7 A* V                                   &csys_id );1 Y6 x. D% W/ L8 d2 d0 m
                UF_CSYS_set_wcs( csys_id );+ h/ n4 p: O0 T" Z3 |
            }( Q$ \: ~/ C- ~& F7 ?- w
            /* Obtain unit vectors and the arc matrix relative to0 e% u( Y( r6 s: S) y6 ?3 Y# [! W( u
             * the Work Part coordinate system.
& T; U8 C  C5 s' ^& p             */
0 ]4 n( o$ y) T            UF_MTX3_x_vec( plane_matrix, x_vec );5 o. e7 {- S$ c
            UF_MTX3_y_vec( plane_matrix, y_vec );/ V3 R7 h% x; K! E1 N0 O
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, x_vec,
: C. Q, w" I/ s& t* y                            UF_CSYS_WORK_COORDS, x_vec );
, I# K$ w* p$ u0 H! {8 H            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, y_vec,
2 |/ r- r+ Q: @6 c" o                            UF_CSYS_WORK_COORDS, y_vec );+ @, Y& p$ D. h3 y" B( Q1 h7 \
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, root_origin,: \4 x% v- K) x6 I( o
                            UF_CSYS_WORK_COORDS, work_origin );
/ W) p3 O1 z, o1 v4 T            UF_VEC3_sub( x_vec, work_origin, my_data.x_axis );
& ~1 i& ]  i' J$ H$ o- n            UF_VEC3_sub( y_vec, work_origin, my_data.y_axis );+ v9 V7 }' a$ ~  ~
            UF_MTX3_initialize( my_data.x_axis, my_data.y_axis,
: |, R. G2 M/ Y8 T* v                             my_data.matrix );
9 g/ q+ p6 U) A            UF_CSYS_create_matrix( my_data.matrix,+ C% X! `$ j4 i  y; h3 C
                                &arc.matrix_tag );
# O; `( |, U, w4 E; e# F  E9 `            do
3 N) Y/ g+ n8 b1 j            {
" k- r5 @/ R0 Y- S                UF_CALL(UF_UI_specify_screen_position(
' `  S  J- r: x                                "Specify arc center",- }" H4 W% g! K1 N3 u
                                NULL,2 n) }+ u* Z/ W; V
                                NULL,1 P0 W/ z7 H2 J" w+ a
                                my_data.abs_ctr,* E6 T1 ~: J* x. A5 _; {, I& R9 O* s
                                &view_tag,8 q' w+ q5 I* {, K" j. ~4 i6 w8 c2 \; ]% b
                                &ctr_resp ));
% o$ g8 z5 p  p- T2 ]& i1 e/ ~5 l                if (ctr_resp == UF_UI_PICK_RESPONSE)
: \, l; X3 b9 D% F$ x4 W/ v                {
3 N# n& p. C4 U4 V' H                    /* Map the arc center to the arc csys.- E) H  I9 W8 n3 l& u
                     */
4 [5 t# M0 ^$ d3 ?5 ~1 R                    UF_MTX3_vec_multiply( my_data.abs_ctr,
! F! ]+ O. ?8 A# b) ]) u  q4 k' w4 V                                          my_data.matrix,
  r" |; c1 \1 g' H- K                                          arc.arc_center );) h7 H' i2 u. N$ H+ ^% b5 B' i) ~+ o
                    UF_CALL(UF_UI_specify_screen_position(6 X% l" o1 A6 z8 p
                                    "Indicate arc radius",
8 g* y5 c* Y' O/ T) j0 y3 a                                    (UF_UI_motion_fn_t)motion_cb,
3 G2 w. i) N. j" ]                                    (void *)&my_data,. c, J, o- B/ _0 A! p; k/ r
                                    arc_edge_pos,
* s% P# m' y; _5 a  H6 K                                    &view_tag,
: G- H: G0 B4 q                                    &pos_resp ));
3 ^' K# T6 U/ I) h9 v& P8 I' U! t                    /* If a position was obtained, create the
' L7 b" h/ z/ K3 X( A' ?% B                     * circle.
. s# W/ H' T/ ]7 j1 ^$ k                     */
4 L3 Y8 N) c  S7 a8 U7 K                    if (pos_resp == UF_UI_PICK_RESPONSE)8 u; P; ?1 u6 e2 q' J
                    {
. w2 j( X; a# ~                        UF_VEC3_distance( my_data.abs_ctr,
; G; C. m5 N. z0 R% w                                        arc_edge_pos,
5 t! d: ~* n; ?6 @9 b6 \: \                                        &arc.radius );! k8 z, u  I# ^$ {1 A/ }
                        UF_CURVE_create_arc( &arc, &arc_id );
) b) W, x! t2 T) c                    }' k% X- w. {5 ?9 K9 u2 t" r$ o
                }
4 x! f. Q- r9 _3 F- t            } while ( (ctr_resp == UF_UI_PICK_RESPONSE) &&0 {+ H- F; L5 Q
                     (pos_resp != UF_UI_CANCEL)           );: f5 N+ |0 x# C8 M! x3 ]
            /* Restore the WCS if it was altered above.
0 s: K# v2 D1 ?- o4 i2 q7 c' m3 g             */
/ E8 F* c& P1 y" T0 {            if (default_plane != 5)8 _# ~; e& l6 l7 g( H
            {
( |; X3 b. i8 l0 O: q* R                UF_CSYS_set_wcs( saved_wcs );
: E; u+ X& J' W) q                UF_OBJ_delete_object( csys_id );7 E- o7 J- H; |" L0 J
            }9 e7 @9 F6 T& [
        }6 o2 H2 ^# b* L
    } while (ctr_resp == UF_UI_BACK && plane_resp == 3);4 k# k9 q+ H% G; x
}
4 K+ X1 L  ^, Y1 l/*ARGSUSED*/
" `' @# k! R. X4 b7 C8 A6 ?void ufusr(char *param, int *reTCode, int param_len)% A4 b7 R. ]1 ?' {, h0 e9 i2 s
{
4 r, H/ q4 ~# f* Q' @  if (!UF_CALL(UF_initialize()))
8 ^) P/ Q  e. m1 F+ ?" m  {
; m+ c5 [7 t* g+ j; c    do_ugopen_api();( U. A' Y- V- z2 c) E
    UF_CALL(UF_terminate());$ Q1 E3 |* b9 S9 [& o
  }: T& g$ y) L. @7 u  ]
}( x3 J- f: d; d6 t' P
int ufusr_ask_unload(void)9 P! M9 k0 ~) s; m  t. t
{" I; C0 Q9 @" M: X+ l' P" {. r
  return (UF_UNLOAD_IMMEDIATELY);# Z" [& p' S. T$ P  b
}; D. X% {' O7 \2 N  v) r2 k
6 _  q+ u( `( F8 _9 s: H  N
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了