|
- /*=============================================================================8 D' {2 W4 S9 V g- j7 W' B9 u
- Copyright (c) 2004 ugS Corp.
& M) s3 T( S! q+ B7 k - All rights reserved
. n h5 w8 I: o8 F - ===============================================================================
" W6 U. ^+ H3 z0 r! v. x - File2Points.cpp& Q, s+ } i; d" u
- 3 K# s4 Q2 v. L) }! f+ P" a- L$ b
- Description: Contains Unigraphics entry points for the application.
6 O2 S/ f; y4 e' W
+ Z% H% F! E, K
( g+ [5 [" \" ^5 q- =============================================================================*/% y: z1 i: y5 R
- " B" P: }, ?$ B$ S" T. K* y
- #ifdef WIN32 J: d" o" k1 D' @1 x$ m# R# A6 m
- #define USE_MFC' o+ C, T' S5 T1 _' B# t. b3 \- M
- #endif
3 F9 P5 ^/ F7 _
: ?9 p: d: ?5 q Q' r" h1 G- #ifdef USE_MFC
! P+ |, R8 ]* u" {) C3 A - #include <afxdlgs.h>3 E# [, E# o! y. t
- #include <afx.h>
( Y# R: p2 u8 ` F- }# z% G: m - #endif. T8 ]9 \* q% W* o$ c, t
- ) t3 Y2 K6 ~) m" c+ P
- #include <NXOpen/Session.hxx>9 T. G& X& ~" F4 c0 j6 }
- #include <NXOpen/Part.hxx>
' D+ B1 x+ W1 z2 ^/ b - #include <NXOpen/PartCollection.hxx>
: C& T, ?4 s" i) a# U9 Q# q - #include <NXOpen/Point.hxx>
5 m7 l/ v+ H9 g - #include <NXOpen/PointCollection.hxx>
0 c" x+ O6 h9 F, f5 M% z: G9 X
8 j' }3 P6 x. r9 K8 [- #include <fstream>
0 G+ f( s# E' c$ {% E
y1 z2 p& U$ m* v0 y- using namespace NXOpen;9 n# c5 H* U6 b% F- n# T1 F6 y, e
- using namespace std;
/ k' `" ?* i8 i- M
. L. B( r. n. W8 t% ?( S- /* Include files */; e* B& r! _6 Y8 `- R/ f5 O! h
- #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
! i; B, w7 y! Y& T. W/ Z$ N - # include <strstream>
8 H) F7 Y" O* l4 d3 p3 T; { - # include <iostream>* m, O, S& s# e( B& `
- using std::ostrstream;
6 P5 j1 [0 N2 v- V; x - using std::endl;
& C( q$ D, U$ O5 @; z7 r - using std::ends;
* g9 l. d/ d# U& v* e$ I - using std::cerr;0 U5 u' B% w& f! T* O3 ?8 [7 D) w( |
- #else
1 y: C& M- r( c1 |! l" o1 @0 L - # include <strstream.h>: x6 ^! D& ?- c- W$ H) U9 F. g
- # include <iostream.h>$ P& |$ |0 }) d9 N
- #endif! {7 ?& E9 d Y R" c
- #include <uf.h>
) ~4 L! r! o y3 F$ \ - #include <uf_ui.h>
3 s& U4 M& u* v/ ?! n - #include <uf_exit.h>/ C0 q* g# K+ d$ U( f" K
- #include <uf.h>; w- p3 E$ S- |+ d4 r
- #include <uf_exit.h>! y+ w8 u* C( L4 A% J/ e1 g
- #include <uf_ui.h>8 b; |4 ~- m$ Q' R
- #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )- M; W( Y% k& t1 W0 U2 c
- # include <strstream>$ b. I- b P' e% ]
- # include <iostream>2 E. A# w- `$ ^0 f
- using std::ostrstream;8 @# d2 d( j+ {3 C- U4 ~" q7 G( j
- using std::endl; # E4 E+ w; p8 L+ [
- using std::ends;$ P7 f# r4 G3 M) y# o5 H
- using std::cerr;7 z1 ?9 a+ R0 D1 }, I2 {
- #else
: {8 o& B2 c) A9 k" L2 S4 D6 \ - # include <strstream.h>1 Y" x q; |1 X& M! Q' R
- # include <iostream.h>
0 l+ _( G' s- Y& f( l5 K - #endif
* ^9 s9 J$ ^* Z( q! z: U$ x( n - static void PrintErrorMessage( int errorCode );2 \8 m4 Z& _- y8 z2 h
- static void PrintErrorMessage( const char* );2 K+ I9 N. W# m7 ~5 ^- a
5 u+ ]2 C/ a/ G- u2 m& M- static int create_filebox( char* prompt, char* filename )+ Y) A l; b ^) U7 J5 e( }
- {& N7 ^9 i) X+ N! Y- j! D
- // We could have used UF_UI_create_filebox on both Windows and Unix.8 l: ?9 R3 C1 q$ L3 r& x
- // The ONLY reason that we use MFC instead of UF_UI_create_filebox on Windows0 C3 [% s" H" O3 D
- // is for the purpose of demonstrating how MFC can be used inside an NX Open application.$ z+ l- P6 Q/ `' @* Q
- #ifdef USE_MFC- n5 _+ a: h0 x1 N9 M3 m
- char Filters[]= "txt files (*.txt)|*.txt|All files (*.*)|*.*||";
( v$ h7 T* O* f
- p1 [; T& s8 ^; A$ Q9 g- // Create an Open dialog; the default file name extension is ".my".
% @. _0 h+ y. Z8 i% X - CFileDialog fileDialog (TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, Filters);
& W# `. C$ _+ M7 o8 `8 | - ' G6 c7 \4 c% `) x1 M- @1 K2 {
- UF_UI_set_prompt( prompt );
' F. n& y0 [ d4 a1 Q9 v - int response = fileDialog.DoModal();
, F u% s7 @4 X2 k3 e1 l - * S2 Z7 O: H1 W! X w
- if ( response == IDOK)
9 P- G7 [$ t- ?$ q* B - {
' S/ N3 f# j6 E - strncpy_s( filename,UF_CFI_MAX_PATH_NAME_SIZE - 1, fileDialog.GetPathName(), UF_CFI_MAX_PATH_NAME_SIZE - 1 );
! ?4 H. |9 x, G7 r8 X# I& |, t - filename[UF_CFI_MAX_PATH_NAME_SIZE-1] = '\0';3 x0 X: T0 _% n6 V8 m, ]2 ?4 B3 A" A
- }
+ i0 O3 C6 ]' t6 Y; z9 ]( x - return response == IDOK ? UF_UI_OK : UF_UI_CANCEL;
1 ?; k- e' f# ?8 F- S6 b6 k0 N: y - #else 3 |/ S$ v) e' z/ c" |1 o4 p
- int response;
6 K4 O1 r) j* p. T: J! B" \; ^) o0 l. ` - char filter[UF_CFI_MAX_PATH_NAME_SIZE] = "*.txt";
/ u2 T) R* n' Q1 k8 I4 l9 x - UF_UI_create_filebox( prompt,
$ O4 Y# g) L* G- h% D - "Select point data file",
/ I1 e: K" y" k O0 s - filter, NULL,/ Q$ y g- J/ Y# E6 w8 U, ^
- filename, &response );3 _. n: n0 A, r* F+ a n
- return response;
' E+ g& @1 J* n4 V B - #endif/ {* R% D! O1 ?3 O
- }$ q3 m* w" I! D; v( U% [1 Z. g6 k
8 T( F$ B; Y8 P0 c- /*****************************************************************************
/ q" i7 Q o) L - ** Activation Methods
# F: }, P: w% ]2 l - *****************************************************************************/. D; ~2 u( g' \' P
- /* Explicit Activation" N2 K) {. d2 X& |9 l" r
- ** This entry point is used to activate the application explicitly, as in' c3 R: B1 z8 R: V# t) T2 L
- ** "File->Execute UG/Open->User Function..." */
5 e% Q' x) C" \6 x( o - extern DllExport void ufusr( char *parm, int *returnCode, int rlen )$ c$ W1 B! v6 V/ T1 l- l
- {
- V# g! U' }2 \$ |" |& Z: m - /* Initialize the API environment */
9 z9 u0 {! y3 A: D2 F - int errorCode = UF_initialize();
+ W4 H; F6 \. I& E% V9 Y$ t* e8 Y
2 u7 T" S+ s9 _+ h! U* R- Session *theSession = Session::GetSession();
+ ^1 R% q5 Y* n: Z
0 s/ P' N' j& _3 c7 P) Q3 k- Part *workPart = theSession->Parts()->Work();
, _" T! W& {* _3 K9 Q - if ( workPart == NULL )2 O* W: O- r, h3 Y0 [: T% g7 m, u
- workPart = theSession->Parts()->NewDisplay("File2Points", Part::UnitsMillimeters);
' @& Y# E$ o0 \$ h% M3 O- i* o* @7 B' ^
6 ~% l! h/ z8 A. ?- char fileName[UF_CFI_MAX_PATH_NAME_SIZE];3 {8 R# N$ ?* X* A: m: S
- if ( create_filebox( "Select file that contains the point coordinate data", fileName ) == UF_UI_OK )
9 n* z& p$ {/ [ - { $ x9 _; e' a4 k7 X- T* C" H
- ifstream file1;
) B" R e6 @" ^3 ]3 m8 O" f
) B9 d3 ]9 }9 k x- file1.open(fileName);
, a. @8 F# i/ V2 V3 S) `; f" v
' o+ y! d9 z- S) {3 o' ^- if ( !file1.is_open() )
% k0 }) `, d S5 E) }1 k x - PrintErrorMessage( "Could not open file\n" );
* @* c3 @9 s* U7 _ - else
0 e; o& \4 M2 `9 ?0 s( g, h! @ - {' w" D8 [& _% D5 R0 b# O" ?
- double x,y,z;3 J2 W* v3 h* O2 R9 \
- char c;6 P+ J: _1 A6 f3 N5 k/ S
- while(!file1.eof())
, @8 t6 K: t0 o5 @0 n2 y5 z+ \3 Y# w - {
0 Z' g+ U9 `% c4 O - file1 >> x >> c >> y >> c >> z;- G* k/ E5 @7 K! o/ q3 t
- if(!file1)5 k1 d' ~% p1 B) n
- {/ i$ i! c) [% Y/ b; i
- PrintErrorMessage("File format error:\n The input file does not have the expected format\n");
9 L& c# ?# }6 D- \/ g - break;
- z" R* m: p& l. Z: { - } , i0 Z6 Z4 N5 x0 P7 f
- NXOpen::Point3d point3d11(x,y,z);
6 H' _! H( R8 {( g. o$ Z. i - Point *point1;
: H- n, q, f+ g1 {1 D' g - point1 = workPart->Points()->CreatePoint(point3d11);+ r0 s9 ?3 v2 n% B& R8 v, l) z
- // The point is invisible when initially created
4 t5 Y: d) w" W# E# j8 R. o - point1->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);
0 i8 d& O9 g7 z5 T% e, E - / b1 U. v: G7 L/ Z. v0 l p* G- k
- // Check for end of file
- g, y3 _8 o* `- S - file1 >> c;7 `( {" Z2 A& }6 J, G; [/ J' h
- if ( !file1.eof() )
! O% t1 o- J/ u% {- y* L) B - file1.unget();/ C/ ~/ D( x0 N, X
- }
3 J: w' P/ f4 D, N" ] - file1.close();. I# V* x) c1 f* o/ E. a
- file1.clear();* h; T8 E2 J% k. `+ s
- }! z3 T6 q2 k9 c6 U% ?( D
" Q2 o# k* _7 T9 D1 f Y- /* To save work part using NXOpen automation APIs */
; X5 g; m; C9 E" K" N2 g' A - /*NXOpen::PartSaveStatus *partSaveStatus;
# c( n, ~: }0 m7 K: P. N - part1->Save(NXOpen::Part::SaveComponentsTrue, NXOpen::Part::CloseAfterSaveTrue, &partSaveStatus);*/
! E% q* k% E* W
# U5 P. r; d) J$ W8 z' {0 H5 n! K- /* Terminate the API environment */5 M: ]6 U! I, J$ {! G' W
- errorCode = UF_terminate();' m; F) L, ~' h! |% m
- }
- t* X3 n, m# I0 A, C - $ s( B: }/ P W6 k& z: P
- /* Print out any error messages */" Q5 u5 d }$ b3 ^
- PrintErrorMessage( errorCode );# D9 B& Q! q/ u+ s& W6 v" `
- }" U# C' m- }3 k
! d V8 _- c ~" g& s# ~- /*****************************************************************************; N: ]; N2 g& r# a; _9 v
- ** Utilities
4 T, f$ G. S' }! p7 H - *****************************************************************************/; j2 ~, ]# g7 f7 I! f" @ Y
- 8 D( ^/ p# q& [! u; q; I4 H
- /* Unload Handler9 }8 M: s! Y0 x6 G
- ** This function specifies when to unload your application from Unigraphics.
. l n0 X, N2 i7 n0 Y6 \& i, ~& U - ** If your application registers a callback (from a MenuScript item or a
+ R( g+ x3 Z! j: r$ j0 ~! c, T - ** User Defined Object for example), this function MUST return6 y Y" }" E: y' n9 p
- ** "UF_UNLOAD_UG_TERMINATE". */3 ?2 W% V5 A) R( h6 ?7 n8 j2 ?6 y I x
- extern int ufusr_ask_unload( void )7 l: c- g& _2 `# V
- {
* Q$ X$ r' ^9 s$ T: S - return(UF_UNLOAD_IMMEDIATELY);
: z. `0 `/ J8 e( E& d6 V; O: a" L( x - }/ w6 Q4 L2 r S6 {& V. i1 q7 L
- /* PrintErrorMessage6 @2 E& M+ i/ K7 r, @0 S
- **" n4 U6 j3 s" ?$ A; w9 [4 i
- ** Prints error messages to standard error and the Unigraphics status
1 b+ T0 F+ R5 @1 o/ A9 h* Q# r G8 ? - ** line. */
* z W0 c+ t3 _3 ^8 D& M; }" i4 _6 | - static void PrintErrorMessage( int errorCode )
' {+ ^; ?) H9 k2 P - {
& b# O) ?# b* Z2 b1 ?! Z1 l - if ( 0 != errorCode )
0 `- m! Y7 B' j2 V - {% F7 A7 K! | e" E% Z/ L
- /* Retrieve the associated error message */$ H$ ~3 W ^" m# G7 E9 ~' f
- char message[133];* v9 B) K, d$ _
- UF_get_fail_message( errorCode, message );+ T6 q# p: ?& L: v7 l4 w9 g" B
- ) m5 o/ ?9 j) L( g* c+ ?
- /* Print out the message */
/ U" B$ x j; n+ X/ q - UF_UI_set_status( message );. ?2 u6 b( Q1 K' ]$ E2 [
- /* Construct a buffer to hold the text. */
% u3 q, a2 W' B1 H p6 n% `/ i - ostrstream error_message;3 L! }! M- p- ]4 ~, |1 o+ B- H- B6 `
- $ S' Q1 n6 |6 r* ^9 W6 S/ G1 H
- /* Initialize the buffer with the required text. */
- V+ a# L. t6 [8 { - error_message << endl2 M8 [+ @7 h# a( b2 e+ y9 Z
- << "Error:" << endl! o' u, s8 j- B+ B8 e
- << message
8 ^7 A2 u' E R2 y" p - << endl << endl << ends;
: e! A. A3 N5 W
/ A, P, ?% I5 n/ h2 |4 R- /* Write the message to standard error */
2 t5 O) v8 i$ Y - PrintErrorMessage(error_message.str());- e; N4 U% _$ X
- }
/ }# ~: d9 K4 ~& ~! i - }0 O0 K3 E6 b7 [1 y7 X
3 Z1 v& c2 I8 O& k! O N- static void PrintErrorMessage( const char* msg )
0 J7 _4 h. Z8 U2 @( u' s - {' [% _/ m9 u4 _2 e2 Z. S
- #ifdef WIN32
8 s. G5 T6 ^7 u4 p% \8 Q0 y - static bool consoleInitialized = false;( o' `- ?% W5 Z, B. R( c2 H( ^1 T
- if ( !consoleInitialized )7 C2 @- X5 i& r4 D, C* h. _4 K
- {
# b' E% }! K# i - AllocConsole();+ t9 B6 u% E! r" p2 D, n) [. U2 D
- FILE *stream;
! L) `+ G7 p! a u4 D/ @ - freopen_s(&stream,"conout$", "w", stdout);3 \; q I B+ Q% ~9 [2 U
- freopen_s(&stream,"conout$", "w", stderr);
4 a5 P$ y. L. q7 ^ - consoleInitialized = true;5 H; y6 a0 P4 j1 L) H& }
- }, B, e, H$ ?9 F3 ]! G- H9 q A0 v( O
- #endif3 g8 @6 M' H; r* x# Q" m3 N* x5 A) O
- cerr << msg;
& M- s; D& Y: G. @" e - }
. a0 M3 e2 g1 n1 W# Z: f
复制代码 |
|