|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
" N m0 N6 O! {) j9 ]' f7 I' q3 j/ [2 i% N$ ~8 Q/ f8 i
NX二次开发源码分享:NXOpen C++创建UDO的过程
- }) u$ O% W! O* _7 S* I
; \8 P9 D* m1 f2 E, J0 u; d
5 Q: z: R/ [6 G* s7 P- q& N6 _UDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!, h/ i5 L* ^; Q' i) e4 U' T
) e7 v2 B" G" z
+ y- B F3 x! x" L @; ?3 ~6 u7 ~4 a; R" S1 k: Q" i
#include <stdio.h>7 ]* n( [4 z9 v* x, ?) b9 V$ |' {
#include <string.h>
* l! A! m i# l( L. `! l' x#include <stdarg.h>( e4 @" k _1 I$ k, O4 m0 x
#include <uf.h>* P3 x8 E; m. O
#include <uf_ui.h>
5 O/ q: `- w/ D% V. ]$ _2 R( U#include <uf_exit.h>. ]6 u i% E( K) A C
#include <uf_disp.h> j( ?! r' ?" J8 u0 }0 q
7 Q: p+ ^' U$ h0 L) b9 ~6 h
static void ECHO(char *format, ...)" p% \ R' N9 _7 D4 b M
{$ E d: t# ?8 g6 M5 s1 ~! w6 K$ m
char msg[UF_UI_MAX_STRING_LEN+1];
' P7 P- s2 \/ c7 G8 F/ L va_list args;
' _$ e$ H2 _$ \: u va_start(args, format);# f3 J; e: }% H$ d5 ]7 N
vsprintf(msg, format, args);9 X1 P( \9 B5 i- u8 w2 i6 J* }2 u
va_end(args);
: Y# i# }& x3 S" X9 |9 i9 y UF_UI_open_listing_window();6 a' e; |6 Z" n2 l$ Y
UF_UI_write_listing_window(msg);/ c1 l. E; z! a4 Y* d4 i( j9 n
UF_print_syslog(msg, FALSE);
7 g' U Z; W8 q}
; V7 l1 @0 i( n$ W' H& z" {. H% S C5 M! I' P; S
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))) t; f# m6 \* h5 y1 P1 m; |
' [* C; R; Z; D! w! P
static int report_error( char *file, int line, char *call, int irc)
3 p! [8 M* b. @3 s$ U3 L{, j3 Z ?( J: w$ u! D
if (irc)
! {0 V) A; ?% b) g. `+ _ {
' A3 r4 R9 O" R) O, V& } char err[133];3 z2 B2 H# ^: e: X+ |
( Q5 |- e4 i' A UF_get_fail_message(irc, err);: X% m) C3 ?2 I0 Q, P" Y0 ~
ECHO("*** ERROR code %d at line %d in %s:\n",0 B& f; U. ~: z# p5 T
irc, line, file);
/ {8 j2 D% P6 r' ~& w ~ ECHO("+++ %s\n", err);! ^! m9 p6 C r2 u. Y
ECHO("%s;\n", call);
1 e0 e, f5 ~$ [. @ }
; X% y# U& i$ q9 }- I& `3 w# A7 V$ K; Y, N( g3 a: W
return(irc);/ R( E7 L0 R) ^1 d, Q8 q
}
8 }" @- Z/ S. B3 v4 E+ X+ g+ M4 j: r$ ^, {* C0 T4 M9 Y( O1 D
#include <NXOpen/Session.hxx> * z8 g5 h4 {9 @) W
#include <NXOpen/Part.hxx>
" \- S% d* Y& Q5 T6 M6 k#include <NXOpen/ParTCollection.hxx> 3 Y* m! A* Q- ~5 g
#include <NXOpen/Callback.hxx>3 ]# p! p, L2 P* S
#include <NXOpen/NXException.hxx>
! @2 s m# b+ K7 P, Q#include <NXOpen/UI.hxx>
+ v) q3 U# M/ X: P' K2 P* n#include <NXOpen/Selection.hxx>. p# F( i, @9 e+ M# [
#include <NXOpen/LogFile.hxx>1 O$ h+ q: g* ~0 K: H: m, Z
#include <NXOpen/NXObjectManager.hxx>+ U a# X; Q: M9 h3 I
#include <NXOpen/ListingWindow.hxx>" r* J4 d0 d# G0 {: X: `
#include <NXOpen/View.hxx>* T) T2 L8 u# X, N/ d
# {0 `% s& i( y7 W. M( A" G#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>
; q8 K$ _( o' A5 m9 c; Q#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>2 X; e: a- c+ K! c) V
#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>
/ E8 h5 l, z8 Y#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>
7 X' o, N3 V1 x5 E#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>/ U! z% i& W0 {; u
#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx>
" i9 p1 F( W+ `( K6 F4 U" o5 h/ a. a#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>
# Z; L2 c1 Z- x5 e$ m#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>
; n, X4 l9 k4 l: `0 B: I4 ]5 T" S m3 u* x" `7 f: F# m* T! y
using namespace NXOpen;$ i8 `0 Z" L x+ |, A6 ~
using namespace NXOpen::UserDefinedObjects;1 p' k' X( X9 n! C1 Z9 J
X* `! ?9 e+ \: C
//static variables
1 a. h- Q, s; w9 Vstatic NXOpen::Session* theSession = NULL;: Z1 R- ~0 m8 I
static UI* theUI = NULL;6 f) u$ G! i3 W
static UserDefinedClass* myUDOclass = NULL;2 L) x+ P% T- e1 u0 b
1 g2 q: c, d% |8 @2 j//------------------------------------------------------------------------------+ x& w4 Z V4 F B; q/ g
// Callback Name: myDisplayCB8 |1 T, d: g9 H6 [
// This is a callback method associated with displaying a UDO.. f4 I7 ~$ j6 ?0 s. e
// This same callback is registered for display, select, fit, and attention point8 K/ L4 M- J" ?
//------------------------------------------------------------------------------3 U4 I* ^4 v5 ~
extern int myDisplayCB(UserDefinedDisplayEvent* displayEvent)% U( s- B8 ?' f& C, I% P2 a5 J
{
9 u2 `1 N7 d. D4 x! |6 F; b- H% g try+ y9 l% v# \! }+ B7 M
{5 V, k3 T9 [6 m& a; L5 r
// Get the doubles used to define the selected screen position for this UDO.6 e. ~/ X: C# j; H1 O
std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();, X& y! B* W# i
+ P9 j6 Q* @" ? // Use the doubles to define points of a triangle
7 E9 U' L( k. T) r+ ?1 H+ d& v, U std::vector<Point3d> myPoints(4);
/ s/ q1 ~4 L0 @1 H" n9 b# j% X4 }
myPoints[0].X = myUDOdoubles[0] + 0;
& h+ |8 {) w. B myPoints[0].Y = myUDOdoubles[1] + 0;
7 L8 a2 ^, ~6 S myPoints[0].Z = myUDOdoubles[2] + 0;' n5 O7 ^' G1 P% Z) L+ f6 a! z
! E- {: ]) l0 R7 j# ?
myPoints[1].X = myUDOdoubles[0] + 100;, s2 H( D+ ~: R, }& C) E) c
myPoints[1].Y = myUDOdoubles[1] + 0;
# i7 u% R- x, w; z% ^' k+ d myPoints[1].Z = myUDOdoubles[2] + 0;
* J5 A2 V7 m8 }6 |) J% T) M; \' \4 o/ h% Z
myPoints[2].X = myUDOdoubles[0] + 0;; X: m- m( v/ ?' c& `- B1 E0 |; `/ m& [
myPoints[2].Y = myUDOdoubles[1] + 100;6 a" M7 L" \/ ^4 Q
myPoints[2].Z = myUDOdoubles[2] + 0;
9 z8 Y2 u/ r$ k
; H) B* V, f: N4 d1 ^; c6 I myPoints[3].X = myUDOdoubles[0] + 0;9 g/ o, ^- R7 ]3 D2 }
myPoints[3].Y = myUDOdoubles[1] + 0;
! ~" c% t0 |1 U7 e: e) K myPoints[3].Z = myUDOdoubles[2] + 0;
! F+ {8 t V6 q& v) B' C4 ]9 E: Q0 X( P, U/ H0 b3 N
// Display the triangle4 p& @" |% ?) x* M5 J( y3 h ^
displayEvent->DisplayContext()->DisplayPolyline(myPoints);
& Y; |! ^1 u' q. [ o0 q; h, O$ a8 ~; Q0 D$ v* Q# g
// Display the text next to the triangle
`2 F8 ]/ Y. G* ] Z Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);; |6 B" b( B2 T3 Q" W: x7 `" l
displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);2 N4 \( Z# k5 o' D% w6 X3 v
A% v# W, H' z5 ? V, @ // UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 6731653
+ z; {% B1 F. U# C // This demonstrates using UF_DISP_display_facets as a work around; M. ~- l$ t3 C+ T* m
double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };/ u: I! t0 V3 V: ~ } E, D: @
double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],% n, Y0 Z7 J: R) I9 [7 q3 Y3 ^
myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2],
3 w* d- Q ?" l, [ myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };9 a# U3 N2 M8 J4 \: l. N3 d
UF_DISP_facet_t facets[1] = { vertices, normals };
* B7 }6 j! z3 b0 R: f8 k- n, X void *context = displayEvent->DisplayContext()->GetHandle();
6 m2 O7 |2 c" E& H! E1 `; C, @& \0 `
UF_initialize();
2 y2 |1 L. }6 U6 T- d UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));
- X$ s% b9 p& O6 c Q UF_terminate();
+ c2 X6 j+ ?0 f5 w' T8 E- T
" a* X; ?! a9 D }
" `! ?$ e3 N: t5 |9 {" p8 P catch (NXException ex)
: p; {: W# Y, F( R* ^+ | {
# W- t1 o$ H1 d4 A+ I; d3 X( o ECHO("CaUGht exception: %s\n", ex.Message());* r: U) \: [& t; P% d" z# g. @
}8 f$ s0 t; i4 D( ~) S2 c9 |
return 0;
! r0 `4 ?" `8 h. a, Y}$ r3 `9 K# d) D: V1 ]
//------------------------------------------------------------------------------6 M4 Z- `8 K: v' [: Y; \" {% D
// Callback Name: myEditCB7 T _, G" q# {% z: x3 E
// This is a callback method associated with editing a UDO. i* T6 Q/ ]4 x
//------------------------------------------------------------------------------
% m" f. o% w8 [3 M! x! \4 pextern int myEditCB(UserDefinedEvent* editEvent)9 t& v3 y, r% a! U4 ^) a) `
{
0 y- F# i ?3 ~4 R7 K try: L" o! [8 \. d
{) ~ x& n( T7 `. F. L1 V
// required for calls to legacy UF routines% h# u8 Z. V+ L; X2 T, R: s& g! {
// such as UF_DISP_add_item_to_display* n N$ y, K3 R# {) n
UF_initialize();
3 s# h9 b# w% Y1 _' Y4 ^2 M |( K/ s: \1 _# t2 ~) v+ ~$ U) V
View* myView = NULL;: e$ `$ k, a+ k1 }0 ^
Point3d myCursor(0,0,0);" P7 ~$ d* I1 [+ P- P8 U, \- o5 H: M' r
) `* p, k) d& V) m
// highlight the current udo we are about to edit- ~! N9 ]# R! x e- S( B* T. L1 C
// this is helpful if multiple udo's were on the selection3 ]. O5 i7 f% N3 k- ^! \- T
// list when the user decided to edit them1 j" u( G6 e7 J- S
editEvent->UserDefinedObject()->Highlight();. L! o7 A/ i1 Z% j: I' M
/ m' n* B$ |% d; l8 O) @) X
// ask the user to select a new origin for this UDO! C" h& f5 B! k' q3 ]" {6 w
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);
1 v. [* ^* i$ c1 @( K; L3 x // we are done asking the user for input... unhighlight the udo
8 j& o9 t5 H+ A- s9 l! u4 w" t; E editEvent->UserDefinedObject()->Unhighlight();# I! y2 d5 l' e# p
; j2 V+ |6 W1 P- j/ H! C) q // use the new screen position (if the user picked one)
0 v9 V1 c! }( G/ i& L$ n/ o+ X if( myResponse == Selection::DialogResponsePick )
* [3 X4 U; x$ p {
2 P v7 c: {3 w5 t4 C std::vector<double> myUDOdoubles(3);
- X- d( d1 W: U6 s$ |! Z- [+ W myUDOdoubles[0] = myCursor.X;" K$ O$ E; h& ]# F; f
myUDOdoubles[1] = myCursor.Y;
4 d$ G1 ?4 \% b- n$ B% \ myUDOdoubles[2] = myCursor.Z;) I3 K2 d& t# F6 z
// store the newly selected origin with the udo
9 o1 ~4 ^! G# s5 j5 I editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);
( S7 }6 @& n5 G/ v* Y6 Q3 p& u- P // add the udo to the display list manually
2 Q$ d' y% v. E7 L4 q# q( t // this will force the udo display to regenerate8 u6 q* ~- M* m6 e
// immediately and show the changes we just made
# F4 s, y8 h2 x UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());; M* h4 x2 N8 r+ s% L5 G
}
, I5 L6 f3 J% N7 e# s3 {: a UF_terminate();
# N/ e. K- ^( X. p1 W0 k5 ~ } catch (NXException ex)! `, U' C; o5 h8 J' C
{7 X' Z D1 n! k! o. _; j3 J
ECHO("Caught exception: %s\n", ex.Message());- P" U: S# g$ P: D1 g; o
} return 0;- }& h5 |% y {, w$ y8 ^
}
' E# V2 U) {. T/ ` b) i0 Q//------------------------------------------------------------------------------
+ U' a+ T7 J8 O: o// Callback Name: myInfoCB$ }+ X, v7 A# V' ]7 N
// This is a callback method associated with querying information for a UDO.+ U4 T' z& }" m) B- ~, D- z* C
// The information is printed in the listing window.
9 w9 z: Y; w5 H. \5 z1 \//------------------------------------------------------------------------------& R: p C9 J, V+ ~; d
extern int myInfoCB(UserDefinedEvent* infoEvent)
% J" K8 a. R" |" N4 Y{
8 f/ i2 n0 V- L% _9 [4 W# ?* G try$ \" B7 [3 g4 `" O! F6 _
{
/ h% n6 P3 z% S- ~ ListingWindow* theLW = theSession->ListingWindow();- Z0 n! r4 o2 j7 e0 R* I! l
char msg[256];
+ J- _$ E9 {* o. P* M theLW->Open();
4 ]: V+ Z) _# r theLW->WriteLine(" ");3 _" F* @$ Q, u5 q3 n5 e
theLW->WriteLine("------------------------------------------------------------");6 B- ` l+ ]! Q5 v% W E
theLW->WriteLine("Begin Custom Information");# [' u0 N3 m, @
theLW->WriteLine(" ");
0 M2 s) ~. P9 A* |$ t sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() );8 p6 f4 f! J) g' M: I
theLW->WriteLine(msg);% o* x+ D' O, G* N* k! P0 h8 S
sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );1 W9 D7 U4 l6 u! c
theLW->WriteLine(msg);
2 R# Q2 N' u$ Z/ A6 H- G' V3 M std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();1 ~, K5 f2 Q4 v/ G+ T7 f9 A; t/ ~
sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );* P! e" f) R* v
theLW->WriteLine(msg);* b: g' S( q1 A* u4 _
sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] );# _0 O) _! [7 {( o% n
theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );
5 D( s9 D7 x6 W3 h) B theLW->WriteLine(msg);
]2 e: R- I$ T9 M( [ theLW->WriteLine(" ");
6 I5 k9 c) @9 c9 ^( `$ T/ ~ theLW->WriteLine("End Custom Information");
; D! y8 J. H$ H3 B# l7 N% C# q }
1 r* h# R0 f2 s; G' @ catch (NXException ex)
# f, a, |7 `( t' ` {
e; ^! @+ @9 o! R: a9 | ECHO("Caught exception: %s\n", ex.Message());. W8 S+ o) x& |: @, L
}7 d+ M& U; @- N& W9 r3 ~7 m
return 0;, W6 S/ b1 v& |, B5 @& `+ b e
}
' w7 P7 Q0 x" Y! ~8 v7 C
7 D; o/ ^9 Y. |: o$ [7 l3 [//------------------------------------------------------------------------------
1 q) q$ H" f+ S% X; R$ k0 |// initUDO
' S! _4 H' Q8 O6 G0 T/ A0 S- X/ v// Checks to see which (if any) of the application's static variables are
- p. H3 O8 l% q1 O( P// uninitialized, and sets them accordingly.
# Z4 K& n& C! V% M// Initializes the UDO class and registers all of its callback methods. ?7 N3 ~; x: J0 K! @3 \) J9 d/ i
//------------------------------------------------------------------------------
0 ^0 e0 @ Q( Z" A+ y/ s# Vstatic int initUDO( bool alertUser); Y$ _5 y/ K8 r+ M, B+ ^
{: i9 q, [5 \2 l# w9 v
try
6 k# z- k" d- E7 U {- s/ E# M; F* l, Q' A2 F4 J! `
if (theSession == NULL)9 I+ E. j# J- i$ [" o$ q6 ?
{. |2 g6 N) h& ?6 I, [ [1 i
theSession = Session::GetSession();+ |) ?+ n! b. w9 I; N; T! @
}
0 ^) N1 O, P5 i- H2 F2 Q- }* w# {$ z if( theUI == NULL )1 F% k* O, V/ H
{- }( x, g( j* g7 i
theUI = UI::GetUI();
' s- A: ?- [9 n- h+ G9 Q. u }
! @3 _ U* X! D% o/ r if (myUDOclass == NULL)+ O% G, X$ {; N- _. x& _, h
{
9 } l5 q* E- z+ x! F if (alertUser)
/ j1 ~0 P- v7 S" g6 l( E0 ^- ] {+ [9 _" T! q: w# m3 j
ListingWindow*& N+ Q+ S" y7 d! d- E# X0 X
theLW = theSession->ListingWindow();) e4 `# T7 M. m
theLW->Open();
! m( v: B6 }' F. p theLW->WriteLine("Registering C++ UDO Class");0 g4 o4 [4 `( q- W6 o( {
}: {+ i& ?8 u2 |) M$ \* l$ r
// Define your custom UDO class
2 B/ U# k# {8 ^' ?4 w myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");! w0 @5 K1 g( y' j
// Setup properties on the custom UDO class
( t( [ w4 N0 V myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);
5 K! M; Z9 |0 h) s" M // Register callbacks for the UDO class
2 T0 g2 `7 N' Z7 H" k, D+ L% J myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));5 e* ~- F1 ^( a3 w- T0 K
myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));1 ~+ k: y6 r6 u i
myUDOclass->AddFitHandler(make_callback(&myDisplayCB));
) V# n! H' n: }1 e8 a# g9 ]1 g* M, O myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));
: @% a' Z" h3 w9 W9 D# g1 D myUDOclass->AddEditHandler(make_callback(&myEditCB));
% \/ {0 g; R& ^ {, P# T8 b8 o myUDOclass->AddInformationHandler(make_callback(&myInfoCB));
1 c: K0 f% G! s7 o- t // Add this class to the list of object types available for selection in NX.: ~" _0 B4 c1 {0 U
// If you skip this step you won't be able to select UDO's of this class,! a7 Q! c8 Z0 }8 A6 a- ]; O
// even though you registered a selection callback.
' Y/ V/ s, V: y0 ^6 k theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);
' G8 H8 h& \; J; Q; T2 T }
n2 f% G5 C& l1 E }9 u0 m4 T) r. j' t
catch (NXException ex)) G8 Q n# `" B9 X" v7 M
{( o# e/ _7 _$ f3 q' j
ECHO("Caught exception: %s\n", ex.Message());
& }9 Z6 m6 h, N( d: H+ j, L2 Y- C } W0 r9 {) p2 y) L# }
return 0;$ z) u6 {3 D& F0 @$ Y, g
}; U* u2 i0 H" ^
: [0 |+ k: |! v( G8 Q//------------------------------------------------------------------------------2 m# y% |9 o: w
// ufusr (Explicit Activation)
9 X( i! T2 I. {9 @// This entry point is used to activate the application explicitly, as in
4 y2 A, a3 o. }/ F/ _! [/ l4 i// "File->Execute UG/Open->NX Open..."; |; G8 I0 x6 Z" u* u) w& N4 k
//------------------------------------------------------------------------------
; i' U! Y9 x, A# G: E" [4 Dextern void ufusr( char *parm, int *returnCode, int rlen )% [+ w1 y5 N, F" \8 y
{% k4 [. Z5 R+ x
try* ^8 C- P% B7 \3 N' K- |; X
{3 k) L- B! Z# y/ n2 P
// required for calls to legacy UF routines
/ M- ?; g: d0 p4 [# | // such as UF_DISP_add_item_to_display
% C2 ~2 D# _2 m; x' l5 w0 M UF_initialize();+ [* M, K; e7 ~% g! f2 @) q
' p B/ q% W, ~& O( O3 C
// initialize the UDO - if we didn't load this library at
, j3 B( F% K3 V4 z( g1 s& ^+ \ G // startup, here is our second chance to load it7 f9 v" ?. y Z- F) w
initUDO(true);
+ [$ l5 z! V; _" U3 |3 [; T5 ~) b$ q; H, E/ H
// if we don't have any parts open create one
, M0 A& u: \* P1 b' P5 I BasePart* myBasePart = theSession->Parts()->BaseDisplay();/ v/ F$ T' e l) x6 W1 G0 j S
if( myBasePart == NULL)1 i+ T4 U$ y5 d7 C2 s
{ q7 @! D- r& t- W; n5 [
myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);2 _9 q/ L/ q$ Y
}* L- b+ H, U @# {
7 Y1 Z/ Y5 e# E6 M View* myView = NULL;
$ L* \1 o7 T) r: W2 r# H Point3d myCursor(0,0,0);
; T* X" X# M% k6 t8 n$ \' R. n
// ask the user to select an origin for this UDO
; k y6 k4 M& W Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);
! a9 o: |7 N& [4 J& I if( myResponse == Selection::DialogResponsePick )- a" Y' Y: _3 C) }' X% D
{3 ^( i5 Q, Q+ Y2 h- ?
// The user selected a point - go ahead and create the udo9 [- U6 d" h8 S1 |$ W
UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();
+ U: M) i. d, i, |! @8 D UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);
0 ^% K/ ~( H. X // set the color property of the udo - just for fun :)1 r! f. o1 c8 L- k, N9 m& X5 P
firstUDO->SetColor(36);
% x: F% r1 c. T4 m4 @5 P1 ? // store the origin selected by the user with the udo
5 [ g) G. q" B6 x std::vector<double> myUDOdoubles(3);$ R, d( r, _! I3 Y$ A
myUDOdoubles[0] = myCursor.X;# ~# R2 i! Y( y
myUDOdoubles[1] = myCursor.Y;) a0 E7 p( H1 I6 h0 w* H9 j
myUDOdoubles[2] = myCursor.Z;
: {! I' I* u6 o+ p firstUDO->SetDoubles(myUDOdoubles);
5 x7 J4 Y( W2 r! F. ~$ k7 [0 ] // add the udo to the display list manually
1 e4 V1 i' D% x6 b // this will force the udo to display immediately
2 d8 N1 F6 M! U6 ` y UF_DISP_add_item_to_display(firstUDO->GetTag());
) m* G9 G! ~. Y4 K @9 `$ |" ^ }3 A4 ]! p! h& _- _+ m! H
UF_terminate();5 | ^7 y. @6 M0 Q1 v) d* i
}
1 s8 u1 R- w$ ]) q; v; Q8 r catch (const NXOpen::NXException& ex)
* t3 n1 M7 w4 A* V {
9 |, r, z/ Q8 n" N0 b ECHO("Caught exception: %s\n", ex.Message());
. w! ?3 {* h- \ }, k r% P! l; Q0 U& k
}
; _8 x2 b% A- D6 \3 T B
4 `& G1 B* A" P: U/ C$ B//------------------------------------------------------------------------------5 [8 D ~( l8 J1 V7 S$ ^% ]: D
// ufsta
/ [# i& t% D' D( z' x' n* x6 W6 c+ O* w// Entrypoint used when program is loaded automatically* t1 t$ s; @- w4 N
// as NX starts up. Note this application must be placed in a
! [4 F, q$ h( e( \// special folder for NX to find and load it during startup.
$ w. Z: e+ W2 G. }! o1 [; Y// Refer to the NX Open documentation for more details on how
& n8 f; f0 K5 c0 V* G& `! i// NX finds and loads applications during startup.
: ]+ _8 P# W3 E8 j( `//------------------------------------------------------------------------------
_! s& x2 |6 O g5 s9 }8 Lextern void ufsta( char *param, int *returnCode, int rlen )
1 R3 f$ r7 @! K/ C* K d{, w1 C) j! O1 Z
try/ R3 R( I- h8 l. m- u- L
{
% s) F: v! z$ Z) V" U( s, |) X initUDO(false);
/ r; q0 b% s" _ }
5 l: B( z. o# O5 q( H catch (const NXOpen::NXException& ex)! u) J) a6 s, L6 {' P0 O7 Y5 [
{
- T3 K. o, X" o8 l: w ECHO("Caught exception: %s\n", ex.Message());
- p; f# m( ?* S" I }& K* o$ l o! u- V; ^4 N i) @1 ~
}
- P( S8 S! C, P9 F! E/ B1 z# c) ~' b9 [" u% e& ]4 A! |6 @2 _- h
//------------------------------------------------------------------------------
4 v I1 P N2 z0 n2 Y& n6 C// ufusr_ask_unload/ x9 `! N2 F. }! A, \* j
// Make sure you specify AtTermination for the unload option.
3 u' q" X) M3 P2 B, W( w2 L% v// If you unload the library before the NX Session Terminates
0 Q8 z7 Q L6 E5 H6 n6 v// bad things could happen when we try to execute a udo" ]2 ^ @9 i: S) T r* \
// callback that no longer exists in the session.3 w0 D1 N- c4 z4 {
//------------------------------------------------------------------------------& _' k1 k/ F7 T2 M# A- ]5 k
extern int ufusr_ask_unload( void )
$ N/ F. [; p# U9 ?{
) H# ^: J- f: `; Z return (int)Session::LibraryUnloadOptionAtTermination;
% _) g" A( \5 T( b5 o}7 x2 O! G- Z) }
/ Q( z2 F/ ]; B' G) C |
|