PLM之家PLMHome-工业软件践行者

[二次开发源码] NX二次开发源码分享:NXOpen C++创建UDO的过程

[复制链接]

2015-2-6 12:43:29 9538 0

admin 发表于 2015-2-6 12:43:29 |阅读模式

admin 楼主

2015-2-6 12:43:29

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

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

x
7 u4 W! e& J" |# m

" s% `5 K+ H3 I: M* H; [NX二次开发源码分享:NXOpen C++创建UDO的过程
* z2 j6 L- J; N# S2 H) Q; @& E8 O  x# y. d# J2 B
  E( s0 v6 H  G( V/ Y, @! W) X$ p
UDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!% J7 U+ {0 ?- T8 f, b
9 v, G; s% \4 Z
4 P+ T1 c( D2 _3 H0 c+ o( R$ K
6 f+ C. P$ [& p& E2 L! ]
#include <stdio.h>% X$ A# t3 _/ g% u- @
#include <string.h>
) d. w0 J/ d& s#include <stdarg.h>( j6 {, V0 Y( a: N& }" W
#include <uf.h>
- t; p) \1 B0 ]& |0 j9 G#include <uf_ui.h>
4 _! V. ?$ D; S. w7 L& H6 Q#include <uf_exit.h>: |. r& \9 P7 J
#include <uf_disp.h>
7 t+ o7 H% V6 c1 C% m4 Z+ T! l! e- [9 g/ C6 `! ^
static void ECHO(char *format, ...)
1 M9 J+ P) c3 D, ]) u9 P{( p/ t2 j' a( G+ e  _3 b
    char msg[UF_UI_MAX_STRING_LEN+1];
+ d7 M5 V7 e# ~2 T    va_list args;
- \/ L  o4 u% z; z1 k    va_start(args, format);' |3 e6 V  |# E' z% Z
    vsprintf(msg, format, args);! V! v, A9 r: E6 `' L& y8 I
    va_end(args);' V) |4 x- a$ R: I
    UF_UI_open_listing_window();
  c) G$ {& y: ?# @8 O" W5 f    UF_UI_write_listing_window(msg);% }) O" R- R2 z9 @0 y
    UF_print_syslog(msg, FALSE);- k" i( H0 G; N5 z
}/ }7 v& O7 u8 J; X8 B) K3 W8 e* y

1 a; I- }# W5 _#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
: k0 D, c  y& S5 z# @$ Z/ M+ t  l6 z# q) [3 k
static int report_error( char *file, int line, char *call, int irc)" g5 L4 Z# D- M
{
5 `& X) `' g8 ]6 I3 A8 |/ u6 o: @7 ?  S6 M$ `    if (irc)
8 L" K( y! A' L- u    {
& W9 y4 a0 M" {8 i        char err[133];: p% Z6 C* Y8 l3 F0 K
9 T9 i/ K9 @8 r, \2 k7 b# Q
        UF_get_fail_message(irc, err);
4 P( y6 D* o0 h& d1 l        ECHO("*** ERROR code %d at line %d in %s:\n",7 ?( L5 m3 B+ K7 Z  ~7 }; o
            irc, line, file);
) ]" O; ~" J; |) p6 ~4 `        ECHO("+++ %s\n", err);9 T& A& Q( O( ?- m% {' A
        ECHO("%s;\n", call);- ^* ?4 ^3 E6 L0 u$ W+ p% @
    }
2 i( k7 x% h7 p+ R' T5 N
% ^8 P# C! t. F3 w4 ~    return(irc);
5 K' T, ]) y2 E9 Q3 f/ S  v}( r; m+ m: Z6 r0 y2 L3 M! V/ [9 V

- s. e* Y& h6 i( u" L& p3 B#include <NXOpen/Session.hxx>
( T8 t7 d! }1 _1 N  s#include <NXOpen/Part.hxx>7 J$ c! P& y9 I$ T4 E  B
#include <NXOpen/ParTCollection.hxx> 3 H7 T! G) I& z+ h, U, B! B
#include <NXOpen/Callback.hxx>' M1 P1 q/ t8 p" A
#include <NXOpen/NXException.hxx>6 _: p$ R, D9 X! i5 d5 v
#include <NXOpen/UI.hxx>
2 B( X: P$ {5 X' E* z9 W- S#include <NXOpen/Selection.hxx>
: q5 p2 y3 A% Q' @#include <NXOpen/LogFile.hxx>
) [3 F( w! T& I  p#include <NXOpen/NXObjectManager.hxx>
% n: C, ?) c2 G* x. t1 o! P#include <NXOpen/ListingWindow.hxx>; m& U* Q" z8 m9 n; O# _
#include <NXOpen/View.hxx>
! l8 Y4 q0 r: v3 C0 }. W3 |% j0 [  O! [% K* r! _% F
#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>+ |+ s' ~  W* S+ F
#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>
. c7 L9 i4 l9 Y) G  b, g#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>
$ X0 j) T4 w1 Y#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>
) Z% d% X+ N, e, u9 D#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>1 Q7 [0 i2 w( K' p2 R! s
#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx>9 i+ l5 ]  ~. P* q$ x1 A
#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>
! `6 I1 |1 c6 N#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>; G) ^0 K5 w) u( j" X, `9 f! T) k1 F

