|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
5 P( A; i E( y. k* X5 E) n% m* h o$ [0 J$ j1 B
NX二次开发源码分享:NXOpen C++创建UDO的过程2 R3 f9 [1 o+ T/ z# R
' r1 s' ]! \. b1 z$ d' a+ U2 S6 U
0 O0 O7 k0 v2 P; N+ U5 PUDO的创建是NX二次开发中最高深的一部分内容,通过这个实例,你可以看到如何注册UDO的回调函数,如何创建UDO,定义UDO的编辑,UDO的信息显示等内容!
, ~, H* K) J u' S O5 h8 N4 g' D* v' J1 |8 B
/ Z; |2 Z) c* p+ a' U
0 O) U8 I. a2 b9 h' l& h6 ^
#include <stdio.h>6 k6 p4 m% f. q: N8 C
#include <string.h>
) O0 N" j7 ~8 W8 C8 ~0 c4 ^- p$ _#include <stdarg.h>, G- K0 z) r. J+ ^% _
#include <uf.h>( g x7 r1 m) l5 `) J
#include <uf_ui.h>5 S9 Z! N& [- Z, W0 T, e2 S
#include <uf_exit.h>
! c( d* u' S- x$ T5 |; N) e+ A#include <uf_disp.h>7 e% {4 X/ C0 y* ~* F1 f) F, w, b/ g
j$ F+ Q/ E& n9 g: p$ Q8 A! Bstatic void ECHO(char *format, ...)
4 Z$ x6 `7 D$ m{
@+ E# [# {$ i char msg[UF_UI_MAX_STRING_LEN+1];
1 T, W z8 R/ {9 J- W va_list args;; K" R4 u) _3 R) J! E) L- A& ~- X" f) T
va_start(args, format);
N) ]' r: f8 x4 [! Q; z vsprintf(msg, format, args);3 j& ` b0 t# S
va_end(args);
) V8 \4 ]* `: @& a! Q* O2 G UF_UI_open_listing_window();& S8 O4 }0 t* K
UF_UI_write_listing_window(msg);& j# u' U4 B a' Q& O
UF_print_syslog(msg, FALSE);
' S' M0 _1 \9 s9 W# A}
9 R7 D( J* o1 {3 t r
4 u- [+ D* ]7 w2 T. F% t* V* H% p#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))% [3 p0 L+ {; W0 ~( x4 @/ y
2 s# ^8 ]* l2 q; W7 w( w
static int report_error( char *file, int line, char *call, int irc)
! M6 G2 B' c% p& Q{; B& ?7 e! Y: C2 W, J1 N" x# N
if (irc)
7 s7 Z# j0 N4 G T' D: T! y {/ i# M8 A8 c, x) f& M1 P6 e' I# u
char err[133];
6 G G8 v. S8 I; w$ _$ U) L+ c Z" N
UF_get_fail_message(irc, err);% ?8 W7 R! y: Q* n' t: x' I7 Z
ECHO("*** ERROR code %d at line %d in %s:\n",! T* n0 n/ q/ z7 f O1 S, z9 A
irc, line, file);7 Y7 a! d5 D1 L% c& F v
ECHO("+++ %s\n", err);4 E7 U# v; O- b. {- W- \ [
ECHO("%s;\n", call);, x V }" x5 u; t2 w
}
: D1 u' X5 `% A8 J) ?
0 o( c3 V) i }3 M- N0 P return(irc);
2 F! o: T# ~3 ^! P}
9 W: M: ?. a/ W
) Z* c- b" {6 N' T! K; t' R& g#include <NXOpen/Session.hxx> 6 T" _0 v$ q& Q
#include <NXOpen/Part.hxx>0 l- f( T8 O, z4 T S1 Q7 B
#include <NXOpen/ParTCollection.hxx> V: w" \0 }' T
#include <NXOpen/Callback.hxx>
( e( y7 q8 I3 s5 a& p0 l#include <NXOpen/NXException.hxx>
8 b, [# o" S6 b) f5 O' J2 W#include <NXOpen/UI.hxx>
& z8 i; u. Q! T8 X5 P#include <NXOpen/Selection.hxx>( Q0 D: ~* @/ P4 n- E+ c \- }) |2 D
#include <NXOpen/LogFile.hxx>2 P3 ^. V" T" e8 `2 D, F( W( d1 _
#include <NXOpen/NXObjectManager.hxx>
* r( V" i: R" j- y7 G$ {# j9 k#include <NXOpen/ListingWindow.hxx>
$ y* R9 L$ g: |1 H6 p. G0 V) f#include <NXOpen/View.hxx>
0 L1 ^8 n+ B, y, `. H1 F( R9 [- k/ w1 y7 U- L8 E" D# U
#include <NXOpen/UserDefinedObjects_UserDefinedClass.hxx>( _ W/ s. _* S! i5 v
#include <NXOpen/UserDefinedObjects_UserDefinedClassManager.hxx>
2 g6 k4 Y1 C( Q9 z#include <NXOpen/UserDefinedObjects_UserDefinedObject.hxx>7 Q; D& Y; J% V! d& \( }/ W
#include <NXOpen/UserDefinedObjects_UserDefinedObjectManager.hxx>9 g: w) {; t ?6 A
#include <NXOpen/UserDefinedObjects_UserDefinedEvent.hxx>
7 O: t9 ^& S9 g#include <NXOpen/UserDefinedObjects_UserDefinedDisplayEvent.hxx># E% d( _: M* X U; _
#include <NXOpen/UserDefinedObjects_UserDefinedLinkEvent.hxx>
9 U, {# U7 J8 y#include <NXOpen/UserDefinedObjects_UserDefinedObjectDisplayContext.hxx>
4 L% \0 R7 X3 n i" h0 b3 n) @* p0 a, K* M& g
using namespace NXOpen;
% g) N( ]* [6 s k ?using namespace NXOpen::UserDefinedObjects;
- ]1 O; M4 G- w+ a
* ]: K# s% d. L5 S1 W9 Y//static variables+ p' G( |1 L( s! H4 v! |2 _
static NXOpen::Session* theSession = NULL;( |+ ~$ i3 w/ a6 Y$ o `4 S
static UI* theUI = NULL;, t C" o0 G4 \: l8 v# ]
static UserDefinedClass* myUDOclass = NULL;. m5 a% J* b% \
1 J5 [: p5 R2 T, H6 {
//------------------------------------------------------------------------------* r3 T! q) }8 d" g" w# N( G8 J
// Callback Name: myDisplayCB- h3 N- Z" W' \8 Y4 W( Y7 F) [9 f
// This is a callback method associated with displaying a UDO.# x j, e; @" ^7 ^# O! f+ z
// This same callback is registered for display, select, fit, and attention point2 D( S& r) Q* S! s/ b; |% P. M
//------------------------------------------------------------------------------; I% ^+ g/ I5 G& z" y, N1 e
extern int myDisplayCB(UserDefinedDisplayEvent* displayEvent)
8 {: B8 v, M5 _{1 _/ l& F3 G4 x4 r: i' x
try
. w! I# ]$ m+ ^/ E" |9 C {
1 U& H2 n# S( A. _ // Get the doubles used to define the selected screen position for this UDO.. F4 P3 U# D; C5 H2 e$ i1 k5 B
std::vector<double> myUDOdoubles = displayEvent->UserDefinedObject()->GetDoubles();
! F% z3 k: |' y! \% I+ a2 X8 v; F+ G, y% t
// Use the doubles to define points of a triangle9 n; r; ?4 i$ V6 w, E6 l
std::vector<Point3d> myPoints(4);
7 x$ h2 f9 }/ ]) |
6 ^7 u3 j: j B% [" O+ X myPoints[0].X = myUDOdoubles[0] + 0;
" n- Q: W9 i, A% U- b myPoints[0].Y = myUDOdoubles[1] + 0;
1 W& h1 s9 G) \' C0 z: E myPoints[0].Z = myUDOdoubles[2] + 0;
$ C5 E t" W% A! h
( [3 H* s) J! z8 [3 a myPoints[1].X = myUDOdoubles[0] + 100;7 I0 m7 t: n4 [
myPoints[1].Y = myUDOdoubles[1] + 0;
' Y2 r9 k. C* s' n c$ k myPoints[1].Z = myUDOdoubles[2] + 0;8 G; E& g. b) L$ ^3 H
M, a) U& r( m/ {# M5 M0 r( x myPoints[2].X = myUDOdoubles[0] + 0;
& D$ E) M! s: A3 Y5 f$ ? myPoints[2].Y = myUDOdoubles[1] + 100;
' h! z. _4 K3 k1 j+ [: t5 I myPoints[2].Z = myUDOdoubles[2] + 0;
u# D2 o9 o; m! Z7 U
( n+ z! A( `8 N% o6 x, ^ myPoints[3].X = myUDOdoubles[0] + 0;
/ O% v$ d& K+ b. O6 l* t2 t myPoints[3].Y = myUDOdoubles[1] + 0;
$ E! I, P: w8 V9 m$ C' k myPoints[3].Z = myUDOdoubles[2] + 0;
~8 r; e- s& d8 v! w, Z& c+ N, P C% Q" E" A. n& e+ S) V
// Display the triangle- L, ]. X4 L, ^0 ?0 W
displayEvent->DisplayContext()->DisplayPolyline(myPoints);
) l, f; ~3 B! p- y: ^ Z9 O) O% Q& v, @( z$ r9 l% v
// Display the text next to the triangle4 @- ^0 V, D* }- H
Point3d myPt = Point3d(myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2]);3 S: a/ r! `5 N' ^' f: C- C
displayEvent->DisplayContext()->DisplayText("C++ UDO", myPt, UserDefinedObjectDisplayContext::TextRefBottomLeft);
0 |& [: _, q0 y7 l0 T2 k9 Q2 g
0 K" N \% E) G/ r // UserDefinedObjectDisplayContext::DisplayFacets fails - see PR 6731653# |9 Q. ]4 J! @! n# C$ j
// This demonstrates using UF_DISP_display_facets as a work around( U0 |' g1 p, v1 q6 \
double normals[9] = { 0,0,1, 0,0,1, 0,0,1 };8 K. q! X2 S: _. p8 w- C
double vertices[9] = { myUDOdoubles[0], myUDOdoubles[1], myUDOdoubles[2],
& w" W5 u0 S {1 h$ r4 ` d8 c& S' ^ myUDOdoubles[0] + 100, myUDOdoubles[1], myUDOdoubles[2],$ x. N0 o! l/ ~0 J1 J9 x
myUDOdoubles[0], myUDOdoubles[1] + 100, myUDOdoubles[2] };0 k) i/ t& f2 p8 j v% i, }( [3 h; X
UF_DISP_facet_t facets[1] = { vertices, normals };5 v- a3 P, f. d) ]7 u o, [7 t' U
void *context = displayEvent->DisplayContext()->GetHandle();% w& M0 I1 l( H9 Z7 O) O
* {, ?+ S7 J0 _4 F! U! }+ R
UF_initialize();
8 t, R, d- N7 X4 ? UF_CALL(UF_DISP_display_facets(facets, 3, 1, UF_DISP_TRIANGLE, context));
% g v+ y" e; X; }9 l! w$ o UF_terminate();) a. ]' Q6 N+ I( p7 H# m- r( H
! d0 b/ s" |% }+ J5 N. D
}
# i) r Z" ` ?& D! C catch (NXException ex)+ V$ f, {# S- D' s, J
{" H" y4 T2 Q; u( L D3 w
ECHO("CaUGht exception: %s\n", ex.Message());
5 I9 P! j0 r$ F6 p7 S$ D) x) u }/ x! {& |+ A: w: P+ |# k# }* f7 t
return 0;0 n# o7 ~9 A6 O
}
2 ^9 u4 d0 ?4 ?0 x; s% r1 Z8 H//------------------------------------------------------------------------------/ t; m, q7 P4 b
// Callback Name: myEditCB* w4 ~3 h9 ?) v* q* E9 u; j
// This is a callback method associated with editing a UDO.& Z9 T0 W/ E l$ D
//------------------------------------------------------------------------------2 H$ l* U7 M& `9 l
extern int myEditCB(UserDefinedEvent* editEvent)
3 I+ `$ K* W3 _1 @{
( w5 f/ i" t6 N* Q try7 U3 I. V, m$ P3 w
{
% ^( X: G0 v1 E5 w // required for calls to legacy UF routines: o% ~6 ^" @9 j' f6 \
// such as UF_DISP_add_item_to_display; D/ x( B+ B, a' ^: F& J( D2 b/ L
UF_initialize();; t- @+ l" N0 S: C. n
* l9 j. K/ J' Z Q7 ^* W View* myView = NULL;/ i) M$ s& l6 D
Point3d myCursor(0,0,0);0 \- B2 y6 M F3 O& l
+ G. h; a# p9 v) |& M, x- L
// highlight the current udo we are about to edit2 B6 e2 b: [' W3 ?
// this is helpful if multiple udo's were on the selection b2 M n' m6 l$ t; k4 ]
// list when the user decided to edit them
2 |! Z- ]( Z8 c. U( Y! o% O7 E5 ] editEvent->UserDefinedObject()->Highlight();7 D, D- o$ B; k0 ?
) c8 A& \) E+ s- C0 B // ask the user to select a new origin for this UDO7 j$ W# D; z" C6 A! T; M7 [
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select New Origin for C++ UDO", &myView, &myCursor);( m/ A* [; k G- ^& e
// we are done asking the user for input... unhighlight the udo
8 e3 x$ e# ?. y; H: h5 W editEvent->UserDefinedObject()->Unhighlight();
' F$ g4 s( Y4 n. A( }: x( j; c* q0 x# P5 L0 U
// use the new screen position (if the user picked one)
0 q4 Q d+ A/ z* ^& l+ b if( myResponse == Selection::DialogResponsePick )
( a6 g* E4 u! v1 E {
" G' j* z) [" w; e+ o std::vector<double> myUDOdoubles(3);! t8 d6 ~* I6 E/ T. i
myUDOdoubles[0] = myCursor.X;) Y4 C" k9 G/ Y) p
myUDOdoubles[1] = myCursor.Y;7 _+ N; ]. R) u4 C; q
myUDOdoubles[2] = myCursor.Z;+ @" E o0 B( A) p7 g ~
// store the newly selected origin with the udo
) s& o+ b3 z1 w2 _, I/ `5 F# g; p editEvent->UserDefinedObject()->SetDoubles(myUDOdoubles);
3 E# t, O, Z1 F+ k // add the udo to the display list manually
6 r j: U5 Z2 l4 L! f // this will force the udo display to regenerate
3 M5 t% ?& U" ~* a1 I1 D8 V" G // immediately and show the changes we just made
2 `8 x: D, F5 I2 c UF_DISP_add_item_to_display(editEvent->UserDefinedObject()->GetTag());, z0 O/ X& J$ D* f/ n1 H5 p7 `
}, t+ n8 v. m" Y" e0 J c
UF_terminate();
' ~2 E2 {, O7 |$ m } catch (NXException ex); O- s/ i8 I$ H/ A9 _& g6 L
{
! Y0 i* Y8 s7 g9 e ECHO("Caught exception: %s\n", ex.Message());
) W! y! y: v* O! f } return 0;8 E7 A! [5 x- A
}
5 p2 q' h" d! }3 j5 F' l- z' Q6 d$ U% H//------------------------------------------------------------------------------
" f, b+ t9 K7 e* | r// Callback Name: myInfoCB
* G' @. e4 w$ R4 X2 z5 |* L; }$ I// This is a callback method associated with querying information for a UDO.
8 D/ a/ v% e1 K0 t. }2 g1 f9 w# \// The information is printed in the listing window.1 l s+ |. W# g0 I2 v3 F8 h4 w
//------------------------------------------------------------------------------
$ L1 o% N5 @7 a# S) ?# |5 C: vextern int myInfoCB(UserDefinedEvent* infoEvent)& [# [! H. f. f8 H" q1 b: M* w
{
9 u5 Z5 h: f, ~. U \ g try
m$ V9 b; L; d0 D- N6 T1 ] {
1 V' I* P! n- |0 q ListingWindow* theLW = theSession->ListingWindow();* t I) e: ^9 d2 L; o/ o
char msg[256];
( j/ U/ Y D9 h9 E, Z7 g0 ^5 I theLW->Open();/ c+ Q7 ?1 Z* ` k. Y: ^* V9 Q
theLW->WriteLine(" ");
2 l) h( M) r* T theLW->WriteLine("------------------------------------------------------------");
6 m& l. j- H7 N theLW->WriteLine("Begin Custom Information");3 n0 }8 e' R; X# t* D C7 _" \: B
theLW->WriteLine(" ");
+ C9 x4 B( X/ h( _ sprintf( msg, "UDO Class Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->ClassName().GetLocaleText() );; p0 ?' ~7 T9 K- ]8 F. H
theLW->WriteLine(msg);
* B5 v# ] K2 V- W+ r, z# a sprintf( msg, "UDO Friendly Name: '%s'", infoEvent->UserDefinedObject()->UserDefinedClass()->FriendlyName().GetLocaleText() );# r! d+ G: D% g2 l9 i
theLW->WriteLine(msg);
6 d# e: p6 O, p( |# U# B std::vector<double> myUDOdoubles = infoEvent->UserDefinedObject()->GetDoubles();
b. ~3 Q# Z/ r1 I. ?' X sprintf( msg, "myUDOdoubles(0) = %f", myUDOdoubles[0] );
8 N& z- b6 b) y; Z" [& i# l theLW->WriteLine(msg);
4 a& M! s$ u6 t3 C4 H sprintf( msg, "myUDOdoubles(1) = %f", myUDOdoubles[1] ); J0 ~ ]1 T9 P4 F" w# t
theLW->WriteLine(msg); sprintf( msg, "myUDOdoubles(2) = %f", myUDOdoubles[2] );
0 q0 K. z% R8 z. X- h" t1 F theLW->WriteLine(msg);
1 y$ ]" x4 e0 q4 B theLW->WriteLine(" ");2 K3 z3 G$ T% |, L
theLW->WriteLine("End Custom Information");6 T6 z/ N6 ?2 f" z0 u: I5 M5 s3 i
}( \9 O0 Y# ?9 _, m, T- Q
catch (NXException ex). d( u2 R8 J5 A
{
& s( Z4 ]( f1 ?( K6 K% T; Q ECHO("Caught exception: %s\n", ex.Message());: u L q) s1 ~! U" ~# t
}& P9 E( h# d; z' G+ v3 W: J
return 0;
0 V5 `+ E% g0 [+ W( T* [& {$ Y5 E. t}
/ o, a/ p( y' ~9 H( z4 W5 N6 C9 l; e- y' o8 M
//------------------------------------------------------------------------------
$ B% z! W- @2 w% W4 w( S, w// initUDO
: j# j7 ~1 G/ w8 t/ _// Checks to see which (if any) of the application's static variables are
* l. C4 n" h7 S2 b( y$ X// uninitialized, and sets them accordingly.; |. h! W6 d% n2 D
// Initializes the UDO class and registers all of its callback methods.- o6 h- D5 O$ }% M. Z+ n! {, e( m
//------------------------------------------------------------------------------
& A1 o: l3 R2 X; Vstatic int initUDO( bool alertUser)4 k3 U4 D% H9 i: j) _$ n; m
{
5 w5 G, y2 c7 T try
& w; @# H5 D# `' h {
1 `7 c3 S" m v if (theSession == NULL)# s' I A8 J! ]. L5 O
{: F/ s; v* w" R' d
theSession = Session::GetSession();8 D6 i9 F* W, D7 }8 C2 G R
} ]2 ]. b1 r1 T, Q: G# B
if( theUI == NULL )
0 i T7 R' b5 w% M* {$ V" J {
! @2 o3 u/ o) \& J theUI = UI::GetUI();# y0 M, h% F9 d9 d; Y3 c
}
. T3 ]- Q6 P( I9 h. E3 P if (myUDOclass == NULL)# A" ?( H1 i0 J' J6 J) T* M
{
( s& q a9 H, \4 N4 V$ W: _ if (alertUser)
, D) P% U" s3 V {! L( c( W% S6 ~! X3 z. ], P
ListingWindow*
, R2 r* i) A3 O" z# V6 n* n# S theLW = theSession->ListingWindow();% C* \; J1 A. {6 i. H. g6 I
theLW->Open();% M" f' G+ \3 b& M' g1 |9 ?
theLW->WriteLine("Registering C++ UDO Class");- L# @: u9 F8 {- \; o
}4 U6 `! C T" N% R. \+ V4 `4 H
// Define your custom UDO class
* v Q' y, h3 l myUDOclass = theSession->UserDefinedClassManager()->CreateUserDefinedObjectClass("Sample_Cpp_UDO", "Sample C++ UDO");
. N- L5 z, s# R: T9 b! J% U% j // Setup properties on the custom UDO class
0 g* z8 Z; Q- S" w$ E myUDOclass->SetAllowQueryClassFromName(UserDefinedClass::AllowQueryClassOn);
" [9 ~% Q- K9 a, ?1 y // Register callbacks for the UDO class+ ^! K3 |( C; X5 f. r
myUDOclass->AddDisplayHandler(make_callback(&myDisplayCB));
+ `! O" ~+ r' X myUDOclass->AddAttentionPointHandler(make_callback(&myDisplayCB));5 D. E b$ J1 Y) L7 G
myUDOclass->AddFitHandler(make_callback(&myDisplayCB));/ p" F$ |# U' [2 p& u& f) S- s
myUDOclass->AddSelectionHandler(make_callback(&myDisplayCB));8 y; R& G6 M+ H2 J
myUDOclass->AddEditHandler(make_callback(&myEditCB));. U& N U+ ^& D. U' S' M% v- `9 t# B0 }
myUDOclass->AddInformationHandler(make_callback(&myInfoCB));
5 Y0 F% l9 Y8 L3 R: y8 [ // Add this class to the list of object types available for selection in NX.
/ p( C% q+ \# u& |/ H // If you skip this step you won't be able to select UDO's of this class,( f: {. C" u, S! H# p9 b1 k
// even though you registered a selection callback.% ~ N2 P7 D# c7 X( n; s& }
theUI->SelectionManager()->SetSelectionStatusOfUserDefinedClass(myUDOclass, true);2 C6 e7 ^9 }3 [: Q2 \3 n" K; b
}- n2 E8 I! B8 v+ Q
}( ? }+ V2 f# H
catch (NXException ex)/ D7 I' R# A! a' e$ ]5 d* h
{( q' r9 Q( a3 \, t8 W
ECHO("Caught exception: %s\n", ex.Message());
1 N8 o" c0 h- a- @$ W0 y& ] }+ \* ^! F7 U% ~* j( y
return 0;
5 o: {; G. i. H3 l" W}1 x; W# g: L- g$ T2 K. ?" n
3 ]1 L$ q: E# G5 d//------------------------------------------------------------------------------7 X% v+ @; w+ c2 o
// ufusr (Explicit Activation)
7 } S/ q9 D) H" w// This entry point is used to activate the application explicitly, as in
3 c0 @! ]( F, g8 B5 ^4 y// "File->Execute UG/Open->NX Open..."9 W8 r3 f7 G+ D O$ E3 z: S, J% z* N
//------------------------------------------------------------------------------
9 _4 {/ q/ \ n. H4 L7 E9 `. Y7 qextern void ufusr( char *parm, int *returnCode, int rlen )
9 H& A' k' p" s( P* O$ R{
: h: Q7 B5 t: `* h! z try; I- X: T3 Q- m
{" t( d( i" t8 r: Q( o; |8 B
// required for calls to legacy UF routines
( U# Z6 ?0 f! r // such as UF_DISP_add_item_to_display/ O/ R& C$ s6 A9 P Y. c0 F. |; T
UF_initialize();" m5 O& Y r% ?9 l) ^; S L6 X5 n. H) j
# [& n9 E n3 C4 u7 t c // initialize the UDO - if we didn't load this library at8 G6 q) j* |7 j; b: ^
// startup, here is our second chance to load it
. f# b0 {. Z% a K initUDO(true);
: P. h; @; y- c5 a1 m+ ]$ S6 }6 d1 \2 m5 U1 d
// if we don't have any parts open create one" _. |( v/ Q$ q- m
BasePart* myBasePart = theSession->Parts()->BaseDisplay();
$ e; E5 h0 a! O! Z/ ^- p( ? if( myBasePart == NULL)
/ [" w2 w2 r' R2 x7 i1 q+ ?# ]# u) Q {: x# h: C8 Q; I$ F: F& y: x7 a
myBasePart = theSession->Parts()->NewBaseDisplay("test_cpp_udo.prt", BasePart::UnitsMillimeters);% t( ?' i8 ]5 l% ?- x+ K
}8 O+ q- z- W2 ]7 E( ^
# T, G( z- J* C
View* myView = NULL;
+ }+ m. N0 z$ j' b: o4 i3 @/ G Point3d myCursor(0,0,0);8 U* v& D/ M3 h- W: a' s" S
8 W7 B* L& U& }$ y7 V" |1 f // ask the user to select an origin for this UDO! I- S% y4 v4 A. p% r* I) V( N
Selection::DialogResponse myResponse = theUI->SelectionManager()->SelectScreenPosition("Select Origin of C++ UDO", &myView, &myCursor);/ m3 \. D9 w, ^$ b; d1 @$ e8 E
if( myResponse == Selection::DialogResponsePick )1 Z* P1 B4 v' `5 G" e0 j/ Z- L7 |
{+ J1 }- z! y# E
// The user selected a point - go ahead and create the udo
% x5 I2 a% x6 M8 D& G! ^' ` UserDefinedObjectManager* myUDOmanager = myBasePart->UserDefinedObjectManager();9 P0 T. D6 M$ ?+ A" a9 ^
UserDefinedObject* firstUDO = myUDOmanager->CreateUserDefinedObject(myUDOclass);
; s% A0 t* z% }- T- b! q // set the color property of the udo - just for fun :)
2 p6 V* N1 ]5 S' D; Q6 A firstUDO->SetColor(36);
5 | R: ]/ w) M% j. I' T // store the origin selected by the user with the udo1 |- r% {5 |; u+ g1 y8 e
std::vector<double> myUDOdoubles(3);' N2 q8 ?' \0 q3 _" M
myUDOdoubles[0] = myCursor.X;
+ [ W( \/ s2 ~: h* M$ K6 i K3 Y+ U myUDOdoubles[1] = myCursor.Y;
8 a' Q" j4 r; u- f- Y+ Z myUDOdoubles[2] = myCursor.Z;+ v3 p4 o7 n' w* D/ q0 M7 `
firstUDO->SetDoubles(myUDOdoubles);7 p/ H/ b" k8 }& e5 T2 K' J" @
// add the udo to the display list manually
1 K' W! I ]. [. [( | // this will force the udo to display immediately
* S5 \; I) m* L/ r5 C UF_DISP_add_item_to_display(firstUDO->GetTag());
0 u# @: y, n- r }
% S' G* G8 I# @; g9 o' c UF_terminate();
* H/ y9 r) I. u }
8 `. w# d' _$ |8 { P catch (const NXOpen::NXException& ex); r4 m* D' y2 d! _' N
{0 R1 e- n% x& r2 C7 P$ u3 V% [5 M
ECHO("Caught exception: %s\n", ex.Message()); h6 C `+ K: t g* ^
}
! d n; {% Y! a" ?( [}
9 I( u H- }. M) k' v0 D5 H
. ^' d( |( n h2 o- m8 x; {//------------------------------------------------------------------------------ R$ j4 V s) Q9 ~" m/ G$ D% G' D
// ufsta
2 r; {+ I) X5 V) Y, s5 T4 ?// Entrypoint used when program is loaded automatically
/ S0 `7 C) S- \3 @/ k// as NX starts up. Note this application must be placed in a t5 ^8 x1 H& K0 p- [
// special folder for NX to find and load it during startup.
; k1 \4 Y9 ]1 J// Refer to the NX Open documentation for more details on how4 O: ^, T; L) b7 l7 K! S
// NX finds and loads applications during startup.
' H5 ]1 _9 c9 D9 u: C0 z, S//------------------------------------------------------------------------------# u O) g: k5 }: N
extern void ufsta( char *param, int *returnCode, int rlen )# [& Q4 B- @2 p' a: u- M0 u3 }
{) f; I# E! t" e
try
; ?) M+ F' @ x. C {& X# N$ `: N) R3 Z
initUDO(false);
- q0 H( m3 n: B9 t$ } }1 p+ e# F. h' v5 ^3 y9 P+ C
catch (const NXOpen::NXException& ex)
% H) x% H1 z3 G, N, A9 Y7 K7 } {
6 r B, ^) r& N: y, H# l ECHO("Caught exception: %s\n", ex.Message());& [' p9 G( e2 l1 R& w& t
}
4 k' I# h+ F* a$ F6 @; Y}, Y1 ^9 t. L; H; k5 V
u: D2 U$ D' b7 a# U
//------------------------------------------------------------------------------
# F! a2 _4 Z9 g- G1 h; o// ufusr_ask_unload
C4 l) U- O( E* P6 X// Make sure you specify AtTermination for the unload option.
9 e; D0 ^! H0 v; d% E// If you unload the library before the NX Session Terminates ` I7 K2 H5 T4 T, b
// bad things could happen when we try to execute a udo% y# C( i# f- S
// callback that no longer exists in the session.
. y8 u/ |& Q1 }0 x% F//------------------------------------------------------------------------------' Y2 l6 p; {) p. g) u. [) v8 ]4 n
extern int ufusr_ask_unload( void )0 D9 f% L' j$ x* ]8 i# g
{
5 A4 \: Q+ T5 R2 x n return (int)Session::LibraryUnloadOptionAtTermination;- C* \/ T0 o3 P6 }+ V4 U& }" G9 ]
}
. ?3 ]9 r8 i. a, [1 T% g
! s: l4 [& [, m0 W |
|