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