|
- /*=============================================================================
9 ^) Y, D- d* r3 N. \. Z: w - Copyright (c) 2004 ugS Corp.* t! n% @- b! S' \9 u" A
- All rights reserved
?0 K+ k7 w" R, H; |; W - ===============================================================================
3 n3 a Z6 R7 i. T% g8 W s - File2Points.cpp0 o. P/ g# h* H1 Y/ m# w$ u
2 L; J4 |6 H, c. S2 e- Description: Contains Unigraphics entry points for the application.
' k) D1 G1 N$ D C: I - / V7 {; F# R4 H9 d. M0 n& c
! y o& a, `" X+ L+ |# s- =============================================================================*/
J( u8 ^4 f. z. V: f0 s, E
. _/ P! f) ~4 K9 C% A1 |3 B) Z- #ifdef WIN32) N- f7 ~4 b$ d% O
- #define USE_MFC2 n' o z8 Q* e4 Z
- #endif" g$ O9 r; f2 A2 g' n. u6 k. ^, K3 s
, _8 R/ P8 U: S" v1 S4 O- #ifdef USE_MFC$ }. s! Y' f7 Z: Z/ `, ]) Y
- #include <afxdlgs.h>3 o7 P% J# {; }- W' Z/ Z8 P/ a2 U
- #include <afx.h>9 p' E2 z! H5 [! ^, d
- #endif2 P" z3 s: r: W5 s8 V5 D6 o4 t
- ) N( z& t7 f" L5 u- W* O
- #include <NXOpen/Session.hxx>0 s6 c( h1 O$ A7 N$ t" [& A" m
- #include <NXOpen/Part.hxx>
5 E) W. D2 Y) P: F8 a - #include <NXOpen/PartCollection.hxx>
$ i1 M {7 j5 g/ k( e3 n* W - #include <NXOpen/Point.hxx>& o3 ?+ O2 V* L0 P* l7 S2 t1 a
- #include <NXOpen/PointCollection.hxx>* ]; H7 q2 _" H+ ]
) J" K- A g: {) T7 C8 n- #include <fstream>0 C- G* \/ c" d
& }% N9 Q/ o, j1 X# X. d5 w3 E- using namespace NXOpen;
( C5 s% j, p Z- P - using namespace std;" r" I2 Z8 `# B% Q2 t6 a& r6 j
6 _9 G8 @# y! Q5 B# P* k- /* Include files */
/ w" s; i4 Y* e. T* z9 O ^ I - #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
* L1 N: R& T [ - # include <strstream>
( \1 d( n4 w( R3 H - # include <iostream>% l& S! F/ X' m
- using std::ostrstream;* V0 R/ k' N" S1 l
- using std::endl; U) {- m# a! Z7 K! E7 d
- using std::ends;
* G" M# u) U$ ~) x i - using std::cerr;8 Z7 g/ E6 C% Q6 {* G/ a
- #else
3 P. `5 m6 Q$ \% }- g- n - # include <strstream.h>
, }5 Z9 n6 i a& R& p. H' s( ? - # include <iostream.h>$ d8 d0 E+ M% I3 G3 V& b
- #endif$ V9 P) Y$ k2 h
- #include <uf.h>1 H2 E9 R& |, Z+ O# P3 u) P
- #include <uf_ui.h>
: r6 K% b# Q7 k - #include <uf_exit.h>" z" G4 J0 l/ g- r4 \, C. r! M" o
- #include <uf.h>& K, {5 l( o6 h+ @& M3 e
- #include <uf_exit.h>. w/ o a0 @4 q' R* F
- #include <uf_ui.h>5 S/ X# M: _% Y- i! n) O
- #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
1 Q3 ?2 s2 t7 o: A - # include <strstream>% T* Z2 `% w+ P8 R4 ]
- # include <iostream>
9 |& K, C. g6 ~& k5 A - using std::ostrstream;
! Y# d9 c4 M% X" L8 K5 | - using std::endl; - T( E5 r m0 Y2 g* |& v3 F' e
- using std::ends;# n+ T2 t7 V, V3 t5 D
- using std::cerr;
7 ]5 u( y& u0 R. c( a) i g - #else
2 z! [' R9 _: ^% n( `0 ]+ y - # include <strstream.h>" H' h! Q9 \3 B$ w
- # include <iostream.h>
/ ?0 z l! k+ u# i$ ^% c - #endif' I+ `9 {' f* ^8 h! _
- static void PrintErrorMessage( int errorCode );7 G: ]8 r& _, |- `# U8 a
- static void PrintErrorMessage( const char* );3 D4 \0 v/ I* j! g3 m$ I m
2 e8 t6 m: F( c0 z- static int create_filebox( char* prompt, char* filename )
: O1 w! d3 a" I$ j6 p8 Z7 N' X - {
' s5 D1 h9 u7 n - // We could have used UF_UI_create_filebox on both Windows and Unix.0 v( a& v& \; A2 D+ D; ~5 j. @
- // The ONLY reason that we use MFC instead of UF_UI_create_filebox on Windows$ p1 G! x* Y& a9 q9 a
- // is for the purpose of demonstrating how MFC can be used inside an NX Open application.
6 R, g% R! ?) g7 s8 w% x - #ifdef USE_MFC5 p F& v& U8 }# f3 e- l+ \' i
- char Filters[]= "txt files (*.txt)|*.txt|All files (*.*)|*.*||";
1 V5 M+ R9 W* O; J2 m. I6 b
# ~4 N+ ~3 N/ s- ~; }; \* B- // Create an Open dialog; the default file name extension is ".my".
' N8 g$ X: a7 U. H - CFileDialog fileDialog (TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, Filters);
0 s' u8 O, C: s - ( I# r7 C8 N% ]: ?
- UF_UI_set_prompt( prompt );" j2 a2 N+ P8 O: Y- w8 K) T
- int response = fileDialog.DoModal();
3 l5 h, x' g6 G' e, }. w/ K! Z
: y3 Q0 x8 j' A8 }, K8 C- if ( response == IDOK)
' g/ A% Q" _! F( v - {
) w$ w A- ^$ E# r, ]7 E - strncpy_s( filename,UF_CFI_MAX_PATH_NAME_SIZE - 1, fileDialog.GetPathName(), UF_CFI_MAX_PATH_NAME_SIZE - 1 );" {$ } l e: Z+ G- `. I
- filename[UF_CFI_MAX_PATH_NAME_SIZE-1] = '\0';
) n) M! y. H" a - }3 l( Z2 E0 R. m5 _: l3 W5 M# W
- return response == IDOK ? UF_UI_OK : UF_UI_CANCEL;( q6 w/ ?* ^- y% H( s4 N: ~
- #else
$ s" d* h! T: F - int response;; u2 |8 V$ ?" i( q, ^7 m+ n
- char filter[UF_CFI_MAX_PATH_NAME_SIZE] = "*.txt";3 c. J1 r; C9 U6 e
- UF_UI_create_filebox( prompt,0 M+ }0 e$ _6 g; \$ M% s
- "Select point data file",
! D. d z: s" \2 g. X1 {) S7 P# k - filter, NULL," B* ~ S4 |9 U( v# Y- k% U6 a
- filename, &response );; I# N7 c" o5 {7 g1 z% ?- a5 Q9 I
- return response;
) Q" H3 f! O; C: T" O - #endif2 Z9 W# y, F) e D( e
- }! H; h* [( E% K. b" Y8 V
5 E7 u/ c% }* a' s, C, s$ s- /*****************************************************************************9 v' S+ Y" H& ?) u
- ** Activation Methods0 [) S% x+ q% g$ v
- *****************************************************************************/- P3 Y$ v# N( o4 R i# d- V! s* I a0 g
- /* Explicit Activation5 w" H% _7 v" q' d% G0 l
- ** This entry point is used to activate the application explicitly, as in& g. K" O2 R* v3 r5 o$ V
- ** "File->Execute UG/Open->User Function..." */
5 Y/ I8 K! w1 B# `. P7 Q1 K4 T( ^$ x - extern DllExport void ufusr( char *parm, int *returnCode, int rlen )6 J0 t8 ^9 Q- Y' O
- {
8 Q, m" C+ q0 h/ w5 G0 s- s - /* Initialize the API environment */
- q. O6 H% F( [- M, i - int errorCode = UF_initialize();" L7 B9 p% O% F
8 u% @2 R0 O* |% P7 ~* M- Session *theSession = Session::GetSession();
) d# @" g* d+ F) _1 n
. L6 y# W; _: d( m- Part *workPart = theSession->Parts()->Work();; \) e n# o6 Z2 T) f
- if ( workPart == NULL )
; ^' l! f9 Q/ }# d% x8 R. o - workPart = theSession->Parts()->NewDisplay("File2Points", Part::UnitsMillimeters);/ x( h9 u7 T8 w8 C
/ ]/ j) a7 q4 Q# M) b0 t6 O6 l+ B- char fileName[UF_CFI_MAX_PATH_NAME_SIZE];
6 s- w! ~- e# F% s' i/ K - if ( create_filebox( "Select file that contains the point coordinate data", fileName ) == UF_UI_OK )
& l2 S& s ]5 G - {
: u' P$ @5 H% I6 o0 ]2 j+ h - ifstream file1; } h3 S. N1 K% B: j% Y
- , v6 d0 z9 p* o q z
- file1.open(fileName);
1 {: r9 l/ o5 H& n; W - 8 q1 u/ h& C5 i3 M. l
- if ( !file1.is_open() )
7 Z \. W' L* |% D - PrintErrorMessage( "Could not open file\n" );8 j1 G/ j/ |4 u
- else
1 |. ]; w# h6 s" g/ g8 Q" ], n/ C G - {
$ @2 |5 b2 C ?% S+ Z: c - double x,y,z;
( \% ^8 q& T8 N5 I - char c;- V4 W: G7 y: O7 j. v! l2 P
- while(!file1.eof())
5 H. S9 p- d) c0 ~9 k+ D - {
2 `, j6 ~ @$ _+ } - file1 >> x >> c >> y >> c >> z;
9 H# L6 g& d) d - if(!file1)6 t J; p2 C% {- Q
- {
# O! ]! y( R5 d) U/ k - PrintErrorMessage("File format error:\n The input file does not have the expected format\n");9 R1 B9 N% t+ @' b4 D. v* p
- break;; \: c8 O# _- [( d7 K) C& J- K2 L( l
- } ( [" e! M" p! |9 v0 Q3 \
- NXOpen::Point3d point3d11(x,y,z);0 ^6 B9 s8 [$ l
- Point *point1;
: z8 v2 L* I' D' G% k0 R" R - point1 = workPart->Points()->CreatePoint(point3d11);
# |1 `/ |4 A9 x) @8 i3 n% }5 ~ - // The point is invisible when initially created
* ?3 V% ~: ^, P+ W - point1->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);: r0 J0 i, {: Y( W9 K
- . |( O' C: T: v1 D, t0 ~5 t
- // Check for end of file+ x L9 Y- n; l* ]
- file1 >> c;
3 ?5 q/ f' F1 q3 z - if ( !file1.eof() )
" V8 k+ O8 J4 b2 Y - file1.unget();
: D$ p( x/ Y* c$ a; y7 F( A4 l - }
6 f0 @7 s% Z$ q1 h( z/ P - file1.close();2 d* x+ x! B3 p/ u* G: J. z2 i
- file1.clear();
6 c; W. A* b$ W! ]$ C& P! J - }0 }! u, g+ W; G( B
- 4 u% W' ?9 n! O. u+ [, {
- /* To save work part using NXOpen automation APIs */+ Z1 g' s. ~# V3 g
- /*NXOpen::PartSaveStatus *partSaveStatus;9 q9 y' v3 z0 t5 J6 J' F; ]
- part1->Save(NXOpen::Part::SaveComponentsTrue, NXOpen::Part::CloseAfterSaveTrue, &partSaveStatus);*/) X& y- |# H, o* o2 c! ?, H
- , W4 i: Q' _( X2 D/ n- S+ R3 o
- /* Terminate the API environment */
, F1 g1 h3 M9 f* N - errorCode = UF_terminate();# Y" J7 u% f+ B- L, Z
- }! | J2 A2 I: m7 o# C7 C( Y
- 4 Y8 C/ U; D4 v U8 O
- /* Print out any error messages */; p& u. ?, H$ V k8 Y5 e
- PrintErrorMessage( errorCode );
) F) ~( ]+ ]4 x# S2 A4 }" Y+ e - }
8 ^4 c& X; H* B1 Z& {
: Y$ X1 [$ J9 s) h2 E3 H1 s1 O9 i4 ` R- /*****************************************************************************( R- y# u* O* |
- ** Utilities
7 ^% f$ ]% U$ X2 U' a3 U! j+ y - *****************************************************************************/
V: ]& I6 f" o5 B) x! l. _ - ) _- e w7 v- t" n1 ?; S
- /* Unload Handler
( j9 E7 ~( |" r P% Q0 z - ** This function specifies when to unload your application from Unigraphics.
4 C# L, C9 _/ ^ - ** If your application registers a callback (from a MenuScript item or a
# S! X5 u* m4 E) _# c, e7 K - ** User Defined Object for example), this function MUST return
/ e; Y7 \/ G8 }4 Q0 { - ** "UF_UNLOAD_UG_TERMINATE". */$ S3 b1 E: ^$ k3 U9 z& S x. f
- extern int ufusr_ask_unload( void )
7 o. `5 W' g' O1 Y. D - {
1 o1 e1 Z1 p9 z0 c* o. _ - return(UF_UNLOAD_IMMEDIATELY);: [/ S& `9 |" [; @
- }( k7 {! |! c8 I0 D$ j. G" [
- /* PrintErrorMessage
' ?- B) v0 m) I9 C% u2 F" j+ K" H - **
% V) M% _" G p3 S; R8 h" T - ** Prints error messages to standard error and the Unigraphics status
+ L2 v1 M. o- V% F - ** line. */
, }8 n% t1 @" b - static void PrintErrorMessage( int errorCode ): @0 M+ T4 u: ? D0 V% u
- {1 E) o" A8 J Q7 T i3 o
- if ( 0 != errorCode )
& b) G( _3 }. f( { - {- w7 \/ a1 B& @' ]
- /* Retrieve the associated error message */
( m( S" F3 B) c; o3 S1 Z6 @" w - char message[133];- w' W9 g+ R' Q
- UF_get_fail_message( errorCode, message );
9 w0 V6 E2 e/ } - $ d7 i6 U3 a) k1 s
- /* Print out the message */' K' m9 s5 h* Q! T
- UF_UI_set_status( message );
0 C9 R; }2 I) c" H9 j - /* Construct a buffer to hold the text. */% ?+ Q9 `$ X& g3 v" D1 e& Z
- ostrstream error_message;
2 h/ |5 P3 x: w' u
& w1 S% t) v; h! ?9 y- /* Initialize the buffer with the required text. */
+ Y7 [. ]; f/ f6 Q! l - error_message << endl
1 G5 ?7 [7 A6 J1 ]! l& q% ^ - << "Error:" << endl; q) \- M; L* g; [/ J
- << message4 ]' ?. \- M+ t, h. S3 ~
- << endl << endl << ends;
+ }7 |4 _% }5 S2 @- s2 R; U - $ D5 C8 Y4 \0 Q! }
- /* Write the message to standard error */
' x! @$ Z5 O# n4 t0 E' O2 S0 y; m - PrintErrorMessage(error_message.str());7 e" C1 l- V4 M' \7 J
- }1 f7 a6 q) s1 ?$ k; y. f# Q
- }
" b+ o# E. r v
& `4 ]; b3 m0 [5 q2 i7 P- static void PrintErrorMessage( const char* msg )& @. G; v3 N) ^* ^4 u4 N
- {- s% Q1 E+ \7 y
- #ifdef WIN32+ D2 e( ]& P* o% i( \
- static bool consoleInitialized = false;
+ \+ d% H& K& G0 t6 @ ]5 s - if ( !consoleInitialized )
' k S4 J5 O) `4 w% e! g+ }9 P# ^ - {
% _, B% ]( r9 @ G1 q, O8 { - AllocConsole();, l( p) M: G4 W# u
- FILE *stream;
0 M: G: j7 J" ^. y& T - freopen_s(&stream,"conout$", "w", stdout);
8 K$ g* E7 l5 K# z3 P' {+ I - freopen_s(&stream,"conout$", "w", stderr);' P# } f8 c: h' R& d8 P6 k
- consoleInitialized = true;
9 O# B4 J2 Y( _0 g - }
' W7 D4 @ L: A - #endif
% i2 v3 _, P) Q! _ - cerr << msg;7 {6 A. c2 D7 A. v9 H& B# u8 x
- }; [ G5 r2 ]3 m$ k6 X1 M
复制代码 |
|