|
- /*=============================================================================4 L( p: }" ?9 W
- Copyright (c) 2004 ugS Corp.
2 G; ^7 Z ^* C6 k' q3 D1 i/ b3 |% C% F( A - All rights reserved
4 [- Q0 I! K6 b J - ===============================================================================
. s" s- u p: q7 B8 O- m N - File2Points.cpp
7 m0 h7 x& A" L7 Q
2 {/ \, `/ P( l1 f6 H+ k' j" \- Description: Contains Unigraphics entry points for the application.
5 F; Y- {' d- A& x, t
/ F5 k. D5 h4 i% A8 h! a( H
9 o2 j/ R1 q" d9 k/ @% \- =============================================================================*/
- z, g; d: h5 R+ I% k
; U! z) i* G# d4 c/ Q S- #ifdef WIN32/ h: w$ p" i* B. M% h/ o& g
- #define USE_MFC
; C) o* Z* o3 U - #endif
3 |! g% j J& V/ R$ w3 S; E' M - $ W( Y0 B; x L3 G2 a4 U3 y
- #ifdef USE_MFC
y8 c) N" J* b4 I& w - #include <afxdlgs.h>
# E4 i, J* z+ n1 J' m0 Q - #include <afx.h>
9 w) A0 |3 Z! F - #endif" \9 o9 h- j a- W( M
- - e3 r; J2 N$ Y% Z# H3 i- L
- #include <NXOpen/Session.hxx>$ A3 Y: c7 ~4 C- B9 |
- #include <NXOpen/Part.hxx>/ d! ?/ }6 _) X( ~% k
- #include <NXOpen/PartCollection.hxx>0 g7 S( [. _4 |* U Y
- #include <NXOpen/Point.hxx>
! Y0 n& f1 ~* H8 x; K - #include <NXOpen/PointCollection.hxx> n; l7 m5 z8 u6 q6 X4 C* j r( v
- * c) D( |* a7 P3 A0 ?
- #include <fstream>
0 A( ]/ }- J. l3 \& F, J
4 s- Z: P7 ^' N/ y- using namespace NXOpen;
0 s" z5 i% D5 U, a. n8 S3 }4 s4 | - using namespace std;
; L6 ], b3 v3 _# A+ N - 2 O. o. u4 k9 `
- /* Include files */4 A! B" C2 y4 N! {) E
- #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
8 x* }' \( d# H, w - # include <strstream>
2 v% x R: i- n6 v/ v - # include <iostream>/ c# n' b" R! H% P4 X& I
- using std::ostrstream;
$ `6 Q0 f* w* v) V - using std::endl; ; j4 |5 B! o$ {& [
- using std::ends;% R3 m5 D+ J& D D
- using std::cerr;
# B, F# Q6 p. S3 i& z& X: h - #else
( ^( i- g1 q0 {& D6 v- @; C* m - # include <strstream.h>0 b) K% l7 e0 A, ^, g& e# F
- # include <iostream.h>. Z+ r1 I* H; M- Z0 ]2 N9 X7 ?
- #endif
# I* Z3 t8 f5 ^/ D7 G$ j$ i9 q - #include <uf.h>7 a; C3 y* r2 H
- #include <uf_ui.h>5 P: d- Z5 b+ Y3 o, C0 R) B8 j
- #include <uf_exit.h># B) \% t- C, V6 n/ w4 \7 M
- #include <uf.h>/ f$ H7 P6 n: ~3 `9 n
- #include <uf_exit.h>5 Y$ X& d+ X0 R9 u0 u3 w
- #include <uf_ui.h>
/ h& r2 ~" S: R% g- \, J8 \ - #if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
) m0 e2 K1 \- @6 Y% J: e - # include <strstream>
4 e! x8 Z- {+ U9 _ - # include <iostream>: l" E' R( r% v% I' f" h+ V4 W& ~& j
- using std::ostrstream;2 v( {, h) s+ v: _% O7 A! g
- using std::endl;
" N/ q3 [! B. R - using std::ends;9 f$ q8 o9 O# I# s* O5 k
- using std::cerr;
* k" {; W+ \- r3 p7 ^3 B. e# G - #else
4 I, M" S7 s# T: P; i - # include <strstream.h>& S3 o, K: m( V/ Q
- # include <iostream.h>
% O( ~! D8 ?$ y1 I; |. } - #endif
* z7 M; i& A) A# \" w+ o3 b% t0 }' r9 T - static void PrintErrorMessage( int errorCode );0 q2 N; E* Q. W/ G2 p1 ~
- static void PrintErrorMessage( const char* );
p3 n9 g6 x6 W8 M/ N3 y - 0 e: o, P8 o9 S; q+ b! ~" r7 \- D: v
- static int create_filebox( char* prompt, char* filename )' C; [8 ]4 a! |0 v& A2 |
- {! i; K8 X/ y: w, @( P
- // We could have used UF_UI_create_filebox on both Windows and Unix.
+ v1 @7 J2 P$ _( H6 N V - // The ONLY reason that we use MFC instead of UF_UI_create_filebox on Windows
0 Q* N* S& i, A/ v/ D( _4 [) |, l2 h - // is for the purpose of demonstrating how MFC can be used inside an NX Open application.
. f2 x: r- z s3 U8 A3 l - #ifdef USE_MFC# V% G' U* q8 _% a3 D
- char Filters[]= "txt files (*.txt)|*.txt|All files (*.*)|*.*||";5 y/ Z) [" W' R" B
- / ?5 f! W# R. V4 E. c$ L$ C$ q
- // Create an Open dialog; the default file name extension is ".my".3 p2 ~! u& n; V2 w
- CFileDialog fileDialog (TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, Filters);" V5 a4 {% n5 }. x# ~, n* T% |
. B3 B9 _6 V0 n0 \- ]! m0 _- UF_UI_set_prompt( prompt );
5 i# i* c3 C- m% ^' r* h6 ]4 w - int response = fileDialog.DoModal();
7 `( ^' J! p1 A" r- R - 4 k O' c- j- i0 E, ~- V( h1 S
- if ( response == IDOK)" h8 ?% y2 Z! @( S7 y
- {& W8 l5 s' f; z9 i) O, O2 O
- strncpy_s( filename,UF_CFI_MAX_PATH_NAME_SIZE - 1, fileDialog.GetPathName(), UF_CFI_MAX_PATH_NAME_SIZE - 1 );1 ]: [7 q* p7 v9 j
- filename[UF_CFI_MAX_PATH_NAME_SIZE-1] = '\0';
( |# s @$ X; {" n5 q- Z3 J - }
8 p4 ^+ k* j) ~% v - return response == IDOK ? UF_UI_OK : UF_UI_CANCEL;# l, d y) y9 b* O
- #else ) D: \( R: J6 n: @( X8 A
- int response;* N$ J6 @; Y F3 [ {
- char filter[UF_CFI_MAX_PATH_NAME_SIZE] = "*.txt";& P- p1 ?. e$ K' e# ^ C
- UF_UI_create_filebox( prompt,
5 M1 l2 [8 P' h# z, T - "Select point data file",* j- K# s `" b2 n' v5 M
- filter, NULL,
* L- R: R. P/ \0 n8 R" f' m - filename, &response );: M8 x D% E- T- X, p) ^4 H3 w
- return response;
0 G. A: r$ t ^7 S - #endif, d7 o# l% r- `4 s: d V4 p1 ~- A+ _
- }( M0 \& j9 U9 @: p
* {, z7 k, v" y2 k' l- /*****************************************************************************; x0 w& r: y3 {0 R
- ** Activation Methods
' V" x+ v" ]$ L3 d - *****************************************************************************/, w3 r3 B. M3 d. u
- /* Explicit Activation( k7 g0 n: z: R, j
- ** This entry point is used to activate the application explicitly, as in
) ~2 c. K' r* T; e1 P @ - ** "File->Execute UG/Open->User Function..." */$ q. j# f N$ u+ _; H# i5 C8 o
- extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
; U# e; e- J2 l8 J2 j9 \" \ - {* }5 l0 i$ o" v. k- `0 U
- /* Initialize the API environment */5 o. B' p3 v! S' ~9 ^; A M
- int errorCode = UF_initialize();6 Y$ o- }5 |) |8 X" A
" M7 H h. b4 s4 D" [/ x- s/ R8 s- Session *theSession = Session::GetSession();
3 B" p0 n$ C2 k! O
3 ?! R. H6 S# B" C4 c8 ?3 b; `- t- Part *workPart = theSession->Parts()->Work();- g* y: v$ S7 _- J% x$ Y
- if ( workPart == NULL )
( |# Y7 n9 _- u) \, x1 ^5 M3 e: x8 H - workPart = theSession->Parts()->NewDisplay("File2Points", Part::UnitsMillimeters);1 ]- W; _4 t8 \7 D, S: o) y
_. [- Q3 u, s& O2 I4 k- char fileName[UF_CFI_MAX_PATH_NAME_SIZE];
. N1 [4 h( j: G3 I2 c7 {4 W- D$ Z - if ( create_filebox( "Select file that contains the point coordinate data", fileName ) == UF_UI_OK )
' N! X% a& _: e - { : F e2 B5 I' ^$ w) X. L
- ifstream file1;: V' C9 w9 } y7 {( |" K/ I9 I
- / Z, G* }0 C# a) S
- file1.open(fileName);
9 J* U0 o# s8 |: Q+ i" u _
% h7 z; N$ M3 a! i* Q$ O" B- if ( !file1.is_open() )
8 n9 V, U( [) s& p* O+ R - PrintErrorMessage( "Could not open file\n" );( n" d7 D% t) B8 b8 R2 y
- else" u. @; w3 C+ B' `0 {- t
- {* [/ G6 I) c- a* R
- double x,y,z;! p! c# H- u( j Q3 @7 t' n& r
- char c;
6 c- I) F4 F9 @ - while(!file1.eof())
2 V% R4 ^: y4 n& f# h - {1 ]) e X% j" z6 r3 Y$ V) Y1 M
- file1 >> x >> c >> y >> c >> z;8 O! j- m7 ?, X* t2 C
- if(!file1)5 ? d- {4 s( n, _
- {. y( d }; D, S2 A+ m
- PrintErrorMessage("File format error:\n The input file does not have the expected format\n");
5 l. N4 q K8 x* a+ z - break;+ [1 A0 N" O- g, C ?
- } 5 X2 g" I9 F* C D8 C1 `9 r
- NXOpen::Point3d point3d11(x,y,z);
$ ?' i" b1 B y% |" z$ r - Point *point1;3 f: p* n5 s# G5 l4 u y; G. C' l
- point1 = workPart->Points()->CreatePoint(point3d11);8 s; X7 L2 B& G5 `( E
- // The point is invisible when initially created
0 h! P0 J7 V' Z; q. [+ e" h - point1->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);
( a5 I" X& }! c# r, _* o: Z
+ w2 D0 G& I, ^- // Check for end of file5 _+ M' D# [* S5 H: u+ H
- file1 >> c;
7 C# p& {& I4 Q - if ( !file1.eof() )' \% u0 n, V& p2 }5 {! h
- file1.unget();
- ]. V; v p' L8 P2 g - }5 f8 s3 E5 M+ }( g
- file1.close();: i9 {& w% a# t6 Y/ o. Z5 Y
- file1.clear();" X- ~0 _% W1 H# x
- }
, u* U: W1 s2 l' e& `6 q- j, r# e: z
% H, Q6 P+ e( N; c8 M+ A& k- /* To save work part using NXOpen automation APIs */
- N* d4 d3 ~- R+ R( N! ?+ j - /*NXOpen::PartSaveStatus *partSaveStatus;% M0 U" ~' C$ G7 O
- part1->Save(NXOpen::Part::SaveComponentsTrue, NXOpen::Part::CloseAfterSaveTrue, &partSaveStatus);*/7 [( B6 `; i% O, `, G3 k3 ?! `0 \
- 5 u' f- H7 i2 k
- /* Terminate the API environment */% h5 W6 e* W& C
- errorCode = UF_terminate();
% x4 H) I6 b4 ?; J7 E5 r/ W - }
# k) d, L1 u" z& D. Y - 1 P7 B4 | z, g& A
- /* Print out any error messages */$ F. g' Y4 d: E6 }
- PrintErrorMessage( errorCode );
! N+ C' v2 J) q6 H3 d W - }
$ p/ C7 b5 F4 j3 r
( W! n# G( f3 V6 ?6 P* l4 Q* N2 z- /*****************************************************************************
: @/ n( w) y' v+ ^0 ~ - ** Utilities+ J3 ?6 O5 T, h) F
- *****************************************************************************/6 p" S) y) w: N' Q. i2 F& X+ P9 E+ b
- + y- V# o" e1 l- z
- /* Unload Handler
$ s7 Y) }+ @. v1 z$ F: C - ** This function specifies when to unload your application from Unigraphics.3 R# j5 d3 P# W9 @* \
- ** If your application registers a callback (from a MenuScript item or a
& m x4 r1 @; a9 [8 q. z - ** User Defined Object for example), this function MUST return
, ^3 w2 w7 D; d# W0 ` - ** "UF_UNLOAD_UG_TERMINATE". */
$ c5 j) c1 l: E: i1 P4 m* F - extern int ufusr_ask_unload( void )
2 \6 m% D1 a5 v' t( c( | - {$ x4 [/ N/ g( v" h: B5 i. r
- return(UF_UNLOAD_IMMEDIATELY);' n# b, [1 r$ f
- }4 B+ t5 `. ?- \( u# \" {( E. k8 P! I
- /* PrintErrorMessage
! O1 w9 T$ P2 f& T - **/ p3 q7 m( I$ b
- ** Prints error messages to standard error and the Unigraphics status
; j( P% h# g' v6 {5 s: l9 Y - ** line. */3 a; |' [5 W6 |: y$ ]% t+ d
- static void PrintErrorMessage( int errorCode )! a: W+ \. T1 H# m
- {
0 m. i3 f2 r( r - if ( 0 != errorCode )
* a5 S7 J3 _2 ^ ?2 o8 k; `7 n - {
8 T6 C. g* ]/ Y4 N) _ - /* Retrieve the associated error message */
c( H9 |/ ?3 X. N* ]7 M( v3 e - char message[133];
8 R) A( y! V% V' K. L - UF_get_fail_message( errorCode, message );6 m- M g9 A7 D5 q2 }* h! {! T/ [
- ( S" h2 D6 q- Q# W* C2 u! W
- /* Print out the message */0 _# K1 [* R- n
- UF_UI_set_status( message );
F# s d: @" E5 B4 k - /* Construct a buffer to hold the text. */6 B9 E( U8 v9 m, j* H5 ?) T! t8 f" y
- ostrstream error_message;8 C. }$ K' x9 J, j+ I
C. g8 i, E6 m: z* W* c+ w- /* Initialize the buffer with the required text. */
0 K7 x, J) d0 w# ~9 S& g" H - error_message << endl
6 D1 c* Z s) X; G/ v! V) e - << "Error:" << endl
3 ` }9 F8 y4 k% R - << message
' k) u6 E/ y+ N2 z - << endl << endl << ends;
8 F: l, F4 C* x. F - $ w o* C+ f0 p4 l s
- /* Write the message to standard error */
; s2 m6 a& F6 i/ M7 }- ^ - PrintErrorMessage(error_message.str());
7 W C+ o9 }9 [1 T% W; H1 p - }
7 @ T4 \, u4 W- u' ?" t - }
. _: k% B" C8 p) b1 J( s - 4 [3 q6 J/ a& b/ y, p; @; ~; m
- static void PrintErrorMessage( const char* msg )
0 X: ] R2 ~! l+ J* c2 O - {$ F0 ?( a! o$ F7 k& ]
- #ifdef WIN32
9 f3 }" z5 }) s; O# l - static bool consoleInitialized = false;! y3 }& J2 W0 X' }2 S- P( L
- if ( !consoleInitialized )2 M6 _: E+ C, E( ~1 f
- {
3 P, n3 O3 n% i" I: o - AllocConsole();" f8 e# S( B, o) H7 Q! R# Y
- FILE *stream;
8 U- V; u6 f* a3 \7 \ - freopen_s(&stream,"conout$", "w", stdout);
. b. Q0 c( Y) O* t - freopen_s(&stream,"conout$", "w", stderr);- |8 l3 H, }8 m
- consoleInitialized = true;
- z. ?5 Y1 A U7 R' @6 y - }
+ B( `+ y# E# _0 |9 s. d: x: ~1 I+ X! W% v( ^ - #endif
( Q% Z6 S, V/ u6 y - cerr << msg;
& n3 |5 M. X/ p; x6 E- N - }. B) Q$ {' }3 {
复制代码 |
|