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

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

admin 楼主

2015-2-9 13:45:19

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

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

x
/ i5 Q$ \+ f9 U3 g1 l
NX二次开发源码分享:动态画圆,指定屏幕点和向量相关用法2 J1 A7 Q3 ]8 K4 X- a- ?. Q& k- w

- g' j/ V$ z. a7 y

' j/ G! l/ ^' g% Q4 F, X. K这个案例很不错的,分享下,程序运行的结果是指定屏幕,拖拉即可动态绘制圆圈!3 V6 ]) L6 t4 G) I
& z! @. v9 f, F% ^8 y+ l$ Y
#include <stdio.h>
3 c- f5 G( I8 t2 P, T9 N#include <uf_defs.h># _& {( \' I* S2 f7 K
#include <uf.h>
+ I* r% s6 l0 c. V#include <uf_ui.h>
4 ]9 l, ^& q! J/ p#include <uf_csys.h>0 g* a7 B" G% p
#include <uf_vec.h>( a% P4 D7 S2 I- C4 ^5 w" j
#include <uf_disp.h>1 z' f2 h/ R+ H7 [8 y! H; S
#include <uf_curve.h>
6 b* {  ?- S4 _8 A$ t; ^( U#include <uf_mtx.h>- O$ c% |- J/ `8 l; o' e
#include <uf_obj.h>
0 x$ B: s5 X6 u/ g6 Jtypedef struct
1 F, R+ N- h: \4 I        { UF_CURVE_arc_t  *arc;1 a4 w" v7 W- P9 U4 o
          double          abs_ctr[3];
  G5 K2 r* ?' d          double          matrix[9];
- }. |# }! e8 [# j$ Z$ L          double          x_axis[3];* Z) E7 I' h" {
          double          y_axis[3];/ F+ D6 H5 ~) d$ y, e3 W2 e' A
        } my_motion_data_t;9 U. }1 z1 a- p2 d6 x7 c( V
/* Define a motion callback to render a visual representation of
, o( e1 _) @) I; G$ U* V: r7 _' C9 @9 G * the circle to be created, along with a bounding box around the
- p: N9 Y2 ?! Y; E  S5 {3 z * circle, an arc of radius 1.0, and a "rubberband" line from the
7 R- t. A: q; l0 M+ U& N* b. V * center to the cursor position.
$ N2 J, Q3 }0 W */
( S) F0 T6 d0 r6 a1 f# _9 ^static void motion_cb( double                   *screen_pos,% Z1 }, i' `4 a6 d
                       UF_UI_motion_cb_data_p_t  motion_cb_data,
; o) t# [( {' E  H* e                       my_motion_data_t        *my_data )' j+ d# z2 x' f6 a
{0 n8 A  u  w% W+ c) I* B
    double radius, pos_array[5][3];( A- ]- h3 x$ O# O* G
    double xrad[3], yrad[3], x_y_vec[3], x_ny_vec[3];
% t5 g' g) C# m" {    /* Calculate the arc radius:  the distance from the arc center
2 _+ w# S4 u; z5 \     * to the current screen position.- c7 S7 ^, ~3 N1 Q
     */# o& O. O9 l' o0 x! V
    UF_VEC3_distance( my_data->abs_ctr, screen_pos, &radius );# y; V9 |6 w2 Y. @
    /* Map the arc center to the "csys of the arc".' ^" H) t& {# f+ M
     */