1 ^+ X9 C# g6 v! P5 i6 I6 ?using namespace NXOpen;2 Z# z: t7 u, W( c
using namespace NXOpen::UserDefinedObjects;
7 z5 c; T( e5 @' r, z+ }  z
+ q; c: G9 D9 J# a  W//static variables: Q8 X1 `' O$ ]
static NXOpen::Session* theSession = NULL;1 [% M# l. R' t% c- ^+ e% D
static UI* theUI = NULL;
' k5 U2 ?& F; @0 J8 V0 b3 tstatic UserDefinedClass* myUDOclass = NULL;
# R! n9 b" E6 x! Q3 ?. i- m) @3 ?" e+ R
//------------------------------------------------------------------------------+ E0 D9 A. h' q4 V, _' `
// Callback Name: myDisplayCB1 C+ s) a) ^, e2 P) F4 ?1 s( p4 g  I
// This is a callback method associated with displaying a UDO.
4 k7 Y2 D2 t" S// This same callback is registered for display, select, fit, and attention point
- l+ `0 Z6 _. E$ V7 [//------------------------------------------------------------------------------: F& p8 E7 |$ B5 i5 D
extern int myDisplayCB(UserDefinedDisplayEvent*  displayEvent)
: ^3 J. A9 H$ s# J0 g9 n0 I. R6 Q{
3 B8 }$ d* d' m    try
$ W& l  S& t" C; F) A5 X    {
  d8 H/ \% {. _- \! u: X( C( K        // Get the doubles used to define the selected screen position for this UDO.- W0 M" G1 M* y' R
        std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();
( i; j$ C; v" W: a% o1 @1 w1 `4 m5 F3 c' F; L/ M- m6 y# A4 j9 [
        // Use the doubles to define points of a triangle
$ c3 f/ j( ^) J# D* b        std::vector<Point3d> myPoints(4);- `5 M: D& y3 c4 f( J+ E

9 w7 T6 a. o% h, \  V        myPoints[0].X = myUDOdoubles[0] + 0;
0 s6 |6 ]4 q' x6 c1 _        myPoints[0].Y = myUDOdoubles[1] + 0;
* _# A6 X+ h, B6 Y! V' o        myPoints[0].Z = myUDOdoubles[2] + 0;
' J4 W; q  a2 _8 z3 Z
: y; e3 n4 @  B6 _; k, G        myPoints[1].X = myUDOdoubles[0] + 100;# L0 v! w) @0 C% Q9 [% i
        myPoints[1].Y = myUDOdoubles[1] + 0;+ J0 W1 H% b5 V$ i
        myPoints[1].Z = myUDOdoubles[2] + 0;+ y% f" ~+ I! i
- D  K4 p" k# j, d' C! a0 l( F% m
        myPoints[2].X = myUDOdoubles[0] + 0;- m% T) p1 `, N
        myPoints[2].Y = myUDOdoubles[1] + 100;& m& T8 o3 ~1 k; o# z" _
        myPoints[2].Z = myUDOdoubles[2] + 0;: _5 g3 G8 D2 P9 n. F# d& d

0 G- J, J2 _  q% U6 \        myPoints[3].X = myUDOdoubles[0] + 0;& k( G; K: V, W. h& @
        myPoints[3].Y = myUDOdoubles[1] + 0;! F3 I# K7 W2 |  f* M5 B, w
        myPoints[3].Z = myUDOdoubles[2] + 0;5 n! _4 C7 Y( G
3 v2 G2 Y5 }8 G7 t8 E, `6 E
        // Display the triangle
