|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
2 k, e9 T; C) u" \
" ^: m( R6 g- r4 b1 zNX二次开发源码分享:NXOpen C++创建UDO的过程
. e2 a& S5 k8 \! s
, F) n6 W& E# o8 q, B- m5 f) h. Y+ V* {" r2 J
UDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!
- \$ r9 d7 x* j" u! R+ m+ s5 v; n( ?, L9 m
, E& V) p: t$ M! a3 A
* ^% c; a$ A: B8 R/ A1 I
#include <stdio.h>
# w- C6 z) o/ w1 X7 _/ s& H2 Z. H#include <string.h>
6 _1 E6 t8 W! B8 Z#include <stdarg.h>
2 `) A; E. g) G! P( M#include <uf.h>& T& f' ^8 e1 L+ b
#include <uf_ui.h>3 K* y: d6 p% L9 v3 z" k
#include <uf_exit.h>; ?. A- I5 Z* Y+ q; v, c' K* I
#include <uf_disp.h>; J0 Z" h: S, p5 ^/ [( ?! J, q
, ]1 g8 d1 f, s1 y6 U
static void ECHO(char *format, ...)
" ?& j* C: U- N6 X: {1 S: l+ R% `{
/ O) g# O: d* J5 J& A E0 `" z6 c char msg[UF_UI_MAX_STRING_LEN+1];
/ a( f3 [& {0 D- \! }' e va_list args;+ c7 S F% ]5 u7 y- n
va_start(args, format);* K: A; ?- w% i% }7 B' P. e
vsprintf(msg, format, args);
& E8 `1 s- I5 z- K va_end(args);
2 K: f! A: g3 M UF_UI_open_listing_window();. l5 `# e/ h6 I+ F- r" R
UF_UI_write_listing_window(msg);' }4 U4 ?! e, M" r @5 I! Y; P4 v9 ^
UF_print_syslog(msg, FALSE);
$ z1 C* C$ r4 q3 C- s! U z}
# |+ ^/ R5 B+ J+ C! _
% v! G' ^2 y8 k2 M+ I% _5 c#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
( O; t2 T; E% a. U$ }8 q! Q$ ]: u P9 ^
static int report_error( char *file, int line, char *call, int irc) r6 o" D Q% @. b8 r/ g
{( B7 q: u3 W7 m Y ?! Q9 ]
if (irc)
' Q9 [3 R* s: N7 U$ Z {/ s5 m7 q( h& i8 H& u
char err[133];
6 M) C l8 m5 I7 S( L. T$ l$ I+ f; c) g! j* y
UF_get_fail_message(irc, err);/ D# b# `1 h' B+ u7 j5 j3 f
ECHO("*** ERROR code %d at line %d in %s:\n",# M! y7 u+ F; I4 r
irc, line, file);
9 T& r. E! {- u& B9 w R7 P ECHO("+++ %s\n", err);- Q% R8 P2 n- C5 D# S
ECHO("%s;\n", call);. X8 \0 |/ \9 a& R0 s
}
. a3 R: L! T- }& F- k# C4 t: T2 I Q/ T- P8 ?# \# O
return(irc);
- e7 m* x/ j8 u- f+ ~}" F2 u5 R+ j0 m! ]4 D3 ~ e
+ N& T% F( t3 C
#include <NXOpen/Session.hxx> ! U; \, l) E" }8 p$ n
#include <NXOpen/Part.hxx>
. p$ H# L4 g" r1 @% o% ]#include <NXOpen/ParTCollection.hxx>
, A9 B' D, X. J: D' j* a#include <NXOpen/Callback.hxx>
6 @' ^: W4 G( G2 W#include <NXOpen/NXException.hxx>
1 z2 e% g9 K. R5 @) m- D#include <NXOpen/UI.hxx>
1 c3 t- W1 `6 t: ?" B4 Q, E4 n% ~#include <NXOpen/Selection.hxx>1 P7 q7 U5 ], e4 e" v& H5 ^: f Q
#include <NXOpen/LogFile.hxx>
3 `6 y4 o& e, d0 W4 K0 o9 w- C4 g#include <NXOpen/NXObjectManager.hxx>
2 |" X0 R8 n) M& z Q' |1 \' ~. q$ E#include <NXOpen/ListingWindow.hxx>
9 _" F5 G* S9 \/ S- r9 Z3 e#include <NXOpen/View.hxx>2 W, t0 J1 v6 n% I M
8 M+ `& C9 r' A1 a; @' D; S#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>
+ y) [7 h1 H+ a9 h6 i& r#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>* P. @1 D- c3 t2 |3 |# q# o
#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>
6 N* b6 N3 o, e: W) ]$ N# ^1 y o#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>
8 t, C: Z; H5 q) {" ~+ Z: T. k#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>0 d4 I3 I' t0 k+ s3 r/ F. z
#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx>
2 k- j( y! y$ f$ Q: k1 }3 R5 V#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>/ g8 L l1 p1 m: W/ k" v
#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>
" t, }2 z& M, V! E8 |/ v9 }# B, `! U
using namespace NXOpen; l1 W( i1 K( ?4 p, A- b2 X
using namespace NXOpen::UserDefinedObjects;
/ S" J# h, o9 w6 a+ z. M* a- z8 w7 p" }: }4 ]
//static variables
' q2 j! R9 _$ x4 e6 x* ^; Y$ ostatic NXOpen::Session* theSession = NULL;
( `/ y P' L1 P' ]* g0 Istatic UI* theUI = NULL;2 B+ V& {7 X% @* ]( t" V
static UserDefinedClass* myUDOclass = NULL;. x. C( E: I& z& y8 e
* G/ S( v; P0 C% U//------------------------------------------------------------------------------2 G8 h) x3 N \
// Callback Name: myDisplayCB
! C* N( Y) g9 A' k8 C// This is a callback method associated with displaying a UDO.6 @" T* q9 }6 T' m7 a( p) d9 ^4 H( Y" ]
// This same callback is registered for display, select, fit, and attention point
: a! F* W9 |/ D- B6 b//------------------------------------------------------------------------------
& p6 K5 V. Y& Vextern int myDisplayCB(UserDefinedDisplayEvent* displayEvent)/ T- f! S, v* I0 D% W9 z6 N
{/ u$ s6 d" c: D7 c
try
5 L& o9 _7 y3 c {
9 s* z2 U9 ]6 g // Get the doubles used to define the selected screen position for this UDO.
P' k5 ?4 Z, {5 |* I2 y5 M8 K std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();
, {1 l% D2 S# K$ S$ k' I i! H- F
// Use the doubles to define points of a triangle
3 { ]: y9 f. [7 O9 I: u5 r std::vector<Point3d> myPoints(4);
" V. M+ N9 i! `5 q: J4 r0 o6 {9 K7 U' s
myPoints[0].X = myUDOdoubles[0] + 0;% I3 X2 p0 V; P- L! T
myPoints[0].Y = myUDOdoubles[1] + 0;3 {) g/ o, j- w! V
myPoints[0].Z = myUDOdoubles[2] + 0;
. g0 F# s1 S1 C X$ p) T6 W8 Q( k! s1 J0 s$ q
myPoints[1].X = myUDOdoubles[0] + 100;9 g$ Q; Z4 [- [; S1 V
myPoints[1].Y = myUDOdoubles[1] + 0;! r2 n$ h5 X2 i3 I+ Z; r2 K
myPoints[1].Z = myUDOdoubles[2] + 0;
j0 M% u# `: j8 k; Z2 {$ h1 d1 H; b: `; O0 D2 Y6 X
myPoints[2].X = myUDOdoubles[0] + 0;6 J& w! {3 T; \3 x
myPoints[2].Y = myUDOdoubles[1] + 100;4 J; u4 E& P' p8 K [6 s {
myPoints[2].Z = myUDOdoubles[2] + 0;
2 O$ i, q5 O; a2 a* X- b$ F$ v3 t
1 a! b% q$ }6 N myPoints[3].X = myUDOdoubles[0] + 0;& \. u& ], H7 q: c! j
myPoints[3].Y = myUDOdoubles[1] + 0;! D% z, t7 V8 Z1 {# W
myPoints[3].Z = myUDOdoubles[2] + 0;
* H$ d# K' k7 f7 H8 n* Y' j/ P! x% Q6 n5 M1 ]/ ]
// Display the triangle
. c# P) g1 r0 ?1 ?: }5 U$ i displayEvent->DisplayContext()->DisplayPolyline(myPoints);; G- j0 W7 E" M" @1 S3 e. h+ i
( g R3 I% r5 f* f5 o7 e
// Display the text next to the triangle
. q+ S P$ n8 R7 w3 L; j Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);
! H A! ?2 D$ u8 n0 g6 |; A5 s displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);
' d! ~% v& O% t* c# D7 W- T. S$ f7 P+ Z, Z! s
// UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 6731653
; `4 n i, e. j9 ^3 M+ [" Q6 ^ // This demonstrates using UF_DISP_display_facets as a work around; G1 O X4 I, X" J3 M: q
double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };
4 ?7 ^5 E' u0 T/ m! _; c U5 i1 X double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],
/ N0 X/ G% ~( z, o myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2],; X$ T8 @' \: v5 T' u
myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };2 t7 q, Z# S1 L* Q: l
UF_DISP_facet_t facets[1] = { vertices, normals };5 e8 \: q6 [, I; a7 t
void *context = displayEvent->DisplayContext()->GetHandle();3 f9 [! Q; K& C: V7 w/ y3 L
3 G( z5 b: k7 W& S+ L+ S UF_initialize();* s$ U* B9 O1 {0 c6 o0 G7 m2 I) R
UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));+ p- v3 ^8 v' l1 I# S5 ~# {
UF_terminate();* A, `8 x! o3 e8 p0 ]
& ~& E3 E! P6 C3 z/ ` }
. ]$ \' B J- T catch (NXException ex)
8 L! p: E& c. O. J% E# T {
! X. x$ b v$ o' f3 M$ Z' R1 E ECHO("CaUGht exception: %s\n", ex.Message());
; T% `/ L% ]# i$ c5 t }5 X) B8 r6 R" L% T( J
return 0;
# J2 |/ C9 h" k+ S' M) i}/ |; p/ P7 q$ p& s0 u0 J6 M* q% y
//------------------------------------------------------------------------------0 j3 c( a2 \+ V5 k7 Y
// Callback Name: myEditCB' @& B+ V* B- u0 ^7 m" S
// This is a callback method associated with editing a UDO.
; }0 f _, T' W3 V' _5 D//------------------------------------------------------------------------------6 u J) o. {8 c0 ?, O
extern int myEditCB(UserDefinedEvent* editEvent)% G6 L7 a+ K/ u* H! J
{* `1 n$ K, n" `. g$ z5 j* P, A2 U
try
k) @; o+ S$ w; ` {4 m9 J: O1 h, N. M+ D* P
// required for calls to legacy UF routines
" ^ M3 y2 ~3 b$ f" n! z- G% h; f // such as UF_DISP_add_item_to_display1 I- u& ^3 T8 t; B. p
UF_initialize();
) P# e. _* c7 ?# U# e# m' p$ ?3 U
# X) d1 e9 G! S( i/ j View* myView = NULL;
. ^3 ], e9 `( y! q# @; F Point3d myCursor(0,0,0);
7 q. @' S9 W4 d
( u$ @6 L4 P+ w/ ^ d" Y // highlight the current udo we are about to edit
: s# o3 e4 y+ c$ N! O // this is helpful if multiple udo's were on the selection
! {* p7 }3 f2 K/ k, Y // list when the user decided to edit them$ d5 b1 D/ ^" l1 U/ N& }
editEvent->UserDefinedObject()->Highlight();/ S* u" u2 B6 k! m- g
1 E7 L; t& F `6 K9 [2 b) M // ask the user to select a new origin for this UDO: O3 P+ L, X: N, r; s' t
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);
; w/ R( ?6 t% u; ~& o1 { // we are done asking the user for input... unhighlight the udo4 y V7 M& h: M0 v
editEvent->UserDefinedObject()->Unhighlight();
0 ^5 c0 U; `3 D( T b- }2 d
% l1 V6 _# y, H$ G/ v // use the new screen position (if the user picked one)
+ R4 M! f" r! v9 n5 Q" C$ s if( myResponse == Selection::DialogResponsePick )) \: E- [ Z0 f u: Z
{, [3 m0 V% j1 M! k9 b, h
std::vector<double> myUDOdoubles(3);9 Q$ Q, O5 S3 C; m+ `& w3 @
myUDOdoubles[0] = myCursor.X;4 k" q9 w- P2 _7 g! u
myUDOdoubles[1] = myCursor.Y;7 |3 o; M( K& f% M. |& @5 U1 g) }( W& }
myUDOdoubles[2] = myCursor.Z;4 i! @$ S { k f9 `5 U+ E
// store the newly selected origin with the udo
# Z$ h; W! V2 v2 y6 o* c5 d4 s editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);
2 v6 r5 Y: D! g7 P // add the udo to the display list manually) L: T! p8 ^" D$ g+ P8 Q
// this will force the udo display to regenerate1 i, j! [/ }* F" h
// immediately and show the changes we just made
; J; b8 K( L% D- } UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());
! H/ |) n4 t" C \' } }
3 Y4 ]- D) p7 N- C1 s UF_terminate();( u5 {' ]* l+ A9 I' h0 F8 ^/ O
} catch (NXException ex)9 G. X. w& o; Q5 ]2 O
{
: v$ p8 E% u9 g+ p3 g1 D ECHO("Caught exception: %s\n", ex.Message());; `+ K5 |6 M; J, D
} return 0;
$ S4 L# W: L' `8 C% G6 D$ ?}
1 |: x H* h0 B+ z' i7 A1 ?+ \4 {0 h//------------------------------------------------------------------------------+ c/ o7 y1 T3 S8 t, ~/ ], S
// Callback Name: myInfoCB! p% q/ Y' G/ h% V6 A& l
// This is a callback method associated with querying information for a UDO.0 I% F& R7 ?* ?: C
// The information is printed in the listing window.' u, o/ w6 T3 y( a* _
//------------------------------------------------------------------------------
: m& T( [8 I8 ^# r$ A9 yextern int myInfoCB(UserDefinedEvent* infoEvent)2 y" S2 T, U5 |9 P" H
{
, c4 {% X0 W- {! M, b; U try
6 F7 |7 L# ^# b- O6 w {4 E5 c5 U1 F* `, D
ListingWindow* theLW = theSession->ListingWindow();" B8 B/ q* w* s" K
char msg[256];
& n q$ K. V+ Q7 `$ x theLW->Open();
3 e# G$ l" z! O theLW->WriteLine(" ");/ S. F0 b: p) |7 }
theLW->WriteLine("------------------------------------------------------------");% i' Q4 ~8 y4 K0 S" U: \: q+ E
theLW->WriteLine("Begin Custom Information");$ D: `9 }" s+ v3 X# H* Z( E) p3 \
theLW->WriteLine(" ");0 ^2 a2 s7 X; Z
sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() ); o [3 w) s) Q! z! Y! y" ]
theLW->WriteLine(msg);
" T% x! o h0 z! m9 ] sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );9 ^' D2 b& } W3 I+ o, r
theLW->WriteLine(msg);. P( e. |( |- k9 s* i }
std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();
+ l+ O% w0 G; }& j sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );
4 l# d& k" H- y c* l( S theLW->WriteLine(msg);
% m4 y, c/ L: ]6 D/ Z sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] );
; P1 m# p: @; x; [" Y theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );
" E/ h! M6 F9 i9 |( |' u' V theLW->WriteLine(msg);
+ G7 W) w7 G9 z. h% r theLW->WriteLine(" ");4 U) s. T6 Q, @! U
theLW->WriteLine("End Custom Information");' s$ m# \# l8 ^; F9 j, r4 _7 ?5 q
}9 q- S& I; X& k: k+ Z6 T
catch (NXException ex)
2 D4 Q* ~1 Q9 v3 r3 [! M {+ q h/ @, K7 o- |7 J+ ~
ECHO("Caught exception: %s\n", ex.Message());
{) s4 V" ~/ C }' q; C6 {. g2 J) Z4 [+ b1 F/ ^
return 0;3 Y9 i/ S! P d. ]; h, D9 R4 {# M
}7 b; V1 ?) q- R. b3 [% ?5 Q( f
& K8 [$ K& z1 i( A( J
//------------------------------------------------------------------------------3 n/ X. ~' X2 h3 c" h4 g
// initUDO
9 \2 q7 E* m3 Y// Checks to see which (if any) of the application's static variables are! ]2 C" O( q, S% {( X2 A$ b
// uninitialized, and sets them accordingly.) {+ N3 K! Q4 D/ z5 N9 q1 v: E) q
// Initializes the UDO class and registers all of its callback methods.
, {( ~" t7 x. f2 l: L% @- [1 @; v//------------------------------------------------------------------------------, R8 L8 `5 m" S/ L! P
static int initUDO( bool alertUser)
9 w q; ]' F* f l' a( e{6 a7 |0 g6 \( N: y2 Y9 k
try
0 ~2 V. S8 L* C( U {
3 k' P7 H1 f( b2 b: c. \; ]# K if (theSession == NULL). V4 _8 S- O, f* i
{; @. N, _; ?9 g
theSession = Session::GetSession();
- \; K6 K3 q% P$ Y" d }. ~0 e* Y' T W+ }( X
if( theUI == NULL )1 f6 p% ]6 `3 {4 |/ U
{. L# r: \) G, D% t O# c
theUI = UI::GetUI();8 t8 i1 h9 b* e: l. m7 q
}( F3 G& N; u, b4 [8 ]6 o; v& r
if (myUDOclass == NULL)$ d) E& T" z' m8 N! E K, \
{" k0 [3 V* z" r
if (alertUser)/ T! y4 |( E/ C- U. Q0 C) [* W
{: k! U L B M
ListingWindow** D- ^4 l- ]5 R- V3 \
theLW = theSession->ListingWindow();' a1 K- o! y% K
theLW->Open();
/ F3 T! x; x" C7 g$ p theLW->WriteLine("Registering C++ UDO Class");
@% v$ n9 k! Y }% E$ ] B; ^, h7 D: r
// Define your custom UDO class( h8 |" s4 e$ |& o0 J7 f
myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");
2 \/ d7 [" S. ]: S. X // Setup properties on the custom UDO class
9 `# L/ b% {, T" f/ W# i myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);
- w8 V8 D9 J4 R; Y6 m ? // Register callbacks for the UDO class, `" ^" h6 s% e
myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));
; y( F. P& F, b! l myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));
7 B% `8 f" Z+ u9 A' t6 a myUDOclass->AddFitHandler(make_callback(&myDisplayCB));) ?- o$ w5 X4 y' R4 p( p
myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));( Q/ N3 z8 R% t% Y. s y! m( C* T
myUDOclass->AddEditHandler(make_callback(&myEditCB));* j& M7 R. C6 V) ?$ I
myUDOclass->AddInformationHandler(make_callback(&myInfoCB));
& R! O- H: e7 w7 m; }4 N // Add this class to the list of object types available for selection in NX.3 M4 T$ Y9 h6 e$ K
// If you skip this step you won't be able to select UDO's of this class,1 t7 f, O7 a' G% H$ }: Q
// even though you registered a selection callback.
0 u6 J( ?; q9 ~) T. s8 [. A theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);0 B U/ _+ `* N' r. t' k" e
}
" v( O- u d. i4 l }) ~4 A( E. e1 M4 g
catch (NXException ex)/ j; [3 x7 s! Y/ T7 y7 N
{
" c m5 p3 \" ?4 _6 G% T' G8 k ECHO("Caught exception: %s\n", ex.Message());
$ r5 ` u" L, ^ }3 V( \9 r; B* P) ]
return 0;
" K+ f0 h2 t4 r' R) _ i( p; i}
/ N6 H' ]; f7 A# D. Z3 r) s. w) s0 ^- z9 Q0 z4 r( r, ?4 e3 K' g: F
//------------------------------------------------------------------------------
8 h% B5 {3 m2 |5 U) J% Q- N// ufusr (Explicit Activation)/ l2 G: Y+ T' }# F! y
// This entry point is used to activate the application explicitly, as in6 c2 L& g4 D8 J, b4 q
// "File->Execute UG/Open->NX Open...", W0 N1 [1 o) m, a
//------------------------------------------------------------------------------+ z6 G" O! l2 `: e: f" S5 T" o
extern void ufusr( char *parm, int *returnCode, int rlen )
& h$ Y% M1 _/ J- a7 ^6 Y6 f{
C% _# [6 c0 g6 ^( z9 ~: O% U try5 E- }. N. T$ \& h' `! u
{
) O+ i; |1 H3 w* w // required for calls to legacy UF routines
# y5 M# c, z8 ?$ C // such as UF_DISP_add_item_to_display! d$ _! ~ F5 e+ D) t
UF_initialize();
$ O* ?* g8 J$ ~( a0 C
, k8 T0 A$ T/ R; \9 a; W( G // initialize the UDO - if we didn't load this library at* z p% r# ]2 R" }. w M
// startup, here is our second chance to load it8 S% ?" B, [% `/ H# n& D
initUDO(true);
0 s S7 `2 P5 n# k, f2 Y' A j% F( o" X( _
// if we don't have any parts open create one* E( @( k& k4 t' u- Q
BasePart* myBasePart = theSession->Parts()->BaseDisplay();
, c. S3 o: l3 a: b1 j j4 z) H if( myBasePart == NULL)7 I4 i7 C5 _8 e8 @0 D( c8 S! k3 G! V* P2 K
{# Q5 A6 W1 W) _& t4 d% U
myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);# u8 }0 |/ f- d
}, s3 A4 T& I( P3 S( b9 k. |& x
& z8 J4 p$ n7 O- r4 \" \ U
View* myView = NULL;: ?! N( ^/ }0 \# P( U4 r3 |1 c
Point3d myCursor(0,0,0);
: {% I! Y: }5 f; ^& E% |# R/ d3 n# v# e& S
// ask the user to select an origin for this UDO7 H3 P. X" u+ g
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);
. x! a7 x O% W% W% S9 Q6 y if( myResponse == Selection::DialogResponsePick )
1 q; X5 H/ T* e0 w3 S6 P$ w {- k! c& K) I( w4 H) k' D
// The user selected a point - go ahead and create the udo
. r' Y: T1 i! k& T( ^4 a UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();) f! P0 D' K7 u8 [" I4 r
UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);6 M G& f9 t2 `+ t0 h
// set the color property of the udo - just for fun :)& N( T& x X& E3 [4 o- y
firstUDO->SetColor(36);# @% O( d! s- J* S* r
// store the origin selected by the user with the udo
) S# ~8 E9 @. ?. u std::vector<double> myUDOdoubles(3);2 H+ _3 l8 s$ ?2 ~$ _
myUDOdoubles[0] = myCursor.X;4 F0 e: g5 ^5 B
myUDOdoubles[1] = myCursor.Y;5 A) C) Z1 ?6 G' V- ^& @3 G, K" X* j
myUDOdoubles[2] = myCursor.Z;
r1 P4 w A" M5 }+ ^. M1 l firstUDO->SetDoubles(myUDOdoubles);6 M6 a% {; R/ F0 [# d: ], f- T
// add the udo to the display list manually2 C+ {" P% p& H T7 [! V+ G
// this will force the udo to display immediately
8 m$ \0 @# P# ~; o+ D$ [ UF_DISP_add_item_to_display(firstUDO->GetTag());2 p! h2 m1 _2 ~, [! ~( }
}' t% w! f0 Y" V4 C4 f( _4 h
UF_terminate();
- H8 j- g, v9 I* d { }3 x0 i( C8 J6 o9 t3 L. E
catch (const NXOpen::NXException& ex)
6 ^0 q- G9 W: ]' h* x {, d) y+ ~) _0 ^' ?& U2 j8 e
ECHO("Caught exception: %s\n", ex.Message());* F. x0 t* _) o, u U
}" A: l" B" v: R
}
. l I$ }6 V/ f) ]4 K f" j, ]4 P7 R8 V/ s0 a9 s% V7 }" [
//------------------------------------------------------------------------------& Z3 a7 a+ U# U0 _
// ufsta7 V) E' I( a: E0 x2 l) [8 D
// Entrypoint used when program is loaded automatically
) T- o6 Y6 J7 H( K& z// as NX starts up. Note this application must be placed in a
; b' i l* f, s5 Y// special folder for NX to find and load it during startup.
6 I' L2 F7 r" @// Refer to the NX Open documentation for more details on how
1 {. b& @- D4 w! h% D2 u+ u! J" S// NX finds and loads applications during startup.
$ ~- j5 k# Y6 J+ q8 M//------------------------------------------------------------------------------6 K- K, \/ C8 j j7 X
extern void ufsta( char *param, int *returnCode, int rlen )
) X. V6 J y5 @9 r/ T{' u N% ^) R% s
try. ^% B8 K- l! t4 d$ F7 Q( i
{- ?. l2 N) Z2 `' Z
initUDO(false);. N) _7 L% u/ D) m9 ]: e: Q8 ]% I& \
}1 S$ M S; x( N) @
catch (const NXOpen::NXException& ex)
; s; ]0 K$ Y+ F {
- S( g& v: q, m' ~ ECHO("Caught exception: %s\n", ex.Message());
/ _( e0 G" I' r/ Q! o }8 z1 p7 F2 u; ?: |3 b, p6 ]
}
2 @4 @8 ?6 [$ G0 j* L
2 r* @' _& |. S1 ~//------------------------------------------------------------------------------, G0 Y( S3 \" i
// ufusr_ask_unload
0 T1 ~( h* X0 d0 `# O3 V// Make sure you specify AtTermination for the unload option.
. D9 h; ~$ y& s, M ^4 n9 ^8 _! Z// If you unload the library before the NX Session Terminates
/ O! X9 a% j% Z) x p// bad things could happen when we try to execute a udo
/ p2 V7 {$ ~1 k! Q1 y// callback that no longer exists in the session.
, V3 p" J" w; g! ]$ z1 a& }//------------------------------------------------------------------------------
3 ?6 b* s9 y: P9 h+ S2 Z, `extern int ufusr_ask_unload( void ) O; v8 z& M7 m- ~$ ~( f* I
{) D" }; m( h8 t( w/ ]
return (int)Session::LibraryUnloadOptionAtTermination;
; F: `( p' \1 \6 k3 p/ E}! O9 x9 L( S$ l t
1 i7 w+ J, C8 l0 v |
|