, M2 q+ x6 E* o: j9 r    UF_MTX3_vec_multiply( my_data->abs_ctr,' b4 T! R1 y8 g4 `6 Q1 E2 [4 ~
                       my_data->matrix,% X; R  v* v" h) z! \& Z
                       my_data->arc->arc_center );8 p4 i6 \# C  }5 `" b
    /* Draw a circle and an arc in the view of the cursor.
3 D# c4 H4 y) O8 f     */- d! T2 i" H' L5 k, Y
    UF_DISP_display_ogp_circle( motion_cb_data->view_tag,
0 `7 W* @4 L( H2 d+ }1 O                            my_data->matrix,
. o* B1 \' F. b& G9 y- T& k' X                            my_data->arc->arc_center,
4 e/ I9 w6 U" _$ K2 I: s9 F) T                            radius );
  e6 n/ q' V& }  p# W! j; I7 G$ s    UF_DISP_display_ogp_arc(    motion_cb_data->view_tag,
3 m/ ^" V& t0 b' N                             my_data->matrix," G- b! u! u) I7 @8 U
                             15.0*DEGRA, 345.0*DEGRA,) B0 h: I) k# h4 A, R
                             my_data->arc->arc_center,3 E% f4 O' e# Z7 _
                             1.0 );; U# ~3 k" W3 N, _3 m9 r4 f! g
    /* Draw a bounding box around the circle.% n3 c! s, |. [; g' `1 z
     */* z/ M  V4 a, ?
    UF_VEC3_scale( radius, my_data->x_axis, xrad );
1 k3 a$ V( A1 t  |- f    UF_VEC3_scale( radius, my_data->y_axis, yrad );% a6 Q( j& R7 h+ @0 {
    UF_VEC3_add( xrad, yrad, x_y_vec  );1 s: {: ~; P2 m2 C' @- T2 a- d: c
    UF_VEC3_sub( xrad, yrad, x_ny_vec );8 ^6 x9 m8 I/ B6 C) C8 I& W$ S% h
    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[0] );
6 t, l; O' g/ n) m! Y5 j. [  m    UF_VEC3_sub( my_data->abs_ctr, x_ny_vec, pos_array[1] );
2 p; l2 T3 b" f: E    UF_VEC3_sub( my_data->abs_ctr, x_y_vec,  pos_array[2] );# V) M# c7 X4 g
    UF_VEC3_add( my_data->abs_ctr, x_ny_vec, pos_array[3] );5 q# ]5 ?; W: W& G
    UF_VEC3_add( my_data->abs_ctr, x_y_vec,  pos_array[4] );# e) T5 X2 h& b( g4 H  @  H" Q
    UF_DISP_display_ogp_polyline( motion_cb_data->view_tag,
8 T0 h) Q% j$ d' N7 y* x* k                              pos_array, 5 );
' a/ H% X) X# i- W    /* Draw a "rubberband" line from the circle center to the3 k6 Z  z- Z2 F- L8 g
     * cursor position.' z; b! b7 C7 t" t# `' |; T: s- b
     */" |+ Y, v% E: B
    UF_DISP_display_ogp_line( motion_cb_data->view_tag,- a, i) ?$ {& w5 L8 w) B* [
                          my_data->abs_ctr,# O- a) {* F2 J" l3 f& S8 X
                          screen_pos );8 ]  @- n, u) B2 u& `
}
" G& ?  Z; H! S6 {0 e) G#define UF_CALL(X) (report( __FILE__, __LINE__, #X, (X)))6 s7 t4 q* M1 n" o& `! ?0 a  f) X
static int report( char *file, int line, char *call, int irc)/ `! b" W& O" w! G
{$ x# O# y- w" Y5 E( x- R6 s- x9 f, [
  if (irc)1 X( i7 R5 f4 C* L$ E1 W
  {9 ?; `/ e0 I. o  |# p
     char    messg[133];& m7 E% b- ~: Y; z" p. x( B
     printf("%s, line %d:  %s\n", file, line, call);