# ~1 H; X' j2 |( ]/ D$ F% M        displayEvent->DisplayContext()->DisplayPolyline(myPoints);. d1 H# M* ]3 W7 @: E: e1 r
7 A8 r7 V0 l0 i( w7 C
        // Display the text next to the triangle" g2 ?$ e' \* ?$ S) f! Y  M# l
        Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);1 Q7 k: h4 e# x
        displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);  x  E: Q5 Z# D. l* C# I

" l+ I! |. e/ L" X7 @* `3 ]  d2 @        // UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 67316538 v9 P- ~* X/ x% D; L
        // This demonstrates using UF_DISP_display_facets as a work around
$ D5 w! M* L& a: y! q% K        double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };8 N7 t6 ]/ j7 {; B3 h
        double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],4 L' K* ?) X+ l5 t
                               myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2],
: \  z( x8 [  i0 P                               myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };
; d+ R, Q; n% l: `6 |7 ]) v7 t        UF_DISP_facet_t facets[1] = { vertices, normals };: n8 V. q; c- b8 K
        void *context = displayEvent->DisplayContext()->GetHandle();
* O: Z0 N3 p" U  A
8 m( Y* j. ?( F) g        UF_initialize();. h6 ^; `, l7 [; o2 Z
        UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));$ U- P) ^- O; ]8 z5 J6 |) f8 M
        UF_terminate();" \2 I( C8 L" O$ Q) F
* |: a; }7 E- l$ x
    }
2 b7 K7 {8 x& s  ~    catch (NXException ex)' [( X/ W# }$ V3 @. G
    {+ @6 U" k# C, G5 @1 j
        ECHO("CaUGht exception: %s\n", ex.Message());
0 y2 A4 Z1 T: p6 f8 T! S    }! o' Z8 [' y: N# E
    return 0;
) U+ {" P2 Q2 u& A}
/ v9 P1 ]) K# g: n//------------------------------------------------------------------------------5 ~# |9 Y5 B9 |$ R
// Callback Name: myEditCB9 ]' V2 @2 O( T, ]) N% L, A* g
// This is a callback method associated with editing a UDO.
# m, f! n# r* w//------------------------------------------------------------------------------( g" R: M. P$ d/ ~+ Z6 S
extern int myEditCB(UserDefinedEvent* editEvent)
* m1 }& o4 f& N* t! [{
. P  s1 o6 A& k9 Z& {5 B3 B) f    try
: c, P" l3 v  q4 U    {
' U+ B1 ~6 j, R( Q) ]' F- F/ u        // required for calls to legacy UF routines2 ?) ?% o3 `; F; _6 m
        // such as UF_DISP_add_item_to_display
6 Y7 R- C, U! X+ b        UF_initialize();$ A. r, d' s0 ?; T& ?8 k: K
, x" j) q2 K( z  n# v
        View* myView = NULL;
4 s6 M* _* U4 R0 l        Point3d myCursor(0,0,0);
* X" T) X0 J) a- d8 k
* i- {$ u6 `: @        // highlight the current udo we are about to edit0 \  Z. q7 X% x- O) s, j: x, G
        // this is helpful if multiple udo's were on the selection$ s; M3 ^. M3 _& k
        // list when the user decided to edit them
8 S. o7 G! O& f" M3 E        editEvent->UserDefinedObject()->Highlight();0 z9 K% w9 V( S: S5 q: j

+ p% Z, U& r/ g        // ask the user to select a new origin for this UDO
. r! D$ a  Y+ `0 v# r) v8 ]        Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);4 U  p6 \. O3 m9 N9 L( G5 A
        // we are done asking the user for input... unhighlight the udo
) U( Q" ?$ N3 V3 q, n$ }        editEvent->UserDefinedObject()->Unhighlight();
4 z- M% ^# D  }) K# \
) N8 j  ?; P5 b6 y& B' ~        // use the new screen position (if the user picked one)- ]9 f6 u0 U) ~
        if( myResponse == Selection::DialogResponsePick )
" N( ]# i. X0 q% J/ C        {% u" c' Z- R3 Q4 s! n0 d7 h  p
            std::vector<double> myUDOdoubles(3);/ p5 `; D6 K9 y* N' h; f
            myUDOdoubles[0] = myCursor.X;6 }- Z# I* `, y  j
            myUDOdoubles[1] = myCursor.Y;% [" `/ B! O# s( C. U# e
            myUDOdoubles[2] = myCursor.Z;8 U; o6 z" h; [5 z
            // store the newly selected origin with the udo
0 Z# d/ _% A  {. R7 d) }% m            editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);
1 Q% ~% _; O  p8 `8 x7 p1 K$ k5 U            // add the udo to the display list manually9 K( i/ g1 {; J; r
            // this will force the udo display to regenerate
$ p6 _4 T/ f9 C9 j3 T            // immediately and show the changes we just made
8 e5 P1 A9 F6 S9 a            UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());
  G3 |1 s& ?) n* ^5 m        }  j# u& ~1 B5 A
        UF_terminate();
' W& H& B: f8 r2 [    } catch (NXException ex)
. ]% i  ^: E( k2 }5 ^" E( w    {
) O: A& Y/ J" ?6 w4 ^7 D        ECHO("Caught exception: %s\n", ex.Message());" t# }# p2 R0 h2 g" l& a8 w: q
    } return 0;# _! n" ]* P2 S. y
}
" N- i+ C3 c- S//------------------------------------------------------------------------------4 J( w4 E5 c% W, h" P! l
// Callback Name: myInfoCB
7 l" r6 i+ a, J8 h* ^// This is a callback method associated with querying information for a UDO.& f4 p9 |/ N/ w. X5 V3 m" M
// The information is printed in the listing window.
$ j$ f5 y+ ]" E//------------------------------------------------------------------------------
9 b. d- N1 G: G3 M6 Nextern int myInfoCB(UserDefinedEvent* infoEvent)
8 x6 I5 P1 R! S{) D9 {! p+ ?  W( R0 m  ]8 @8 j) {' x
    try
; l" S5 V( o( a  s3 N! d    {- F5 |- L/ ^4 H" U) Y8 H# P4 y( K
        ListingWindow* theLW = theSession->ListingWindow();
  P" Q7 i9 f3 I        char msg[256];
. L" s2 ^1 \- w2 R' p        theLW->Open();
; X3 Z5 F: u  L% H        theLW->WriteLine(" ");
! O! Z1 i; |8 o5 K4 T1 o3 ~        theLW->WriteLine("------------------------------------------------------------");
8 {+ Q4 G6 r8 A4 P3 `" o1 {        theLW->WriteLine("Begin Custom Information");. [1 L7 J4 L$ @
        theLW->WriteLine(" ");
1 g% F* M) Q* d+ W0 e2 F, c. ^        sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() );
' g6 z+ v( b8 e+ R$ I        theLW->WriteLine(msg);) A( l+ Q. D+ l4 o% m' F5 I
        sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );
