|
- /*=============================================================================
, c* W' |8 }' Y$ ?( e( ] - Copyright (c) 2004 ugS Corp.
- a& r% h6 X5 [! s9 k - All rights reserved
) K" i9 i- j. Z' t - ===============================================================================2 p+ ` n2 s l$ w4 ^$ a8 x, z
- File2Points.cpp
1 v; U( y. d7 a - " r9 X. `* L) o/ f8 n
- Description: Contains Unigraphics entry points for the application.: F' g6 l! I5 H6 C
" M6 d( M, G) r$ d# o4 Q* Y% r, Q
& I( o/ [% g( ~, j7 J& z; V- =============================================================================*/' V9 R' J' H" {* r* ^
4 C1 |) E5 u' E; }8 A) k- #ifdef WIN32
( e% _0 o( k; n) ~ - #define USE_MFC
% s+ N! l! ~6 ? - #endif& F3 o! c" j8 m0 n% y
* }* i2 \ c& H. ?* \# Y- #ifdef USE_MFC9 e" E! G! O; D9 J5 {) a, l
- #include <afxdlgs.h># r/ d! H$ A+ Y+ O, x: D, X8 m
- #include <afx.h>
; M5 Z4 ~8 B( _2 A* z; S - #endif5 t3 L4 x9 l: E1 O6 K- l4 r
+ E3 T7 g$ C: e- #include <NXOpen/Session.hxx>
9 A; U$ k2 m/ I - #include <NXOpen/Part.hxx># a; g- m$ z: ^2 e
- #include <NXOpen/PartCollection.hxx>
. E1 E! Q3 g$ d+ J. x/ ^$ x0 M1 l - #include <NXOpen/Point.hxx>+ {9 }' U% q8 |& N( Y
- #include <NXOpen/PointCollection.hxx>. D5 a" d5 r9 X2 J& Q% T
% f" O# l/ \$ w* w) T3 }' S- #include <fstream>
" J, X0 E0 [7 J* b/ }' B a
/ D3 F5 `0 T" m& c: c% u- using namespace NXOpen;
5 M0 n \. g0 ]8 a7 } - using namespace std;
! _ Q( A: n+ t+ b' K' O9 u& l - 0 I4 _5 e, A* f" n/ h6 n
- /* Include files */) k3 T$ P5 y. U, S# u t% U
- #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
+ z3 h' j5 ]; r9 ] - # include <strstream>
' K* d4 v' v* _/ p o - # include <iostream>& Y7 l0 U& X8 I8 I4 \" U
- using std::ostrstream;
& `3 W3 B4 Y- n/ p - using std::endl;
: H. f) {& R, i4 [ - using std::ends;
8 u7 {! a6 Z* ^$ { - using std::cerr;
k2 O: s8 n9 R! @ - #else# `4 R6 Z+ I# E" w" n2 d3 x
- # include <strstream.h>
. V$ X1 O. c, J& r" z8 |- w - # include <iostream.h>
: X) Z( `+ G$ E3 k - #endif9 ?9 A! [, _$ d' G# ~: k
- #include <uf.h>
: i8 F$ u1 I- T& T1 N( ~. m - #include <uf_ui.h>$ ]# j7 I, Y3 B B/ I8 d* X
- #include <uf_exit.h>
5 X6 Y" H) Y6 n& w! V5 y/ c - #include <uf.h>1 o" ~- f; I! K4 G' r: F, C E$ Y6 n
- #include <uf_exit.h>/ A6 U2 ^5 G) ?4 d+ b6 F
- #include <uf_ui.h>
/ I) u# }: [) ^4 K6 c - #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
- \; t+ Z! |8 j* ` - # include <strstream>" b1 l% a, t8 @
- # include <iostream>
& i* w9 X2 {/ j$ j+ ~1 t) D$ y - using std::ostrstream;" Q5 t) e5 g/ c! U; W1 X$ [/ X) \
- using std::endl;
- L3 l; K4 Q* |2 q" T( N1 D. } - using std::ends;
2 g2 i8 F: @/ t0 ^4 t - using std::cerr;
; D/ J; u' h) t - #else) H. |( p+ B& @ L, I- y6 [& y
- # include <strstream.h>
- b+ E) K( J# ]# X3 k - # include <iostream.h>
: Y4 M( F& K4 w - #endif2 ]- M a4 b5 X! \/ ]
- static void PrintErrorMessage( int errorCode );
9 D' w' {# r* E k" v' X3 u* } - static void PrintErrorMessage( const char* );
8 ]. l3 j9 v3 z' U q) O
$ |0 Z% _0 E. |$ a2 b! c4 P- static int create_filebox( char* prompt, char* filename )7 S- i! U3 t: w# Z0 V
- {
- ~1 S! B G4 J9 I% G; c - // We could have used UF_UI_create_filebox on both Windows and Unix.& }. ]1 x3 q, C9 f8 a
- // The ONLY reason that we use MFC instead of UF_UI_create_filebox on Windows7 i4 a. ^ Z) h- u, k# n
- // is for the purpose of demonstrating how MFC can be used inside an NX Open application.- b9 ]/ `8 p( \/ `6 {3 E2 M/ r
- #ifdef USE_MFC$ K7 `' p9 y: t0 R' E; o$ {. `
- char Filters[]= "txt files (*.txt)|*.txt|All files (*.*)|*.*||";' ^9 u' M3 u) |; G0 A( S
, F' e/ m+ f$ v( q( J- // Create an Open dialog; the default file name extension is ".my".
& f3 G8 G' w8 j* s4 @! z/ n7 | - CFileDialog fileDialog (TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, Filters);% k& P. I. w G1 F" G
; v5 S& |" W; X* c1 c6 c; ]! i- UF_UI_set_prompt( prompt );5 U& \- v$ `# w. n! O
- int response = fileDialog.DoModal();" o/ P1 M3 S$ ^) u
- / w; a9 H9 |' w" r
- if ( response == IDOK)9 E x' [8 t2 v4 w- Y
- {
/ H3 |& H! s' y% q- v. S. j - strncpy_s( filename,UF_CFI_MAX_PATH_NAME_SIZE - 1, fileDialog.GetPathName(), UF_CFI_MAX_PATH_NAME_SIZE - 1 );
) D/ Z* S# X n, Y& H3 Q: q1 e J - filename[UF_CFI_MAX_PATH_NAME_SIZE-1] = '\0';
6 N7 o2 T; |1 ?6 w @) P2 n - }% w: _4 Q* d0 y; V- p
- return response == IDOK ? UF_UI_OK : UF_UI_CANCEL;
p' B" i2 y( m$ y) k - #else 7 ] ^# v' ~2 \5 s6 q
- int response;1 o( v9 y/ p/ s8 g
- char filter[UF_CFI_MAX_PATH_NAME_SIZE] = "*.txt";. Y% w% b+ q; J9 B* Q% X
- UF_UI_create_filebox( prompt,
5 U$ c5 k9 f/ N2 W: D - "Select point data file",
+ K2 ?# y1 N6 D- [% g$ s - filter, NULL,
8 v; O+ d5 l6 A8 w7 C - filename, &response );, K: T4 \! G1 I
- return response;- H! v2 {1 x" o/ h
- #endif
8 ?9 e1 X) u; c, n - }
3 k' r! n6 N1 j* r% J: f4 ` - ' h$ z4 s5 k H' p
- /*****************************************************************************: v& v3 A9 d- E4 u1 @# @
- ** Activation Methods
) R2 C! T6 x+ X) K1 A - *****************************************************************************/
) E; r' O2 _$ x - /* Explicit Activation; {0 s0 F' C7 f6 r( Y2 i+ @
- ** This entry point is used to activate the application explicitly, as in
* V1 t1 i' p* A8 Z - ** "File->Execute UG/Open->User Function..." */7 B0 @8 Z5 V4 N( l J
- extern DllExport void ufusr( char *parm, int *returnCode, int rlen ). K2 T' l2 A7 J+ S
- {* C8 s9 m7 n7 c+ T$ T6 ?4 b
- /* Initialize the API environment */
: s9 H; `) U9 D - int errorCode = UF_initialize();
t. A. l( z5 b. w) @' ? - ) p8 Z3 t! d' U: y
- Session *theSession = Session::GetSession();
" S7 L! e- s P' n, K
# m% s2 a! [; K G8 y3 J8 a2 h H+ @- Part *workPart = theSession->Parts()->Work();
% D2 W$ D. j+ I9 `. r) X N - if ( workPart == NULL )
! a5 _; R" O; E: Z9 |$ @ - workPart = theSession->Parts()->NewDisplay("File2Points", Part::UnitsMillimeters);' t2 B+ i1 G9 D' ?
- % L7 y& [5 y0 \" e, c
- char fileName[UF_CFI_MAX_PATH_NAME_SIZE];, g/ P3 B* m) O1 U1 J: W" [% E
- if ( create_filebox( "Select file that contains the point coordinate data", fileName ) == UF_UI_OK )
3 L7 |9 u2 f/ f4 B( `; m- | - { 6 ]0 [/ ~8 Z" W' n ?% \! P
- ifstream file1;
9 V( |! u" [( |1 K! u6 a$ s- r" C
9 V. V: u" I6 P5 Z/ ~- file1.open(fileName);: l6 b" k: m8 _/ m* b; |# f
- ) k2 R* L8 M, {' o% t6 e
- if ( !file1.is_open() )
& w0 U( @- d- {+ x - PrintErrorMessage( "Could not open file\n" );
: d0 h$ K+ @/ H4 f0 }8 @% R& R. ? - else. v- }% I2 v/ c4 b/ Y! j
- {
+ p9 V1 g# r1 y1 w, b3 Q - double x,y,z;
8 W9 w" P I0 \7 M6 F - char c;
* t% K% B5 y, P9 L$ w) ? - while(!file1.eof())
8 C8 R7 i8 x& V - {' i% g4 u4 j1 u- ?4 u; Z& {' ~
- file1 >> x >> c >> y >> c >> z;( W4 p: A! L% s" I3 I' S. K& V/ v W
- if(!file1)! n/ S2 x8 A2 f7 t! b+ R) p
- {
7 S: |+ v8 h# t - PrintErrorMessage("File format error:\n The input file does not have the expected format\n");
9 v; E' P) X. J" _" t" {1 D - break;- d7 n5 n% b3 x$ W1 @
- } 3 b! T8 y% v3 z1 N. M
- NXOpen::Point3d point3d11(x,y,z);
0 z7 q# P& Q- w0 Q: {$ b5 ? - Point *point1;
0 z# Y/ @9 b: j/ ] - point1 = workPart->Points()->CreatePoint(point3d11);
9 Z3 ^ P% p7 W/ j2 e! E - // The point is invisible when initially created; i% ?6 s- b6 M; i: a. J
- point1->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);9 K. ]7 L( H. O0 ~8 g8 l, h( c
7 \+ C; U. N5 `& }- // Check for end of file
% O4 ^' U- @3 N' t+ g/ `- i. F0 t- m - file1 >> c;' I6 {4 C! G q: j
- if ( !file1.eof() )
7 I. W% O8 [9 K1 c& |! `: R9 n9 ~ - file1.unget();% a! p1 v7 a8 K" Z# c6 C
- }7 d" I! E# m$ z6 S$ f3 n
- file1.close();5 Q3 u( A6 ^* @8 p
- file1.clear();
8 q- w% K5 x& _( l5 [- y - }
6 O* f- {% `1 v" z - 3 {8 `8 u& B' m0 L; i9 [ S1 \
- /* To save work part using NXOpen automation APIs */: `8 J% g- d% I2 S. ^8 e3 n% z
- /*NXOpen::PartSaveStatus *partSaveStatus;' T1 }: y! F6 m) @7 y3 V
- part1->Save(NXOpen::Part::SaveComponentsTrue, NXOpen::Part::CloseAfterSaveTrue, &partSaveStatus);*/
& V0 {' ]) }. Y - + F5 C6 }& }9 ^! E! F- o5 p
- /* Terminate the API environment */: b8 \' Z( ?: g% U* @
- errorCode = UF_terminate();9 D0 m: i) q( `& L- n) Y
- }* B. r8 u1 ?! d/ r! u! |" E
- d5 i5 n; |. i/ u2 G- /* Print out any error messages */
: u( g7 b% X" k3 ?' G2 z; g - PrintErrorMessage( errorCode );
6 @! S. ^+ B( B" R+ x - }; a- R" \+ R6 Z- u# l
/ W9 Q2 b$ I3 M: ~# t- /*****************************************************************************" ^- I. k% L, v' u2 c- Z
- ** Utilities
( z [& q6 Q8 r9 H7 U - *****************************************************************************/( _& o3 B7 C$ h$ c$ \! ^' K
- : l+ E) K) P* J) F6 e
- /* Unload Handler) R) ?* m. H& b4 p$ g
- ** This function specifies when to unload your application from Unigraphics.( R: x0 } F! q; Z9 {
- ** If your application registers a callback (from a MenuScript item or a
# B7 J/ r) r' @$ d - ** User Defined Object for example), this function MUST return
/ l4 o3 l$ V3 |( a+ @( w - ** "UF_UNLOAD_UG_TERMINATE". */7 D! [2 U, j6 I' m6 Q' `1 ]
- extern int ufusr_ask_unload( void )
% J2 ~ J/ B8 W* w, m - {
1 Z) t& Z' V! C2 {' h - return(UF_UNLOAD_IMMEDIATELY);. {! `5 Q4 {% k7 F S
- }, m9 }" u) g) U: H' p" ?' P
- /* PrintErrorMessage' l" \4 j) b8 Q9 ~- T
- **& p5 g6 O+ J! j" C
- ** Prints error messages to standard error and the Unigraphics status! k: ^% E0 y9 |! d; }- l
- ** line. */6 P. m) P0 x( y! r1 ]$ x+ G# p3 Q! H
- static void PrintErrorMessage( int errorCode )% H, W0 ^4 W2 C, @% c# R5 p
- {
) [+ w1 B X! V, t - if ( 0 != errorCode )* L# W P$ Y. a; N. W' ^8 I: J0 G
- {* B1 M3 P/ k0 y$ _ \
- /* Retrieve the associated error message */
: S7 i) B3 f, R8 h6 q- L6 a& | - char message[133];
9 V) V5 V/ A4 h7 T. \4 a0 ? - UF_get_fail_message( errorCode, message );( {' _7 b7 h/ m+ U% I8 k3 W3 q
- 6 l& ~: N, p7 u7 {- n
- /* Print out the message */# q* C! L# B) ^3 u. C% |' {- w
- UF_UI_set_status( message );$ r1 h ^/ _; c) {! H
- /* Construct a buffer to hold the text. */6 W' W. b! s# ~$ p s
- ostrstream error_message;
& G/ c6 o0 J2 q2 b% J% v$ E! z
% j X7 j- _% c) o1 w/ _% I- /* Initialize the buffer with the required text. */
B1 a- x2 Z( D ?- o! ~8 ^ - error_message << endl W- L* Q9 ~0 V* x) k( t5 u9 C
- << "Error:" << endl8 b8 i6 B* P; w) y- E
- << message/ h: o: w6 ~( f& s) _& {
- << endl << endl << ends;
% u2 j: r1 |% z. ?! E4 @ - 5 |: _+ ]0 g7 M
- /* Write the message to standard error */6 h( L- S( X: u& E, q
- PrintErrorMessage(error_message.str());7 e2 [: v; V8 H8 W! W
- }
8 B7 J1 v* r W# f! E8 h - }
: ]5 E8 I9 i8 u+ B; x( z - / s' C# c# W1 B8 R: ^
- static void PrintErrorMessage( const char* msg )
4 I& u6 M" K- p( {1 w, U - {
2 y3 B f* A" {4 Z" p - #ifdef WIN323 Z6 z2 F7 Q; _$ W- ^
- static bool consoleInitialized = false;
+ ?' G$ K6 X8 t* m( l7 R3 c - if ( !consoleInitialized )
0 m: u$ x4 A9 ^: Y. a! F) Z P - {& j0 J. V8 L2 x; {2 n, H# r
- AllocConsole();7 w+ J+ N* z" _) E" y. f9 B& M
- FILE *stream;
. F$ Z0 X$ N+ S3 V6 P8 Z5 r - freopen_s(&stream,"conout$", "w", stdout);
4 O8 K6 {: w9 I, ?0 t7 u' x - freopen_s(&stream,"conout$", "w", stderr);: x) G- z X2 G; U- T' W# p( e
- consoleInitialized = true;
- Y9 Y; M5 a4 q9 H1 O - }
+ C0 T/ w/ m! d; ?5 ~' @ - #endif0 ], K' v, g5 W- b
- cerr << msg;5 Y R) B+ O) W Y5 I+ \3 H
- }
/ Q1 S5 r) F7 P
复制代码 |
|