|
- /*=============================================================================
1 j! h( ^: l6 e4 ]$ k- P+ r& c7 p - Copyright (c) 2004 ugS Corp.% N4 T! k2 c) `8 ?/ F+ Y7 |$ V, @
- All rights reserved
# x8 t5 p4 X! s - ===============================================================================
) o) } w7 ~2 B# k - File2Points.cpp
' K! ~6 H) q7 s+ S! R
% \, e- S. o& f9 b( ?- Description: Contains Unigraphics entry points for the application.
% ]2 O o9 ~) _) i# C
" a, }6 Q' ^* Y! v+ O
. u: g* p, L0 d* h- =============================================================================*/# T; r8 W" E/ B, q& u% G
- ' T: j/ [7 ?# G( t
- #ifdef WIN32& H; a; n! u, }/ M5 l; [
- #define USE_MFC
* @0 S/ y+ q0 B" n: [: I - #endif2 m0 m8 f5 F4 D1 ? P- M; t. X3 z% x
- . v" `7 X) X6 G
- #ifdef USE_MFC
8 X2 m5 H1 e% S1 ~ - #include <afxdlgs.h>. ^4 F, L7 T% W% f. Z4 L4 U8 ]
- #include <afx.h>/ U3 f. j; L# C4 ^: l$ `
- #endif
1 A P0 O: B6 I3 k4 P; m - . F9 z# z, }5 u7 z
- #include <NXOpen/Session.hxx>; T& I: D$ a9 Q4 n
- #include <NXOpen/Part.hxx>( P6 l( P5 N$ P4 ~
- #include <NXOpen/PartCollection.hxx>& G6 Y! g3 j9 M
- #include <NXOpen/Point.hxx>
. r9 P3 z9 I% }& D, V - #include <NXOpen/PointCollection.hxx>
8 ` S. j& {! }5 g0 ~
/ E. _% W: p% T; g* g6 ^- #include <fstream>* J6 p4 X. E+ V$ N2 P0 A
" u8 V9 ^% x6 v* E) ~6 x- using namespace NXOpen;( H3 v2 U9 S( W
- using namespace std;
4 S' l- ]4 e- M# ~/ l/ x$ y+ I
8 S1 H+ L/ j$ l9 M7 Z# M- /* Include files */
3 `3 r* E* e4 h/ k0 I - #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
# y, ]2 t) t2 ?5 l0 n - # include <strstream>
& ~! L3 D; ]9 G3 V& M; G - # include <iostream>
! d) j. h* N& S3 p& y3 x - using std::ostrstream;
% N* Q h- j! H - using std::endl;
& S) {1 n. Y$ b+ f; n+ ^; V - using std::ends;
6 Q. B7 _6 K, d! f& ] - using std::cerr;0 F2 O# e4 e. A
- #else
@" ]- t/ b0 l- e& T9 t - # include <strstream.h>
4 |9 L; P4 t& ^) g- P* ]" X0 Q - # include <iostream.h>
5 w7 g( @; [9 K, e) x - #endif
j8 o% L7 }( N1 I8 V3 L+ c - #include <uf.h>
4 k$ Q# G8 ?/ A. k7 e - #include <uf_ui.h>. z; l! `5 d+ @! b% J4 j
- #include <uf_exit.h>
, C, E- a/ V. z: M8 a8 ^6 A) A9 s - #include <uf.h>! j( B0 o, m. h2 l) T, F
- #include <uf_exit.h>
2 `+ v: ]- N& L# n y5 F& I. E - #include <uf_ui.h>6 I- U( m" n& w/ U) q
- #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
4 ~- I3 B' t* s* ~& E - # include <strstream>6 p v; Y2 T: s
- # include <iostream>
1 I/ c: P! \2 d$ @0 i; @ - using std::ostrstream;
& T4 T& A8 g' N2 L: \" R - using std::endl; : l, O" M; y0 R/ N7 r0 f2 X0 t
- using std::ends;, N& T- Y W+ c
- using std::cerr;* N$ @4 e; d p, }$ H- u l; z% {7 A
- #else
; q, x" Z4 y/ h- Z) O, _ - # include <strstream.h>* r9 h0 F% O7 l+ C$ H2 V4 S1 c
- # include <iostream.h>. v6 T8 G: V7 I/ c" m
- #endif% M- v6 a- x; J" }6 T1 s# J- w
- static void PrintErrorMessage( int errorCode );
( g/ ~3 _% \; ?! B& R) \9 p* M/ n - static void PrintErrorMessage( const char* );9 m3 s& f+ Q. O( ?# J5 i% D( i0 ]
- ) k0 W" ]+ @8 ?' I
- static int create_filebox( char* prompt, char* filename )$ |$ k7 t \& H
- {
, m) w; Q5 b5 Y( L - // We could have used UF_UI_create_filebox on both Windows and Unix.4 @3 H" P# N2 }5 V. K) H# R0 e. h
- // The ONLY reason that we use MFC instead of UF_UI_create_filebox on Windows
' d! r: k$ ?% {+ T; \8 L: L# R - // is for the purpose of demonstrating how MFC can be used inside an NX Open application.- j$ `* H2 ^+ v8 b; P
- #ifdef USE_MFC
) y0 z. k% \/ v- j$ P* x# ? - char Filters[]= "txt files (*.txt)|*.txt|All files (*.*)|*.*||";( }8 j F5 x9 {, {) V1 G# N
7 t; d2 z( p' K) L; e) f: j8 t2 n- // Create an Open dialog; the default file name extension is ".my".
4 |' g) D, j0 p, \2 T, Z- m - CFileDialog fileDialog (TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, Filters);
+ h" N7 ~1 i1 N: Q6 U! M/ E+ a
# b" j& q# O, i: s, p2 }+ n- UF_UI_set_prompt( prompt );, @1 i6 s! A2 M$ `4 v- e
- int response = fileDialog.DoModal();9 U# }1 L3 h3 E9 [- l
- # G+ x. S# x ~- g' |0 i- h9 \1 F
- if ( response == IDOK)' S# _ e+ y# n
- {, s- N7 C) H: N, J" f+ T& U
- strncpy_s( filename,UF_CFI_MAX_PATH_NAME_SIZE - 1, fileDialog.GetPathName(), UF_CFI_MAX_PATH_NAME_SIZE - 1 );, h9 h' s1 ] ^6 e
- filename[UF_CFI_MAX_PATH_NAME_SIZE-1] = '\0';
9 t7 P% K( O0 \" `0 `! r - }" ?; h7 N6 t1 w# V
- return response == IDOK ? UF_UI_OK : UF_UI_CANCEL;
; i: Z& ~7 R7 t8 g- d - #else / L x X) s2 }1 X! O, T- T: Q
- int response;
7 X, Y( F0 O5 b1 m" G) L - char filter[UF_CFI_MAX_PATH_NAME_SIZE] = "*.txt";
8 h: u0 S; z8 L. `! {( z' k3 N! ?( H - UF_UI_create_filebox( prompt,
* }. ]: _ D, C8 a% E - "Select point data file",1 x6 I1 |' `, _8 s
- filter, NULL,
& G& P) [% i; n& [7 G! e - filename, &response );
+ i7 m# p' M$ `4 j( E# V - return response;. V8 i! n7 D8 }5 P, W+ x9 o
- #endif
) t5 Y2 o% E0 K' b" f( l' Z - }
& e* [- |; n3 c* g - & ] J" O( n x! n
- /*****************************************************************************8 @/ n4 y$ G3 w. T2 i! c8 M8 d/ m I
- ** Activation Methods' W( g; W$ ?5 E( l( ^
- *****************************************************************************/6 H' @8 w$ v% P
- /* Explicit Activation
- i( F# Z/ H# Y: O' j9 t$ @! i5 D6 e% [ - ** This entry point is used to activate the application explicitly, as in# H* ~; T" @7 }5 z# H
- ** "File->Execute UG/Open->User Function..." */' Z3 v$ \ ]! g, [. j3 u! J) D1 R
- extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
0 n1 d( E/ j0 u3 K - {
- n# o" N" Q. @ - /* Initialize the API environment */' o {8 {% O- Y+ e$ Y
- int errorCode = UF_initialize();/ Q% j7 z0 [7 P! J
- . Q. w: V( j% o* x8 j
- Session *theSession = Session::GetSession();
6 ?- J; B/ z6 e) [
/ t7 H8 ]! d5 t; a6 G- Part *workPart = theSession->Parts()->Work();
( Q+ M/ i( b: d/ F - if ( workPart == NULL )
9 W7 _4 X1 j7 `# O1 {, E' J - workPart = theSession->Parts()->NewDisplay("File2Points", Part::UnitsMillimeters);& E6 p! K8 O4 G& i1 l3 z4 f
, a5 P8 N, Q6 P6 D3 `- char fileName[UF_CFI_MAX_PATH_NAME_SIZE];$ R, M' Y' t: l
- if ( create_filebox( "Select file that contains the point coordinate data", fileName ) == UF_UI_OK )
8 S6 o6 F& Z$ @/ K% F& b! e - { 0 e. z) G- q1 C6 s% U2 O
- ifstream file1;
( Y0 J" ^ [/ [, I* Y8 X
0 Q7 [* T# z3 n- file1.open(fileName);$ [5 S2 y5 R* w# L6 E
- & v3 T. F' C- X
- if ( !file1.is_open() )
" K# }$ K/ [5 ?- e% ^& t - PrintErrorMessage( "Could not open file\n" );
8 l: R: c* o5 y; Z$ P) m - else$ R* |' |* Z* D, F* x `
- {
5 s! I! ?: z* L$ q) `% B6 l - double x,y,z;$ {3 G4 z' V% ]+ {: w: w
- char c;0 W; j3 z6 l3 Q' f y/ V" G
- while(!file1.eof())# O7 _ F v% N" y/ d
- {. w6 A' J7 f7 k- T4 r( C$ t
- file1 >> x >> c >> y >> c >> z;8 ]$ T6 j" M% I4 l' ?
- if(!file1); b; H, i) g, `; C6 C, @3 N2 j2 n
- {
! @1 A' f. b. m+ l# Y3 S! { - PrintErrorMessage("File format error:\n The input file does not have the expected format\n");2 P- b2 a& F. m: w
- break;
2 c" S0 a$ Y' `/ }1 u) V$ s - } % g# A- v4 K8 a$ F4 U# [ Z# ^
- NXOpen::Point3d point3d11(x,y,z);
1 f8 `7 e) @- T3 s/ M. @; j4 C - Point *point1;
7 d$ t+ M2 C' c* F. }' [ - point1 = workPart->Points()->CreatePoint(point3d11);# I' S D% m1 g. W4 Z
- // The point is invisible when initially created. Z9 J) Z3 z( c9 @
- point1->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);
& j% l) A+ _; G) r. u( ^
0 z; l( n% ]1 B% [. l0 Q3 O W3 H$ y- // Check for end of file' p7 O# X5 B9 O; L( z0 Z/ b0 b( Q: q
- file1 >> c;; \* J- @' R% ~2 \) w8 H
- if ( !file1.eof() )6 |8 y6 z; ~9 V
- file1.unget();: e3 V' B9 _$ n
- }
% K& ?0 [, P/ L: A - file1.close();
* Y( y" T& |1 E) o - file1.clear();/ `+ ^) \4 s% O9 m) f+ w8 _
- }
' t. K. \# y9 r' G - ( | H9 Z% O5 \# A6 ~& K# G
- /* To save work part using NXOpen automation APIs */" w9 K& k3 c. E
- /*NXOpen::PartSaveStatus *partSaveStatus; V1 x3 _* j1 y
- part1->Save(NXOpen::Part::SaveComponentsTrue, NXOpen::Part::CloseAfterSaveTrue, &partSaveStatus);*/
, }$ a6 m: Y$ A- ^, S2 K( ` - ) D7 { g! m, a A a' Z
- /* Terminate the API environment */
6 H$ ~6 a( A" F+ u7 k - errorCode = UF_terminate();
' H. T. A# a: ^$ t - }
2 n8 Y Y3 @5 {4 H8 ~+ p - + u$ s( P' O% q% C# m
- /* Print out any error messages */
# v1 E& ]# Z2 K( Z0 U2 Q9 c - PrintErrorMessage( errorCode );
6 r( A5 Z" N* |* l4 Q8 v: B& L2 b' _ - }
/ I8 A, {9 H5 N5 d4 c" q
) W0 R6 z6 d) d% S( p- \) v& y W- /*****************************************************************************3 V' M: g u( }8 F p
- ** Utilities, k7 T: v: t, f2 W
- *****************************************************************************/; @/ c0 R8 n! ^9 E7 Q) J; _2 p
- 5 o. H5 q3 R0 F$ i( r7 D
- /* Unload Handler
p4 p# g/ J+ Q* b - ** This function specifies when to unload your application from Unigraphics.
) t6 q" b% T: q) V& Y. Y. S - ** If your application registers a callback (from a MenuScript item or a# ^ }; p! x- k' z" r7 s; k
- ** User Defined Object for example), this function MUST return+ D1 O! z& g: X# u
- ** "UF_UNLOAD_UG_TERMINATE". */
2 ^! f% ~. W3 S& V& ? - extern int ufusr_ask_unload( void )
& ~1 q! G. h& y - {9 v5 Q: W1 G" {
- return(UF_UNLOAD_IMMEDIATELY);
2 E9 c. u! X# g' v* f - }
|; H6 l$ {6 R1 U - /* PrintErrorMessage, j4 R& ~; i8 a" D: {
- **4 P/ Q# x2 Z, Z7 @6 Z8 N+ k" P
- ** Prints error messages to standard error and the Unigraphics status
, |( D4 q" D4 f- ^6 A( ~7 @ - ** line. */
, m% B% N: h1 e& t4 ~ - static void PrintErrorMessage( int errorCode )
1 ?; E v, b& E) E; b2 h6 o* @3 `% l - {
; F W; n! ^# J - if ( 0 != errorCode )( j- m2 x( L7 ?* O, S8 |
- {9 J, V& v8 Q0 ~% c& |- d
- /* Retrieve the associated error message */2 q7 Y& N! P+ P- T* p0 M; o
- char message[133];
. |# R2 ~$ X3 p; l6 [: w* w - UF_get_fail_message( errorCode, message );- [+ ^4 E5 ~9 P! G
0 y- c1 x7 C" c& z+ \4 W5 p- /* Print out the message */
" F8 u$ F; L2 U3 i1 G% t - UF_UI_set_status( message );4 k! [; O$ n; F4 V- O6 v- p1 F" N
- /* Construct a buffer to hold the text. */7 F8 w& z4 i- I
- ostrstream error_message;1 P d3 N- Z& i7 i% p
- 3 i1 ]$ r: h% N; @, |8 O4 N
- /* Initialize the buffer with the required text. */6 {- {- I# U7 ^9 ]9 Z2 h7 L
- error_message << endl" Z' w0 n/ c5 N/ p8 J7 J" M5 v
- << "Error:" << endl' K$ n, P4 \7 k# _7 f, O
- << message P! J. }/ Q) Z. x7 ?
- << endl << endl << ends;
4 S7 u+ x" p7 b1 ? W" R. ~4 z _
2 R* X9 w Q/ R# A- /* Write the message to standard error */; E* k( m# a& ^* `3 ^
- PrintErrorMessage(error_message.str());
/ S1 ?- D# b" w. ~0 q - }
4 q# O- H: b; e0 O, K; h+ @ - }9 H W+ u' H5 _! o
, d4 T' f% @6 M9 }4 V/ |* B! O) d" x3 N- static void PrintErrorMessage( const char* msg )
* V5 d5 G, D, L - {% p) j8 ?; g- k
- #ifdef WIN32
% a. ?- s1 p6 d0 M( a - static bool consoleInitialized = false;
2 j3 [4 y- ^/ z. u K9 `( N - if ( !consoleInitialized )0 X* h. o2 C. B @+ D* ~/ I
- {0 H. ?9 O$ k3 H3 }! {2 n5 E
- AllocConsole();
: n( j: z2 K) {3 v/ s0 @ - FILE *stream;- Z; x3 n) V' S: S
- freopen_s(&stream,"conout$", "w", stdout);
' @1 ]9 h1 @" i/ U4 k/ H - freopen_s(&stream,"conout$", "w", stderr);/ d5 T5 s" P, j) |+ _' ~ w
- consoleInitialized = true;
. S& m' I- E& g3 D& y - }
% J& j% ] q7 s7 Q - #endif
; H5 ]( x4 T! f2 b T0 s# B( ~ - cerr << msg;# {: b) B' \+ A ?- A% E# X
- }
* x7 f/ e& t& G
复制代码 |
|