+ j9 }  C/ O: T" O6 U2 }- f0 T1 p' _        theLW->WriteLine(msg);
8 M0 E* {, l) K3 N8 U; \        std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();
$ o* j  W/ O/ e        sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );: W. q, l4 `" W
        theLW->WriteLine(msg);3 b. F' n4 F; _! s2 {- W+ D! X8 Y
        sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] );  x! k  |# k- x6 b
        theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );
% u8 b/ ]& O. t        theLW->WriteLine(msg);1 H# t1 @7 w6 U7 n
        theLW->WriteLine(" ");
8 K( f7 Y6 Q8 X        theLW->WriteLine("End Custom Information");# v: w. }, \! X
    }
7 u4 d! c8 b; t  a2 S* c    catch (NXException ex)8 Z% k. t* T6 V! N1 y5 K% b- |
    {
: L  _2 w- ^. I        ECHO("Caught exception: %s\n", ex.Message());
) I4 I7 J+ I2 A" _' M$ e    }
# c! @; _% k3 D$ [2 \! h: V    return 0;
) g# E" Q5 Q9 }; D}3 A  _  w5 u5 ?% ^

' H1 K) o3 _6 O, j//------------------------------------------------------------------------------
/ c+ I1 [, v+ ^  i% _! \// initUDO: _* j; M5 d) a9 a7 u
// Checks to see which (if any) of the application's static variables are+ A4 ?9 ^3 `, {
// uninitialized, and sets them accordingly.
) b# l6 g3 Y+ i3 g& A. b) ?+ y6 {// Initializes the UDO class and registers all of its callback methods.- v4 X3 @6 W* z$ A4 }+ Q
//------------------------------------------------------------------------------
  }! P7 a& ^  g* R- Q3 istatic int initUDO( bool alertUser)
