|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
6 V4 j2 P4 t! \6 H1 t1 t
4 M# s. X# O8 E8 MNX二次开发源码分享:NXOpen C++创建UDO的过程) A' J) \) j0 H( s4 I; Q. a
8 i/ n8 V! X2 \, C# u9 U; Y# Y {9 l, Q3 V/ y& U* f- N$ G5 w: S
UDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!/ E4 ^) O" e% L7 x3 r, w* X
. u8 J: f6 e! b% _+ E2 r: ?
) @ H" D, S# v$ h+ I$ j! J! ?
! v1 p7 @1 I* z& Y4 J$ r#include <stdio.h>1 P, L8 B) S, @: }) _" }
#include <string.h>$ m% \. r* m3 A" I/ C) n
#include <stdarg.h>
& F4 `% I/ p# L#include <uf.h>' O6 C4 L! X* x) l4 }8 H" h
#include <uf_ui.h>
1 D# N, C- K5 G4 B#include <uf_exit.h>
N8 v1 n- I& F2 t) p K#include <uf_disp.h>2 X$ M. S' h/ i* Y4 _6 J9 b
7 |( n" D4 [- e0 _
static void ECHO(char *format, ...)( k' `" T! l/ @" |* }
{4 c+ I& n* _1 g1 Z: w* c: p4 Q
char msg[UF_UI_MAX_STRING_LEN+1];
9 m: ]2 W. Q# o/ q5 @) ]; y' w va_list args;
# ]# N: T+ @. ?2 L/ _ va_start(args, format);
, `' _" c, ~& n vsprintf(msg, format, args);9 v3 _4 Y9 s. W5 l1 Z
va_end(args);
9 o0 W% K- j3 d b' A UF_UI_open_listing_window();
n' C* D# h' d$ E* ?0 Z0 _7 o UF_UI_write_listing_window(msg);
% E8 L `! Z& ^! K7 g3 c% r& h UF_print_syslog(msg, FALSE);9 k0 N7 C8 w4 K( | Z
}
1 ^' _; f4 g$ M9 a8 }8 T2 k% P4 c6 Z1 M! P" d( J R
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
7 ~/ E5 H/ F8 N& B: y7 t: j# D, U/ w5 H' e5 ~
static int report_error( char *file, int line, char *call, int irc): ^' h4 U: B' e
{3 w3 g; i. N5 T% z1 t1 b8 B1 `
if (irc)
4 E5 R" |& o1 a6 E, X {* o3 V- [* ~! r2 M$ h8 J8 n
char err[133];; k, l6 _" X3 D! ]" g
& ~& d' _0 r6 g6 \3 G C$ {' A3 u+ V UF_get_fail_message(irc, err);/ o! R- [; H% H _6 Z1 Q6 W
ECHO("*** ERROR code %d at line %d in %s:\n",- \" Q3 F+ ~- E* t
irc, line, file);0 ^9 r5 E) N$ O+ D& |
ECHO("+++ %s\n", err);
: q G/ e, R; h; l# c) B- r ECHO("%s;\n", call);5 Y2 W4 |# B2 T, c4 o, v3 d
}$ {, o8 u7 |/ }5 k
8 d7 {$ {; W6 i
return(irc);+ q* ~2 A3 }! d/ A6 V
}1 j$ {" ?) O) x) x2 H$ u- e% Y
8 p/ z1 i6 i6 T& ?#include <NXOpen/Session.hxx> 5 Q- {; U. b9 T) N
#include <NXOpen/Part.hxx>
) g3 p/ M9 s7 u. G#include <NXOpen/ParTCollection.hxx> ) w3 p9 m/ D' h5 Y
#include <NXOpen/Callback.hxx>" i& R/ l6 ^$ R8 |' p+ {
#include <NXOpen/NXException.hxx>% O8 e8 n0 Y% [( _! J) m* W
#include <NXOpen/UI.hxx>) s: `+ H& v: H, Y1 r. B
#include <NXOpen/Selection.hxx>) |" I+ b4 n! ]
#include <NXOpen/LogFile.hxx>
9 N7 c2 x6 V, I% j3 I#include <NXOpen/NXObjectManager.hxx>
; s6 n2 F e5 k H9 c#include <NXOpen/ListingWindow.hxx>
3 I2 h3 ]4 }1 i. k1 v' Z- d#include <NXOpen/View.hxx>
! V& Z* j8 Z2 W# t
8 C+ C. ], U# p" w. I$ W#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>
8 n5 n% b Z' A! l7 o3 ]#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>7 Z. i8 l4 c9 q0 t
#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>( {+ O& K# S; _+ ]4 {3 P' E
#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>
+ C5 ]0 |; l7 y/ ?- t5 D#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>5 E# S- E7 n% D3 U
#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx>
$ b/ t3 Q2 d; J& `+ |6 g#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>8 E$ y1 e' ]% R( ]
#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>" X4 C! ~( p+ W: P! R9 y8 z
: ?- ]1 I5 ?0 C: jusing namespace NXOpen;
$ t- S8 @* I' Jusing namespace NXOpen::UserDefinedObjects;
; ~) y' A7 b# x! t2 F5 g9 `- A0 s4 O
//static variables; U7 k/ S' G3 s0 L5 S" e! V0 a" ^ B
static NXOpen::Session* theSession = NULL;
% F& i. s6 n. J+ Vstatic UI* theUI = NULL;
6 d: ]6 x% @* Ustatic UserDefinedClass* myUDOclass = NULL;
5 j0 P8 e# C% d% d& O! d! S
$ A0 Y; q6 S x: G0 {//------------------------------------------------------------------------------) U3 P: T% e1 r2 s& A
// Callback Name: myDisplayCB* ~0 C& C7 H' W9 X: `& Q7 I
// This is a callback method associated with displaying a UDO.! q( S, Q5 W6 a \
// This same callback is registered for display, select, fit, and attention point' o) Q7 M/ T* I
//------------------------------------------------------------------------------) }5 S; U" z2 {" V I& f
extern int myDisplayCB(UserDefinedDisplayEvent* displayEvent)
) s. e) T! [: o: M: Y; x2 Y{
2 X$ e( C( p, G5 U. T% L, V try0 ~/ r7 f- P- C, O, W
{" {4 U* G4 x* P/ [6 f1 Q+ Z
// Get the doubles used to define the selected screen position for this UDO.
) Y( S% a) V+ V% l& ] W4 Z std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();! u- x/ x1 U0 J5 i' z' b
8 l9 L, I6 B3 e/ Y // Use the doubles to define points of a triangle
$ A% j5 z6 P" a4 b std::vector<Point3d> myPoints(4);
$ f) r6 v) R h2 ^; w
# v/ E9 e0 o/ a: C9 P( \ myPoints[0].X = myUDOdoubles[0] + 0;
; r: M" l w+ e1 W myPoints[0].Y = myUDOdoubles[1] + 0;
) @- @- C$ B3 Y$ i( b( y myPoints[0].Z = myUDOdoubles[2] + 0;9 ~% F7 c) ?, r
2 Z- P2 L7 _4 \5 z( A! d( B" b; X
myPoints[1].X = myUDOdoubles[0] + 100;
1 E% |0 W/ C, Q/ A+ N myPoints[1].Y = myUDOdoubles[1] + 0;
: d! _" ^ x* Q5 ?8 x myPoints[1].Z = myUDOdoubles[2] + 0;# {1 L$ a/ [6 u, G
2 Z+ I2 i" ~' q. W: o3 k2 Q myPoints[2].X = myUDOdoubles[0] + 0;! S! ]. m$ B7 Y9 U$ A, A
myPoints[2].Y = myUDOdoubles[1] + 100;
* l$ l" G/ g Y9 A) ?: B% K. C myPoints[2].Z = myUDOdoubles[2] + 0;
. Z& J2 \/ q" D# C3 N/ l( p/ e; D0 z' |) F! J& h# u
myPoints[3].X = myUDOdoubles[0] + 0;" G3 M* g9 N3 f# Y' I# j
myPoints[3].Y = myUDOdoubles[1] + 0;
7 O# X% \$ {2 r1 I( T9 C3 M myPoints[3].Z = myUDOdoubles[2] + 0;
' q6 [9 P5 N+ M4 |8 w) J( {8 P9 k& E1 s* t
// Display the triangle3 W3 L2 c" u ]( n0 B6 |( {
displayEvent->DisplayContext()->DisplayPolyline(myPoints);
6 {* o j. z% x; X0 G* j5 B* h- M& `8 W$ U5 w, h/ c
// Display the text next to the triangle
& @6 Y+ y1 z+ E& m+ ^ Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);3 h; M4 q7 B- k% h& b( |
displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);
& e0 I+ o3 _/ A, c$ S" i7 x/ K1 S$ ]: B; L& K! b
// UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 67316531 v" p! ~- {7 B$ F1 _9 F8 q! Z: |
// This demonstrates using UF_DISP_display_facets as a work around) v; e t: r& A! P& k
double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };/ a: e/ w# N8 q) ~2 z- q5 t# c8 b' D4 e) Q
double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],1 f/ b. l& P2 S+ v/ K. I
myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2],; l5 E9 n1 @" E
myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };
' A8 B: V# N% R/ I4 E/ ^ UF_DISP_facet_t facets[1] = { vertices, normals };$ ]0 \; i! `& B
void *context = displayEvent->DisplayContext()->GetHandle();$ K( _& V; k U0 I) M
5 }. ?7 g1 {/ J/ F: r! r& j C UF_initialize();2 {" L: Q ^( d& T' ]) N
UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));8 _4 O V: x. Z. q: i/ [4 P* d
UF_terminate();" ], K1 f% r2 C# p4 T$ `
5 c/ M6 }1 ?$ ? B& J
}" F) x7 f- x8 J6 T9 v6 m/ S8 L# B
catch (NXException ex)7 {% f% l1 W' M( ?- c/ f. S4 w
{9 W8 `8 r! q; [% w9 P3 B: k
ECHO("CaUGht exception: %s\n", ex.Message());4 _, h& m2 E4 P2 h7 T0 }
}
0 j6 f' s+ }4 l return 0;
" y8 ^3 {( N, y4 `" w& s}3 R; ^, P+ x! s0 e7 v. k, V. H
//------------------------------------------------------------------------------
9 E c$ j* [2 j8 ? j// Callback Name: myEditCB
( }/ M- G5 P' Z# b+ K# u L6 g// This is a callback method associated with editing a UDO.
. @4 o) J7 M+ k$ x! D6 p//------------------------------------------------------------------------------
6 T2 u% V6 N$ i% C0 Xextern int myEditCB(UserDefinedEvent* editEvent)0 f y8 D4 s& |5 Y2 B& k
{
1 Z- V1 _+ H, S try! b2 z6 p6 d5 B) @2 u% D D- S' j
{
% T, k& P- ~6 P: i) f1 l // required for calls to legacy UF routines
$ d4 R4 l$ U- y0 N/ ? // such as UF_DISP_add_item_to_display: Y. w& X q4 z
UF_initialize();! W2 W4 M4 i) z# v3 y/ q
- d0 ^+ ]0 G7 O3 N. k View* myView = NULL;
/ D; ^3 L$ Q9 M& c! m) `2 Y Point3d myCursor(0,0,0);
7 l$ n5 x7 N- P4 O9 T, x* L0 H5 N1 o- ]' y2 K
// highlight the current udo we are about to edit
' l% F" z! o9 T, ] // this is helpful if multiple udo's were on the selection
# }6 }+ i/ r1 D) D* @) ?) c' C // list when the user decided to edit them/ X& ?: g# X7 C. n A3 c. [0 w3 q
editEvent->UserDefinedObject()->Highlight();
( A( o. n( Q6 w$ U: Z- u9 ~& w; ^1 q. ~/ z J
// ask the user to select a new origin for this UDO
2 V: L X+ s1 ^3 K Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);
5 y4 r8 d3 M2 g // we are done asking the user for input... unhighlight the udo ]. c1 K- E1 s
editEvent->UserDefinedObject()->Unhighlight();" s6 T8 s7 w# s& n' J( K( _$ X
' ^/ n4 v: b4 A, O0 y$ E
// use the new screen position (if the user picked one)
2 l# ^6 T" d& B! d! v% m if( myResponse == Selection::DialogResponsePick )
& K: Y2 d/ L( z, g& B {, T; M8 }( r, G* M7 o* r& u
std::vector<double> myUDOdoubles(3);
: Q) Q7 b! O8 I. q4 W: _/ t' @ myUDOdoubles[0] = myCursor.X;) t) v. k$ E3 b J. V2 w
myUDOdoubles[1] = myCursor.Y;8 M3 _. a+ X2 X6 r- ?- U
myUDOdoubles[2] = myCursor.Z;
$ a) h' ^: _: R( {; J // store the newly selected origin with the udo0 f0 N+ O6 H1 i& h7 a
editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);6 e! {' \9 f" N6 {' u
// add the udo to the display list manually0 }# R( ?8 _! C4 r, h) b
// this will force the udo display to regenerate
1 A+ B* z4 h. y% e% o. X3 s z // immediately and show the changes we just made
" P; L2 m2 T. u; B$ c UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());
7 ]4 [! F( Q3 i }
Q8 L5 n! P6 N9 r a7 p; h UF_terminate();
3 ]+ G0 R; i: @: _ } catch (NXException ex)4 Y/ x" F" W2 G- A
{1 M5 P# w5 S* O) E1 Q) x
ECHO("Caught exception: %s\n", ex.Message());8 Q" q+ U* g5 c) y
} return 0;
! q( b9 I: l. Q0 C& G, Z}
$ E. B% ]$ e% F9 o; l- @//------------------------------------------------------------------------------& E; A+ _2 x: d+ h3 _# ~
// Callback Name: myInfoCB. P2 i1 p% V U
// This is a callback method associated with querying information for a UDO.
# j9 h& Z; P8 L; T9 Y( m// The information is printed in the listing window.* U- N; X0 L2 H9 \$ K4 a+ F/ G
//------------------------------------------------------------------------------
f9 @6 ?, O6 U" h+ Sextern int myInfoCB(UserDefinedEvent* infoEvent)- F/ X/ B) y$ s3 n
{
4 K+ ^' S1 y Z: R try
' s% e: G1 _& p {5 g/ _; K! r7 ]) n! n* W
ListingWindow* theLW = theSession->ListingWindow();
6 I) U7 u# ?& d$ C5 R! ~$ Z- l* |* d char msg[256];$ c/ X7 g( C2 ^/ f6 O
theLW->Open();1 g6 R! G" w; ]9 ~0 p
theLW->WriteLine(" ");
; H' M) g# K2 |/ O6 V% d1 }' {, V* W1 l theLW->WriteLine("------------------------------------------------------------");
" K, E, d8 X+ g* W* W3 V# A theLW->WriteLine("Begin Custom Information");
& m9 e4 I: `* }+ u3 u k theLW->WriteLine(" ");
( X$ C& p( C) H/ k0 } sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() );
: F6 G/ X0 d; J( P2 \# D theLW->WriteLine(msg);
3 w2 |! {& Q5 U( M sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );7 ?. \: Y! I/ S& w1 k( R
theLW->WriteLine(msg);
; \5 e$ M4 u. D# T% @ std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();3 F, `) n8 b& j+ S+ I5 ^, [
sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );
4 s4 [" Y# M7 h/ Y: q4 i2 y8 Y6 y; [5 Y theLW->WriteLine(msg);" r; H% Y! _/ M
sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] );' `' }; y5 j- L
theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );+ x( V4 G/ [0 y, d
theLW->WriteLine(msg);
/ ?, j8 S# c2 @" V. N# c8 y; | theLW->WriteLine(" ");2 c# S7 L0 s6 C9 p
theLW->WriteLine("End Custom Information");
+ _* Y' U4 I5 c% e6 n }
' f% r$ t; c6 c: x7 ?) Q catch (NXException ex)! k; J, d$ c5 w* [, i1 X$ P( V
{1 X _+ D( y+ y" \) O
ECHO("Caught exception: %s\n", ex.Message());
: d. [0 |% s. J( `/ Y" H }
8 g) o5 T# {% j& x! r return 0;
" G# j1 B, ^2 ]% u5 E# D}
& L- F |8 @0 @/ X% ]5 Y( u% Y% P" F; C6 ^; K
//------------------------------------------------------------------------------% P" ~9 `& G- z+ P) N: g8 }( W
// initUDO
+ ?9 [! d4 i, L1 z R// Checks to see which (if any) of the application's static variables are
4 q4 \0 Z% [4 E: e* B' V" d// uninitialized, and sets them accordingly.
+ z8 V# H# J+ m2 M/ E// Initializes the UDO class and registers all of its callback methods.
- x U& { w6 P# ]//------------------------------------------------------------------------------& g7 o m% G+ M, w! d, |, W
static int initUDO( bool alertUser), X6 n# z! k) J+ D& p6 ?
{: }$ p, a' n. H# R+ x3 e* M
try3 j$ k+ W" @6 p R5 `8 t% J
{
* l* k9 U: c1 v X0 M1 P6 }7 U if (theSession == NULL)
0 j! H, d |- I. \ {) F) R1 I$ S4 r
theSession = Session::GetSession();
5 O1 B. q$ ^( \: B2 \5 q/ H( f }
2 C! y* N V5 K+ \ N if( theUI == NULL )
5 D* D8 T8 ~8 Z {
/ I+ G, g) E. U theUI = UI::GetUI();4 n1 A- A! g% @9 n% c9 K. G
}- |, b, B; H0 I
if (myUDOclass == NULL)* b, Z N1 P3 [( e* j, r' V
{
X6 }$ b$ ]( {3 F. P if (alertUser)
. n* R& {2 T0 m1 A, t7 d( n {
/ B( u- H0 p) T0 F6 {) C ListingWindow*
/ ^2 u7 c1 k6 `- b# |$ w& ] theLW = theSession->ListingWindow();
- E; b7 P' T. [& ~6 E theLW->Open();
/ B3 J* n V7 B* b6 e( I theLW->WriteLine("Registering C++ UDO Class");" s; ?2 y5 A$ l) G {% q9 f
}
9 B- Y$ G1 y) | // Define your custom UDO class
2 N; p! V6 N5 s* g" b: A8 ?$ J myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");
2 X3 u% R" }, e5 r: {, f2 x7 E1 r // Setup properties on the custom UDO class
( g) `' ^+ |3 W$ R9 Y2 N+ V myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);4 Z# ~' n) y4 T- V8 B) P5 B
// Register callbacks for the UDO class+ e1 ?0 Y# C, Y
myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));* {3 |0 V; t/ L8 P0 }
myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));
8 C- b+ Y# C7 B, d+ @2 ~7 V myUDOclass->AddFitHandler(make_callback(&myDisplayCB));
- ?) J- L3 n0 `" o* m: R4 V% t myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));' o: u/ [" a7 `2 c& R+ F6 f! ]2 q
myUDOclass->AddEditHandler(make_callback(&myEditCB));
4 t9 O( u# f, G, W( b5 l: d' j myUDOclass->AddInformationHandler(make_callback(&myInfoCB));3 j' i; k5 C# y5 W! |# i
// Add this class to the list of object types available for selection in NX.1 r3 _3 e( j% M7 w1 {( Y* Z# u2 k
// If you skip this step you won't be able to select UDO's of this class,+ ]4 i' r/ m" R; c+ X
// even though you registered a selection callback.7 ]6 ?4 ?# i; n0 X& }
theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);9 r! x; z% H7 @4 X8 o1 ?) v
}
$ ^6 m9 r4 q; Q3 f) o }, [5 K) n+ h* W" ~1 }- Z2 ~ K
catch (NXException ex)8 B# M9 ]5 V+ [; G5 \
{
' n8 X' o3 x* B+ Q3 f* ?" E+ ^! A ECHO("Caught exception: %s\n", ex.Message());1 d. n) k9 `2 s6 z, @$ p I
}
% r% E/ C) R; \' ~ return 0;
% m- v r4 b# q' U}
1 @2 P4 z/ p2 @
9 B& L7 V' P6 S//------------------------------------------------------------------------------) Z/ N2 b1 e: I4 H
// ufusr (Explicit Activation)' Z- y% P' K4 y: |" L
// This entry point is used to activate the application explicitly, as in
5 k, U/ S9 Y8 I1 d( u// "File->Execute UG/Open->NX Open..."5 m: h. p# T4 G! A( v8 X
//------------------------------------------------------------------------------/ ^/ y/ O. E: V" p# A
extern void ufusr( char *parm, int *returnCode, int rlen )
, t0 T/ i& Y" g7 G7 m{
' S& ?1 K: C1 U: y5 i6 T% Y try
: x! D, K4 ^9 I' [! z {
6 x; g9 a8 S) e* Q // required for calls to legacy UF routines
} D* O4 R6 H* G& G // such as UF_DISP_add_item_to_display
, a; f# z8 A% r& ] UF_initialize();* N+ f) Q- e& V$ }8 \' S* E# x
# ~/ `5 p8 U3 C
// initialize the UDO - if we didn't load this library at
0 t3 _( S0 L' F // startup, here is our second chance to load it" e3 H3 E. h/ u1 T
initUDO(true);
4 J" M! N, Y" C* M- M' S$ y' w/ ?5 A
5 U$ I2 c( c+ Q8 x9 p1 v, Z // if we don't have any parts open create one
8 {; e/ U) x: O {7 k6 y w. | BasePart* myBasePart = theSession->Parts()->BaseDisplay();
U% g; S7 i6 T9 h7 Q/ J if( myBasePart == NULL)7 U I* E; V" b: r% x7 B- a$ e2 \
{
" R0 @# t: F, l6 T3 y% P9 V% C myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);
, `2 t5 r2 q% {$ a) J" u }
5 | M4 e" }2 o' p' z/ V, a I' X9 X; s4 e+ D9 R9 q
View* myView = NULL;
# Z' k. B8 e. h5 Q5 G# |- v Point3d myCursor(0,0,0);
1 f" J" ~% Z/ G# d- Z* Y2 V; ~' V9 I8 e) u- ^8 x. A D9 ?) w+ ?: U, s6 u: ]
// ask the user to select an origin for this UDO8 \7 R$ k/ v; p: i: q: S7 V1 }
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);5 N0 R; k: E ]5 {7 E) f7 U8 B1 B
if( myResponse == Selection::DialogResponsePick )
+ y8 x. L* B2 z# q3 w, d3 ^& D$ q8 c1 B {
: j% d+ [# f/ c6 ?& X" h3 }) K // The user selected a point - go ahead and create the udo
5 U" P6 ]$ F3 p( z; U5 E UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();, D9 c2 S: ?$ g0 P2 T; o
UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);
; z7 _7 ^. g, ~3 ? // set the color property of the udo - just for fun :)# l& v0 b8 J) t
firstUDO->SetColor(36);
/ i+ [+ @* g& d7 g% t // store the origin selected by the user with the udo; H: I |9 L1 |0 L
std::vector<double> myUDOdoubles(3);
* V& Z7 U( }4 A0 f$ S6 X$ z myUDOdoubles[0] = myCursor.X;% K! j0 |; [9 _2 k$ s) h
myUDOdoubles[1] = myCursor.Y;+ h& K4 [: ~ A4 b% f1 }1 r7 y
myUDOdoubles[2] = myCursor.Z;5 ~8 s8 w- i& f( Z- U$ Y/ F9 |3 N9 i
firstUDO->SetDoubles(myUDOdoubles);9 @& W. X; L( L7 z/ ]) W& p
// add the udo to the display list manually) ?) B% W( `9 H" D
// this will force the udo to display immediately7 r. f" q* D0 l3 }% \
UF_DISP_add_item_to_display(firstUDO->GetTag());
4 X, n& r( n2 R2 q+ K0 Q1 h }. t$ @$ N* U; P4 O
UF_terminate();% z, s- m% s8 d) R( x+ V
} C }( D* l+ B* ^- T
catch (const NXOpen::NXException& ex); s( ^5 W1 }6 B* E1 x
{0 e" b) Q: [. k4 p8 \
ECHO("Caught exception: %s\n", ex.Message());
9 h$ n5 k/ ?; C, y/ R }, N0 ?8 H( ^0 S
}
" q5 c6 v- {8 G" @% |9 x) U, Q: c5 f7 |- D$ N: Z( Y' I9 Y
//------------------------------------------------------------------------------
) L/ Q, l" F5 ~- _( ~+ V// ufsta
- K( a+ N4 _+ \& |1 N. q; r- Q// Entrypoint used when program is loaded automatically; A& ~# P- Q8 ]( J
// as NX starts up. Note this application must be placed in a3 L7 U. c$ r. u! C- x; I) D
// special folder for NX to find and load it during startup.
2 Y3 f( F* W5 r// Refer to the NX Open documentation for more details on how
# J8 h/ G$ I/ @" |// NX finds and loads applications during startup.
# q8 \% z2 u6 {& t+ v& W: l//------------------------------------------------------------------------------
6 D8 d( D2 W+ u3 p$ ^; I8 |- pextern void ufsta( char *param, int *returnCode, int rlen ) ~: [( O, A. V* C
{( g' p: [, Y, h# |& s' p
try1 u q9 M2 x% s2 D0 r5 u
{4 r R: ?* ` L* u& ]! R# I% ~ o
initUDO(false);
: {9 ~0 `! W7 F% W8 a! d }
" T% `; \- `0 B catch (const NXOpen::NXException& ex)$ J X% }7 C- m5 x3 _
{
/ Z' S1 y' E; j0 j2 Y" m* t0 I ECHO("Caught exception: %s\n", ex.Message());
( v. F. U, H+ R. z: f4 _ }( ?1 w5 ~# a0 X4 K6 k+ q
}9 q+ q8 `" m. [: J7 S
7 c$ Z1 k7 \' Z4 n
//------------------------------------------------------------------------------
( K' W A# q1 `8 Y* [// ufusr_ask_unload
- ]! g7 o- I% I3 f# q6 m// Make sure you specify AtTermination for the unload option.
6 h/ \' I7 C, b& Z// If you unload the library before the NX Session Terminates& h0 ~8 z- n F7 x! W- b5 s9 L9 m
// bad things could happen when we try to execute a udo
2 N; n! C1 P. c5 [! E" e4 L// callback that no longer exists in the session.' Q( U0 u- W7 ]1 a F+ [, ~
//------------------------------------------------------------------------------! _' l8 \) } Q/ t1 e4 k/ L. R, t0 K, @
extern int ufusr_ask_unload( void )
3 ^9 z/ U- \: X1 m# t" v{
: S0 a; v& K$ p3 r2 K5 f( i return (int)Session::LibraryUnloadOptionAtTermination;1 P5 y# y' l" X( d; b
}% J1 x0 `: ?5 J" D3 K
( O5 ~3 k' F1 D' T
|
|