|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
8 v9 Q$ O2 A# Q8 z' M
2 m; ? m0 q' G+ T9 {- a6 XNX二次开发源码分享:NXOpen C++创建UDO的过程
9 b" ^1 S, d* b9 b2 ^
* f8 d7 G4 I. S4 G$ G( `- P$ v* s: K9 z
UDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!
( A9 ]/ `/ J/ x1 A
8 }; X& W! w9 s
O; l- Y% ^/ s& p% j4 v2 y& f
$ U, h( P4 P z2 l#include <stdio.h>
( G9 \2 M$ ?0 I! S( W1 H: D7 M% v8 q$ h#include <string.h>
6 M2 S h5 X0 t5 f7 _% b0 v2 E5 y#include <stdarg.h>" d0 z, g* Z; f( D
#include <uf.h>' o9 ~) T- w. T- t5 H
#include <uf_ui.h># {4 H. {% y# J$ b5 Z% l8 m
#include <uf_exit.h>, @: g' J0 {/ i* c9 u% N
#include <uf_disp.h>* n o& J/ L& h1 I
4 v) z7 z4 G, K( istatic void ECHO(char *format, ...)3 W; W' a* S9 b0 M4 K& K: W/ q/ \) q
{0 `* k" A0 U, ]7 _
char msg[UF_UI_MAX_STRING_LEN+1];
0 f2 W! ~; y8 O) k, K" C% Z va_list args;
# o" ]+ }! L0 K/ @ va_start(args, format);
: Y5 O/ b! i* `& y! l6 R vsprintf(msg, format, args);/ b$ u C c7 _2 c
va_end(args);9 [( V- ?, h0 E2 E
UF_UI_open_listing_window();& d* r3 A! n; U( v+ R4 l0 A/ u* K- e
UF_UI_write_listing_window(msg);
0 o9 h7 G7 z0 P! @ UF_print_syslog(msg, FALSE);
* E0 ?8 B& \$ G}3 h3 [ [$ ^9 e/ R$ }3 a5 ?
5 Y& x$ @, Z7 ^9 s
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))1 ~$ v) D/ e% k# q5 p& A/ g
. ^( g' m `# a6 j8 _* y; kstatic int report_error( char *file, int line, char *call, int irc)" K" T+ Q7 D" e9 ~$ ?
{
; ?9 \/ U. f$ z, K6 j if (irc)1 d5 I* k9 [5 S! |
{
+ ?' t( y5 z: `; N$ y# t/ \ char err[133];
8 W/ M1 }6 P0 s
9 E C* {- }) { UF_get_fail_message(irc, err);
3 a, Y5 g; J( N0 U" }$ d$ ]3 c ECHO("*** ERROR code %d at line %d in %s:\n",
* u9 W u2 @1 @( r irc, line, file);" N& J; Y6 F2 g8 j7 d. H ?6 w
ECHO("+++ %s\n", err);
* G2 d9 K! n" U. P7 X ECHO("%s;\n", call);! a' T s3 l' D9 C
}
% O7 o& g S9 z( K1 r5 b( q) v" v) H1 F& C+ i' g
return(irc);
3 R* Y% j. I4 U1 S' a# g}
( o0 u& F; A+ y5 z3 J: ?- V* O1 p9 d1 \ \: L( M+ ?
#include <NXOpen/Session.hxx>
' w* I5 J' l+ D2 D#include <NXOpen/Part.hxx>
. f: }$ q) W3 s4 I5 j- S#include <NXOpen/ParTCollection.hxx> ! R$ Y) g& `' c% x: z
#include <NXOpen/Callback.hxx>
- X4 m3 B9 J. v. o) `#include <NXOpen/NXException.hxx>4 p; B$ w0 ^: Y) }
#include <NXOpen/UI.hxx>0 f4 O/ M! m% _: [
#include <NXOpen/Selection.hxx>
$ K3 f6 E- V9 M2 o/ k5 q1 a# R2 ?3 c#include <NXOpen/LogFile.hxx>2 p1 {4 c3 U" U& z, D( q; w/ h% J4 y
#include <NXOpen/NXObjectManager.hxx>
! }: H% u$ d. g& k4 r; ]9 U#include <NXOpen/ListingWindow.hxx>
. l0 A0 J. E* F, F#include <NXOpen/View.hxx>
" _( Q g2 U7 n5 K& `! l9 h
% v/ e7 V7 Z8 m3 f- n#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>
# R/ e4 J& X" {4 b9 f0 h; \#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>4 n0 B( ^- T' G. S- [: l- Z- s6 ]
#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>
' M! V! z8 T" \% [; c( z#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>
& t) ?, B- _' X3 E4 \, v#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>, X, J7 T3 j5 z1 ]: u h' k7 D* T
#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx>
7 c. g/ ^2 l. h7 ~& h" i2 P#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>
& R9 j- Z# P8 l1 M7 z2 ?# D0 E#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>0 T/ k4 v+ i( k4 y
* f' ^8 r$ r4 J4 M/ R8 g; kusing namespace NXOpen;$ i" f) h; i/ V8 I1 b
using namespace NXOpen::UserDefinedObjects;
' k4 f2 g( R5 `" W0 h: R# Z; _1 L) S4 [3 @/ c6 ~0 [
//static variables
: V( z8 U" y' |: F* Y2 g+ kstatic NXOpen::Session* theSession = NULL;* X* O8 Y, G8 o& S/ F* ~
static UI* theUI = NULL;
9 l' `9 i5 @2 O* p4 m. F$ ]static UserDefinedClass* myUDOclass = NULL;
' @, O+ u& v. G3 P, e
: l/ z, I5 x% z/ C, l( ^/ \+ E//------------------------------------------------------------------------------
$ D& ^9 K! c3 U* J, n; W2 `+ _// Callback Name: myDisplayCB
! ~! l( g+ V- M$ V- N% ?// This is a callback method associated with displaying a UDO.
9 h; d) M( B* F+ K! z// This same callback is registered for display, select, fit, and attention point
1 d% S2 n$ C, M& {. {) S3 W//------------------------------------------------------------------------------
( N4 m/ j4 G, }! x7 \extern int myDisplayCB(UserDefinedDisplayEvent* displayEvent)& `$ ^, b/ m0 Y* z+ \
{% L% a- `6 e2 D" _* U# W
try
) f2 m# e `: ~4 B {
! m5 Z. {" f/ C7 u# _2 B$ R // Get the doubles used to define the selected screen position for this UDO.
4 R* I, ]9 M1 C2 L) f% J% u std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();4 s0 [' f) r* A$ Q R0 C% f: f( q5 \8 H
+ }% }9 J! I. U+ m$ D
// Use the doubles to define points of a triangle
/ T7 V) I. y5 t/ Y2 f std::vector<Point3d> myPoints(4);7 |$ H! E- G$ r' ~- A
/ `+ y: h7 n7 Q+ m: V) N myPoints[0].X = myUDOdoubles[0] + 0;7 F/ z% Q& o' j) S2 t
myPoints[0].Y = myUDOdoubles[1] + 0;
. n) F/ |/ ^, e2 C# R; l* }9 i myPoints[0].Z = myUDOdoubles[2] + 0;
0 r. w( I: D* ] y1 i' V+ r
6 x: c+ L$ a% F% s6 J, o myPoints[1].X = myUDOdoubles[0] + 100;0 t# _: N* P$ z7 A: r, C. ]
myPoints[1].Y = myUDOdoubles[1] + 0;7 {, {4 M8 H4 g7 D. j' ~% M6 B
myPoints[1].Z = myUDOdoubles[2] + 0;
9 e3 i c/ S' n% g P$ r$ R& a7 {7 O- n T5 u7 l! P
myPoints[2].X = myUDOdoubles[0] + 0;( G4 N, d! B- n
myPoints[2].Y = myUDOdoubles[1] + 100;
+ ~0 F4 B, _( G; F; I myPoints[2].Z = myUDOdoubles[2] + 0;9 S. x, ^8 M- p+ X6 v1 O4 [
7 i& G7 d( i9 { myPoints[3].X = myUDOdoubles[0] + 0;$ F# i$ v/ r* P1 H
myPoints[3].Y = myUDOdoubles[1] + 0;: E& n: }: j/ j1 B* s
myPoints[3].Z = myUDOdoubles[2] + 0;
3 x7 p1 o i- U- w! r+ A
7 k1 f+ J' ]# } // Display the triangle
, p4 L% w' Y, d+ v# b* A1 L+ }8 t displayEvent->DisplayContext()->DisplayPolyline(myPoints);" R9 `" W( U j5 M% l- x: S
/ ?! S0 V: s5 i" v8 v6 a6 V# h
// Display the text next to the triangle
+ U% T4 ]/ d c1 P Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);/ Y5 G8 R l7 @' s, k4 E
displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);
5 \0 y# K) O0 F+ [! i' Q0 l( J' M9 ]8 d8 N5 N0 F
// UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 67316533 R7 a" E# K- d# f
// This demonstrates using UF_DISP_display_facets as a work around
& J6 A. r, ^9 n3 ^, d double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };* w0 W T1 m6 N# B# Y5 k
double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],
) Y% N3 n* h y5 Y/ z9 m/ A( H myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]," r7 `) C% h8 U
myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };* u4 A: q4 o, q# A4 w9 \2 j; Q( i% d
UF_DISP_facet_t facets[1] = { vertices, normals };6 n5 r7 o; ?* T: v# d( ]
void *context = displayEvent->DisplayContext()->GetHandle();# T( C$ T& j- T- R, _% }; D$ h7 t
; M$ Z" y$ X Z0 l' S UF_initialize();
: Z2 S4 k6 g( R UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));
$ j2 U+ D- I: K5 i3 J# P# N UF_terminate();
5 P' r% P0 M6 x' z0 ^
& t. N) f: I5 R: a+ k* s( H }: b+ J; E. y8 P B- ^
catch (NXException ex)2 C" |- Q% V1 A9 |# q+ A
{$ S$ h' T2 J* u3 n* n: d& I$ e
ECHO("CaUGht exception: %s\n", ex.Message());, o1 L( q' }4 W- P& a
}4 p2 Q0 i$ m3 L5 S* |8 B
return 0;+ f! u1 i3 i) t. F4 J" n7 g" Q
}
) R3 S- z" K& h! f; [5 g4 W//------------------------------------------------------------------------------+ `4 Q! H' [) L5 o& ?( s. n
// Callback Name: myEditCB
8 h9 O6 F" e8 C3 P/ V8 p// This is a callback method associated with editing a UDO.% \7 h* a; s2 ~7 m" B& h# E
//------------------------------------------------------------------------------9 J$ ^( a0 d3 F4 S+ j, N4 R
extern int myEditCB(UserDefinedEvent* editEvent)
( h T1 f( o' D* [5 p. Y, d{
! R, [8 L/ i0 n. l1 v4 D try1 o4 ?9 h6 d' z" \- [# S+ m1 M+ q
{% R: ?6 ?& ~2 v1 O
// required for calls to legacy UF routines
1 `; K* L1 Z& z( u2 \5 `0 O. q7 l // such as UF_DISP_add_item_to_display
/ N7 t, f3 ]2 E9 r# L UF_initialize();
+ u P7 E3 N! f* ~6 S- n
+ ^7 F; y; i* u# i View* myView = NULL;
4 g& e' v* ^0 h! n# k, n Point3d myCursor(0,0,0);' U4 I& A+ ]! K: j
6 K5 O0 B! Y- S x& W7 D. Z0 o // highlight the current udo we are about to edit$ w- j& S" D% ~6 `
// this is helpful if multiple udo's were on the selection
( s( R1 t* T9 f: O' @5 J // list when the user decided to edit them
) V0 } d% _( k editEvent->UserDefinedObject()->Highlight();3 M5 o: e/ d/ \2 z1 t8 l
, O: [1 L U% ^ // ask the user to select a new origin for this UDO3 d9 S4 K# n' j+ Z8 _' L
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);
! }: O& Y5 u- I // we are done asking the user for input... unhighlight the udo( h! i6 p4 [4 R7 f2 z* k" |
editEvent->UserDefinedObject()->Unhighlight();
7 v# T, w, c$ t ?# W
0 m. Y# g+ y) j7 `1 m3 M5 B // use the new screen position (if the user picked one)8 i8 V5 k; \2 \1 u- y
if( myResponse == Selection::DialogResponsePick ): A( a! {4 V: c" U
{
. G3 o3 |. s- T5 e* A std::vector<double> myUDOdoubles(3);
( v R- ~9 Z3 l; ] myUDOdoubles[0] = myCursor.X;( s: |2 Z1 P$ n |* C, n
myUDOdoubles[1] = myCursor.Y;
: S' J+ t+ q1 ~. p- F2 s myUDOdoubles[2] = myCursor.Z;) _, N) c1 U$ ^, x
// store the newly selected origin with the udo9 s+ g" N" @% z0 G$ G
editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);- O. T: a8 X1 E6 c! x& m
// add the udo to the display list manually
- `: r% |4 P. A$ @" h // this will force the udo display to regenerate7 r) s8 m D% _/ w( J+ @" d
// immediately and show the changes we just made
2 ^! t/ x0 O W7 N9 ^ UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());
7 }( X. T' W p( o: N W }1 Y5 }# M9 Q# D' D
UF_terminate();8 g3 m+ _+ t7 [4 x/ i* G
} catch (NXException ex)
1 H5 t0 c1 `! n- e4 y% g {
: `& E8 o2 S/ I$ `/ |' k; N ECHO("Caught exception: %s\n", ex.Message()); W& Y8 C9 H9 u$ |! r
} return 0;4 f1 x! @. e, b; X& x
}2 W# M. d G& V5 o2 h5 m: y
//------------------------------------------------------------------------------0 V5 [& @, G. d/ R1 b2 Y- x0 T
// Callback Name: myInfoCB/ M& Z$ f( T& t, O
// This is a callback method associated with querying information for a UDO.9 @) v6 }& S% M$ s1 U
// The information is printed in the listing window.9 {1 W/ @% @* Y
//------------------------------------------------------------------------------
* d+ u$ T C9 hextern int myInfoCB(UserDefinedEvent* infoEvent)
% e: i/ f }5 Y( _{
' E$ K+ Z/ g5 |! w& q' G [. r+ m8 b try. W8 c/ K. A& b( }2 h8 O. c
{+ Q `" s2 |# N8 M! B, V0 z4 V
ListingWindow* theLW = theSession->ListingWindow();5 G, [+ e& @ J0 i/ q5 e
char msg[256];
3 m ]( {3 k( z; _3 J( s. q! B theLW->Open();0 l3 J2 T* `4 U8 \( a' @$ H8 g; O! O
theLW->WriteLine(" ");9 m* V/ h3 U" ~. _3 j2 R' i8 c1 z. q
theLW->WriteLine("------------------------------------------------------------");# P) p/ y7 q+ w8 z' |3 c7 k" i
theLW->WriteLine("Begin Custom Information");
* O3 T0 F7 l) V) `" a theLW->WriteLine(" ");
* Y% ?$ p3 L, u4 ? sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() );/ `6 w$ x* X# j- v1 B( ?, x! c
theLW->WriteLine(msg);
4 P! l- c, g2 a0 f. R0 o% j sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );0 P; W) s/ o- \/ W; W
theLW->WriteLine(msg);5 q6 t8 m4 { p- J
std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();. | l! m4 z' z! P$ ?
sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );
* r# d. e: D3 ^+ ^: E, H theLW->WriteLine(msg);
& C4 Z" s) `& [; V& ~- `6 L sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] );
+ J/ x1 \& H4 i6 n. T# ?( i theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );
% o/ G9 h3 S4 W+ g" P* Z theLW->WriteLine(msg);0 x& ]# h, _7 j! E6 v r/ |( x* Z
theLW->WriteLine(" ");
, Z1 k' z4 `6 x! B' B! E' c theLW->WriteLine("End Custom Information");
/ \1 J" u5 I5 g0 C" ~ }. Z0 e7 r/ Q: `9 ?$ G* l3 H& \
catch (NXException ex)$ F2 k( E [# G6 V- x% r
{( f' T$ L1 i! q- y, O, [" i& O
ECHO("Caught exception: %s\n", ex.Message());; a& x" V5 b9 I0 Y
}8 }( z6 F* h* T
return 0;% ]( H" |& B( N0 V: l; r/ f; t
}
0 c+ q- E( M* J9 i* |1 X& G2 G( x& f7 Y
//------------------------------------------------------------------------------* J2 T* R9 ^2 o% l
// initUDO
3 s- V/ l! l9 x% X// Checks to see which (if any) of the application's static variables are/ {) G6 v* E* ^5 k$ B
// uninitialized, and sets them accordingly.7 Q9 K9 W8 e, C; Z. {& n0 r
// Initializes the UDO class and registers all of its callback methods.
+ L) ]1 N7 g- S- H+ `% J" {//------------------------------------------------------------------------------
/ M2 U! k! L6 w" R# N# A0 e% gstatic int initUDO( bool alertUser)" A( I; `& Y& K# ]" u
{
! i/ c. X5 z& V6 }# o8 a W try) d5 A) h! K7 @1 C
{
3 k5 |+ e4 G( F: P7 T" ? if (theSession == NULL)' D9 q1 h1 U3 m6 H/ ?* J7 K
{% j% I# r9 o2 V* ]
theSession = Session::GetSession();
! A) O) a' q/ ~4 v% J) s7 D }
" a4 ^( c6 I: R" T3 |/ B0 T if( theUI == NULL )
; o$ j- D9 d5 |8 x9 f) T7 m7 k( g {
6 M( f3 v2 ~% N: {2 C; o% n9 s* A theUI = UI::GetUI();; z4 t P1 d J0 z; L( ?
}3 D; n& l) y; ?
if (myUDOclass == NULL)8 B8 E, O' W! `+ a
{! {2 n' d' j" P: h6 G6 ~
if (alertUser)
, w* y, u$ q1 x# x' @. I# N {' Y% e: i4 ~4 {7 Q' {5 [+ @% y9 h
ListingWindow*
9 ~/ C9 S$ `% W9 P/ p/ P theLW = theSession->ListingWindow();
7 \0 ?4 \; @, [& o# T! C0 L theLW->Open(); ]9 [5 q- S: B) F' R& R
theLW->WriteLine("Registering C++ UDO Class");
; m* B+ y* S* n4 k, m& g9 q0 ? }+ i$ M) f2 c" R, W, ]5 r3 n8 v2 K
// Define your custom UDO class. a2 L( Y; Z: W( e, D" r; C6 x" l" \( m
myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");5 ^6 F" f( {. I" y' j8 x7 _
// Setup properties on the custom UDO class
- Y' k( u" h% i& w" c3 n myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);
( c' k$ ^; Z, Z2 d5 x7 G9 E" r // Register callbacks for the UDO class
3 b7 i# Z' O P) V" Q1 D' a myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));
' Y/ ^ h: T0 \# _. }2 R3 Q" Y myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));9 p1 z" I3 Y, G" o& x2 v! F
myUDOclass->AddFitHandler(make_callback(&myDisplayCB));
, M7 ?8 y6 q( l0 q6 i ~9 ]$ E myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));
+ M4 U0 Z% ^. W" d/ g2 P* b6 u myUDOclass->AddEditHandler(make_callback(&myEditCB));
K6 e: n5 \) D0 g- ]+ p# h myUDOclass->AddInformationHandler(make_callback(&myInfoCB));
" z) I, U- P+ Y( n. ~( D // Add this class to the list of object types available for selection in NX.( L! R t$ o4 p
// If you skip this step you won't be able to select UDO's of this class,5 J" T" c8 p, P7 ^- ^; r& ^. u8 ~
// even though you registered a selection callback.: G6 D, @- f, ` N) ^; P$ F8 m9 H
theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true); e9 a' i3 a: U; O: v
}4 \1 T/ V# u& |* N) @) T$ H
}$ t; ]& D6 e- ~6 O
catch (NXException ex)- t& A+ m* k/ Z# V+ [
{
4 I5 d4 m. n: h; H) y( e8 e( c. D0 v ECHO("Caught exception: %s\n", ex.Message());0 q9 V% k( k# U1 N9 f
}: [6 v! g: N/ n8 F1 @
return 0;
# E6 V( ~+ V/ r0 {0 y}$ K8 K5 j" N; c1 a
4 A% _" U7 w8 v7 |
//------------------------------------------------------------------------------
# {: T7 g. |: c/ ~7 N// ufusr (Explicit Activation)
% d7 z/ ~& T: v% E2 L; v, t// This entry point is used to activate the application explicitly, as in
) Y- h o0 C. x8 c// "File->Execute UG/Open->NX Open..."
8 O2 x, @ @5 [6 Y4 H! T//------------------------------------------------------------------------------: b$ I* w2 M; [6 G2 |
extern void ufusr( char *parm, int *returnCode, int rlen )
8 y( D, g$ F6 J{% Q F+ o! Y$ }$ }4 |+ u
try
1 X& R$ f' U% @7 B {& B& n6 [& \3 \: q+ S. g; `
// required for calls to legacy UF routines+ q3 s! R4 Z" S0 s8 k% j
// such as UF_DISP_add_item_to_display: x4 M5 M ~" \( i
UF_initialize();
& J$ e9 u) W0 O5 } e. `5 Q1 w
0 U1 h, K T/ U2 U, e- Q0 N // initialize the UDO - if we didn't load this library at
% j/ @/ ]2 g7 d9 R // startup, here is our second chance to load it
" [1 l5 Q5 K) _( D9 D initUDO(true);9 f9 L, D$ ~5 O! v% j. u
' w" Q: a2 V& D) s7 F6 k7 b
// if we don't have any parts open create one1 c" l( B [5 ]) t Q& M
BasePart* myBasePart = theSession->Parts()->BaseDisplay();
9 ]: k1 N& ?$ c8 p if( myBasePart == NULL)8 b. b* Z+ `/ L1 d/ Z6 ^+ ]
{4 ~1 Q, U' Z: @6 n
myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);. C* W$ C" W: U4 b" p1 k
}
B+ K2 P! h, j _9 K; ~8 I9 w q- k2 e
View* myView = NULL;
- {3 }8 c6 C3 w' D$ ?+ Y! W Point3d myCursor(0,0,0);
& c+ j" \ e9 |8 e; {" f+ K( W9 e1 E& V
// ask the user to select an origin for this UDO- p% S1 p5 `- ?& C+ \* a* p/ z
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);! }7 f% P% J) Z, j5 n5 e
if( myResponse == Selection::DialogResponsePick )
( N- Y0 q( S$ v {, d) P. J* u/ A; J
// The user selected a point - go ahead and create the udo" G/ ]1 Z5 z6 m, `* b! `2 X
UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();7 r4 P4 N) Q" W) Z# J# E
UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);$ l6 o8 W, b; r6 X7 d1 D* p
// set the color property of the udo - just for fun :)! @$ N) p$ D0 n5 Y% ?2 M
firstUDO->SetColor(36);
) d* p$ ]3 P# D: q; X* i // store the origin selected by the user with the udo. V% g+ B: ^4 o7 x8 \, O
std::vector<double> myUDOdoubles(3);
4 j6 _+ b- M L* I) X. d myUDOdoubles[0] = myCursor.X;7 w! o. [: B2 K, P) q
myUDOdoubles[1] = myCursor.Y;$ A" F! f7 P1 d) I$ ?
myUDOdoubles[2] = myCursor.Z;
1 Y v6 E& B$ T firstUDO->SetDoubles(myUDOdoubles);
9 ~. ]- v( N1 h2 w // add the udo to the display list manually
: g! h: D+ s* |) i // this will force the udo to display immediately4 q) p% d) k, T0 O+ V ^' G
UF_DISP_add_item_to_display(firstUDO->GetTag());. }0 d% u: c0 q( W/ o
}+ p2 \* }3 r3 d( U" H! @
UF_terminate();2 G2 _2 O1 q& O$ ?
}7 ^' y' @" g6 i
catch (const NXOpen::NXException& ex)
2 q/ L/ c* ?1 V {/ x: m2 n6 g4 @3 t9 }) r3 J/ O
ECHO("Caught exception: %s\n", ex.Message());- S2 }$ A$ G, |$ H/ p
}
7 [5 d9 R' T: j8 t}
5 y, G8 O3 E- \- k$ l Z9 w6 X$ j
9 V2 j" N R* M//------------------------------------------------------------------------------. w, l; n+ W' _2 U4 J
// ufsta; ~' ~/ ^7 ^( W& x7 I
// Entrypoint used when program is loaded automatically4 G( C& \% U* \6 R* O1 w
// as NX starts up. Note this application must be placed in a
: w3 G8 R, e$ b# r: q// special folder for NX to find and load it during startup.6 Q! L- A. p2 q
// Refer to the NX Open documentation for more details on how6 s2 y: w5 R( @8 w# G; `
// NX finds and loads applications during startup.
3 z5 T! M; Y# L8 H8 A//------------------------------------------------------------------------------) n" I; g/ y; ~" g
extern void ufsta( char *param, int *returnCode, int rlen )" U, K' d6 x3 |: }, u! ]
{, G2 K, {: w% B5 {' L' Y' T
try: ?3 \, F. m1 y1 L; a
{$ `2 k) G- b+ U0 @
initUDO(false);( x/ r! H2 e, u9 l& V( j; N
}! b4 k+ D2 n" W: L, {
catch (const NXOpen::NXException& ex)
1 j+ w8 v# V% a {
3 m7 c+ }* E. d5 { ECHO("Caught exception: %s\n", ex.Message());* m3 v' B5 i+ ?$ k- D9 ]3 U0 a; I
}2 x0 ~: ]; j/ L B" V- r
}
7 O! o; e" g+ Y8 H) l+ P
' H& A& y- }( n//------------------------------------------------------------------------------/ o0 h3 m. p5 l
// ufusr_ask_unload
5 U7 f# j* b( P" Y( A, p, y// Make sure you specify AtTermination for the unload option.* ~) N* h8 q7 Y) ? J4 V
// If you unload the library before the NX Session Terminates( l" N# H. }( w7 G9 U. f
// bad things could happen when we try to execute a udo( ?5 T" K, o+ ]: A# ?( A8 d9 P
// callback that no longer exists in the session.
. j, M4 v: }- K* _//------------------------------------------------------------------------------ q2 `" e. q# a1 M; o' x, G: x
extern int ufusr_ask_unload( void )
) Q9 v& x; ]8 n( n{
- `2 x" L5 z2 D4 l7 I- R return (int)Session::LibraryUnloadOptionAtTermination;
; ]& ^. a- t r) J4 Q. v9 b) M}
) V; K3 @- @( s+ T6 v6 ]
; V# K% \$ [. y |
|