0023776: Redesign of MFC samples after V2d viewer removing
[occt.git] / samples / mfc / standard / Common / ImportExport / ImportExport.cpp
CommitLineData
7fd59977 1// ImportExport.cpp: implementation of the CImportExport class.
2//
3//////////////////////////////////////////////////////////////////////
4
5#include "stdafx.h"
6#include "ImportExport.h"
5c1f974e 7#include <OCC_App.h>
7fd59977 8
9#include "SaveCSFDBDlg.h"
10
11#include "SaveSTEPDlg.h"
12
13#include "TColStd_SequenceOfAsciiString.hxx"
14#include "TColStd_SequenceOfExtendedString.hxx"
15#include "OSD_Timer.hxx"
16
17#include "IGESControl_Reader.hxx"
18#include "STEPControl_Controller.hxx"
19
20#include <BRepAlgo.hxx>
21#include <FSD_File.hxx>
22#include <ShapeSchema.hxx>
23#include <PTopoDS_HShape.hxx>
24#include <Storage_HSeqOfRoot.hxx>
25#include <Storage_Root.hxx>
26#include <PTColStd_PersistentTransientMap.hxx>
27#include <MgtBRep.hxx>
28#include <PTColStd_TransientPersistentMap.hxx>
29#include <IGESControl_Controller.hxx>
30#include <IGESControl_Writer.hxx>
31#include <Interface_Static.hxx>
32#include <STEPControl_Reader.hxx>
33#include <Geom_Curve.hxx>
34#include <STEPControl_Writer.hxx>
35#include <TopoDS_Compound.hxx>
36#include <Geom_Line.hxx>
37#include <StlAPI_Writer.hxx>
38#include <VrmlAPI_Writer.hxx>
39#include <VrmlData_Scene.hxx>
40#include <VrmlData_ShapeConvert.hxx>
41#include <VrmlData_Appearance.hxx>
42#include <VrmlData_Material.hxx>
43#include <VrmlData_Group.hxx>
44#include <VrmlData_ListOfNode.hxx>
45#include <VrmlData_ShapeNode.hxx>
46
47#include <XSControl_WorkSession.hxx>
48#include <STEPConstruct_Styles.hxx>
49#include <TColStd_HSequenceOfTransient.hxx>
50#include <STEPConstruct.hxx>
51#include <StepVisual_StyledItem.hxx>
52
53#ifdef _DEBUG
54#undef THIS_FILE
55static char THIS_FILE[]=__FILE__;
56//#define new DEBUG_NEW
57#endif
58
59//////////////////////////////////////////////////////////////////////
60// Construction/Destruction
61//////////////////////////////////////////////////////////////////////
62
63Handle(TopTools_HSequenceOfShape) CImportExport::BuildSequenceFromContext(const Handle(AIS_InteractiveContext)& anInteractiveContext,
64 Handle(Quantity_HArray1OfColor)& anArrayOfColors,
65 Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
66{
67 Handle(TopTools_HSequenceOfShape) aSequence;
68 Standard_Integer nb = anInteractiveContext->NbCurrents(), i = 1;
69 if (!nb)
70 return aSequence;
71
72 aSequence = new TopTools_HSequenceOfShape();
73 anArrayOfColors = new Quantity_HArray1OfColor(1, nb);
74 anArrayOfTransparencies = new TColStd_HArray1OfReal (1, nb);
75
76 Handle(AIS_InteractiveObject) picked;
77 for(anInteractiveContext->InitCurrent();anInteractiveContext->MoreCurrent();anInteractiveContext->NextCurrent())
78 {
79 picked = anInteractiveContext->Current();
80 if (anInteractiveContext->Current()->IsKind(STANDARD_TYPE(AIS_Shape)))
81 {
82 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(picked);
83 TopoDS_Shape aShape = aisShape->Shape();
84 aSequence->Append(aShape);
85
86 Quantity_Color color;
87 aisShape->Color(color);
88 anArrayOfColors->SetValue(i, color);
89
90 Standard_Real transparency = aisShape->Transparency();
91 anArrayOfTransparencies->SetValue(i, transparency);
92
93 i++;
94 }
95 }
96 return aSequence;
97}
98
99//======================================================================
100//= =
101//= BREP =
102//= =
103//======================================================================
104
105int CImportExport::ReadBREP(const Handle_AIS_InteractiveContext& anInteractiveContext,LPCTSTR InitialDir /* = NULL*/)
106{
107 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadBREP(InitialDir);
108 if(aSequence->IsEmpty())
109 return 1;
110 Handle_AIS_Shape aShape;
111 for(int i=1;i<= aSequence->Length();i++){
112 aShape = new AIS_Shape(aSequence->Value(i));
113 anInteractiveContext->SetDisplayMode(aShape, 1, Standard_False);
114 anInteractiveContext->Display(aShape, Standard_False);
115 anInteractiveContext->SetCurrentObject(aShape, Standard_False);
116 }
117 return 0;
118}
119
120Handle(TopTools_HSequenceOfShape) CImportExport::ReadBREP(LPCTSTR InitialDir /* = NULL*/) // not by reference --> the sequence is created here !!
121{
122 CFileDialog dlg(TRUE,
123 NULL,
124 NULL,
125 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
126 "BREP Files (*.brep , *.rle)|*.brep; *.BREP; *.rle; *.RLE; |All Files (*.*)|*.*||",
127 NULL );
128
129TCHAR tchBuf[80];
130
131CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
132CString initdir = (CASROOTValue + "\\..\\data\\occ");
133
134dlg.m_ofn.lpstrInitialDir = initdir;
135
136 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
137
138 if (dlg.DoModal() == IDOK)
139 {
140 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
141 CString filename = dlg.GetPathName();
142 TopoDS_Shape aShape;
143 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
144 Standard_Boolean result = ReadBREP(aFileName,aShape);
145 if (result)
146 {
147 if (!BRepAlgo::IsValid(aShape))
148 MessageBox(AfxGetMainWnd()->m_hWnd,"Warning: The shape is not valid!","Cascade Warning",MB_ICONWARNING);
149 aSequence->Append(aShape);
150 }
151 else
152 MessageBox(AfxGetMainWnd()->m_hWnd,"Error: The file was not read","Cascade Error",MB_ICONERROR);
153
154 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
155 }
156
157 return aSequence;
158}
159//----------------------------------------------------------------------
160
161Standard_Boolean CImportExport::ReadBREP(const Standard_CString& aFileName,
162 TopoDS_Shape& aShape)
163{
164 BRep_Builder aBuilder;
165 Standard_Boolean result = BRepTools::Read(aShape,aFileName,aBuilder);
166 return result;
167}
168
169void CImportExport::SaveBREP(const Handle_AIS_InteractiveContext& anInteractiveContext)
170{
171 anInteractiveContext->InitCurrent();
172 if (anInteractiveContext->NbCurrents() == 0){
173 AfxMessageBox("No shape selected for export!");
174 return;
175 }
176 Handle(TopTools_HSequenceOfShape) aHSequenceOfShape;
177 Handle(Quantity_HArray1OfColor) anArrayOfColors;
178 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
179 aHSequenceOfShape = BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies);
180 int aLength = aHSequenceOfShape->Length();
181 if (aLength == 1){
182 TopoDS_Shape RES = aHSequenceOfShape->Value(1);
183 CImportExport::SaveBREP(RES);
184 } else {
185 TopoDS_Compound RES;
186 BRep_Builder MKCP;
187 MKCP.MakeCompound(RES);
188 for (Standard_Integer i=1;i<=aLength;i++)
189 {
190 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
191 if ( aShape.IsNull() )
192 {
193 continue;
194 }
195 MKCP.Add(RES, aShape);
196 }
197 CImportExport::SaveBREP(RES);
198 }
199}
200
201Standard_Boolean CImportExport::SaveBREP(const TopoDS_Shape& aShape)
202{
203 CFileDialog dlg(FALSE,_T("*.brep"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
204 "BREP Files (*.brep)|*.brep;|BREP Files (*.BREP)|*.BREP;||", NULL );
205
206TCHAR tchBuf[80];
207
208CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
209CString initdir = (CASROOTValue + "\\..\\data\\occ");
210
211dlg.m_ofn.lpstrInitialDir = initdir;
212
213 Standard_Boolean result = Standard_False;
214 if (dlg.DoModal() == IDOK)
215 {
216 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
217 CString filename = dlg.GetPathName();
218 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
219 result = SaveBREP(aFileName,aShape);
220 if (!result)
221 MessageBox(0,"Error : The shape or shapes were not saved.",
222 "CasCade Error",MB_ICONERROR);
223 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
224 }
225 return result;
226}
227
228//----------------------------------------------------------------------------------------
229Standard_Boolean CImportExport::SaveBREP(const Standard_CString& aFileName,
230 const TopoDS_Shape& aShape)
231{
232 Standard_Boolean result = BRepTools::Write(aShape,aFileName);
233 return result;
234}
235
236
237//======================================================================
238//= =
239//= CSFDB =
240//= =
241//======================================================================
242
243
244TCollection_AsciiString CImportExport::BuildStorageErrorMessage( Storage_Error anError)
245
246{
247 TCollection_AsciiString aMessage("Storage Status :");
248 switch ( anError ) {
249 case Storage_VSOk :
250 aMessage += "no problem \n";
251 break;
252 case Storage_VSOpenError :
253 aMessage += "OpenError while opening the stream \n";
254 break;
255 case Storage_VSModeError :
256 aMessage += "the stream is opened with a wrong mode for operation \n";
257 break;
258 case Storage_VSCloseError :
259 aMessage += "CloseError while closing the stream \n";
260 break;
261 case Storage_VSAlreadyOpen :
262 aMessage += "stream is already opened \n";
263 break;
264 case Storage_VSNotOpen :
265 aMessage += "stream not opened \n";
266 break;
267 case Storage_VSSectionNotFound :
268 aMessage += "the section is not found \n";
269 break;
270 case Storage_VSWriteError :
271 aMessage += "error during writing \n";
272 break;
273 case Storage_VSFormatError :
274 aMessage += "wrong format error occured while reading \n";
275 break;
276 case Storage_VSUnknownType :
277 aMessage += "try to read an unknown type \n";
278 break;
279 case Storage_VSTypeMismatch :
280 aMessage += "try to read a wrong primitive type (read a char while expecting a real) \n";
281 break;
282 case Storage_VSInternalError :
283 aMessage += "internal error \n ";
284 break;
285 case Storage_VSExtCharParityError :
286 aMessage += "problem with 16bit characters, may be an 8bit character is inserted inside a 16bit string \n";
287 break;
288 default :
289 aMessage += "Unknown Status ";
290 aMessage += anError;
291 aMessage += " \n";
292 break;
293 }
294 return aMessage;
295}
296
297void CImportExport::ReadCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
298{
299 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadCSFDB();
300 for(int i=1;i<= aSequence->Length();i++)
301 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)), Standard_False);
302}
303
304Handle(TopTools_HSequenceOfShape) CImportExport::ReadCSFDB() // not by reference --> the sequence is created here !!
305{
306 CFileDialog dlg(TRUE,
307 NULL,
308 NULL,
309 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
310 "CSFDB Files (*.CSFDB , *.csf)|*.csfdb; *.csf; |All Files (*.*)|*.*||",
311 NULL );
312
313TCHAR tchBuf[80];
314
315CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
316CString initdir = (CASROOTValue + "\\..\\data\\csfdb");
317
318dlg.m_ofn.lpstrInitialDir = initdir;
319
320 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();;
321 if (dlg.DoModal() == IDOK)
322 {
323 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
324 CString filename = dlg.GetPathName();
325 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
326 TCollection_AsciiString Message;
327 Standard_Boolean result = ReadCSFDB(aFileName,aSequence,Message);
328 if (result)
329 {
330 // Display The Message :
331 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
332
333
334 }
335 else
336 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
337 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
338 }
339 return aSequence;
340}
341
342Standard_Boolean CImportExport::ReadCSFDB(const Standard_CString& aFileName,
343 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape, // out parameter
344 TCollection_AsciiString& ReturnMessage) // out parameter
345{
346 // an I/O driver
347 FSD_File f;
348
349 // the applicative Schema
350 Handle(ShapeSchema) s = new ShapeSchema;
351
352 // a Read/Write data object
353 Handle(Storage_Data) d = new Storage_Data;
354
355 d->ClearErrorStatus();
356
357 // Check file type
358 if ( FSD_File::IsGoodFileType( aFileName ) != Storage_VSOk) {
359 ReturnMessage = "Bad file type for ";
360 ReturnMessage += aFileName;
361 ReturnMessage += " \n";
362 return Standard_False;
363 }
364
365 // Open the archive, Read mode
366 Storage_Error err = f.Open(aFileName, Storage_VSRead);
367 // Read all the persistent object in the file with the schema
368 if ( err != Storage_VSOk ) {
369 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
370 return Standard_False;
371 }
372
373 d = s->Read( f );
374 err = d->ErrorStatus() ;
375
376 if ( err != Storage_VSOk ) {
377 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
378 return Standard_False;
379 }
380 // Close the file driver
381 f.Close();
382
383 ReturnMessage += "Application Name :"; ReturnMessage += d->ApplicationName();ReturnMessage += "\n";
384 ReturnMessage += "Application Version :"; ReturnMessage += d->ApplicationVersion();ReturnMessage += "\n";
385 ReturnMessage += "Data type :"; ReturnMessage += d->DataType();ReturnMessage += "\n";
386 ReturnMessage += "== User Infos : ==\n";
387 const TColStd_SequenceOfAsciiString& UserInfo = d->UserInfo();
388 for (int i=1;i<=UserInfo.Length();i++)
389 {ReturnMessage += UserInfo(i);ReturnMessage += "\n";}
390 ReturnMessage += "== Comments : ==\n";
391 const TColStd_SequenceOfExtendedString& Comments=d->Comments();
392 for ( int i=1;i<=Comments.Length();i++)
393 {ReturnMessage += Comments(i);ReturnMessage += "\n";}
394 ReturnMessage += "----------------\n";
395
396
397 // Read all the root objects
398 // Get the root list
399 Handle(Storage_HSeqOfRoot) roots = d->Roots();
400 Handle(Standard_Persistent) p;
401 Handle(Storage_Root) r;
402 Handle(PTopoDS_HShape) aPShape;
403 for ( int i = 1; i <= roots->Length() ; i++ )
404 {
405 // Get the root
406 r = roots->Value(i);
407
408 // Get the persistent application object from the root
409 p = r->Object();
410
411 // Display information
412 ReturnMessage += "Persistent Object "; ReturnMessage += i; ReturnMessage += "\n";
413 ReturnMessage += "Name :"; ReturnMessage += r->Name(); ReturnMessage += "\n";
414 ReturnMessage += "Type :"; ReturnMessage += r->Type(); ReturnMessage += "\n";
415
416 aPShape = Handle(PTopoDS_HShape)::DownCast(p);
417
418 if ( !aPShape.IsNull() )
419 {
420 // To Be ReWriten to suppress the cout,
421 // and provide a CallBack method for dynamic information.
422 // Get the persistent shape
423 PTColStd_PersistentTransientMap aMap;
424 TopoDS_Shape aTShape;
425 MgtBRep::Translate(aPShape,aMap,aTShape,
426 MgtBRep_WithTriangle);
427 aHSequenceOfShape->Append(aTShape);
428 }
429 else
430 {
431 ReturnMessage += "Error -> Unable to read\n";
432 }
433 }
434 return Standard_True;
435}
436//----------------------------------------------------------------------
437void CImportExport::SaveCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
438{
439 anInteractiveContext->InitCurrent();
440 if (anInteractiveContext->NbCurrents() == 0){
441 AfxMessageBox("No shape selected for export!");
442 return;
443 }
444 Handle(Quantity_HArray1OfColor) anArrayOfColors;
445 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
446 CImportExport::SaveCSFDB(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
447}
448
449Standard_Boolean CImportExport::SaveCSFDB(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
450{
451 Standard_Boolean result = Standard_False;
452 CFileSaveCSFDBDialog aDlg(NULL);
453 aDlg.m_TriangleMode = MgtBRep_WithTriangle;
454 if (aDlg.DoModal() == IDOK) {
455 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
456 CString filename = aDlg.GetPathName();
457 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
458 TCollection_AsciiString Message;
459 MgtBRep_TriangleMode selection = aDlg.m_TriangleMode;
460 Standard_Boolean result = SaveCSFDB(aFileName,aHSequenceOfShape,Message);
461 if (result)
462 {
463 // Display The Message :
464 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
465 }
466 else
467 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
468 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
469 }
470 return result;
471}
472
473Standard_Boolean CImportExport::SaveCSFDB(const Standard_CString& aFileName,
474 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
475 TCollection_AsciiString& ReturnMessage,// out parameter
476 MgtBRep_TriangleMode aTriangleMode /* = MgtBRep_WithTriangle */)
477{
478 Standard_Boolean ReturnValue = Standard_True;
479 if (aHSequenceOfShape->Length() == 0)
480 {
481 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
482 return Standard_False;
483 }
484
485 // an I/O driver
486 FSD_File f;
487
488 // the applicative Schema containing
489 // Pesistent Topology and Geometry
490 Handle(ShapeSchema) s = new ShapeSchema;
491
492 // a Read/Write data object
493 Handle(Storage_Data) d = new Storage_Data;
494
495 d->ClearErrorStatus();
496
497 // To Be ReWriten to suppress the Strings,
498 // and provide a CallBack method for dynamic information.
499
500 d->SetApplicationName (TCollection_ExtendedString("SampleImportExport"));
501 d->SetApplicationVersion("1");
502 d->SetDataType(TCollection_ExtendedString("Shapes"));
503 d->AddToUserInfo("Try to store a Persistent set of Shapes in a flat file");
504 d->AddToComments(TCollection_ExtendedString("application is based on CasCade 2.0"));
505
506 // Open the archive, Write mode
507 Storage_Error err = f.Open(aFileName, Storage_VSWrite);
508
509 if ( err != Storage_VSOk ) {
510 ReturnMessage += BuildStorageErrorMessage(err);
511 return Standard_False;
512 }
513
514 PTColStd_TransientPersistentMap aMap;
515 ReturnMessage += "The Object have be saved in the file ";
516 ReturnMessage += aFileName;
517 ReturnMessage += "\n with the names : ";
518
519 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
520 {
521 TopoDS_Shape aTShape= aHSequenceOfShape->Value(i);
522 TCollection_AsciiString anObjectName("anObjectName_");
523 anObjectName += i;
524 ReturnMessage += anObjectName;
525 ReturnMessage += " \n";
526
527 if ( aTShape.IsNull() )
528 {
529 ReturnMessage += " Error : Invalid shape \n";
530 ReturnValue = Standard_False;
531 continue;
532 }
533
534 //Create the persistent Shape
535
536 Handle(PTopoDS_HShape) aPShape =
537 MgtBRep::Translate(aTShape, aMap, MgtBRep_WithTriangle);
538
539
540 // Add the object in the data structure as root
541 // To Be ReWriten to suppress the cout,
542 // and provide a CallBack method for dynamic information.
543 d->AddRoot(anObjectName, aPShape);
544 }
545
546 // Write the object in the file with the schema
547 s->Write( f, d);
548
549 // Close the driver
550 f.Close();
551
552 if ( d->ErrorStatus() != Storage_VSOk )
553 {
554 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
555 return Standard_False;
556 }
557 return ReturnValue;
558}
559
560
561//======================================================================
562//= =
563//= IGES =
564//= =
565//======================================================================
566
567
568
569void CImportExport::ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
570{
571 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadIGES();
572 for(int i=1;i<= aSequence->Length();i++)
573 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)));
574
575}
576
577Handle(TopTools_HSequenceOfShape) CImportExport::ReadIGES()// not by reference --> the sequence is created here !!
578{
579 CFileDialog dlg(TRUE,
580 NULL,
581 NULL,
582 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
583 "IGES Files (*.iges , *.igs)|*.iges; *.igs|All Files (*.*)|*.*||",
584 NULL );
585
586TCHAR tchBuf[80];
587
588CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
589CString initdir = (CASROOTValue + "\\..\\data\\iges");
590
591dlg.m_ofn.lpstrInitialDir = initdir;
592
593 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();
594 if (dlg.DoModal() == IDOK)
595 {
596 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
597 CString C = dlg.GetPathName();
598 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
599 Standard_Integer status = ReadIGES(aFileName,aSequence);
600 if (status != IFSelect_RetDone)
601 {
602 MessageBox(0,"Error : The file is not read","CasCade Error",MB_ICONERROR);
603 }
604
605 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
606 }
607 return aSequence;
608}
609//----------------------------------------------------------------------
610
611Standard_Integer CImportExport::ReadIGES(const Standard_CString& aFileName,
612 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
613{
614
615 IGESControl_Reader Reader;
616
617 Standard_Integer status = Reader.ReadFile(aFileName);
618
619 if (status != IFSelect_RetDone) return status;
620 Reader.TransferRoots();
621 TopoDS_Shape aShape = Reader.OneShape();
622 aHSequenceOfShape->Append(aShape);
623
624 return status;
625}
626//----------------------------------------------------------------------
627
628void CImportExport::SaveIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
629{
630 anInteractiveContext->InitCurrent();
631 if (anInteractiveContext->NbCurrents() == 0){
632 AfxMessageBox("No shape selected for export!");
633 return;
634 }
635 Handle(Quantity_HArray1OfColor) anArrayOfColors;
636 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
637 CImportExport::SaveIGES(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
638}
639
640Standard_Boolean CImportExport::SaveIGES(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
641{
642 if (aHSequenceOfShape->Length() == 0)
643 {
644 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
645 return Standard_False;
646 }
647
648 CFileDialog dlg(FALSE,_T("*.iges"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
649 "IGES Files (*.iges )|*.iges;|IGES Files (*.igs )| *.igs;||", NULL );
650
651TCHAR tchBuf[80];
652
653CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
654CString initdir = (CASROOTValue + "\\..\\data\\iges");
655
656dlg.m_ofn.lpstrInitialDir = initdir;
657
658 Standard_Boolean result=Standard_False;
659 if (dlg.DoModal() == IDOK)
660 {
661 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
662 CString C = dlg.GetPathName();
663 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
664
665
666 result = SaveIGES(aFileName,aHSequenceOfShape);
667 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
668 }
669 return result;
670}
671//----------------------------------------------------------------------
672
673Standard_Boolean CImportExport::SaveIGES(const Standard_CString& aFileName,
674 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
675{
676
677 IGESControl_Controller::Init();
678 IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
679 Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
680
681 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
682 ICW.AddShape (aHSequenceOfShape->Value(i));
683
684 ICW.ComputeModel();
685 Standard_Boolean result = ICW.Write(aFileName );
686 return result;
687}
688
689//======================================================================
690
691//======================================================================
692//= =
693//= STEP =
694//= =
695//======================================================================
696
697void CImportExport::ReadSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
698{
699 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadSTEP();
700 if (!aSequence.IsNull()) {
701 for(int i=1;i<= aSequence->Length();i++)
702 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)), Standard_False);
703 }
704}
705
706Handle(TopTools_HSequenceOfShape) CImportExport::ReadSTEP()// not by reference --> the sequence is created here !!
707{
708 CFileDialog dlg(TRUE,
709 NULL,
710 NULL,
711 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
712 "STEP Files (*.stp;*.step)|*.stp; *.step|All Files (*.*)|*.*||",
713 NULL );
714
715TCHAR tchBuf[80];
716
717CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
718CString initdir = (CASROOTValue + "\\..\\data\\step");
719
720dlg.m_ofn.lpstrInitialDir = initdir;
721
722 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
723 if (dlg.DoModal() == IDOK)
724 {
725 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
726 CString C = dlg.GetPathName();
727 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
728 IFSelect_ReturnStatus ReturnStatus = ReadSTEP(aFileName,aSequence);
729 switch (ReturnStatus)
730 {
731 case IFSelect_RetError :
732 MessageBox(0,"Not a valid Step file","ERROR",MB_ICONWARNING);
733 break;
734 case IFSelect_RetFail :
735 MessageBox(0,"Reading has failed","ERROR",MB_ICONWARNING);
736 break;
737 case IFSelect_RetVoid :
738 MessageBox(0,"Nothing to transfer","ERROR",MB_ICONWARNING);
739 break;
740 }
741 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
742 }
743 return aSequence;
744}
745
746IFSelect_ReturnStatus CImportExport::ReadSTEP(const Standard_CString& aFileName,
747 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
748{
749 aHSequenceOfShape->Clear();
750
751 // create additional log file
752 STEPControl_Reader aReader;
753 IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);
754 if (status != IFSelect_RetDone)
755 return status;
756
757 aReader.WS()->MapReader()->SetTraceLevel(2); // increase default trace level
758
759 Standard_Boolean failsonly = Standard_False;
760 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
761
762 // Root transfers
763 Standard_Integer nbr = aReader.NbRootsForTransfer();
764 aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
765 for ( Standard_Integer n = 1; n<=nbr; n++) {
766 Standard_Boolean ok = aReader.TransferRoot(n);
767 }
768
769 // Collecting resulting entities
770 Standard_Integer nbs = aReader.NbShapes();
771 if (nbs == 0) {
772 return IFSelect_RetVoid;
773 }
774 for (Standard_Integer i=1; i<=nbs; i++) {
775 aHSequenceOfShape->Append(aReader.Shape(i));
776 }
777
778 return status;
779}
780
781
782//----------------------------------------------------------------------
783void CImportExport::SaveSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
784{
785 anInteractiveContext->InitCurrent();
786 if (anInteractiveContext->NbCurrents() == 0){
787 AfxMessageBox("No shape selected for export!");
788 return;
789 }
790 Handle(Quantity_HArray1OfColor) anArrayOfColors;
791 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
792 CImportExport::SaveSTEP(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
793}
794
795// Return True if no error
796Standard_Boolean TestFacetedBrep(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
797{
798 Standard_Boolean OneErrorFound = Standard_False;
799 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
800 {
801 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
802
803 TopExp_Explorer Ex(aShape,TopAbs_FACE);
804 while (Ex.More() && !OneErrorFound)
805 {
806 // Get the Geom_Surface outside the TopoDS_Face
807 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(Ex.Current()));
808 // check if it is a plane.
809 if (!aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
810 OneErrorFound=Standard_True;
811 Ex.Next();
812 }
813 TopExp_Explorer Ex2(aShape,TopAbs_EDGE);
814 while (Ex2.More() && !OneErrorFound)
815 {
816 // Get the Geom_Curve outside the TopoDS_Face
817 Standard_Real FirstDummy,LastDummy;
818 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(Ex2.Current()),FirstDummy,LastDummy);
819 // check if it is a line.
820 if (!aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
821 OneErrorFound=Standard_True;
822 Ex2.Next();
823 }
824 }
825 return !OneErrorFound;
826}
827
828IFSelect_ReturnStatus CImportExport::SaveSTEP(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
829{
830 if (aHSequenceOfShape->Length() == 0)
831 {
832 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
833 return IFSelect_RetError;
834 }
835
836 IFSelect_ReturnStatus status;
837
838 CFileSaveSTEPDialog aDlg(NULL);
839
840 aDlg.m_Cc1ModelType = STEPControl_ManifoldSolidBrep;
841
842 if (aDlg.DoModal() == IDOK) {
843 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
844 CString C = aDlg.GetPathName();
845 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
846
847 STEPControl_StepModelType selection = aDlg.m_Cc1ModelType;
848
849 if(selection == STEPControl_FacetedBrep)
850
851 if (!TestFacetedBrep(aHSequenceOfShape))
852 {
853 MessageBox(0,"At least one shape doesn't contain facetes","CasCade Warning",MB_ICONWARNING);
854 return IFSelect_RetError;
855 }
856
857
858 status = SaveSTEP(aFileName,aHSequenceOfShape,selection);
859 switch (status)
860 {
861 case IFSelect_RetError:
862 MessageBox(0,"Incorrect Data","ERROR",MB_ICONWARNING);
863 break;
864 case IFSelect_RetFail:
865 MessageBox(0,"Writing has failed","ERROR",MB_ICONWARNING);
866 break;
867 case IFSelect_RetVoid:
868 MessageBox(0,"Nothing to transfer","ERROR",MB_ICONWARNING);
869 break;
870 }
871 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
872 }
873 return status;
874}
875//----------------------------------------------------------------------------------------
876IFSelect_ReturnStatus CImportExport::SaveSTEP(const Standard_CString& aFileName,
877 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
878
879const STEPControl_StepModelType aValue /* =TopoDSToCc1Act_ManifoldSolidBrep */ )
880
881{
882 // CREATE THE WRITER
883
884 STEPControl_Writer aWriter;
885
886 IFSelect_ReturnStatus status;
887 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
888 {
889 status = aWriter.Transfer(aHSequenceOfShape->Value(i), aValue);
890 if ( status != IFSelect_RetDone ) return status;
891 }
892 status = aWriter.Write(aFileName);
893 return status;
894}
895
896
897
898//======================================================================
899//= =
900//= STL =
901//= =
902//======================================================================
903
904void CImportExport::SaveSTL(const Handle(AIS_InteractiveContext)& anInteractiveContext)
905{
906 anInteractiveContext->InitCurrent();
907 if (anInteractiveContext->NbCurrents() == 0){
908 AfxMessageBox("No shape selected for export!");
909 return;
910 }
911 Handle(Quantity_HArray1OfColor) anArrayOfColors;
912 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
913 CImportExport::SaveSTL(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
914}
915
916Standard_Boolean CImportExport::SaveSTL(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
917{
918 CFileDialog dlg(FALSE,_T("*.stl"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
919 "stl Files (*.stl)|*.stl;|STL Files (*.STL)|*.STL;||", NULL );
920
921TCHAR tchBuf[80];
922
923CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
924CString initdir = (CASROOTValue + "\\..\\data\\stl");
925
926dlg.m_ofn.lpstrInitialDir = initdir;
927
928 Standard_Boolean result;
929
930 if (dlg.DoModal() == IDOK) {
931 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
932 CString C = dlg.GetPathName();
933 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
934 TCollection_AsciiString Message;
935 result = SaveSTL(aFileName,aHSequenceOfShape,Message);
936 if (result)
937 {
938 // Display The Message :
939 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
940 }
941 else
942 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
943 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
944 }
945 return result;
946}
947
948Standard_Boolean CImportExport::SaveSTL(const Standard_CString& aFileName,
949 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
950 TCollection_AsciiString& ReturnMessage)
951{
952 Standard_Boolean ReturnValue = Standard_True;
953 if (aHSequenceOfShape->Length() == 0)
954 {
955 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
956 return Standard_False;
957 }
958
959 PTColStd_TransientPersistentMap aMap;
960 ReturnMessage += "The Object have be saved in the file ";
961 ReturnMessage += aFileName;
962 ReturnMessage += "\n with the names : ";
963
964 TopoDS_Compound RES;
965 BRep_Builder MKCP;
966 MKCP.MakeCompound(RES);
967
968 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
969 {
970 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
971 TCollection_AsciiString anObjectName("anObjectName_");
972 anObjectName += i;
973 ReturnMessage += anObjectName;
974 ReturnMessage += " \n";
975
976 if ( aShape.IsNull() )
977 {
978 ReturnMessage += " Error : Invalid shape \n";
979 ReturnValue = Standard_False;
980 continue;
981 }
982
983 MKCP.Add(RES, aShape);
984 }
985
986 StlAPI_Writer myStlWriter;
987 myStlWriter.Write(RES, aFileName);
988
989 return ReturnValue;
990}
991
992
993//======================================================================
994//= =
995//= VRML =
996//= =
997//======================================================================
998
999void CImportExport::SaveVRML(const Handle(AIS_InteractiveContext)& anInteractiveContext)
1000{
1001 anInteractiveContext->InitCurrent();
1002 if (anInteractiveContext->NbCurrents() == 0){
1003 AfxMessageBox("No shape selected for export!");
1004 return;
1005 }
1006 Handle(Quantity_HArray1OfColor) anArrayOfColors;
1007 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
1008 CImportExport::SaveVRML(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies),
1009 anArrayOfColors, anArrayOfTransparencies);
1010}
1011
1012Standard_Boolean CImportExport::SaveVRML(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
1013 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
1014 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
1015{
1016 CFileDialog dlg(FALSE,_T("*.vrml"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
1017 "vrml Files (*.vrml)|*.vrml;|vrm Files (*.vrm)|*.vrm;||", NULL );
1018
1019TCHAR tchBuf[80];
1020
1021CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
1022CString initdir = (CASROOTValue + "\\..\\data\\vrml");
1023
1024dlg.m_ofn.lpstrInitialDir = initdir;
1025
1026 Standard_Boolean result;
1027
1028 if (dlg.DoModal() == IDOK) {
1029 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1030 CString C = dlg.GetPathName();
1031 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
1032 TCollection_AsciiString Message;
1033 result = SaveVRML(aFileName,aHSequenceOfShape,anArrayOfColors, anArrayOfTransparencies, Message);
1034 if (result)
1035 {
1036 // Display The Message :
1037 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
1038 }
1039 else
1040 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
1041 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1042 }
1043 return result;
1044}
1045
1046Standard_Boolean CImportExport::SaveVRML(const Standard_CString& aFileName,
1047 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
1048 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
1049 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies,
1050 TCollection_AsciiString& ReturnMessage)
1051{
1052 Standard_Boolean ReturnValue = Standard_True;
1053 if (aHSequenceOfShape->Length() == 0)
1054 {
1055 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
1056 return Standard_False;
1057 }
1058
1059 PTColStd_TransientPersistentMap aMap;
1060 ReturnMessage += "The Object has been saved in the file ";
1061 ReturnMessage += aFileName;
1062 ReturnMessage += "\n with the names : ";
1063
1064 // VRML scene.
1065 VrmlData_Scene scene;
1066 VrmlData_ShapeConvert converter(scene/*, 0.001*/); // from mm to meters
1067 Standard_Integer iShape = 1; // Counter of shapes
1068
1069 for ( int i = 1; i <= aHSequenceOfShape->Length(); i++ )
1070 {
1071 // Shape
1072 TopoDS_Shape shape = aHSequenceOfShape->Value( i );
1073 if ( shape.IsNull() )
1074 {
1075 ReturnMessage += " Error : Invalid shape \n";
1076 ReturnValue = Standard_False;
1077 continue;
1078 }
1079
1080 // Color
1081 Quantity_Color color; // yellow
1082 if (!anArrayOfColors.IsNull())
1083 color = anArrayOfColors->Value(i);
1084
1085 // Transparency
1086 Standard_Real transparency = 0.0;
1087 if (!anArrayOfTransparencies.IsNull())
1088 transparency = anArrayOfTransparencies->Value(i);
1089
1090 // Give a name to the shape.
1091 TCollection_AsciiString name("Shape");
1092 name += TCollection_AsciiString(iShape++);
1093 converter.AddShape(shape, name.ToCString());
1094 ReturnMessage += name;
1095 ReturnMessage += '\n';
1096
1097 // Check presence of faces in the shape.
1098 TopExp_Explorer expl(shape, TopAbs_FACE);
1099 if (expl.More())
1100 converter.Convert(true, false, 0.01); // faces only
1101 else
1102 converter.Convert(false, true, 0.01); // edges only
1103
1104 // Name of the color & transparency.
1105 // It will be uniquely saved in VRML file.
1106 TCollection_AsciiString cname = Quantity_Color::StringName(color.Name());
1107 cname += transparency;
1108
1109 // Make the appearance (VRML attribute)
1110 Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(scene.FindNode(cname.ToCString()));
1111 if (appearance.IsNull())
1112 {
1113 // Not found ... create a new one.
1114 Handle(VrmlData_Material) material = new VrmlData_Material(scene, cname.ToCString(), 0.2, 0.2, transparency);
1115 material->SetDiffuseColor(color);
1116 material->SetEmissiveColor(color);
1117 material->SetSpecularColor(color);
1118 scene.AddNode(material, false);
1119 appearance = new VrmlData_Appearance(scene, cname.ToCString());
1120 appearance->SetMaterial(material);
1121 scene.AddNode(appearance, false);
1122 }
1123
1124 // Apply the material to the shape of entity.
1125 Handle(VrmlData_Group) group = Handle(VrmlData_Group)::DownCast(scene.FindNode(name.ToCString()));
1126 if (!group.IsNull())
1127 {
1128 VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
1129 for (; itr.More(); itr.Next())
1130 {
1131 Handle(VrmlData_Node) node = itr.Value();
1132 if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1133 {
1134 Handle(VrmlData_ShapeNode) shape = Handle(VrmlData_ShapeNode)::DownCast(node);
1135 shape->SetAppearance(appearance);
1136 }
1137 else if (itr.Value()->DynamicType() == STANDARD_TYPE(VrmlData_Group))
1138 {
1139 Handle(VrmlData_Group) groupc = Handle(VrmlData_Group)::DownCast(itr.Value());
1140 VrmlData_ListOfNode::Iterator itrc = groupc->NodeIterator();
1141 for (; itrc.More(); itrc.Next())
1142 {
1143 Handle(VrmlData_Node) nodec = itrc.Value();
1144 if (nodec->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1145 {
1146 Handle(VrmlData_ShapeNode) shapec = Handle(VrmlData_ShapeNode)::DownCast(nodec);
1147 shapec->SetAppearance(appearance);
1148 }
1149 } // for of group nodes...
1150 } // if (it is a shape node...
1151 } // for of group nodes...
1152 } // if (!group.IsNull...
1153 } // iterator of shapes
1154
1155 // Call VRML writer
1156 ofstream writer(aFileName);
1157 writer<<scene;
1158 writer.close();
1159
1160 /* Old approach to store shapes in VRML (without color & transparency).
1161 TopoDS_Compound RES;
1162 BRep_Builder MKCP;
1163 MKCP.MakeCompound(RES);
1164
1165 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
1166 {
1167 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
1168 TCollection_AsciiString anObjectName("anObjectName_");
1169 anObjectName += i;
1170 ReturnMessage += anObjectName;
1171 ReturnMessage += " \n";
1172
1173 if ( aShape.IsNull() )
1174 {
1175 ReturnMessage += " Error : Invalid shape \n";
1176 ReturnValue = Standard_False;
1177 continue;
1178 }
1179
1180 MKCP.Add(RES, aShape);
1181 }
1182
1183 VrmlAPI_Writer myVrmlWriter;
1184 myVrmlWriter.Write(RES, aFileName);
1185 */
1186
1187 return ReturnValue;
1188}
1189
1190
1191
1192