|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
; c9 b* x# U" f, C$ z7 M& ^; J9 y% F/ d$ n" D3 X1 c1 [
NX二次开发源码分享:NXOpen C++创建UDO的过程% {& ~! c! n- G" j4 H
8 d+ @0 {0 e; K
8 [% X Q8 [6 _( ~- j9 tUDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!0 V7 q7 B8 u, ^9 K+ t$ s" `
* p; p) ]3 E$ u* U9 U- V
! X! D: C$ a7 f- B2 ^3 t# I3 [$ s4 N0 W2 p! u* y, N( W$ k" c, J' `: @. B
#include <stdio.h>
" d/ z4 j0 `" E% x#include <string.h>
# \! Z) y. S+ |& C/ d% ?0 D" E2 W#include <stdarg.h>( @. E8 | N5 N' _( ]) T# f( W7 @
#include <uf.h>
% V, M! [& ]" o7 i' y$ g% G6 B#include <uf_ui.h>* Y# W! y9 m Z" x9 l+ v
#include <uf_exit.h>
8 R$ s9 L9 E7 V#include <uf_disp.h>
N8 T& t5 S" Y; d( E5 p& D, D* z# H1 A6 L
static void ECHO(char *format, ...)1 V% B% W7 C2 y4 s. z
{
' k: X% s+ m* i char msg[UF_UI_MAX_STRING_LEN+1];" W# [& G. J% I0 y' L
va_list args;( F% o, [) m( k2 C
va_start(args, format);# W5 a; {, T5 P( ?' `2 P5 ]
vsprintf(msg, format, args);
' j) h5 `$ D, c5 }( C* \) m; y va_end(args);6 ~* U7 I7 o E$ S
UF_UI_open_listing_window();% a2 K) N7 |$ R- Z1 [. Y
UF_UI_write_listing_window(msg);
R- W: `( v% }7 ^6 w7 [' s UF_print_syslog(msg, FALSE);; O7 x% [7 _! _# B c# f( F
}
# G2 i! v* M7 d3 F. ^
0 u9 H( ]* r- s2 f#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))1 L7 N; p, M+ j1 F' \
% O9 s2 F4 W: ]# O8 z: U6 xstatic int report_error( char *file, int line, char *call, int irc)
' z8 |* k5 Q5 H% y{: b! R/ L- Y: E E& Q- x. S; I
if (irc)% v; }) f, \) O$ f" n% D2 l
{! X8 z6 K* f0 h) K# ]( i
char err[133];
$ b- U0 o. A/ @' c
7 J+ X, t- W0 t( @ UF_get_fail_message(irc, err);; W/ M4 C7 V: m* J3 E8 T
ECHO("*** ERROR code %d at line %d in %s:\n",3 _9 d8 L* [8 o9 S! C
irc, line, file);9 R' k C) L$ L. c/ C
ECHO("+++ %s\n", err);: K2 L7 c$ }6 L; W: U
ECHO("%s;\n", call);
" g1 L1 m+ f. P: \1 P8 q }
' p4 M0 e) t3 `; N9 e8 W7 C, a; X4 A9 ^# w
return(irc);
, K D1 Q" {/ a4 j: A7 g* ~}' B9 c) ?3 s X: w; |; m; w
" d+ `( ?- |( v6 S# |, L7 `
#include <NXOpen/Session.hxx>
" N, e& D! _# A! B. z( p- Y% K#include <NXOpen/Part.hxx>
/ i' o9 V2 U0 H, \7 Y2 d#include <NXOpen/ParTCollection.hxx>
- p5 `! [* D* ~+ K( W% K#include <NXOpen/Callback.hxx>
3 v9 J6 H d% Q- {4 }7 B' I* i5 g#include <NXOpen/NXException.hxx>9 y4 _$ _2 U4 X% S# x; w
#include <NXOpen/UI.hxx>7 S. x7 a9 ?% v9 h7 d5 h% I& J* Z6 j
#include <NXOpen/Selection.hxx>' B7 h+ O% \% t6 g
#include <NXOpen/LogFile.hxx>; i/ q8 b' i% J
#include <NXOpen/NXObjectManager.hxx>
- b6 F! j0 C# \1 [3 v#include <NXOpen/ListingWindow.hxx>
$ }, i! o4 @8 z$ T; ~8 x#include <NXOpen/View.hxx>6 x W* e( \/ h' y8 s: G {, p1 s8 g
, g9 t$ a) a. s, g, p
#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>
$ _4 f& u! G4 G( j#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>
5 t+ ?7 {2 D" A2 ]6 D% X9 O#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>
; M, K( ]. k y7 K#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>
X' p# p8 X) l) b2 I#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>0 O/ Y& P' p7 K4 g* V8 F; J
#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx>
! W! U/ A$ E9 E/ I$ g0 ~3 u#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>9 Z4 Z1 P3 x9 Y' [( e" Q2 y5 b
#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>5 r3 @* j% m2 X8 n
' A7 ?; b: P9 s( m
using namespace NXOpen;
) R2 @! M" `6 {+ D8 X3 Ausing namespace NXOpen::UserDefinedObjects;) n! e$ }7 `7 h% I8 [
7 |% n# b' c) r' w: q//static variables
7 Z! O) }( n4 ]static NXOpen::Session* theSession = NULL;$ V: N2 X% K$ v3 j6 d
static UI* theUI = NULL;
. g1 S- w+ ]8 mstatic UserDefinedClass* myUDOclass = NULL;
+ U3 j2 `. x5 C- ?7 r1 R! a# [7 b5 g8 C& e0 V2 L0 z
//------------------------------------------------------------------------------
2 Z/ [, W w4 Z7 B$ s// Callback Name: myDisplayCB
: z! R/ f7 m, b2 b// This is a callback method associated with displaying a UDO.% P& P5 w, ?4 K( y
// This same callback is registered for display, select, fit, and attention point
0 Z! w4 \, D! y! d( E//------------------------------------------------------------------------------
# r v# f% ~6 L0 i; F' Iextern int myDisplayCB(UserDefinedDisplayEvent* displayEvent)' ?6 c# ^3 A* d( w. A5 A7 V
{& x; M- @, W4 j0 D+ A
try
5 n8 Y+ H3 ? f {
; O3 ] e6 Y- m+ E6 E8 g: z // Get the doubles used to define the selected screen position for this UDO.
' f! L/ o8 B% B1 [# B std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();
. ?/ t: S& P: I" E5 x( v2 {+ X) d" f! y( `) [# U% i: _4 V0 e* V0 i
// Use the doubles to define points of a triangle# ]% z% c) u' O
std::vector<Point3d> myPoints(4);
7 K) _+ q0 |2 ?# d- C' c: e( v! h8 @, f) h2 ^5 b& Z
myPoints[0].X = myUDOdoubles[0] + 0;* ~0 V) M- o& `" V+ L" Y9 w
myPoints[0].Y = myUDOdoubles[1] + 0;
8 e2 T3 u. A2 M4 [ myPoints[0].Z = myUDOdoubles[2] + 0;( j9 B$ L' I6 W- | V
8 r- H1 `8 i, ?. Q: O myPoints[1].X = myUDOdoubles[0] + 100;9 ~) G8 R. A1 x3 U/ S4 l. h
myPoints[1].Y = myUDOdoubles[1] + 0;) M! S0 F2 s8 W( ^7 `* k
myPoints[1].Z = myUDOdoubles[2] + 0;7 t* f: `7 @! x6 z( j& ^, K
2 X) J5 e+ F6 B6 A: h myPoints[2].X = myUDOdoubles[0] + 0;+ g6 c2 g; L2 r t9 K
myPoints[2].Y = myUDOdoubles[1] + 100;
9 ]4 I4 m; Z. p1 C/ {% b! b3 U/ d0 |. [2 V myPoints[2].Z = myUDOdoubles[2] + 0;, w/ t" }# v- l' ~8 ^5 N' d$ y* J
3 i% z& v" s; t4 L5 ^- R7 K# O9 F% T
myPoints[3].X = myUDOdoubles[0] + 0;! G0 j6 @2 l$ t, g& l9 G4 P
myPoints[3].Y = myUDOdoubles[1] + 0;& u# } @( D N7 l* I: @9 N
myPoints[3].Z = myUDOdoubles[2] + 0;
( n" i2 k5 X9 |. z# q" x" \) F8 T$ ~8 N% j) ^' W5 k' c8 N4 R
// Display the triangle
* l, h" c# h3 P; [1 k6 u$ B4 a5 e displayEvent->DisplayContext()->DisplayPolyline(myPoints);- |5 i) v8 K: q: {4 j
( M% T. k/ j# P5 l" F% m
// Display the text next to the triangle
4 M4 D: y$ {% ^5 O# ~) Y" a. A Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);4 H$ K/ q" ~" o7 |
displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);
, b( W# p8 c5 G% ], F0 R/ B" Q0 z
+ Q9 J+ l/ L1 I3 a0 d // UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 6731653. Q7 _, s5 Q9 h4 ^: j
// This demonstrates using UF_DISP_display_facets as a work around
: \' S7 g4 g" d- E5 f+ y0 L double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };
8 l6 D2 ]5 w% Y7 P double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],1 \" W/ d9 X* Q, n- u8 [( q3 G
myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]," J5 B: h7 @+ Z% p3 O; p# }5 k
myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };
9 L; y9 f6 T } UF_DISP_facet_t facets[1] = { vertices, normals };
! R. u g9 {* R5 s void *context = displayEvent->DisplayContext()->GetHandle();/ K" @4 c2 w( ~1 q0 b+ y# Y
5 c) I) @% K( }; h UF_initialize();7 H# v9 w+ O; E5 w" ~9 u8 P9 P6 M
UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));
7 D% S1 k/ {% n- j4 v UF_terminate();
" E8 L1 S) `8 u+ x- A3 m' c* U. ~+ [5 j7 v- F( ]- J( I. N
}
% T8 m6 F0 @: U" _$ \ catch (NXException ex)0 M1 x4 o5 ?( A5 e( q! g' i
{6 s. I1 F) \ R% h7 O: u
ECHO("CaUGht exception: %s\n", ex.Message());7 @/ Q, u+ n+ M
}
& _/ _: Q1 c9 D) e" e return 0;8 H4 S: J0 N7 T/ b; M
}0 W3 E) U) X; I/ g% m
//------------------------------------------------------------------------------) \# i* S; Q( ?& Z7 z3 m: v
// Callback Name: myEditCB% }; q0 z5 _0 t
// This is a callback method associated with editing a UDO.0 S" D0 s9 Q l0 h7 Z' V' ]
//------------------------------------------------------------------------------
# j1 x$ O m7 b; @6 W Textern int myEditCB(UserDefinedEvent* editEvent); }" Q+ G7 g' u* {% Y) o
{
+ O6 C R. T' M6 Q( E try
% c: @7 n0 K3 w7 n& [ {: |+ U9 k9 [6 G1 w0 M: Y
// required for calls to legacy UF routines
4 {$ |& ~% r: y( t% _8 H1 a$ I, A // such as UF_DISP_add_item_to_display
% Z, l/ V- w! e! v& Q UF_initialize();
; y0 E @ ?) Q' m& t5 b6 P; V" X6 t0 P: C/ |4 T/ S
View* myView = NULL;
* F& ~, W+ d, T: B- p/ F% P- s Point3d myCursor(0,0,0);
* q3 J4 B* j/ s
& F( c( M6 a4 G // highlight the current udo we are about to edit
2 n7 R9 ?1 X' Q- U4 R // this is helpful if multiple udo's were on the selection D$ ?# F1 V5 u/ G I% M6 E, V
// list when the user decided to edit them
7 o# [- t) ?8 j editEvent->UserDefinedObject()->Highlight();
- i4 i! O( X9 v! M0 E4 a+ i( G) W# K, N% H( W$ Y4 U5 w
// ask the user to select a new origin for this UDO: C% a7 E; E2 G$ D2 j
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);- g0 S& }8 W; x
// we are done asking the user for input... unhighlight the udo5 g/ c) x& T) Q! l0 |8 q& G
editEvent->UserDefinedObject()->Unhighlight();# m7 X% V# k; ^. W6 c: K0 [
) P& s) q7 b- w9 Z // use the new screen position (if the user picked one), c5 x+ J( Y' l% w) q
if( myResponse == Selection::DialogResponsePick )9 r9 f9 M- l* A+ R- q4 b
{6 H0 @$ J+ R! K
std::vector<double> myUDOdoubles(3);% W1 o ~4 z" @: g$ O: H, \/ |
myUDOdoubles[0] = myCursor.X;) q! B5 E* N' v L
myUDOdoubles[1] = myCursor.Y;
& S! s, Q$ u' n3 r5 f myUDOdoubles[2] = myCursor.Z;& g7 Q+ `& \* V" d. K! c
// store the newly selected origin with the udo$ T; c! n. } \
editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);
, p# u. g9 K4 O$ @4 C- h- E // add the udo to the display list manually
$ G6 `0 Y6 a1 W' z9 C" A+ m. t d/ F // this will force the udo display to regenerate3 f; M3 e1 _$ R7 k: k
// immediately and show the changes we just made0 o; q* Z, ^8 i7 j, Q* K
UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());
# S. {& B4 l. J9 \' F( V/ K3 } }% \; V" \% I( y: X9 D3 F* h( U
UF_terminate();
- l3 [ j& J" m! X! x. x2 d$ Y( x } catch (NXException ex)
/ \6 K7 w9 z0 }2 ] {
: _9 B; F. m+ U2 r, J- E ECHO("Caught exception: %s\n", ex.Message());
1 o u# s4 t) m9 N } return 0;( s; v4 \7 b/ L4 G2 G: x
}
5 {. B! {2 \" d5 W" @) l, h, y//------------------------------------------------------------------------------
6 e, F: p- ?' I" p, ^0 W1 ]% _// Callback Name: myInfoCB
, P& J3 Y' K' w: N0 E. I// This is a callback method associated with querying information for a UDO.
# G0 P. L! z1 q2 a// The information is printed in the listing window.* ?. M; r9 k% W8 B
//------------------------------------------------------------------------------3 G" R; S9 j p* y" F# t
extern int myInfoCB(UserDefinedEvent* infoEvent)
3 X: b- \ g8 V% e{3 S7 U2 O: g% b
try
5 C9 j9 v+ W* b {! y/ W4 ~2 R3 E1 o$ Q* w0 P
ListingWindow* theLW = theSession->ListingWindow();, L. C3 {. S: B+ W3 x' |( }) G
char msg[256];
7 }5 a1 G: o: \ theLW->Open();
! N) }6 U: w, [2 k1 J theLW->WriteLine(" ");
# ~# ~9 N( ~9 |8 S4 | theLW->WriteLine("------------------------------------------------------------");
* r3 c& b( z. n' V/ H theLW->WriteLine("Begin Custom Information");
: `8 Y+ d& O5 P$ m theLW->WriteLine(" ");
: {5 ?8 Z6 ^: J5 ~. ~! W m2 G- T sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() );# b* M& ~1 v7 D
theLW->WriteLine(msg);8 F5 T9 w$ F1 \
sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );
, K3 W8 e3 D! x8 A" Q- r theLW->WriteLine(msg);; j4 }( t- N/ b8 h" ?
std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();/ P; U! g7 A5 h3 Z
sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );+ l( A2 ~; s3 L$ X$ w1 B3 Z
theLW->WriteLine(msg);
1 k, z" D" D9 W% a# ~9 E5 g' V! {4 T sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] );
5 |2 j" O$ ~( o( ?' m3 _- k theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );0 ~- U0 V+ \. }4 T | W
theLW->WriteLine(msg);0 C9 [6 y2 _3 r! A1 m) H
theLW->WriteLine(" ");
# z. k- F( i0 [9 g theLW->WriteLine("End Custom Information");
* M- F: v3 b+ h6 c. ~( N }) U" c* l4 L4 U+ ?. K @) p$ V/ _
catch (NXException ex)
+ l7 j% p- G9 H$ R7 w+ z6 P {& ^! m% {% E* f) ^4 c' `
ECHO("Caught exception: %s\n", ex.Message());
# s& F/ _: D5 n8 n S0 @ }
# H/ w9 p6 F3 ^ return 0;
/ ?9 ]" Q/ }, q# X Z: f, j( l}0 N g: }# g1 @* j% V( M0 \
! A) K( \9 [2 s; K% J, ~. J//------------------------------------------------------------------------------
+ m6 u6 x/ ?# Q' P6 o// initUDO Q7 m+ Q6 `; x6 R( S9 g
// Checks to see which (if any) of the application's static variables are. s0 W$ X* v# {, t' \0 j, H
// uninitialized, and sets them accordingly.
8 O, x1 e; a7 q8 N( d: v// Initializes the UDO class and registers all of its callback methods.) W5 Q8 q6 s y& B5 P
//------------------------------------------------------------------------------
) \( G+ P* c, q8 ^. x' G# S: lstatic int initUDO( bool alertUser)
' H1 }( ^- o5 j/ m; a2 ]{
7 j' u6 H; P, O! X try0 ]+ A }; ?& ^& a6 q: f2 r
{
/ E$ e" h* P- @4 k, B if (theSession == NULL)
8 L4 j+ U( _# G {
! D) {6 L* Z& s/ r, ^% l theSession = Session::GetSession();" ]: e. h0 m2 l1 U, u
}
8 i, T8 E: F# ?# p& k1 m if( theUI == NULL )4 ]: ]- u* h. R g
{7 Z: O5 s$ X& M
theUI = UI::GetUI();
# v$ |. g" @$ j- h; a6 v }
; s; |3 a2 O) c" q9 Y: L' o$ Q if (myUDOclass == NULL)
; u# P2 M$ O3 [9 I1 z0 f" B. g { d& ^/ C J. q) _, u6 ]! ^. J& E
if (alertUser); g5 t1 K% w" d$ j! D0 ?6 P% d
{' ~, e' r5 K* p5 q5 b* O' f
ListingWindow*
0 q, s u. I% G) e, y theLW = theSession->ListingWindow(); D! Y) J, t# G/ R9 Z0 D" P
theLW->Open();
% c2 I) k9 L2 W6 h5 M theLW->WriteLine("Registering C++ UDO Class");) X0 l, m& I! G1 L% ]
}
9 B8 a) Z; c1 B3 ] // Define your custom UDO class/ Y; F2 J' a2 F6 h& \- c
myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");' V5 K" _9 O* a& E& m- ?
// Setup properties on the custom UDO class
' G9 r" A8 f1 f0 Q4 g8 j6 e3 g myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);
& ?7 a# }% E8 L8 ?8 e8 @# F$ w0 o // Register callbacks for the UDO class0 Z1 j n$ n" n. ?
myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));
( F5 N g9 L8 U: }5 ~! x myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));8 w: L. A0 P8 X* E/ I& m$ A: S
myUDOclass->AddFitHandler(make_callback(&myDisplayCB));+ T4 t- ?( ?( W+ v6 u" L1 o' K
myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));
- Z8 `0 U/ u4 Y4 i5 j; W! ` myUDOclass->AddEditHandler(make_callback(&myEditCB));
* H; t7 n0 ^, m. c! N myUDOclass->AddInformationHandler(make_callback(&myInfoCB));
7 q7 X! ^1 R# l% \ // Add this class to the list of object types available for selection in NX.
7 _" v' J2 @$ w) Q6 r4 E7 v3 d // If you skip this step you won't be able to select UDO's of this class," ?9 B* Q K% D+ x! m$ D: {
// even though you registered a selection callback.
- v# Q8 B( Y; b7 `4 N theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);# r: M% M8 U5 R
}* n5 c& N9 G2 o5 u4 V
}% b7 \6 \, x1 |5 ^. V
catch (NXException ex)4 `' ~( d! |1 Q
{# O+ J. m4 O2 n0 m( Q r
ECHO("Caught exception: %s\n", ex.Message());
& D- ?3 ^+ [4 B }
* |* w& M+ P3 i) V& \* l: b) y v return 0;* h3 b$ l4 V4 d/ _
}5 q" R0 _5 V9 ?: v
+ I: _2 P I( s" g+ V9 }8 r; ^' w//------------------------------------------------------------------------------
" ^+ ~, \) g: K' F// ufusr (Explicit Activation)" z H7 S7 ]1 Q' |
// This entry point is used to activate the application explicitly, as in' ^3 s3 D& ^, S. d J- e% @
// "File->Execute UG/Open->NX Open...") q$ W1 r% @; I, w3 e2 y, @
//------------------------------------------------------------------------------8 _8 t0 A/ E i' k/ `
extern void ufusr( char *parm, int *returnCode, int rlen )
6 N V3 ^$ j/ I6 ~{
: x$ f6 s2 k; t5 M& I! x. } try
1 J! Z3 |4 l% G2 m( W, U& j T% _ {
& Y) i+ u+ J7 c+ K" V // required for calls to legacy UF routines# ^. O* Y; \& W$ m3 `" h8 W, m
// such as UF_DISP_add_item_to_display
H1 E6 O! R/ [# O% y- R& F UF_initialize();
; v) c5 y# O5 y# u: R7 E+ T( n I. q6 `8 q4 x5 D
// initialize the UDO - if we didn't load this library at
+ q; F: |) Y) b( H // startup, here is our second chance to load it
% j& I0 Y/ j+ ]; \! i) ~2 e initUDO(true);" s6 O0 `' r2 E' ]
. T6 B3 p: i5 i7 S u# P' g( j // if we don't have any parts open create one
) r0 W0 z: Z$ g' G0 L BasePart* myBasePart = theSession->Parts()->BaseDisplay();* r" s5 `* r0 H
if( myBasePart == NULL)" H; t. H5 O3 z: \
{2 B% D; H+ ]* H9 O% e# c
myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);1 ^6 }5 r9 a. ]% o! a& H7 B
}
7 G3 I7 u0 ~0 M% f3 N8 J, v8 i! s
- H# u& \3 [* a# X: T View* myView = NULL;
5 p! n3 M8 g5 a! J$ d3 A& o Point3d myCursor(0,0,0);) C: [1 }3 {/ N* j, d
$ e+ m2 V. t. m
// ask the user to select an origin for this UDO& L' c8 d6 f+ f/ Z
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);
$ Q2 z+ [4 n' S6 z if( myResponse == Selection::DialogResponsePick )7 ^2 ^; y2 c+ X7 ^2 E: g) G
{' w& A0 y( o/ g `+ @; H6 {
// The user selected a point - go ahead and create the udo) v, S0 k0 P' P6 S7 c" K
UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();
& w) \+ N7 V7 [; Y' [# g" ^/ R- | UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);/ l1 S- F6 M& B# m" [& x1 R9 B
// set the color property of the udo - just for fun :)0 Z- i! E" t/ x C; z$ u! q2 R
firstUDO->SetColor(36);# {' y5 Q' z5 }$ N: l( f: e
// store the origin selected by the user with the udo) n0 \% o: T" ~* ^- I' ~( N
std::vector<double> myUDOdoubles(3);
0 V! z; h! t8 q: _3 h# e myUDOdoubles[0] = myCursor.X;
5 t8 a- d4 R) [) m4 Z myUDOdoubles[1] = myCursor.Y;. r% b/ C4 p/ H
myUDOdoubles[2] = myCursor.Z;3 P) _; ]) T8 l8 n4 F& l; x
firstUDO->SetDoubles(myUDOdoubles);; \' B3 ~( G7 s0 X% t- a1 g
// add the udo to the display list manually6 u9 K" j$ ^- \6 G% t# p1 C
// this will force the udo to display immediately
7 }% M" F3 _* V9 I1 A% d UF_DISP_add_item_to_display(firstUDO->GetTag());
9 y: h+ Z. Q# b* D }) m# j" v+ w9 q9 N+ _+ n8 t# {
UF_terminate();% F3 t6 z' ~0 Y4 ~* m: q' {2 |
}( V. r) `) a7 U" t& B
catch (const NXOpen::NXException& ex)4 `2 { X( d* W6 G+ x5 F: V
{
' i" I1 i s5 C0 _2 c, N ECHO("Caught exception: %s\n", ex.Message());6 H( f. ` d% Y
}
* @- P' H, N+ D6 T) s}
6 A/ g. [/ c, D: K/ y5 v. r: ~- D) ^$ U8 A) H
//------------------------------------------------------------------------------9 Y- y: u4 ^+ q8 |( N! L
// ufsta
0 Y* a! S. j; O1 x// Entrypoint used when program is loaded automatically1 j; d i* B/ m5 _* d9 P
// as NX starts up. Note this application must be placed in a
6 t$ c* Y t) I6 j// special folder for NX to find and load it during startup.6 I3 Z% ?" y# j6 k5 J
// Refer to the NX Open documentation for more details on how0 t# G j/ g( G+ w6 G
// NX finds and loads applications during startup.# H Q( b6 K& o A$ Z4 |( O8 w
//------------------------------------------------------------------------------0 M" I0 S" x! }! t) a6 g0 x
extern void ufsta( char *param, int *returnCode, int rlen )
) q) j% c8 `4 f4 @# m+ l{9 B- z ^4 j' e3 A
try1 g. l9 v3 g s, C$ C( N
{9 h9 l7 A% N: m f% n, q2 D: m' i9 ]
initUDO(false);* Q- l0 X2 ]; d
}/ R$ p$ M7 {9 s2 n7 C' M j, O
catch (const NXOpen::NXException& ex)
8 [8 p1 D4 k- ]* N! L5 I E4 U: u {6 d* n# w% d; e$ ~ c7 j- Q, B
ECHO("Caught exception: %s\n", ex.Message());9 s, K, u) K- k6 f) g$ v
}
" R% ?6 x% V1 s" M/ j}
) k/ ~# j3 h1 Z3 s6 A6 U! g6 t9 ~% w4 `% G" @9 R
//------------------------------------------------------------------------------
( M5 v, R7 ?1 ~- j: x// ufusr_ask_unload
, E% \8 b) m) B& n) @* [5 _' G! c// Make sure you specify AtTermination for the unload option.
8 f! t$ t" L$ M1 Z- e// If you unload the library before the NX Session Terminates
& h7 x2 e5 J" [8 R! l" @3 j! ?// bad things could happen when we try to execute a udo- c9 P$ c9 E5 z1 d1 a9 ?' @
// callback that no longer exists in the session. H& F4 t+ E1 U8 Y0 `4 c0 y' B! G
//------------------------------------------------------------------------------
5 Y, _& L3 Y R3 bextern int ufusr_ask_unload( void )
6 O8 D0 C$ F, I$ o" k$ I& A) A{
( ~! q6 C3 t& s |; r) l return (int)Session::LibraryUnloadOptionAtTermination;
' Y. Q2 R5 V: H! ~}
L# l; m5 u8 L; \) r! w/ C+ s C
1 l$ ~ x/ D2 T |
|