1 // ImportExport.cpp: implementation of the CImportExport class.
3 //////////////////////////////////////////////////////////////////////
6 #include "ImportExport.h"
9 #include "SaveCSFDBDlg.h"
11 #include "SaveSTEPDlg.h"
13 #include "TColStd_SequenceOfAsciiString.hxx"
14 #include "TColStd_SequenceOfExtendedString.hxx"
15 #include "OSD_Timer.hxx"
17 #include "IGESControl_Reader.hxx"
18 #include "STEPControl_Controller.hxx"
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>
47 #include <XSControl_WorkSession.hxx>
48 #include <STEPConstruct_Styles.hxx>
49 #include <TColStd_HSequenceOfTransient.hxx>
50 #include <STEPConstruct.hxx>
51 #include <StepVisual_StyledItem.hxx>
55 static char THIS_FILE[]=__FILE__;
56 //#define new DEBUG_NEW
59 //////////////////////////////////////////////////////////////////////
60 // Construction/Destruction
61 //////////////////////////////////////////////////////////////////////
63 Handle(TopTools_HSequenceOfShape) CImportExport::BuildSequenceFromContext(const Handle(AIS_InteractiveContext)& anInteractiveContext,
64 Handle(Quantity_HArray1OfColor)& anArrayOfColors,
65 Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
67 Handle(TopTools_HSequenceOfShape) aSequence;
68 Standard_Integer nb = anInteractiveContext->NbCurrents(), i = 1;
72 aSequence = new TopTools_HSequenceOfShape();
73 anArrayOfColors = new Quantity_HArray1OfColor(1, nb);
74 anArrayOfTransparencies = new TColStd_HArray1OfReal (1, nb);
76 Handle(AIS_InteractiveObject) picked;
77 for(anInteractiveContext->InitCurrent();anInteractiveContext->MoreCurrent();anInteractiveContext->NextCurrent())
79 picked = anInteractiveContext->Current();
80 if (anInteractiveContext->Current()->IsKind(STANDARD_TYPE(AIS_Shape)))
82 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(picked);
83 TopoDS_Shape aShape = aisShape->Shape();
84 aSequence->Append(aShape);
87 aisShape->Color(color);
88 anArrayOfColors->SetValue(i, color);
90 Standard_Real transparency = aisShape->Transparency();
91 anArrayOfTransparencies->SetValue(i, transparency);
99 //======================================================================
103 //======================================================================
105 int CImportExport::ReadBREP (const Handle_AIS_InteractiveContext& anInteractiveContext)
107 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadBREP();
108 if(aSequence->IsEmpty())
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);
120 Handle(TopTools_HSequenceOfShape) CImportExport::ReadBREP()
122 CFileDialog dlg(TRUE,
125 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
126 L"BREP Files (*.brep , *.rle)|*.brep; *.BREP; *.rle; *.RLE; |All Files (*.*)|*.*||",
129 CString CASROOTValue;
130 CASROOTValue.GetEnvironmentVariable (L"CASROOT");
131 CString initdir = (CASROOTValue + "\\..\\data\\occ");
133 dlg.m_ofn.lpstrInitialDir = initdir;
135 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
137 if (dlg.DoModal() == IDOK)
139 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
140 CString filename = dlg.GetPathName();
142 Standard_Boolean result = ReadBREP (filename, aShape);
145 if (!BRepAlgo::IsValid(aShape))
146 MessageBoxW (AfxGetMainWnd()->m_hWnd, L"Warning: The shape is not valid!", L"Cascade Warning", MB_ICONWARNING);
148 aSequence->Append(aShape);
151 MessageBoxW (AfxGetMainWnd()->m_hWnd, L"Error: The file was not read", L"Cascade Error", MB_ICONERROR);
153 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
158 //----------------------------------------------------------------------
160 Standard_Boolean CImportExport::ReadBREP(CString aFileName,
161 TopoDS_Shape& aShape)
165 std::filebuf aFileBuf;
166 std::istream aStream (&aFileBuf);
167 if (!aFileBuf.open (aFileName, ios::in))
169 return Standard_False;
172 BRep_Builder aBuilder;
173 BRepTools::Read (aShape, aStream, aBuilder);
174 return !aShape.IsNull();
177 void CImportExport::SaveBREP(const Handle_AIS_InteractiveContext& anInteractiveContext)
179 anInteractiveContext->InitCurrent();
180 if (anInteractiveContext->NbCurrents() == 0){
181 AfxMessageBox (L"No shape selected for export!");
184 Handle(TopTools_HSequenceOfShape) aHSequenceOfShape;
185 Handle(Quantity_HArray1OfColor) anArrayOfColors;
186 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
187 aHSequenceOfShape = BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies);
188 int aLength = aHSequenceOfShape->Length();
190 TopoDS_Shape RES = aHSequenceOfShape->Value(1);
191 CImportExport::SaveBREP(RES);
195 MKCP.MakeCompound(RES);
196 for (Standard_Integer i=1;i<=aLength;i++)
198 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
199 if ( aShape.IsNull() )
203 MKCP.Add(RES, aShape);
205 CImportExport::SaveBREP(RES);
209 Standard_Boolean CImportExport::SaveBREP(const TopoDS_Shape& aShape)
211 CFileDialog dlg (FALSE, L"*.brep",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
212 L"BREP Files (*.brep)|*.brep;|BREP Files (*.BREP)|*.BREP;||", NULL);
214 CString CASROOTValue;
215 CASROOTValue.GetEnvironmentVariable (L"CASROOT");
216 CString initdir = (CASROOTValue + "\\..\\data\\occ");
218 dlg.m_ofn.lpstrInitialDir = initdir;
220 Standard_Boolean result = Standard_False;
221 if (dlg.DoModal() == IDOK)
223 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
224 CString filename = dlg.GetPathName();
225 result = SaveBREP (filename, aShape);
227 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd,
228 L"Error : The shape or shapes were not saved.",
229 L"CasCade Error", MB_ICONERROR);
230 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
235 //----------------------------------------------------------------------------------------
236 Standard_Boolean CImportExport::SaveBREP (CString aFileName,
237 const TopoDS_Shape& aShape)
239 std::filebuf aFileBuf;
240 std::ostream aStream (&aFileBuf);
241 if (!aFileBuf.open (aFileName, ios::out))
243 return Standard_False;
246 BRepTools::Write (aShape, aStream);
247 return Standard_True;
251 //======================================================================
255 //======================================================================
258 TCollection_AsciiString CImportExport::BuildStorageErrorMessage( Storage_Error anError)
261 TCollection_AsciiString aMessage("Storage Status :");
264 aMessage += "no problem \n";
266 case Storage_VSOpenError :
267 aMessage += "OpenError while opening the stream \n";
269 case Storage_VSModeError :
270 aMessage += "the stream is opened with a wrong mode for operation \n";
272 case Storage_VSCloseError :
273 aMessage += "CloseError while closing the stream \n";
275 case Storage_VSAlreadyOpen :
276 aMessage += "stream is already opened \n";
278 case Storage_VSNotOpen :
279 aMessage += "stream not opened \n";
281 case Storage_VSSectionNotFound :
282 aMessage += "the section is not found \n";
284 case Storage_VSWriteError :
285 aMessage += "error during writing \n";
287 case Storage_VSFormatError :
288 aMessage += "wrong format error occured while reading \n";
290 case Storage_VSUnknownType :
291 aMessage += "try to read an unknown type \n";
293 case Storage_VSTypeMismatch :
294 aMessage += "try to read a wrong primitive type (read a char while expecting a real) \n";
296 case Storage_VSInternalError :
297 aMessage += "internal error \n ";
299 case Storage_VSExtCharParityError :
300 aMessage += "problem with 16bit characters, may be an 8bit character is inserted inside a 16bit string \n";
303 aMessage += "Unknown Status ";
311 void CImportExport::ReadCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
313 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadCSFDB();
314 for(int i=1;i<= aSequence->Length();i++)
315 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)), Standard_False);
318 Handle(TopTools_HSequenceOfShape) CImportExport::ReadCSFDB() // not by reference --> the sequence is created here !!
320 CFileDialog dlg(TRUE,
323 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
324 L"CSFDB Files (*.CSFDB , *.csf)|*.csfdb; *.csf; |All Files (*.*)|*.*||",
327 CString CASROOTValue;
328 CASROOTValue.GetEnvironmentVariable(L"CASROOT");
329 CString initdir = (CASROOTValue + "\\..\\data\\csfdb");
331 dlg.m_ofn.lpstrInitialDir = initdir;
333 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();;
334 if (dlg.DoModal() == IDOK)
336 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
337 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
338 TCollection_AsciiString aFileName (aFileNameW, '?');
339 TCollection_AsciiString Message;
340 Standard_Boolean result = ReadCSFDB (aFileName.ToCString(), aSequence, Message);
341 CString aMsg (Message.ToCString());
342 MessageBoxW(AfxGetApp()->m_pMainWnd->m_hWnd, aMsg, result ? L"CasCade" : L"CasCade Error", result ? MB_OK : MB_ICONERROR);
343 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
348 Standard_Boolean CImportExport::ReadCSFDB(const Standard_CString& aFileName,
349 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape, // out parameter
350 TCollection_AsciiString& ReturnMessage) // out parameter
355 // the applicative Schema
356 Handle(ShapeSchema) s = new ShapeSchema;
358 // a Read/Write data object
359 Handle(Storage_Data) d = new Storage_Data;
361 d->ClearErrorStatus();
364 if ( FSD_File::IsGoodFileType( aFileName ) != Storage_VSOk) {
365 ReturnMessage = "Bad file type for ";
366 ReturnMessage += aFileName;
367 ReturnMessage += " \n";
368 return Standard_False;
371 // Open the archive, Read mode
372 Storage_Error err = f.Open(aFileName, Storage_VSRead);
373 // Read all the persistent object in the file with the schema
374 if ( err != Storage_VSOk ) {
375 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
376 return Standard_False;
380 err = d->ErrorStatus() ;
382 if ( err != Storage_VSOk ) {
383 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
384 return Standard_False;
386 // Close the file driver
389 ReturnMessage += "Application Name :"; ReturnMessage += d->ApplicationName();ReturnMessage += "\n";
390 ReturnMessage += "Application Version :"; ReturnMessage += d->ApplicationVersion();ReturnMessage += "\n";
391 ReturnMessage += "Data type :"; ReturnMessage += d->DataType();ReturnMessage += "\n";
392 ReturnMessage += "== User Infos : ==\n";
393 const TColStd_SequenceOfAsciiString& UserInfo = d->UserInfo();
394 for (int i=1;i<=UserInfo.Length();i++)
395 {ReturnMessage += UserInfo(i);ReturnMessage += "\n";}
396 ReturnMessage += "== Comments : ==\n";
397 const TColStd_SequenceOfExtendedString& Comments=d->Comments();
398 for ( int i=1;i<=Comments.Length();i++)
399 {ReturnMessage += Comments(i);ReturnMessage += "\n";}
400 ReturnMessage += "----------------\n";
403 // Read all the root objects
405 Handle(Storage_HSeqOfRoot) roots = d->Roots();
406 Handle(Standard_Persistent) p;
407 Handle(Storage_Root) r;
408 Handle(PTopoDS_HShape) aPShape;
409 for ( int i = 1; i <= roots->Length() ; i++ )
414 // Get the persistent application object from the root
417 // Display information
418 ReturnMessage += "Persistent Object "; ReturnMessage += i; ReturnMessage += "\n";
419 ReturnMessage += "Name :"; ReturnMessage += r->Name(); ReturnMessage += "\n";
420 ReturnMessage += "Type :"; ReturnMessage += r->Type(); ReturnMessage += "\n";
422 aPShape = Handle(PTopoDS_HShape)::DownCast(p);
424 if ( !aPShape.IsNull() )
426 // To Be ReWriten to suppress the cout,
427 // and provide a CallBack method for dynamic information.
428 // Get the persistent shape
429 PTColStd_PersistentTransientMap aMap;
430 TopoDS_Shape aTShape;
431 MgtBRep::Translate(aPShape,aMap,aTShape,
432 MgtBRep_WithTriangle);
433 aHSequenceOfShape->Append(aTShape);
437 ReturnMessage += "Error -> Unable to read\n";
440 return Standard_True;
442 //----------------------------------------------------------------------
443 void CImportExport::SaveCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
445 anInteractiveContext->InitCurrent();
446 if (anInteractiveContext->NbCurrents() == 0){
447 AfxMessageBox (L"No shape selected for export!");
450 Handle(Quantity_HArray1OfColor) anArrayOfColors;
451 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
452 CImportExport::SaveCSFDB(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
455 Standard_Boolean CImportExport::SaveCSFDB(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
457 Standard_Boolean result = Standard_False;
458 CFileSaveCSFDBDialog aDlg(NULL);
459 aDlg.m_TriangleMode = MgtBRep_WithTriangle;
460 if (aDlg.DoModal() == IDOK)
462 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
463 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )aDlg.GetPathName());
464 TCollection_AsciiString aFileName (aFileNameW, '?');
465 TCollection_AsciiString Message;
466 //MgtBRep_TriangleMode selection = aDlg.m_TriangleMode;
467 Standard_Boolean result = SaveCSFDB (aFileName.ToCString(), aHSequenceOfShape, Message);
468 CString aMsg (Message.ToCString());
469 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, aMsg, result ? L"CasCade" : L"CasCade Error", result ? MB_OK : MB_ICONERROR);
470 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
475 Standard_Boolean CImportExport::SaveCSFDB(const Standard_CString& aFileName,
476 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
477 TCollection_AsciiString& ReturnMessage,// out parameter
478 MgtBRep_TriangleMode /*aTriangleMode*/ /* = MgtBRep_WithTriangle */)
480 Standard_Boolean ReturnValue = Standard_True;
481 if (aHSequenceOfShape->Length() == 0)
483 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
484 return Standard_False;
490 // the applicative Schema containing
491 // Pesistent Topology and Geometry
492 Handle(ShapeSchema) s = new ShapeSchema;
494 // a Read/Write data object
495 Handle(Storage_Data) d = new Storage_Data;
497 d->ClearErrorStatus();
499 // To Be ReWriten to suppress the Strings,
500 // and provide a CallBack method for dynamic information.
502 d->SetApplicationName (TCollection_ExtendedString("SampleImportExport"));
503 d->SetApplicationVersion("1");
504 d->SetDataType(TCollection_ExtendedString("Shapes"));
505 d->AddToUserInfo("Try to store a Persistent set of Shapes in a flat file");
506 d->AddToComments(TCollection_ExtendedString("application is based on CasCade 2.0"));
508 // Open the archive, Write mode
509 Storage_Error err = f.Open(aFileName, Storage_VSWrite);
511 if ( err != Storage_VSOk ) {
512 ReturnMessage += BuildStorageErrorMessage(err);
513 return Standard_False;
516 PTColStd_TransientPersistentMap aMap;
517 ReturnMessage += "The Object have be saved in the file ";
518 ReturnMessage += aFileName;
519 ReturnMessage += "\n with the names : ";
521 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
523 TopoDS_Shape aTShape= aHSequenceOfShape->Value(i);
524 TCollection_AsciiString anObjectName("anObjectName_");
526 ReturnMessage += anObjectName;
527 ReturnMessage += " \n";
529 if ( aTShape.IsNull() )
531 ReturnMessage += " Error : Invalid shape \n";
532 ReturnValue = Standard_False;
536 //Create the persistent Shape
538 Handle(PTopoDS_HShape) aPShape =
539 MgtBRep::Translate(aTShape, aMap, MgtBRep_WithTriangle);
542 // Add the object in the data structure as root
543 // To Be ReWriten to suppress the cout,
544 // and provide a CallBack method for dynamic information.
545 d->AddRoot(anObjectName, aPShape);
548 // Write the object in the file with the schema
554 if ( d->ErrorStatus() != Storage_VSOk )
556 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
557 return Standard_False;
563 //======================================================================
567 //======================================================================
571 void CImportExport::ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
573 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadIGES();
574 for(int i=1;i<= aSequence->Length();i++)
575 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)));
579 Handle(TopTools_HSequenceOfShape) CImportExport::ReadIGES()// not by reference --> the sequence is created here !!
581 CFileDialog dlg(TRUE,
584 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
585 L"IGES Files (*.iges , *.igs)|*.iges; *.igs|All Files (*.*)|*.*||",
588 CString CASROOTValue;
589 CASROOTValue.GetEnvironmentVariable (L"CASROOT");
590 CString initdir = (CASROOTValue + "\\..\\data\\iges");
592 dlg.m_ofn.lpstrInitialDir = initdir;
594 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();
595 if (dlg.DoModal() == IDOK)
597 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
598 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
599 TCollection_AsciiString aFileName (aFileNameW, '?');
600 Standard_Integer status = ReadIGES (aFileName.ToCString(), aSequence);
601 if (status != IFSelect_RetDone)
603 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : The file is not read", L"CasCade Error", MB_ICONERROR);
606 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
610 //----------------------------------------------------------------------
612 Standard_Integer CImportExport::ReadIGES(const Standard_CString& aFileName,
613 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
616 IGESControl_Reader Reader;
618 Standard_Integer status = Reader.ReadFile(aFileName);
620 if (status != IFSelect_RetDone) return status;
621 Reader.TransferRoots();
622 TopoDS_Shape aShape = Reader.OneShape();
623 aHSequenceOfShape->Append(aShape);
627 //----------------------------------------------------------------------
629 void CImportExport::SaveIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
631 anInteractiveContext->InitCurrent();
632 if (anInteractiveContext->NbCurrents() == 0){
633 AfxMessageBox (L"No shape selected for export!");
636 Handle(Quantity_HArray1OfColor) anArrayOfColors;
637 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
638 CImportExport::SaveIGES(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
641 Standard_Boolean CImportExport::SaveIGES(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
643 if (aHSequenceOfShape->Length() == 0)
645 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
646 return Standard_False;
649 CFileDialog dlg(FALSE, L"*.iges",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
650 L"IGES Files (*.iges )|*.iges;|IGES Files (*.igs )| *.igs;||", NULL);
652 CString CASROOTValue;
653 CASROOTValue.GetEnvironmentVariable (L"CASROOT");
654 CString initdir = (CASROOTValue + "\\..\\data\\iges");
656 dlg.m_ofn.lpstrInitialDir = initdir;
658 Standard_Boolean result=Standard_False;
659 if (dlg.DoModal() == IDOK)
661 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
663 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
664 TCollection_AsciiString aFileName (aFileNameW, '?');
666 result = SaveIGES (aFileName.ToCString(), aHSequenceOfShape);
667 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
671 //----------------------------------------------------------------------
673 Standard_Boolean CImportExport::SaveIGES(const Standard_CString& aFileName,
674 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
677 IGESControl_Controller::Init();
678 IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
679 Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
681 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
682 ICW.AddShape (aHSequenceOfShape->Value(i));
685 Standard_Boolean result = ICW.Write(aFileName );
689 //======================================================================
691 //======================================================================
695 //======================================================================
697 void CImportExport::ReadSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
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);
706 Handle(TopTools_HSequenceOfShape) CImportExport::ReadSTEP()// not by reference --> the sequence is created here !!
708 CFileDialog dlg(TRUE,
711 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
712 L"STEP Files (*.stp;*.step)|*.stp; *.step|All Files (*.*)|*.*||",
715 CString CASROOTValue;
716 CASROOTValue.GetEnvironmentVariable(L"CASROOT");
717 CString initdir = (CASROOTValue + "\\..\\data\\step");
719 dlg.m_ofn.lpstrInitialDir = initdir;
721 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
722 if (dlg.DoModal() == IDOK)
724 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
725 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
726 TCollection_AsciiString aFileName (aFileNameW, '?');
727 IFSelect_ReturnStatus ReturnStatus = ReadSTEP (aFileName.ToCString(), aSequence);
728 switch (ReturnStatus)
730 case IFSelect_RetError :
731 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Not a valid Step file", L"ERROR", MB_ICONWARNING);
733 case IFSelect_RetFail :
734 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Reading has failed", L"ERROR", MB_ICONWARNING);
736 case IFSelect_RetVoid :
737 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Nothing to transfer", L"ERROR", MB_ICONWARNING);
740 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
745 IFSelect_ReturnStatus CImportExport::ReadSTEP(const Standard_CString& aFileName,
746 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
748 aHSequenceOfShape->Clear();
750 // create additional log file
751 STEPControl_Reader aReader;
752 IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);
753 if (status != IFSelect_RetDone)
756 aReader.WS()->MapReader()->SetTraceLevel(2); // increase default trace level
758 Standard_Boolean failsonly = Standard_False;
759 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
762 Standard_Integer nbr = aReader.NbRootsForTransfer();
763 aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
764 for ( Standard_Integer n = 1; n<=nbr; n++) {
765 /*Standard_Boolean ok =*/ aReader.TransferRoot(n);
768 // Collecting resulting entities
769 Standard_Integer nbs = aReader.NbShapes();
771 return IFSelect_RetVoid;
773 for (Standard_Integer i=1; i<=nbs; i++) {
774 aHSequenceOfShape->Append(aReader.Shape(i));
781 //----------------------------------------------------------------------
782 void CImportExport::SaveSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
784 anInteractiveContext->InitCurrent();
785 if (anInteractiveContext->NbCurrents() == 0){
786 AfxMessageBox (L"No shape selected for export!");
789 Handle(Quantity_HArray1OfColor) anArrayOfColors;
790 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
791 CImportExport::SaveSTEP(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
794 // Return True if no error
795 Standard_Boolean TestFacetedBrep(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
797 Standard_Boolean OneErrorFound = Standard_False;
798 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
800 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
802 TopExp_Explorer Ex(aShape,TopAbs_FACE);
803 while (Ex.More() && !OneErrorFound)
805 // Get the Geom_Surface outside the TopoDS_Face
806 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(Ex.Current()));
807 // check if it is a plane.
808 if (!aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
809 OneErrorFound=Standard_True;
812 TopExp_Explorer Ex2(aShape,TopAbs_EDGE);
813 while (Ex2.More() && !OneErrorFound)
815 // Get the Geom_Curve outside the TopoDS_Face
816 Standard_Real FirstDummy,LastDummy;
817 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(Ex2.Current()),FirstDummy,LastDummy);
818 // check if it is a line.
819 if (!aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
820 OneErrorFound=Standard_True;
824 return !OneErrorFound;
827 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
829 if (aHSequenceOfShape->Length() == 0)
831 MessageBox (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
832 return IFSelect_RetError;
835 IFSelect_ReturnStatus status = IFSelect_RetVoid;
837 CFileSaveSTEPDialog aDlg(NULL);
839 aDlg.m_Cc1ModelType = STEPControl_AsIs;
841 if (aDlg.DoModal() == IDOK) {
842 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
843 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )aDlg.GetPathName());
844 TCollection_AsciiString aFileName (aFileNameW, '?');
846 STEPControl_StepModelType selection = aDlg.m_Cc1ModelType;
848 if(selection == STEPControl_FacetedBrep)
850 if (!TestFacetedBrep(aHSequenceOfShape))
852 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"At least one shape doesn't contain facetes", L"CasCade Warning", MB_ICONWARNING);
853 return IFSelect_RetError;
857 status = SaveSTEP (aFileName.ToCString(), aHSequenceOfShape, selection);
860 case IFSelect_RetError:
861 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Incorrect Data", L"ERROR", MB_ICONWARNING);
863 case IFSelect_RetFail:
864 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Writing has failed", L"ERROR", MB_ICONWARNING);
866 case IFSelect_RetVoid:
867 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Nothing to transfer", L"ERROR", MB_ICONWARNING);
870 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
874 //----------------------------------------------------------------------------------------
875 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Standard_CString& aFileName,
876 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
878 const STEPControl_StepModelType aValue /* =TopoDSToCc1Act_ManifoldSolidBrep */ )
883 STEPControl_Writer aWriter;
885 IFSelect_ReturnStatus status;
886 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
888 status = aWriter.Transfer(aHSequenceOfShape->Value(i), aValue);
889 if ( status != IFSelect_RetDone ) return status;
891 status = aWriter.Write(aFileName);
897 //======================================================================
901 //======================================================================
903 void CImportExport::SaveSTL(const Handle(AIS_InteractiveContext)& anInteractiveContext)
905 anInteractiveContext->InitCurrent();
906 if (anInteractiveContext->NbCurrents() == 0){
907 AfxMessageBox (L"No shape selected for export!");
910 Handle(Quantity_HArray1OfColor) anArrayOfColors;
911 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
912 CImportExport::SaveSTL(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
915 Standard_Boolean CImportExport::SaveSTL(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
917 CFileDialog dlg(FALSE, L"*.stl", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
918 L"stl Files (*.stl)|*.stl;|STL Files (*.STL)|*.STL;||", NULL);
920 CString CASROOTValue;
921 CASROOTValue.GetEnvironmentVariable(L"CASROOT");
922 CString initdir = (CASROOTValue + "\\..\\data\\stl");
924 dlg.m_ofn.lpstrInitialDir = initdir;
926 Standard_Boolean result = Standard_False;
928 if (dlg.DoModal() == IDOK) {
929 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
930 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
931 TCollection_AsciiString aFileName (aFileNameW, '?');
932 TCollection_AsciiString Message;
933 result = SaveSTL (aFileName.ToCString(), aHSequenceOfShape, Message);
934 CString aMsg (Message.ToCString());
935 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, aMsg, result ? L"CasCade" : L"CasCade Error", result ? MB_OK : MB_ICONERROR);
936 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
941 Standard_Boolean CImportExport::SaveSTL(const Standard_CString& aFileName,
942 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
943 TCollection_AsciiString& ReturnMessage)
945 Standard_Boolean ReturnValue = Standard_True;
946 if (aHSequenceOfShape->Length() == 0)
948 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
949 return Standard_False;
952 PTColStd_TransientPersistentMap aMap;
953 ReturnMessage += "The Object have be saved in the file ";
954 ReturnMessage += aFileName;
955 ReturnMessage += "\n with the names : ";
959 MKCP.MakeCompound(RES);
961 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
963 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
964 TCollection_AsciiString anObjectName("anObjectName_");
966 ReturnMessage += anObjectName;
967 ReturnMessage += " \n";
969 if ( aShape.IsNull() )
971 ReturnMessage += " Error : Invalid shape \n";
972 ReturnValue = Standard_False;
976 MKCP.Add(RES, aShape);
979 StlAPI_Writer myStlWriter;
980 myStlWriter.Write(RES, aFileName);
986 //======================================================================
990 //======================================================================
992 void CImportExport::SaveVRML(const Handle(AIS_InteractiveContext)& anInteractiveContext)
994 anInteractiveContext->InitCurrent();
995 if (anInteractiveContext->NbCurrents() == 0){
996 AfxMessageBox (L"No shape selected for export!");
999 Handle(Quantity_HArray1OfColor) anArrayOfColors;
1000 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
1001 CImportExport::SaveVRML(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies),
1002 anArrayOfColors, anArrayOfTransparencies);
1005 Standard_Boolean CImportExport::SaveVRML(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
1006 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
1007 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
1009 CFileDialog dlg(FALSE, L"*.vrml", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
1010 L"vrml Files (*.vrml)|*.vrml;|vrm Files (*.vrm)|*.vrm;||", NULL);
1012 CString CASROOTValue;
1013 CASROOTValue.GetEnvironmentVariable(L"CASROOT");
1014 CString initdir = (CASROOTValue + "\\..\\data\\vrml");
1016 dlg.m_ofn.lpstrInitialDir = initdir;
1018 Standard_Boolean result = Standard_False;
1020 if (dlg.DoModal() == IDOK) {
1021 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1022 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
1023 TCollection_AsciiString aFileName (aFileNameW, '?');
1024 TCollection_AsciiString Message;
1025 result = SaveVRML (aFileName.ToCString(), aHSequenceOfShape, anArrayOfColors, anArrayOfTransparencies, Message);
1026 CString aMsg (Message.ToCString());
1027 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, aMsg, result ? L"CasCade" : L"CasCade Error", result ? MB_OK : MB_ICONERROR);
1028 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1033 Standard_Boolean CImportExport::SaveVRML(const Standard_CString& aFileName,
1034 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
1035 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
1036 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies,
1037 TCollection_AsciiString& ReturnMessage)
1039 Standard_Boolean ReturnValue = Standard_True;
1040 if (aHSequenceOfShape->Length() == 0)
1042 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
1043 return Standard_False;
1046 PTColStd_TransientPersistentMap aMap;
1047 ReturnMessage += "The Object has been saved in the file ";
1048 ReturnMessage += aFileName;
1049 ReturnMessage += "\n with the names : ";
1052 VrmlData_Scene scene;
1053 VrmlData_ShapeConvert converter(scene/*, 0.001*/); // from mm to meters
1054 Standard_Integer iShape = 1; // Counter of shapes
1056 for ( int i = 1; i <= aHSequenceOfShape->Length(); i++ )
1059 TopoDS_Shape shape = aHSequenceOfShape->Value( i );
1060 if ( shape.IsNull() )
1062 ReturnMessage += " Error : Invalid shape \n";
1063 ReturnValue = Standard_False;
1068 Quantity_Color color; // yellow
1069 if (!anArrayOfColors.IsNull())
1070 color = anArrayOfColors->Value(i);
1073 Standard_Real transparency = 0.0;
1074 if (!anArrayOfTransparencies.IsNull())
1075 transparency = anArrayOfTransparencies->Value(i);
1077 // Give a name to the shape.
1078 TCollection_AsciiString name("Shape");
1079 name += TCollection_AsciiString(iShape++);
1080 converter.AddShape(shape, name.ToCString());
1081 ReturnMessage += name;
1082 ReturnMessage += '\n';
1084 // Check presence of faces in the shape.
1085 TopExp_Explorer expl(shape, TopAbs_FACE);
1087 converter.Convert(true, false, 0.01); // faces only
1089 converter.Convert(false, true, 0.01); // edges only
1091 // Name of the color & transparency.
1092 // It will be uniquely saved in VRML file.
1093 TCollection_AsciiString cname = Quantity_Color::StringName(color.Name());
1094 cname += transparency;
1096 // Make the appearance (VRML attribute)
1097 Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(scene.FindNode(cname.ToCString()));
1098 if (appearance.IsNull())
1100 // Not found ... create a new one.
1101 Handle(VrmlData_Material) material = new VrmlData_Material(scene, cname.ToCString(), 0.2, 0.2, transparency);
1102 material->SetDiffuseColor(color);
1103 material->SetEmissiveColor(color);
1104 material->SetSpecularColor(color);
1105 scene.AddNode(material, false);
1106 appearance = new VrmlData_Appearance(scene, cname.ToCString());
1107 appearance->SetMaterial(material);
1108 scene.AddNode(appearance, false);
1111 // Apply the material to the shape of entity.
1112 Handle(VrmlData_Group) group = Handle(VrmlData_Group)::DownCast(scene.FindNode(name.ToCString()));
1113 if (!group.IsNull())
1115 VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
1116 for (; itr.More(); itr.Next())
1118 Handle(VrmlData_Node) node = itr.Value();
1119 if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1121 Handle(VrmlData_ShapeNode) shape = Handle(VrmlData_ShapeNode)::DownCast(node);
1122 shape->SetAppearance(appearance);
1124 else if (itr.Value()->DynamicType() == STANDARD_TYPE(VrmlData_Group))
1126 Handle(VrmlData_Group) groupc = Handle(VrmlData_Group)::DownCast(itr.Value());
1127 VrmlData_ListOfNode::Iterator itrc = groupc->NodeIterator();
1128 for (; itrc.More(); itrc.Next())
1130 Handle(VrmlData_Node) nodec = itrc.Value();
1131 if (nodec->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1133 Handle(VrmlData_ShapeNode) shapec = Handle(VrmlData_ShapeNode)::DownCast(nodec);
1134 shapec->SetAppearance(appearance);
1136 } // for of group nodes...
1137 } // if (it is a shape node...
1138 } // for of group nodes...
1139 } // if (!group.IsNull...
1140 } // iterator of shapes
1143 ofstream writer(aFileName);
1147 /* Old approach to store shapes in VRML (without color & transparency).
1148 TopoDS_Compound RES;
1150 MKCP.MakeCompound(RES);
1152 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
1154 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
1155 TCollection_AsciiString anObjectName("anObjectName_");
1157 ReturnMessage += anObjectName;
1158 ReturnMessage += " \n";
1160 if ( aShape.IsNull() )
1162 ReturnMessage += " Error : Invalid shape \n";
1163 ReturnValue = Standard_False;
1167 MKCP.Add(RES, aShape);
1170 VrmlAPI_Writer myVrmlWriter;
1171 myVrmlWriter.Write(RES, aFileName);