( h& h, y7 h* }9 K* L, u{
- y) I1 K2 c5 W5 Q! y8 B3 K    try
. g9 T6 E! @+ A, L+ i0 o    {
" J8 A3 \: Z  f9 u* }/ ]        if (theSession == NULL)
0 p1 T& T% f' r! n2 k) |# [        {
) E# n9 _5 C4 i* p            theSession = Session::GetSession();0 |; W1 _$ M9 M
        }: T7 X: [* A) @$ z
        if( theUI == NULL )5 {2 Z. b- |3 C! I2 X- `  E
        {
: K6 b6 p, {4 z% K' t" h$ {7 Z/ N            theUI = UI::GetUI();
1 h/ F* Z* D4 s( h, k6 l9 d        }  [) k7 I. E% L* X
        if (myUDOclass == NULL)
. Z; Z/ J8 S. z4 d8 j( Q3 K        {; z# e6 r( C/ ^9 [# K8 P9 `' ^
            if (alertUser)
0 _3 ^! x" ]( n# T            {5 {; l+ n7 u/ X/ X1 V3 K$ b
                ListingWindow*% T/ c9 }1 x8 J5 {+ n: |
                    theLW = theSession->ListingWindow();. u* n: w$ |) o$ A
                theLW->Open();0 M) `7 f+ m/ c+ c
                theLW->WriteLine("Registering C++ UDO Class");) o. x4 l& m# F# X8 A; [
            }
7 ^/ a" s) x4 k7 I+ r& ^            // Define your custom UDO class
2 W3 g; y/ [9 |2 I! M1 C& \4 w) h            myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");+ X$ p' s- J4 i- o+ u
            // Setup properties on the custom UDO class, W5 v- v* n8 ?8 a) w+ X
            myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);6 H2 g& Q+ E8 R3 i' `) o* O% H" h
            // Register callbacks for the UDO class
! B* P3 u# a* Z9 m8 V# ?3 r            myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));
+ B- f# y0 o8 s9 L! j7 m            myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));8 h. H5 v% U4 W/ D/ A' R2 }" J
            myUDOclass->AddFitHandler(make_callback(&myDisplayCB));7 t( h! i/ b/ M3 n1 a; J, R
            myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));