( V- R* V* t5 n; Z     (UF_get_fail_message(irc, messg)) ?
7 \6 J: ]! [  i8 A; X( ~       printf("    returned a %d\n", irc) :- m# {1 p* U: R* f
       printf("    returned error %d:  %s\n", irc, messg);
9 M! l) h. `( P8 F* _/ t  }
, G. u. F9 ~( X7 s8 w1 B1 {# ]1 V  return(irc);
3 ?) Z! u' f3 Z6 M* H4 U3 e}& q+ m2 M8 C; p) M  N  O8 t9 F
static void do_UGopen_api(void)
+ S" A1 a# d$ L{8 X, t1 i+ R1 D& n4 h
    int               default_plane;
8 L  n5 v/ b9 j) V4 O- e: L0 O& c; v    int               plane_resp, ctr_resp, pos_resp;! i) P4 _! O$ L* W: C
    tag_t             plane_tag, saved_wcs, mtx_id, csys_id,
2 ~' c9 r& V7 I' R6 K9 {) P3 I+ k, k                      arc_id, view_tag;5 p$ k2 k) Z, i5 o% |+ Y  W/ m
    double            plane_matrix[9], plane_origin[3],! X) H' W! ~: \- S! t
                      x_vec[3], y_vec[3], arc_edge_pos[3];, r6 j: |+ j$ q2 b! y6 N0 g! j
    double           root_origin[3] = {0.0, 0.0, 0.0};
- E( |5 [8 a) L2 q    double           work_origin[3];
. j6 W# J! m% B( U    UF_CURVE_arc_t    arc;
8 D+ F9 z! Y2 Y& Y5 a! J9 \    my_motion_data_t  my_data;! A1 i" T  w" V2 n5 Z1 \% U+ Y
4 G) M$ V0 M# P% \2 N, P# Q2 p
    my_data.arc    = &arc;9 v1 s$ z7 d, \/ k) P
    arc.start_angle =   0.0;
1 C: l& i& ]+ N' u. Z, S    arc.end_angle   = TWOPI;    /* Create a full circle. */! P2 B% W2 u: ^. N
    default_plane   = 5;       /* Default plane = WCS */
' P( _+ t* c. {2 U" F) `  a    do) D% S9 V- e! Z4 F
    {9 L- S6 g( c  n. a, |" r3 c
        /* Specify the plane on which the circle is to be created.
1 b* |1 Z6 B+ g         */+ F- O- P+ O- p: A/ ^% y( x
        UF_CALL(UF_UI_specify_plane(; {2 d& q' r. ?; g* p
                         "Specify plane for circle creation",
3 f- Z0 T; D% f; r* T                         &default_plane,
8 i3 |& H- L9 F4 l, H2 |7 c* M                         1,0 y2 p8 A. `) r4 y5 [+ C
                         &plane_resp,
9 }# W$ I( e: M1 {2 F                         plane_matrix,
& _7 |+ q3 a! q1 \4 ~  B* ~                         plane_origin,
/ \8 V0 p$ s) j% n/ l$ U                         &plane_tag ));
+ C/ {- F" c( U2 B2 N* n3 L        if (plane_resp ==3)
' {2 Q$ F. [" P2 W        {: ^+ J- I8 g9 w; @& A
            /* Save the current WCS for future restoration.
) S& C) f" ~# j+ Z3 e" }4 ^             */5 V8 i7 m7 X& E
            UF_CSYS_ask_wcs( &saved_wcs );6 j/ l) k  w9 D& H2 {
            /* Move the WCS to the specified plane. This is0 V6 a' O. f) T7 f; T" G4 |
             * necessary because the position passed to the motion9 V+ B- T3 b3 X2 P! b# I2 j
             * callback, and the position returned by
9 @& V) d: m1 ?: ~             * UF_UI_specify_screen_position, is the screen
/ R3 q3 `" t# w$ j7 `' f8 F             * position projected onto the WCS XY plane." H3 b# P" y" ]7 s: m, I/ P
             */
  F7 _3 o! `( |9 |5 M* K/ J" R, O( u            if (default_plane != 5)! n7 D8 B: @; o) p
            {0 G; A( O( Q9 U( ~# d" U1 f
                UF_CSYS_create_matrix( plane_matrix, &mtx_id );1 w+ Q1 V! t. G7 _: q! ^6 _
                UF_CSYS_create_csys(  plane_origin, mtx_id,
% @: D5 l9 M2 w. l                                   &csys_id );
* i3 K/ Z$ O- j3 Q4 z                UF_CSYS_set_wcs( csys_id );
% B/ B3 P! r' W3 g( i4 J  _7 r. y            }4 G  Z! ?) U- y' z, _- f% b
            /* Obtain unit vectors and the arc matrix relative to
- y6 ~1 |' W; R1 n# K! R& V             * the Work Part coordinate system.
! ^8 U4 B0 O% ^: h+ Z9 L+ X: T1 e             */
, c( q# N; a$ ~& x$ h            UF_MTX3_x_vec( plane_matrix, x_vec );* r% }" `6 x% d! k
            UF_MTX3_y_vec( plane_matrix, y_vec );6 n& P9 _. `6 z# |, i
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, x_vec,
  G; T: K9 i/ c) \                            UF_CSYS_WORK_COORDS, x_vec );
4 C" t9 Z! F! I% x* l2 w( d! c3 A            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, y_vec,# ?' Q! P0 q! X5 g( v6 f+ F: d
                            UF_CSYS_WORK_COORDS, y_vec );0 _! M! Z) _7 ^% X
            UF_CSYS_map_point( UF_CSYS_ROOT_COORDS, root_origin,
; W+ ]. w- n2 y3 x  c7 }9 d                            UF_CSYS_WORK_COORDS, work_origin );* C6 S5 p0 F" s# K+ R
            UF_VEC3_sub( x_vec, work_origin, my_data.x_axis );5 i( ^9 P  ?. Q% x; z
            UF_VEC3_sub( y_vec, work_origin, my_data.y_axis );
" O7 ^6 L  x: W! k; H8 ~& M3 ]            UF_MTX3_initialize( my_data.x_axis, my_data.y_axis,
9 D! r! X; I. A5 }( T4 c. x- j2 e0 ^                             my_data.matrix );- N& t# O0 I' M4 N; C3 b
            UF_CSYS_create_matrix( my_data.matrix,5 [- f/ b: d7 q; z9 M) z! |
                                &arc.matrix_tag );6 F# M! m3 J( b, B& b: n
            do
9 Z5 t" o$ f8 {0 K8 N) _! U4 N            {6 V% t* R; y9 b' D9 j6 s
                UF_CALL(UF_UI_specify_screen_position(
9 q: A8 u! V9 A' M- C! {7 X, T8 y                                "Specify arc center",
. |3 W+ b& k% B  s                                NULL,
2 Y2 i3 E3 \9 e                                NULL,/ C* m6 {. ^' d7 m, _
                                my_data.abs_ctr,3 c+ b2 @" O. {/ M' S
                                &view_tag,
# n/ g- i3 b7 O4 n: _0 `                                &ctr_resp ));& ?6 f- O1 j: [; G- [/ b1 C! h
                if (ctr_resp == UF_UI_PICK_RESPONSE)
+ u" Z; T* o! r5 _$ N3 o- j                {) m* V4 r" R- X$ h, t: p1 u
                    /* Map the arc center to the arc csys.
, U; b7 k7 |4 E4 M                     */; E& H/ T% d7 p6 R
                    UF_MTX3_vec_multiply( my_data.abs_ctr,
1 T9 r3 j3 Z. \" N0 a1 ~                                          my_data.matrix,; |; Q( u- C( @3 q1 ~
                                          arc.arc_center );3 G4 f! _- b5 o+ X& @/ S5 _
                    UF_CALL(UF_UI_specify_screen_position(
$ P6 V: Q5 S: f9 h7 B8 H# M" @                                    "Indicate arc radius",
7 U4 A1 p/ ^' @% T) Q                                    (UF_UI_motion_fn_t)motion_cb,& c+ a) s! q( ?; M' I. L; d$ i
                                    (void *)&my_data,+ h( z  D6 s5 ?$ N
                                    arc_edge_pos,* ^; Z7 F& U9 o; R4 z) [& _5 _
                                    &view_tag,
% N( b9 @' t. t! M# Y. v+ S                                    &pos_resp ));# C( n0 v2 d' c, B5 a) `5 ]4 T+ E  `
                    /* If a position was obtained, create the# o1 T8 E+ _( E6 l- {; N
                     * circle.4 A! o! P. g1 O
                     */
6 E% x, D: x7 i/ K6 r; C                    if (pos_resp == UF_UI_PICK_RESPONSE)
" }. |# }0 S, Y9 E( i/ A                    {+ ?/ `5 J5 G3 X
                        UF_VEC3_distance( my_data.abs_ctr,' i: l, D8 U# ^" E# z+ u) I
                                        arc_edge_pos,
$ @1 K8 J8 R- s                                        &arc.radius );
0 p4 j8 J7 |& T3 ^9 i$ e                        UF_CURVE_create_arc( &arc, &arc_id );8 f" {- [% e/ y4 z
                    }
$ d$ V( V+ L& [" g  y1 D                }
7 W' {6 E, s) q2 _+ Y) Y3 T' l            } while ( (ctr_resp == UF_UI_PICK_RESPONSE) &&' D3 P/ u+ }8 Q6 y' L2 w
                     (pos_resp != UF_UI_CANCEL)           );& K4 Y# L- H! Y
            /* Restore the WCS if it was altered above.
# Q0 w# t6 O6 `1 |, \% B             */
4 y5 X- a5 o/ \% J& a* N            if (default_plane != 5)4 ?% I% Q: }2 C1 g
            {* U6 i1 `1 ]! U
                UF_CSYS_set_wcs( saved_wcs );$ A( N  L) O% m8 r. S$ u
                UF_OBJ_delete_object( csys_id );3 F( P2 ]$ V! x
            }& M8 a7 X+ _  p* m. C8 m! Y
        }
$ O9 F" L) w- o8 @    } while (ctr_resp == UF_UI_BACK && plane_resp == 3);
( ]4 \, \0 z; n2 D0 \" a# P: E2 b1 P}$ o8 ~, v# p( w
/*ARGSUSED*/  h) g5 S+ Q" i! M
void ufusr(char *param, int *reTCode, int param_len)
, f7 y7 @+ ~% K+ W* m7 L8 d5 C{
) C" z' N' u' p" j  if (!UF_CALL(UF_initialize())), k! a" K/ w9 f1 F1 H6 V
  {9 H) \7 g9 `% Z# j* o/ G3 Z
    do_ugopen_api();& d! E* J! m- n) \6 ]( T
    UF_CALL(UF_terminate());
% [( C9 J/ i( k, x3 p* T+ f+ D  }
8 U( _  E5 {* g6 g& x+ L}
- i$ e, C# g4 o( a# Jint ufusr_ask_unload(void)
3 u6 H9 p9 z8 o6 V  a" [{
5 K* N% {. g6 P8 r/ v$ A  return (UF_UNLOAD_IMMEDIATELY);
* V6 b) s% x( }$ @% f8 g}
2 `* g9 u& n3 o9 g
7 \2 i7 V+ F2 e' U
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了