|
- /*=============================================================================
6 K: w4 U5 F8 u3 X9 B1 H& C - Copyright (c) 2004 ugS Corp.
3 z3 F; w. Z3 R( q, ?$ T - All rights reserved. k( a Q/ l8 @1 f. `" k( \
- ===============================================================================
e5 O; Z; F* D9 r - File2Points.cpp) \4 M+ w! ? J; k) b5 D. a
- - x% d5 z1 h: ?/ p- L z9 m
- Description: Contains Unigraphics entry points for the application.
5 i( X+ d$ q; S2 ~7 S - 6 P. z: Y0 o% |
' b# o3 r' @4 T- Y- =============================================================================*/
; b4 I A: C5 g - % U9 O1 y- ?4 y
- #ifdef WIN32
+ o0 i5 q [' p3 b( W - #define USE_MFC
: [5 Y# \4 F9 ~# s - #endif
5 _" l0 \/ k0 B9 \9 x, }: i; F) c( L9 w
6 W* t# K+ S) S3 I& v- #ifdef USE_MFC
5 u; d; a) B3 I$ \ - #include <afxdlgs.h>$ a, u5 r; Z6 @# L
- #include <afx.h>3 Y) V2 A& g, n$ Q
- #endif) K s% |. ]" M. M! P/ F
. v. r, p' { U, ~) V- #include <NXOpen/Session.hxx>
4 i( p- p8 |% Y! K; [- C - #include <NXOpen/Part.hxx>+ c; z c' H0 f; D
- #include <NXOpen/PartCollection.hxx>) b3 S& R( K3 ]& o7 ?$ p( k7 h
- #include <NXOpen/Point.hxx>
* B3 m- G0 B6 Y* Q) G - #include <NXOpen/PointCollection.hxx>. h! v) [6 A; F2 B$ O& g
- * m6 I S0 \% L6 @3 c
- #include <fstream>0 a+ m" Q% n; q* V
4 o% J8 w) t. f* A/ n- using namespace NXOpen;
; C. X' `& E1 ], I9 ] - using namespace std;3 N$ h+ j7 f, k$ T! w/ L
) \3 i$ A: r; x: H( |; M- /* Include files */3 d. w- i0 V; Z8 i
- #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )& b/ Z" y9 J ~+ P; w: E$ D4 }
- # include <strstream>
5 {( d$ r: }) G" a# l: R( H; ^ - # include <iostream>
/ P$ }' }0 |5 M2 {8 O7 D5 v - using std::ostrstream;
Q+ {5 w8 C; {* n - using std::endl;
" \/ e0 }) P$ ?2 M - using std::ends;
& z1 r% Z P g+ Q3 B2 P - using std::cerr;
0 I2 | G. V" x* u" r$ L0 m) R( M - #else, N9 `1 J4 j; W2 i0 V: M. {4 B9 D
- # include <strstream.h>- ]; K m f( U' u, O
- # include <iostream.h>6 N! H$ K6 Q; U2 z9 a4 q/ K
- #endif4 e' n8 ~- r2 S x$ B( |& G
- #include <uf.h>
% r6 S8 s' ~& [! x - #include <uf_ui.h>5 S3 r; Z! V" |& c
- #include <uf_exit.h>
7 Q8 t8 S( ^% K9 C$ t# n7 n6 R - #include <uf.h>
) H4 g% Y( m+ ]* _/ L! b - #include <uf_exit.h>
2 g2 n ] C+ g7 o5 k - #include <uf_ui.h>
2 D2 w! z3 i: B- w u% ^' E: ^ - #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )* ^6 f" i# L# ?7 H; I
- # include <strstream># M' a; f4 j |7 D. C, N! K+ `- E
- # include <iostream>
! U6 ] U+ p+ A# J5 R c - using std::ostrstream;4 M1 x$ d F# X3 k$ W3 R
- using std::endl;
; p [* i2 |" J' P3 Z - using std::ends;
" C2 v0 @, h( X( W; @/ t. R" G" } - using std::cerr;. D1 M; V% p* h( i2 R9 T
- #else8 |5 N B' C7 u5 U
- # include <strstream.h>; b; l( E% d r8 ~5 c7 f
- # include <iostream.h>
6 {6 s3 @8 @9 r. V/ P5 J; `0 \; Z, M - #endif6 H. X) B( a% a" B6 J. Z& p
- static void PrintErrorMessage( int errorCode );
# L; h7 o; X+ W( Q+ u+ _ - static void PrintErrorMessage( const char* );
( G! K. b# l# \# a' ~! ^7 U% A - 5 R7 @5 D6 M1 [) i& |0 N# g
- static int create_filebox( char* prompt, char* filename )
+ R Y7 m, Z% q1 L - {) c" `( ~+ V8 {& q; n" R
- // We could have used UF_UI_create_filebox on both Windows and Unix.. J, m4 G. [- O L# K1 \
- // The ONLY reason that we use MFC instead of UF_UI_create_filebox on Windows0 n4 m! R* m- x9 Z
- // is for the purpose of demonstrating how MFC can be used inside an NX Open application.3 J# R% f; C6 R7 n9 f( t
- #ifdef USE_MFC2 {! A0 J+ b. y2 e
- char Filters[]= "txt files (*.txt)|*.txt|All files (*.*)|*.*||";
! D1 V p* b0 G+ F! l$ S
& R4 ` s8 p1 o7 X6 f I! B, T- // Create an Open dialog; the default file name extension is ".my".2 O/ j+ K. R, A6 G4 j
- CFileDialog fileDialog (TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, Filters);- T( v% j& f% Z( Z& g( F% n- c5 \3 ] }; ?
) e( z4 V/ J( v! ?: o- UF_UI_set_prompt( prompt );6 K) A: u7 P6 _( X1 n0 X9 [
- int response = fileDialog.DoModal();
: ?3 F4 }& L7 @ O1 q' d
5 x, Y6 n5 @% N; L8 ^9 S- if ( response == IDOK)0 r7 a) x! ^, W
- {# x# D u" C: v5 @1 [/ ?; ^7 z: P
- strncpy_s( filename,UF_CFI_MAX_PATH_NAME_SIZE - 1, fileDialog.GetPathName(), UF_CFI_MAX_PATH_NAME_SIZE - 1 );
5 [$ T$ Y3 s8 {% F3 G% r! ]1 ]% g, P - filename[UF_CFI_MAX_PATH_NAME_SIZE-1] = '\0';- F2 s0 U" G3 z% o; L
- }3 y" m/ |- f% R
- return response == IDOK ? UF_UI_OK : UF_UI_CANCEL;
/ {7 g. `2 x/ m' C - #else
, U) Q/ o* D8 K# W - int response;; @% a& u8 L% t& X
- char filter[UF_CFI_MAX_PATH_NAME_SIZE] = "*.txt";
4 C/ w3 D' A2 N. \ - UF_UI_create_filebox( prompt,
3 Z4 S, ]1 J$ H# m* G - "Select point data file",% f/ o0 x# T6 z+ j0 {8 L! T
- filter, NULL,7 ]0 T7 o+ x: [/ u" ?5 I6 L
- filename, &response );4 M, A5 K& Q! \1 Z9 P
- return response;
* V; O. M3 I% V7 M8 K) C1 K" U( v, D+ Y - #endif2 Y: `6 {$ }* O" d( G; l8 Z: v* n
- }: m) Q3 s3 l" B1 m Z* n( r. U
- * e- S' U: g6 M3 Y( [! r! p8 @
- /*****************************************************************************6 b$ W& A$ v" l( a5 y( u
- ** Activation Methods6 U* Z8 m/ K9 M8 }; y) o7 {" T
- *****************************************************************************/% q) D& |2 J' F6 g2 U
- /* Explicit Activation$ O% v/ R7 O6 ]" S; W
- ** This entry point is used to activate the application explicitly, as in P* n! A& u* l$ X
- ** "File->Execute UG/Open->User Function..." */7 K# } `1 H" Q) e, `! p
- extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
* e R2 c9 i: l7 H* n* a4 ?# Q - {
% p, M$ ~6 H9 g - /* Initialize the API environment */
_+ E1 K9 b% H! |1 ` - int errorCode = UF_initialize();6 V4 N; c. @9 s. p( C8 e( Q
- . p& U8 S% o5 {# e! W% U
- Session *theSession = Session::GetSession();
( I9 G" _' P: }4 i - , o: h8 T+ u3 p( @
- Part *workPart = theSession->Parts()->Work();
* q" f0 e4 b8 J - if ( workPart == NULL )& [' ~% Z( q+ K8 ?2 H/ ?0 `* `
- workPart = theSession->Parts()->NewDisplay("File2Points", Part::UnitsMillimeters);
2 n# _9 k( L$ |( p% h
9 g' r8 {. p: V+ K; j- char fileName[UF_CFI_MAX_PATH_NAME_SIZE];
+ [+ c) {# x9 C$ |+ D0 ?" F$ s0 \ - if ( create_filebox( "Select file that contains the point coordinate data", fileName ) == UF_UI_OK )$ f' L( F) E. F8 D
- { ' r2 z4 a2 ^4 p1 y$ E
- ifstream file1;
N& R0 W7 J J+ j8 q: u& K - ' \1 P( U* i2 q& S; ? r
- file1.open(fileName);
. @" x& r0 Q4 v# f5 O& s( `. f0 Y! ~" g
4 s0 }3 h" Q! F8 i5 V' f- N4 _- if ( !file1.is_open() )9 ~4 X- @8 j2 O9 k9 p* M" D: f
- PrintErrorMessage( "Could not open file\n" );- R; g1 T5 O* v" O0 L! e3 e
- else
$ j3 a$ Z, x% w' ]% {' q - {
& q+ j6 _! l& I$ s9 P$ y5 C - double x,y,z;# S( Q1 p! E) \8 b2 u
- char c;
# S/ g- T. y4 | - while(!file1.eof())
5 J8 Z1 o' ~$ Z - {
" ~; x2 S+ `+ j0 g - file1 >> x >> c >> y >> c >> z;+ n4 x5 i* N- ^" {" l
- if(!file1) W1 c% w3 E) A% g
- {7 R5 n' `3 g/ z
- PrintErrorMessage("File format error:\n The input file does not have the expected format\n");1 U/ K! ], q9 _
- break;
2 Y' N v+ ^; l - }
p7 F. r _ N% O7 H# Z3 B- z" ? - NXOpen::Point3d point3d11(x,y,z);& N; J. a) ~/ U3 b$ e
- Point *point1;
8 M7 i' A& i) V$ i4 G) ?. V - point1 = workPart->Points()->CreatePoint(point3d11);
3 o4 s; P! N; g+ \ - // The point is invisible when initially created
, t$ V! _, V* p g! E" y- ] - point1->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);2 V- f6 C! J9 N
- % O: O9 T% E3 c% W
- // Check for end of file
' y" j1 @" d9 H0 [3 `# X6 |0 S' [ - file1 >> c;
. b% V% r' ^8 m$ P' m - if ( !file1.eof() )
6 H. \4 L0 ]. E9 f0 f" c5 b6 K8 [' a - file1.unget();+ }5 c5 X4 \2 P7 o
- }
! l' \2 i, A6 {6 T6 @ - file1.close();
" T) R7 c' o8 Z2 H! y) s) ~3 C9 x; M - file1.clear();
% b6 [! D9 ?% C7 X- Z% L - }5 d' x. R) Q: h/ u$ G
- 7 ]+ q( v8 \' O% p
- /* To save work part using NXOpen automation APIs */
1 J4 \9 B2 ?7 V, a" w1 z+ H( w" I( _2 v' } - /*NXOpen::PartSaveStatus *partSaveStatus;
?$ V9 h- X5 V/ F P# L - part1->Save(NXOpen::Part::SaveComponentsTrue, NXOpen::Part::CloseAfterSaveTrue, &partSaveStatus);*/
f3 D- _; h) `9 o
m H: O; S+ {- e9 N- /* Terminate the API environment */8 h3 Q8 W6 Z. ~: o+ l# e8 `% T; j
- errorCode = UF_terminate();
7 w6 G. K, i& E - }8 l3 C" R5 J+ r: b: x
- . t- V7 Z+ G; W. L9 `
- /* Print out any error messages */" P& ~' l k# S @ f0 p
- PrintErrorMessage( errorCode );" y2 k7 B0 `1 [1 B0 [* T) w
- }
# N3 d2 H v% w) P
3 @7 e/ |! T! K- /*****************************************************************************
- j. g6 p; R- K: G7 W - ** Utilities5 G6 F$ z$ \" V( C# h/ }# @3 B
- *****************************************************************************/. d) N' f# u3 E" A! T1 B( z" x5 A
/ B5 q T9 x9 X- /* Unload Handler
3 H* q9 `% M+ n& S - ** This function specifies when to unload your application from Unigraphics.; N4 Y5 K) _ e, I1 p
- ** If your application registers a callback (from a MenuScript item or a' n7 g0 y. b$ S; [/ ], m
- ** User Defined Object for example), this function MUST return
6 y. {: r. k3 g - ** "UF_UNLOAD_UG_TERMINATE". */4 g! C+ f, t: ?
- extern int ufusr_ask_unload( void )
* [# Y) o% f* c" m5 j: a - {+ `; Q6 K# p0 p T1 ^7 t( x
- return(UF_UNLOAD_IMMEDIATELY);
8 P( @) e3 Y# @2 y. s4 s1 |7 ^ - }
+ ~, K" q- R7 J5 q. q8 } - /* PrintErrorMessage
H- X' S% R6 U E - **
$ r. O) Q. M) ^- }+ B9 l5 E - ** Prints error messages to standard error and the Unigraphics status
( S1 F3 j( ]# Y7 j4 j, a+ c6 c - ** line. */
& t, E: a% \. M% w R$ d - static void PrintErrorMessage( int errorCode )# [" p! w9 N/ I( @" c+ c- `' e$ j' l& \
- {
) @8 o/ z$ |1 T5 S. ?6 K* { - if ( 0 != errorCode )
. E W6 f1 R4 ]/ N0 v9 D1 O8 e# z - {
. L& M. ~' ?8 c: x! D# D/ @ - /* Retrieve the associated error message */& u, l8 ?; Z- Y
- char message[133];
# s) W; U, U9 F: G$ d - UF_get_fail_message( errorCode, message );
' t, g; r) Q; q1 v: ]1 Q, k [ - w2 u, W. ]/ v l$ U
- /* Print out the message */
0 a: r1 @9 Y; S D1 b/ P - UF_UI_set_status( message );. p* e# V Q2 p _( c
- /* Construct a buffer to hold the text. */& P- q+ {6 O% P8 b
- ostrstream error_message;
& E7 ]1 R4 P" X4 f
8 D" Q' g* y4 }1 a$ ] u- /* Initialize the buffer with the required text. */
% ^# A" K$ H) T4 `# b - error_message << endl8 f( ^' Z# v m! d
- << "Error:" << endl
' W+ W9 }7 d; a4 T - << message0 D- W( t S7 g! I
- << endl << endl << ends;
) u& W4 `# o$ j; k3 [! O - ; G; Z4 P) K+ L' h- n% {
- /* Write the message to standard error */ f+ y$ w9 ?, b* r7 _1 ~$ e3 Y" P4 E
- PrintErrorMessage(error_message.str());
6 v: E2 |. e9 `& E7 k( g2 U - }
: m, F) D4 Z2 `# \ - }
2 ^ Y$ w6 R' P" U* @. l1 x - 1 I2 e) C# f# b* D
- static void PrintErrorMessage( const char* msg )" r) j3 K2 D F, q* V4 |9 T5 l/ }; H. f
- {
! S [) {0 ]' L5 ~: n0 r) i8 U - #ifdef WIN32 h7 Z! ^; W7 F& J1 }/ R, O' @
- static bool consoleInitialized = false;2 n8 u. L' Z7 v' P$ ^2 X, M
- if ( !consoleInitialized )3 O9 u& \6 u/ z0 E
- {* @+ w7 i$ W8 W# l. r
- AllocConsole();) _7 B3 A- a/ ]$ M2 ]
- FILE *stream;+ a" K3 e3 X0 f/ e: g- s8 P+ \
- freopen_s(&stream,"conout$", "w", stdout);! C" i6 a9 k, ?; Z0 s6 F! y0 `- O
- freopen_s(&stream,"conout$", "w", stderr);
0 A3 _6 W, E9 {, [; L" d4 W1 u - consoleInitialized = true;: o6 H3 q! N' d% A. v( T6 p5 n
- }
8 L5 c |* ~, m' I - #endif
: s8 n2 D. l/ ?# O- |2 [ - cerr << msg;
; I/ ^% M' P# ~+ y. O - }
' {1 \$ H% q- q. s3 } D- |
复制代码 |
|