' W+ y1 J/ k2 }7 a: o4 c            myUDOclass->AddEditHandler(make_callback(&myEditCB));
) o+ @! U% _  R5 z& P            myUDOclass->AddInformationHandler(make_callback(&myInfoCB));
$ ]- |8 T$ v# z" d7 F* a            // Add this class to the list of object types available for selection in NX.
, T3 c) Q( u5 M            // If you skip this step you won't be able to select UDO's of this class,
" w! Y7 H1 Q* e, Y* S/ o            // even though you registered a selection callback.* L$ b# y7 `% Y5 J2 L. E- X$ G
            theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);
6 _" n+ ~9 R* f3 |( Y        }
7 s9 K4 v: W$ c6 K5 D    }0 b% N5 r: V1 C& I
    catch (NXException ex)
# d! r6 e3 |/ v, B2 \% I- i    {
' S! V4 g  ^( r9 d        ECHO("Caught exception: %s\n", ex.Message());
) O. u) `2 C  D! _+ H    }. T" R  ?9 r# j/ N) x# u. s
    return 0;
0 x# _6 u0 f6 z: l& V0 G+ j2 J0 ?  r5 M}$ V# B5 c7 x& v$ S
! N6 w8 J; Y) Y2 G" W0 }
//------------------------------------------------------------------------------! H4 l, s3 e) [  ?
// ufusr (Explicit Activation)5 W4 z! i" N$ O& {+ L* j
// This entry point is used to activate the application explicitly, as in
, y! s& U$ @! y. Q& ]// "File->Execute UG/Open->NX Open..."4 R2 o; D6 d* I# c0 X& m
//------------------------------------------------------------------------------! I0 F/ O* D; Y8 ?/ C) y8 |
extern void ufusr( char *parm, int *returnCode, int rlen )
4 x2 z& b6 N2 g% V! }{
  X  W) N, @5 `& p- j. F    try
, O5 W2 w9 t0 {    {; N9 C6 m/ T" Q- s
        // required for calls to legacy UF routines
  u/ \1 L" O9 @, d        // such as UF_DISP_add_item_to_display
3 Q, V6 k8 Y4 J; s! T5 Y        UF_initialize();# S! i1 v7 i$ U! t

  I3 f: g# P- s7 q1 A7 W        // initialize the UDO - if we didn't load this library at
& @$ M- m6 h4 Y" e5 H4 V7 ^$ X# q        // startup, here is our second chance to load it
7 ?; R9 @  b  ]5 v$ ~/ B3 e        initUDO(true);
: v# M- w2 N! J2 T8 J8 W3 z
% Z- M& l! v, l3 |        // if we don't have any parts open create one
. o5 S) g; j4 q; y        BasePart* myBasePart = theSession->Parts()->BaseDisplay();
- x# L& c" m$ t4 l- j0 ^% B. E        if( myBasePart == NULL)8 s2 F. p( b$ P
        {
, |& ^% O8 i/ s5 R            myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);# O6 ^, u  d0 q8 y1 s5 y
        }3 B$ O# e% b& V+ V' U
2 p& U" T$ T; D' w9 d+ @+ ~7 J$ S
        View* myView = NULL;
