|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
+ |" j. u+ G9 p, l$ a6 Y! x# r# i5 t
NX二次开发源码分享:NXOpen C++创建UDO的过程0 d5 H4 n4 |0 I, ]7 W" g; w5 Q% i' P
# \6 r# M* s, A1 P$ r1 n# |2 {
: p4 W, [* j3 z( H+ I( P7 R. ^2 a: O
UDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!
9 O }- l$ y `
7 d# T' g8 y$ o3 h$ Y2 y Z1 i2 k: j( w) T) m9 c
! H4 {0 `6 L' r* g5 a
#include <stdio.h>
) P5 M' @) S3 z1 d% j7 F( |! C#include <string.h>1 o7 A/ v2 U& H, g0 U3 f" O
#include <stdarg.h>
: H) ~6 `- `) B* N2 Y#include <uf.h>
8 M5 o1 F S1 g0 r1 {8 S#include <uf_ui.h>5 v5 n5 T2 _: H( k
#include <uf_exit.h>
3 d% B2 C; _$ O7 c; u# e3 K#include <uf_disp.h>, M6 m2 v! |! G# {7 Z$ d
& H1 d+ L8 j' ?" ?( @static void ECHO(char *format, ...)! B. E$ b! B0 W5 ^, X; \" P( F
{: y0 s9 l. _% W4 |
char msg[UF_UI_MAX_STRING_LEN+1]; Z1 i1 ^+ e7 D' H" _+ c( @0 P( N
va_list args;
4 {/ {2 t0 o& x! X+ s. J va_start(args, format);" Y* n$ p# _# d, |3 d2 t2 X) L( Q
vsprintf(msg, format, args);" Y" c5 ]8 ?+ q. o" M3 D2 y' V
va_end(args);( d/ r) h+ a4 X# J- F/ ]
UF_UI_open_listing_window();" f0 \0 K% u" E9 t" l
UF_UI_write_listing_window(msg);# ^7 Y- {" P, K2 g/ N# m6 l
UF_print_syslog(msg, FALSE);
5 |: \/ s, x" K}+ V/ u1 o1 ?; e1 n: w+ B5 ?, V
* o! k b1 n9 h3 G* x
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))) K, t6 z4 T9 X" G% @* n' Q
' k, ]& C ]/ Y% e/ ?* {! R
static int report_error( char *file, int line, char *call, int irc)% z% t+ `* N, E7 t, O2 u! j3 Z
{
8 k+ V* E) O, `- U+ Z4 ? if (irc)
9 @" E: d; t' r- A {* I" X; s8 H1 y6 c9 z T0 _6 }
char err[133];
l- A6 r* c% r s S ], P7 X$ q; C7 S9 t( N; B8 m
UF_get_fail_message(irc, err);& P( z3 {4 B9 @6 q
ECHO("*** ERROR code %d at line %d in %s:\n",' G9 G n- ?% u6 T, @
irc, line, file);
( _3 G7 {% k2 e6 D5 I0 Y ECHO("+++ %s\n", err);6 R I P$ x" l
ECHO("%s;\n", call);6 n T4 a7 v g* A4 v# u
}
' A, z$ \6 b- [: O' d! V/ s
5 w: g4 f1 g0 K return(irc);. G3 B h$ Z/ }2 t, P( [8 b" L6 G9 `
}+ W% J5 u! ~( U
5 H3 S2 i: n. ]" `8 G#include <NXOpen/Session.hxx>
; Q) x. I( x7 _$ B! n" s5 }; a" e#include <NXOpen/Part.hxx>! L3 L4 J1 y1 |3 `
#include <NXOpen/ParTCollection.hxx>
) ^$ m" z& Z6 H#include <NXOpen/Callback.hxx>
# ]* Q/ i+ f$ x9 R#include <NXOpen/NXException.hxx>
. ~! t" b, o8 |$ z#include <NXOpen/UI.hxx>
! U0 Y0 k: {: L- r5 a" e, [#include <NXOpen/Selection.hxx>
9 Z$ h; m3 J2 N# T k9 N( Z#include <NXOpen/LogFile.hxx>* B6 l: w8 c$ R) ^- ~5 _; z/ H
#include <NXOpen/NXObjectManager.hxx>
' w% C0 s- z% ]$ x' z/ q#include <NXOpen/ListingWindow.hxx>
& i. p5 w u; R- c9 {+ [#include <NXOpen/View.hxx>
4 Z- V6 [# y7 n: J6 _3 c) L6 v
5 N/ x% g- B9 u7 F9 v! k#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>
; I9 @7 i8 h+ ? G. g#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>
. t7 _- d d T0 T5 g5 v3 ^#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>
, O' V2 Q" F+ d v: |#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>9 X1 g1 b2 W t9 L/ z
#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>
% d" Q4 ^9 @# O#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx>1 z6 R5 z+ T3 }' {" s, ]/ O
#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>" S& g, D- u: K* ^. M4 O) D( P3 V
#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>
; [/ H% w C8 ?4 z
9 C n4 E v: G6 tusing namespace NXOpen;+ a7 @+ \/ S6 v; `1 q0 j3 U
using namespace NXOpen::UserDefinedObjects;
. C- J: d6 W6 V1 N0 k' [
# b A4 Q9 ~ Y# y1 D- `//static variables
6 K$ p+ Q/ ^/ h- \6 ?7 g" Cstatic NXOpen::Session* theSession = NULL;- b* I# B& N4 y: R% B
static UI* theUI = NULL;
+ r% c( q4 p# C7 u' Qstatic UserDefinedClass* myUDOclass = NULL;8 S4 o" m7 Z9 s) T
* K' t8 S( _& x! w
//------------------------------------------------------------------------------
) F; I- U: y, u* m/ m1 F3 w& r// Callback Name: myDisplayCB
* _6 a3 V5 ^) Z// This is a callback method associated with displaying a UDO.0 [* R4 s) P+ R/ j/ s
// This same callback is registered for display, select, fit, and attention point9 ~( a. h- ^2 p* W* T4 G
//------------------------------------------------------------------------------
, g( q4 z4 x+ Q2 Kextern int myDisplayCB(UserDefinedDisplayEvent* displayEvent)
1 h3 S& A/ \) p3 j7 }. r- _) U{
# x6 E( [$ \4 V) Y0 w" Y% b try
; G- k+ p! q4 e3 Y! p {7 C/ v& O# ~2 {( E! D. \4 K
// Get the doubles used to define the selected screen position for this UDO.6 S$ X- F: a* q# o
std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();
$ {# \" n% K: x$ R
& k0 ?/ Z9 x. ?6 J. W3 g: I // Use the doubles to define points of a triangle }/ _& S p$ }! |% x* O' E+ q
std::vector<Point3d> myPoints(4);9 u% c1 z. x, `8 S& O' f
8 \1 F. J6 _7 q' N# I/ g( |5 X
myPoints[0].X = myUDOdoubles[0] + 0;
# k* |/ Q' p( y- B myPoints[0].Y = myUDOdoubles[1] + 0;8 v' T9 {- e7 w4 q2 _
myPoints[0].Z = myUDOdoubles[2] + 0;
% ~ w! j) p5 i9 M' E+ \6 O% G1 _0 D
1 R, @% ?: p$ S- D" ~$ j& b; ?5 u myPoints[1].X = myUDOdoubles[0] + 100;) a3 m+ R$ M* r# l/ z8 R
myPoints[1].Y = myUDOdoubles[1] + 0;0 R6 B3 W' z( B& y- h
myPoints[1].Z = myUDOdoubles[2] + 0;
4 P' L( q+ b- h1 o1 @" K; f" r1 [
myPoints[2].X = myUDOdoubles[0] + 0;
" A! w, u1 a% O" a( w& S myPoints[2].Y = myUDOdoubles[1] + 100;: g8 t3 D" p! S; f8 t1 w9 G
myPoints[2].Z = myUDOdoubles[2] + 0;" k, J; L- L, g
, Z3 R- T1 t; j0 F
myPoints[3].X = myUDOdoubles[0] + 0;
: F2 c A3 F% ^1 K/ d$ F. m$ f" w8 S3 _ myPoints[3].Y = myUDOdoubles[1] + 0;
, C; E5 l; r: O* d2 y myPoints[3].Z = myUDOdoubles[2] + 0;
& A1 r6 c. L K5 }4 [" b5 R' O
- V, |' {5 t/ k" `! t5 S# x0 ? // Display the triangle
5 Z: ~, r/ p3 p, _ S$ { displayEvent->DisplayContext()->DisplayPolyline(myPoints);
5 M `/ d0 o2 o) E; b& z: Q' I1 W+ x1 z1 [: f, k
// Display the text next to the triangle
+ J |( p F0 ^- o! e! E Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);
g* q0 s) `# Q/ X Q* s' P/ W displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);/ `" M* o3 \4 J) B9 Q7 Z. X/ y
0 S' N. J( J. y }$ D0 ~
// UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 6731653/ F! m1 a/ P8 I$ P
// This demonstrates using UF_DISP_display_facets as a work around
; S3 C8 v. j6 Z. N double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };( l" V5 Y1 |8 L+ Y8 y- ~: u; n
double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],
{2 V7 `( r6 v5 u7 @ myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2],
6 A" g+ l1 G% m `, d myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };
: n. t0 w8 h* E& M9 C; _( G! K4 t UF_DISP_facet_t facets[1] = { vertices, normals };5 U3 R I7 V. s3 a6 e7 D
void *context = displayEvent->DisplayContext()->GetHandle();! t4 g3 w2 i8 j0 }" I+ k
/ k0 k9 F* Q7 ^ UF_initialize();
3 S+ s+ ^5 e7 d' ^! Y UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));
- M4 G8 h% w( T: c: {# V) A UF_terminate();+ p- G0 H: |" Q h) k) R5 |
, O2 N: h5 F( K* S$ } }8 i% X1 y+ ^1 |5 P/ X- A8 u& g
catch (NXException ex). B$ L1 F8 g* G& S6 }& y% k
{3 R- h4 I3 l4 H2 c1 G0 b
ECHO("CaUGht exception: %s\n", ex.Message());) V: o: F3 ~$ g
}2 l/ J7 A% \& Q" h: @; a r
return 0;/ ^# `9 T! q$ l: d, d0 v9 L' g
}
3 g2 f. p% M3 W3 b5 y//------------------------------------------------------------------------------1 A, s$ P: d! t/ g. {1 Z: }& ]
// Callback Name: myEditCB
/ y+ W, }. s5 r9 K1 w// This is a callback method associated with editing a UDO.
: a5 [; K! S3 Z: |, x% m) ~//------------------------------------------------------------------------------3 g" @5 c& u4 Z
extern int myEditCB(UserDefinedEvent* editEvent): ]7 v" }7 `' g2 p& a0 W! r: H, `
{
8 h4 { D' Y1 q! e0 R6 D! q3 z try) Y* J+ p V1 [6 M
{
. m1 G* _6 q3 V2 o# I1 ? // required for calls to legacy UF routines4 P0 [2 F% b# `
// such as UF_DISP_add_item_to_display# d, A6 ^# ?9 Q" T. S1 p' l$ R1 I' ? I: I
UF_initialize();; {8 i" ^$ h4 j7 v* T) J
- ~! }2 ~6 `& h Y# k9 S9 g2 ~ C; l; U
View* myView = NULL;
' H/ z! m% K$ L Point3d myCursor(0,0,0);
6 t, G/ B- Z5 h6 A0 {8 R" v% w
4 r+ E" z( L" e/ Y3 V, Z // highlight the current udo we are about to edit3 @1 ?; j7 b7 z* J3 }" o* i
// this is helpful if multiple udo's were on the selection
! r) \! n* N! X" `4 `* e& ~1 [! O1 r // list when the user decided to edit them
2 ^$ z: B' b0 p; s* V editEvent->UserDefinedObject()->Highlight();- \& F4 w+ A* i0 H, `2 u
4 i! c/ d; ], m' F& \ // ask the user to select a new origin for this UDO+ l, c# Y+ Z% R9 t
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);' l8 \' g9 M$ t, F+ e' {1 Z! N/ A
// we are done asking the user for input... unhighlight the udo; }: w1 w+ q) ?* O/ s
editEvent->UserDefinedObject()->Unhighlight();( ~* x8 I4 C5 B
% s4 a* P8 A# S, M2 X // use the new screen position (if the user picked one)
: ~0 l9 F9 X5 a$ V6 l if( myResponse == Selection::DialogResponsePick )
8 E5 r @. \& Y6 T4 i {
; h! L+ F" ?) c' y9 o/ t std::vector<double> myUDOdoubles(3);
9 L& y$ @+ \. } myUDOdoubles[0] = myCursor.X;! ?0 y, ~# Y. L) t+ j
myUDOdoubles[1] = myCursor.Y;" p5 R& ?8 r4 T) O
myUDOdoubles[2] = myCursor.Z;
6 n" |& {% @( g. a8 z // store the newly selected origin with the udo+ D( X* i( a4 k# x: L2 F3 w U
editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);/ Y1 b) t. L4 `% u( i
// add the udo to the display list manually/ c1 h# q! y9 b
// this will force the udo display to regenerate
6 ^/ t+ m" c2 g) k( |: }9 Q // immediately and show the changes we just made
: p. _3 ?1 v4 Q$ i! S" l: N/ C UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());
# {6 O% k# ], H4 T: T: A/ ?" q& c! x }
' z- s9 `. D' P2 W. b UF_terminate();* x& {/ S: n4 D, |4 c1 v3 J. A
} catch (NXException ex); I: x4 t1 [ r A7 R2 H
{8 C) {' P1 ^" B" q) i- ^
ECHO("Caught exception: %s\n", ex.Message());
3 U0 p( Y1 x" L2 H% M0 j } return 0;* J% Y6 L% T0 C* i# n" ^- k5 j
}
. x5 @* E+ U' G% R7 j7 {//------------------------------------------------------------------------------
& T) ~" J. n- x" A( Y/ h// Callback Name: myInfoCB C( W) K0 f" h# r" a, e0 E
// This is a callback method associated with querying information for a UDO.' d5 B: p* p+ I5 s0 v
// The information is printed in the listing window.6 y! ^9 [1 Y2 H7 [8 z8 R) u- w
//------------------------------------------------------------------------------9 {. @8 O% Z! W% N+ z# F7 s
extern int myInfoCB(UserDefinedEvent* infoEvent)0 _- E( m( F8 e* H) O" a) z
{
% ]" [# h8 y. S* b try
- H! O( ~" c5 ^' I {" x7 z" ?5 C: q8 ~) O- [
ListingWindow* theLW = theSession->ListingWindow();
9 D* m4 q7 f8 u5 M char msg[256];
2 E0 J7 _( H. i) F. W8 H7 { theLW->Open();
6 Q% Q- x$ ?, b! C+ W0 n theLW->WriteLine(" "); |2 |2 M" W( z; x' {
theLW->WriteLine("------------------------------------------------------------");
& x3 T& c2 Y- D, h. X. x theLW->WriteLine("Begin Custom Information");
- T/ Y; E+ Z! N* t* J+ `4 H theLW->WriteLine(" ");
; G2 y. L, D0 Y4 J9 Q sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() );
7 `7 i$ d: W8 |3 ^2 o) `( H- _ theLW->WriteLine(msg);
3 f/ l6 A1 l, W9 n( ~& O' b* U sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );9 L5 j5 N7 n8 t- B+ {8 r6 V
theLW->WriteLine(msg);+ N# F' R5 c, K4 T' I$ |7 O% s @6 m
std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();2 N; G" l P' [6 j; j) X
sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );' t- g" x' w0 ~" B3 i
theLW->WriteLine(msg);
* F H) r4 F8 K" q7 ^ sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] );5 p- b! S7 X0 V! p! A( T; f
theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );. G" e% M4 Q$ i
theLW->WriteLine(msg);' k0 Z' P2 a8 l& D/ e
theLW->WriteLine(" ");" F9 S+ I2 R' c
theLW->WriteLine("End Custom Information");3 W" K6 ?) ]$ z' q$ E
}
, T, L. r9 C2 T catch (NXException ex)
' L# z j0 _8 X' o ?+ d2 G0 \ {2 J- u7 E% @6 l& Q: k
ECHO("Caught exception: %s\n", ex.Message());
! C* O" L) W2 ]" d/ v" _ }
" g9 g, A0 Y# k( ~) B return 0;
1 w! G8 x6 p+ B6 R8 U7 a}" H; J- O5 q! s8 x
0 z8 w4 S8 Y+ I9 {; B% K
//------------------------------------------------------------------------------
' y' s$ \5 i' ^" ~8 m// initUDO* N6 E# N# }& l$ C6 U
// Checks to see which (if any) of the application's static variables are+ C0 ^8 c: U" R c9 ?6 O0 K
// uninitialized, and sets them accordingly.
: }, r8 P5 D1 h0 J// Initializes the UDO class and registers all of its callback methods.2 ]* }" G' {( T. a* d/ o0 B
//------------------------------------------------------------------------------8 P8 k, P; j1 U, a" }7 O% C
static int initUDO( bool alertUser)/ q- t% X6 g, D- C v
{3 F- }# W0 h3 Y6 _" ^" J; o
try$ U! L- Y1 b* L! r1 X) T0 A+ M
{
) i+ E' d0 o4 K6 q5 i% K6 I/ N if (theSession == NULL)" r/ N N6 ^' N! a
{
% K) |: S( ?- A; M( O. y" z theSession = Session::GetSession();
w' U% |! L: U, X }
. U4 { h! r( L5 S' P if( theUI == NULL )
* Z9 b4 t% V* E+ l4 B' y {
- x& k7 N4 X. \( O theUI = UI::GetUI();0 k1 K3 U* r* c9 L
}
% a0 H6 w& a) _% z: N3 M if (myUDOclass == NULL); k# J0 X/ Z1 O3 \
{7 D5 t. f Z2 L* k& A
if (alertUser)
; P R6 _+ }! \# s* v* O) e { `/ `4 G9 D4 g$ k( j2 O
ListingWindow*% n" F! j$ ]$ g" N* m( ^/ y* T( a
theLW = theSession->ListingWindow();# R; {: T! L7 ]! f1 S5 K( Z
theLW->Open();. i: H* b' `2 g/ q1 r, J
theLW->WriteLine("Registering C++ UDO Class");5 D% A- S- U6 ?7 x8 I) I
}
7 x$ t) p; e& z# O7 J( Z // Define your custom UDO class$ m# E! y5 X) e# ~" l r- [
myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");9 d: p9 m( k* G( h" E
// Setup properties on the custom UDO class
$ O1 r- e% c4 E( _! G7 Z4 C myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);
; U: Y; A/ c' H1 p$ ?; ~. N // Register callbacks for the UDO class
& m1 l' f7 Z V9 a( L8 l myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));4 g3 S$ b3 d n
myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));
1 N: v: p2 K+ E+ U) P myUDOclass->AddFitHandler(make_callback(&myDisplayCB));
' S; a! @0 ]0 o8 k$ E* j# G0 S myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));
# F: ? O/ r2 Z myUDOclass->AddEditHandler(make_callback(&myEditCB));+ J7 X- v! u5 a# G/ @4 m" i% I% \$ |, F
myUDOclass->AddInformationHandler(make_callback(&myInfoCB));
# }5 x2 D. n2 ^. X // Add this class to the list of object types available for selection in NX.
+ \1 h$ a) \7 |) C3 L, g3 ]5 { // If you skip this step you won't be able to select UDO's of this class,
5 T! K( Q5 Z4 O; O" n // even though you registered a selection callback.
) w9 Y( N* l7 j5 a* Q' i1 h theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);
& ]5 d" U8 d6 ?; t0 b. R) J, o }9 X# x0 V- G' G9 C: `
}! m4 E7 \- a! \# L
catch (NXException ex)
$ Z' c% o2 k" P7 V& F {
7 f3 o$ J7 g/ U# I% Y: e" L! y) r ECHO("Caught exception: %s\n", ex.Message());
: B( X5 F& W4 J8 G* V! P7 K p: T }
/ M9 z$ h+ Q3 U! ~ return 0;
5 ^0 B% u0 I) {+ f6 g- M- M& M; g}
7 A; o7 C1 a# e7 w% l/ T v3 i9 I8 D; L
//------------------------------------------------------------------------------
1 i/ K0 |6 s& I// ufusr (Explicit Activation)% e7 l1 i; a% D0 f, L8 X2 O
// This entry point is used to activate the application explicitly, as in
8 `$ b% Z4 V2 `0 u7 G: ~' X$ {& Y// "File->Execute UG/Open->NX Open...". S6 a, o& h1 W: Q$ h
//------------------------------------------------------------------------------
( E8 U$ ?5 j: m/ N+ H) Mextern void ufusr( char *parm, int *returnCode, int rlen )- J+ }- N; V0 |/ _
{
0 }( y- j1 T3 ^3 P( x! F try& s# \4 O! `% ?- m# x- X
{
+ L+ }, G' k A4 @ // required for calls to legacy UF routines
+ t# J' K4 e- @; _1 k4 W* z! o // such as UF_DISP_add_item_to_display) J8 c* a' Q, [' h' w3 Q( e3 p8 |
UF_initialize();4 |) @8 B7 B c g5 y' |& L
$ I# H2 E8 D' {/ N) b# R // initialize the UDO - if we didn't load this library at% u: B6 |: W4 N# a4 |- n" U# h
// startup, here is our second chance to load it
$ `& M6 s# @) m% Z9 x3 u, H initUDO(true);: J+ Y) W% i" C7 i U/ O- y2 l
4 Z3 ~. d0 b# B# s8 l; c* U
// if we don't have any parts open create one& l- ^6 R! ]: S
BasePart* myBasePart = theSession->Parts()->BaseDisplay();
# @" x; i$ K/ A% ^ x7 g- ? if( myBasePart == NULL)
& Q5 u, y1 m3 A" L) O8 f' ?$ Q9 C {2 }2 e% n9 w5 j
myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);
. Q5 j# x+ \( b: K# {8 `/ D# o }1 |7 z1 @! w. I1 O N
0 g% h! `$ d' ?8 L' L
View* myView = NULL;
8 x! F& N+ p$ v0 m v3 I6 h Point3d myCursor(0,0,0);/ m( N! \4 e- t
2 J S2 _( j" h8 X$ h1 x
// ask the user to select an origin for this UDO) I0 g7 Z9 e5 o) o
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);
- C# f: ^9 s" F" M' ~- k if( myResponse == Selection::DialogResponsePick ); |) w# }( q! n2 u y- U, m
{
* ?2 o: c1 l2 S4 ?1 c S* t6 q. ] // The user selected a point - go ahead and create the udo1 T0 x8 z' ?- m' v- _. p$ b: H
UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();) E' l# n$ U' _! e
UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);6 A9 U7 E8 G( T
// set the color property of the udo - just for fun :): ]. Y, a3 _8 m3 Q9 k
firstUDO->SetColor(36);
4 i; f7 d. |+ Y2 k9 z; s // store the origin selected by the user with the udo
6 C( P/ e/ `- B7 s/ O0 o std::vector<double> myUDOdoubles(3);# z) _' [# o* b' D
myUDOdoubles[0] = myCursor.X; p( W! r4 j \0 w) }/ z1 z( u; [
myUDOdoubles[1] = myCursor.Y;. v( o8 X% k) ~5 X7 A4 e
myUDOdoubles[2] = myCursor.Z;
: t4 C1 u7 p6 T! v' U: x7 n firstUDO->SetDoubles(myUDOdoubles);% Q' j2 ^8 ?7 i+ q& V: L
// add the udo to the display list manually
$ }% |0 Q; R: b# {4 T/ z* n // this will force the udo to display immediately7 |0 L, ?; @( f s" M g3 a
UF_DISP_add_item_to_display(firstUDO->GetTag());/ S$ P$ Z% Z9 U0 k9 E; k8 {
}- e8 o6 {2 [ A
UF_terminate();7 x9 m& j2 N" G6 R
}
' W; y5 `7 @/ T catch (const NXOpen::NXException& ex)
0 k8 K( r. A; @ {
! c2 x- i3 r! Z, K' r2 R6 ~; d3 b/ s ECHO("Caught exception: %s\n", ex.Message());$ L: C# ^% F* t6 z" y
} f* Q# z3 g1 p3 G- ]# V x
}: g2 Q9 d4 h0 a2 v- q
7 f. G- L: A& E; ]* X
//------------------------------------------------------------------------------
) ~3 d% G% E- t// ufsta3 }/ W e L/ Q3 n
// Entrypoint used when program is loaded automatically
$ E! h& A: _8 j4 [3 P$ z// as NX starts up. Note this application must be placed in a
0 k# K+ _) d4 @: P# l// special folder for NX to find and load it during startup.
" K" q4 L: R8 a. e+ D/ r( x// Refer to the NX Open documentation for more details on how
4 S0 X, M. y! v o$ S5 v* o// NX finds and loads applications during startup.
5 P- X0 U e' y//------------------------------------------------------------------------------$ e1 \4 _+ [* s, V- @4 Y4 N5 C
extern void ufsta( char *param, int *returnCode, int rlen )& M0 l, ]& \4 p2 ]5 t( R! w& L) h
{% S& P2 [# U$ `6 i/ L& M
try* u' {( P, b- A x9 ~- s
{
: `& ]5 h4 L8 y: i) N# Y0 p initUDO(false);
0 H, L5 @7 ?, M: ^ }" L, H M, I+ t" c
catch (const NXOpen::NXException& ex)
# O9 d3 O- k! z {
, N; q# I% W3 p: |0 j; p ECHO("Caught exception: %s\n", ex.Message());3 r; _, a, W2 s. y, f3 ]% v( F
}
5 U& s; |" X! F) I# w4 T( r9 s}' h3 S s4 t$ Q
5 x1 R) `# ^! w( T
//------------------------------------------------------------------------------
& A. I5 A, z8 G8 K// ufusr_ask_unload
" n4 z3 o; [+ p4 t// Make sure you specify AtTermination for the unload option. E% K5 y; @5 u- X2 d9 z$ ?- b
// If you unload the library before the NX Session Terminates, n# ]# s, d9 @, l S* t: d
// bad things could happen when we try to execute a udo$ _! z# Y" o& x {" a1 {
// callback that no longer exists in the session.- [! q# |+ B) {1 Q" b0 ~& o* z, G0 D' P
//------------------------------------------------------------------------------% \* ^5 t6 T8 Y- r
extern int ufusr_ask_unload( void ): \' q) Q/ p7 W5 {
{9 `- l6 A4 h' V
return (int)Session::LibraryUnloadOptionAtTermination;
* i4 J. |/ ~# Z3 y}! x8 B& {* D9 A3 {! A+ ]
7 H( J# C( p8 B: D9 {1 w2 F, _ |
|