# h, O4 Z( V7 v7 X- r* z% x        Point3d myCursor(0,0,0);
2 Y5 A7 W- e( C$ [( Q" s2 z2 z- f7 x/ R+ F
        // ask the user to select an origin for this UDO2 h+ X+ D5 P  L# ~9 q
        Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);$ z) L4 C% H7 `
        if( myResponse == Selection::DialogResponsePick )8 b2 x0 b$ R/ ~- G* q
        {
! p! n) l0 S. E  b9 {/ x            // The user selected a point - go ahead and create the udo( W& H9 }4 ?/ L+ w
            UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();
* O0 C( c- m: i            UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);" ^7 Z1 H6 F) \8 L. b+ E
            // set the color property of the udo - just for fun :)$ ?' `+ q/ p" R4 y0 K4 [2 L9 Z& {
            firstUDO->SetColor(36);
, k6 S, s7 l$ e1 S/ x$ u6 C7 \            // store the origin selected by the user with the udo
8 n  ^0 w" F1 u( Z7 e- q            std::vector<double> myUDOdoubles(3);
6 H0 {4 T2 {& M& h) l" G            myUDOdoubles[0] = myCursor.X;
3 `$ g. `; I1 ^$ w. B) f            myUDOdoubles[1] = myCursor.Y;
$ b0 {+ U+ P2 n5 t# `            myUDOdoubles[2] = myCursor.Z;
" y6 T& @7 F5 c+ n9 }% }            firstUDO->SetDoubles(myUDOdoubles);
8 P4 w# G1 i0 i: @            // add the udo to the display list manually8 X7 @. Q. V! g; G- f4 S6 J
            // this will force the udo to display immediately5 d- V$ M& S/ X* l
            UF_DISP_add_item_to_display(firstUDO->GetTag());
3 p# n  b+ {. A8 T5 Z, U        }. F2 n2 h- [* n* h
        UF_terminate();1 @9 }/ t& O: _! u3 j# {5 @  @
    }4 i1 Z9 j& C. o9 o, h( _' d" f$ |
    catch (const NXOpen::NXException& ex)
, `0 U+ O; n( o* s9 e    {, w* A- u3 g8 H
        ECHO("Caught exception: %s\n", ex.Message());8 I! S  k, \( A- [
    }
! X' z4 A8 a* D}9 I( h9 V4 m; h6 C7 {8 \. m) ~- w

2 a  D% r0 k9 k0 R1 y7 z9 B//------------------------------------------------------------------------------! e$ |" r; W7 ~
// ufsta
$ d0 Q  I# ]% ]: ~: e7 d: Y// Entrypoint used when program is loaded automatically" O- H4 I. Y- I% ?
// as NX starts up. Note this application must be placed in a7 L2 q, b8 d4 ]+ f, p: O2 L  w) U
// special folder for NX to find and load it during startup.
$ c' b2 o5 ~' x5 T// Refer to the NX Open documentation for more details on how
# X# }+ R$ r9 ]// NX finds and loads applications during startup.
7 |+ k" a# Y& Z" A//------------------------------------------------------------------------------* W0 C* k8 F" p
extern void ufsta( char *param, int *returnCode, int rlen )) S2 V1 f$ }5 W' W
{
2 a6 R% x; c3 B: E& q- B" ~    try
! D) \; U- K' w& t: A. K; w; O7 S, w    {  F. g+ m# S* Q& h# o
        initUDO(false);4 s, l  C: @  \: F( w: C8 h1 a: R
    }2 ], k, v2 {/ L0 w7 z( X
    catch (const NXOpen::NXException& ex)
5 f! d) ^# i: H2 c( C" h    {  u1 M" Q0 H+ ?" h
        ECHO("Caught exception: %s\n", ex.Message());% m; ?! o; \& ]8 _
    }
& h' v9 Q5 O9 C3 C}
) L8 `2 w2 X! g- }8 d" |  F5 f/ I: Q* m( W* L
//------------------------------------------------------------------------------
( g. H/ }# {+ B# D# V/ n" E, t. ?// ufusr_ask_unload
; y. S" @2 e; `1 w' I  Z: w4 V// Make sure you specify AtTermination for the unload option.' A' k, F8 m/ {0 e
// If you unload the library before the NX Session Terminates* C  D8 l1 C# s+ U0 M4 I
// bad things could happen when we try to execute a udo3 c% Q7 J( F2 s) j
// callback that no longer exists in the session.
; C$ ~& }8 x" g; S! m  j//------------------------------------------------------------------------------% H% s0 n/ B. P/ c) ~; z
extern int ufusr_ask_unload( void )/ s7 y6 ^% j' ^' n: r& C
{. `: s  y7 J( Y  X
    return (int)Session::LibraryUnloadOptionAtTermination;* O0 @$ O9 \2 ?1 X9 U
}
7 |( @3 W- k6 P5 l; O1 s  W- E, R. v
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了