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,LPCTSTR InitialDir /* = NULL*/)
107 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadBREP(InitialDir);
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(LPCTSTR /*InitialDir*/ /* = NULL*/) // not by reference --> the sequence is created here !!
122 CFileDialog dlg(TRUE,
125 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
126 "BREP Files (*.brep , *.rle)|*.brep; *.BREP; *.rle; *.RLE; |All Files (*.*)|*.*||",
131 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
132 CString initdir = (CASROOTValue + "\\..\\data\\occ");
134 dlg.m_ofn.lpstrInitialDir = initdir;
136 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
138 if (dlg.DoModal() == IDOK)
140 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
141 CString filename = dlg.GetPathName();
143 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
144 Standard_Boolean result = ReadBREP(aFileName,aShape);
147 if (!BRepAlgo::IsValid(aShape))
148 MessageBox(AfxGetMainWnd()->m_hWnd,"Warning: The shape is not valid!","Cascade Warning",MB_ICONWARNING);
150 aSequence->Append(aShape);
153 MessageBox(AfxGetMainWnd()->m_hWnd,"Error: The file was not read","Cascade Error",MB_ICONERROR);
155 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
160 //----------------------------------------------------------------------
162 Standard_Boolean CImportExport::ReadBREP(const Standard_CString& aFileName,
163 TopoDS_Shape& aShape)
165 BRep_Builder aBuilder;
166 Standard_Boolean result = BRepTools::Read(aShape,aFileName,aBuilder);
170 void CImportExport::SaveBREP(const Handle_AIS_InteractiveContext& anInteractiveContext)
172 anInteractiveContext->InitCurrent();
173 if (anInteractiveContext->NbCurrents() == 0){
174 AfxMessageBox("No shape selected for export!");
177 Handle(TopTools_HSequenceOfShape) aHSequenceOfShape;
178 Handle(Quantity_HArray1OfColor) anArrayOfColors;
179 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
180 aHSequenceOfShape = BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies);
181 int aLength = aHSequenceOfShape->Length();
183 TopoDS_Shape RES = aHSequenceOfShape->Value(1);
184 CImportExport::SaveBREP(RES);
188 MKCP.MakeCompound(RES);
189 for (Standard_Integer i=1;i<=aLength;i++)
191 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
192 if ( aShape.IsNull() )
196 MKCP.Add(RES, aShape);
198 CImportExport::SaveBREP(RES);
202 Standard_Boolean CImportExport::SaveBREP(const TopoDS_Shape& aShape)
204 CFileDialog dlg(FALSE,_T("*.brep"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
205 "BREP Files (*.brep)|*.brep;|BREP Files (*.BREP)|*.BREP;||", NULL );
209 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
210 CString initdir = (CASROOTValue + "\\..\\data\\occ");
212 dlg.m_ofn.lpstrInitialDir = initdir;
214 Standard_Boolean result = Standard_False;
215 if (dlg.DoModal() == IDOK)
217 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
218 CString filename = dlg.GetPathName();
219 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
220 result = SaveBREP(aFileName,aShape);
222 MessageBox(0,"Error : The shape or shapes were not saved.",
223 "CasCade Error",MB_ICONERROR);
224 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
229 //----------------------------------------------------------------------------------------
230 Standard_Boolean CImportExport::SaveBREP(const Standard_CString& aFileName,
231 const TopoDS_Shape& aShape)
233 Standard_Boolean result = BRepTools::Write(aShape,aFileName);
238 //======================================================================
242 //======================================================================
245 TCollection_AsciiString CImportExport::BuildStorageErrorMessage( Storage_Error anError)
248 TCollection_AsciiString aMessage("Storage Status :");
251 aMessage += "no problem \n";
253 case Storage_VSOpenError :
254 aMessage += "OpenError while opening the stream \n";
256 case Storage_VSModeError :
257 aMessage += "the stream is opened with a wrong mode for operation \n";
259 case Storage_VSCloseError :
260 aMessage += "CloseError while closing the stream \n";
262 case Storage_VSAlreadyOpen :
263 aMessage += "stream is already opened \n";
265 case Storage_VSNotOpen :
266 aMessage += "stream not opened \n";
268 case Storage_VSSectionNotFound :
269 aMessage += "the section is not found \n";
271 case Storage_VSWriteError :
272 aMessage += "error during writing \n";
274 case Storage_VSFormatError :
275 aMessage += "wrong format error occured while reading \n";
277 case Storage_VSUnknownType :
278 aMessage += "try to read an unknown type \n";
280 case Storage_VSTypeMismatch :
281 aMessage += "try to read a wrong primitive type (read a char while expecting a real) \n";
283 case Storage_VSInternalError :
284 aMessage += "internal error \n ";
286 case Storage_VSExtCharParityError :
287 aMessage += "problem with 16bit characters, may be an 8bit character is inserted inside a 16bit string \n";
290 aMessage += "Unknown Status ";
298 void CImportExport::ReadCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
300 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadCSFDB();
301 for(int i=1;i<= aSequence->Length();i++)
302 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)), Standard_False);
305 Handle(TopTools_HSequenceOfShape) CImportExport::ReadCSFDB() // not by reference --> the sequence is created here !!
307 CFileDialog dlg(TRUE,
310 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
311 "CSFDB Files (*.CSFDB , *.csf)|*.csfdb; *.csf; |All Files (*.*)|*.*||",
316 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
317 CString initdir = (CASROOTValue + "\\..\\data\\csfdb");
319 dlg.m_ofn.lpstrInitialDir = initdir;
321 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();;
322 if (dlg.DoModal() == IDOK)
324 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
325 CString filename = dlg.GetPathName();
326 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
327 TCollection_AsciiString Message;
328 Standard_Boolean result = ReadCSFDB(aFileName,aSequence,Message);
331 // Display The Message :
332 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
337 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
338 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
343 Standard_Boolean CImportExport::ReadCSFDB(const Standard_CString& aFileName,
344 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape, // out parameter
345 TCollection_AsciiString& ReturnMessage) // out parameter
350 // the applicative Schema
351 Handle(ShapeSchema) s = new ShapeSchema;
353 // a Read/Write data object
354 Handle(Storage_Data) d = new Storage_Data;
356 d->ClearErrorStatus();
359 if ( FSD_File::IsGoodFileType( aFileName ) != Storage_VSOk) {
360 ReturnMessage = "Bad file type for ";
361 ReturnMessage += aFileName;
362 ReturnMessage += " \n";
363 return Standard_False;
366 // Open the archive, Read mode
367 Storage_Error err = f.Open(aFileName, Storage_VSRead);
368 // Read all the persistent object in the file with the schema
369 if ( err != Storage_VSOk ) {
370 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
371 return Standard_False;
375 err = d->ErrorStatus() ;
377 if ( err != Storage_VSOk ) {
378 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
379 return Standard_False;
381 // Close the file driver
384 ReturnMessage += "Application Name :"; ReturnMessage += d->ApplicationName();ReturnMessage += "\n";
385 ReturnMessage += "Application Version :"; ReturnMessage += d->ApplicationVersion();ReturnMessage += "\n";
386 ReturnMessage += "Data type :"; ReturnMessage += d->DataType();ReturnMessage += "\n";
387 ReturnMessage += "== User Infos : ==\n";
388 const TColStd_SequenceOfAsciiString& UserInfo = d->UserInfo();
389 for (int i=1;i<=UserInfo.Length();i++)
390 {ReturnMessage += UserInfo(i);ReturnMessage += "\n";}
391 ReturnMessage += "== Comments : ==\n";
392 const TColStd_SequenceOfExtendedString& Comments=d->Comments();
393 for ( int i=1;i<=Comments.Length();i++)
394 {ReturnMessage += Comments(i);ReturnMessage += "\n";}
395 ReturnMessage += "----------------\n";
398 // Read all the root objects
400 Handle(Storage_HSeqOfRoot) roots = d->Roots();
401 Handle(Standard_Persistent) p;
402 Handle(Storage_Root) r;
403 Handle(PTopoDS_HShape) aPShape;
404 for ( int i = 1; i <= roots->Length() ; i++ )
409 // Get the persistent application object from the root
412 // Display information
413 ReturnMessage += "Persistent Object "; ReturnMessage += i; ReturnMessage += "\n";
414 ReturnMessage += "Name :"; ReturnMessage += r->Name(); ReturnMessage += "\n";
415 ReturnMessage += "Type :"; ReturnMessage += r->Type(); ReturnMessage += "\n";
417 aPShape = Handle(PTopoDS_HShape)::DownCast(p);
419 if ( !aPShape.IsNull() )
421 // To Be ReWriten to suppress the cout,
422 // and provide a CallBack method for dynamic information.
423 // Get the persistent shape
424 PTColStd_PersistentTransientMap aMap;
425 TopoDS_Shape aTShape;
426 MgtBRep::Translate(aPShape,aMap,aTShape,
427 MgtBRep_WithTriangle);
428 aHSequenceOfShape->Append(aTShape);
432 ReturnMessage += "Error -> Unable to read\n";
435 return Standard_True;
437 //----------------------------------------------------------------------
438 void CImportExport::SaveCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
440 anInteractiveContext->InitCurrent();
441 if (anInteractiveContext->NbCurrents() == 0){
442 AfxMessageBox("No shape selected for export!");
445 Handle(Quantity_HArray1OfColor) anArrayOfColors;
446 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
447 CImportExport::SaveCSFDB(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
450 Standard_Boolean CImportExport::SaveCSFDB(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
452 Standard_Boolean result = Standard_False;
453 CFileSaveCSFDBDialog aDlg(NULL);
454 aDlg.m_TriangleMode = MgtBRep_WithTriangle;
455 if (aDlg.DoModal() == IDOK)
457 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
458 CString filename = aDlg.GetPathName();
459 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
460 TCollection_AsciiString Message;
461 //MgtBRep_TriangleMode selection = aDlg.m_TriangleMode;
462 Standard_Boolean result = SaveCSFDB(aFileName,aHSequenceOfShape,Message);
465 // Display The Message :
466 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
469 MessageBox(0,Message.ToCString(),"CasCade Error",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 MessageBox(0,"No Shape in the HSequence!!","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 "IGES Files (*.iges , *.igs)|*.iges; *.igs|All Files (*.*)|*.*||",
590 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
591 CString initdir = (CASROOTValue + "\\..\\data\\iges");
593 dlg.m_ofn.lpstrInitialDir = initdir;
595 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();
596 if (dlg.DoModal() == IDOK)
598 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
599 CString C = dlg.GetPathName();
600 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
601 Standard_Integer status = ReadIGES(aFileName,aSequence);
602 if (status != IFSelect_RetDone)
604 MessageBox(0,"Error : The file is not read","CasCade Error",MB_ICONERROR);
607 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
611 //----------------------------------------------------------------------
613 Standard_Integer CImportExport::ReadIGES(const Standard_CString& aFileName,
614 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
617 IGESControl_Reader Reader;
619 Standard_Integer status = Reader.ReadFile(aFileName);
621 if (status != IFSelect_RetDone) return status;
622 Reader.TransferRoots();
623 TopoDS_Shape aShape = Reader.OneShape();
624 aHSequenceOfShape->Append(aShape);
628 //----------------------------------------------------------------------
630 void CImportExport::SaveIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
632 anInteractiveContext->InitCurrent();
633 if (anInteractiveContext->NbCurrents() == 0){
634 AfxMessageBox("No shape selected for export!");
637 Handle(Quantity_HArray1OfColor) anArrayOfColors;
638 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
639 CImportExport::SaveIGES(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
642 Standard_Boolean CImportExport::SaveIGES(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
644 if (aHSequenceOfShape->Length() == 0)
646 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
647 return Standard_False;
650 CFileDialog dlg(FALSE,_T("*.iges"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
651 "IGES Files (*.iges )|*.iges;|IGES Files (*.igs )| *.igs;||", NULL );
655 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
656 CString initdir = (CASROOTValue + "\\..\\data\\iges");
658 dlg.m_ofn.lpstrInitialDir = initdir;
660 Standard_Boolean result=Standard_False;
661 if (dlg.DoModal() == IDOK)
663 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
664 CString C = dlg.GetPathName();
665 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
668 result = SaveIGES(aFileName,aHSequenceOfShape);
669 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
673 //----------------------------------------------------------------------
675 Standard_Boolean CImportExport::SaveIGES(const Standard_CString& aFileName,
676 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
679 IGESControl_Controller::Init();
680 IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
681 Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
683 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
684 ICW.AddShape (aHSequenceOfShape->Value(i));
687 Standard_Boolean result = ICW.Write(aFileName );
691 //======================================================================
693 //======================================================================
697 //======================================================================
699 void CImportExport::ReadSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
701 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadSTEP();
702 if (!aSequence.IsNull()) {
703 for(int i=1;i<= aSequence->Length();i++)
704 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)), Standard_False);
708 Handle(TopTools_HSequenceOfShape) CImportExport::ReadSTEP()// not by reference --> the sequence is created here !!
710 CFileDialog dlg(TRUE,
713 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
714 "STEP Files (*.stp;*.step)|*.stp; *.step|All Files (*.*)|*.*||",
719 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
720 CString initdir = (CASROOTValue + "\\..\\data\\step");
722 dlg.m_ofn.lpstrInitialDir = initdir;
724 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
725 if (dlg.DoModal() == IDOK)
727 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
728 CString C = dlg.GetPathName();
729 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
730 IFSelect_ReturnStatus ReturnStatus = ReadSTEP(aFileName,aSequence);
731 switch (ReturnStatus)
733 case IFSelect_RetError :
734 MessageBox(0,"Not a valid Step file","ERROR",MB_ICONWARNING);
736 case IFSelect_RetFail :
737 MessageBox(0,"Reading has failed","ERROR",MB_ICONWARNING);
739 case IFSelect_RetVoid :
740 MessageBox(0,"Nothing to transfer","ERROR",MB_ICONWARNING);
743 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
748 IFSelect_ReturnStatus CImportExport::ReadSTEP(const Standard_CString& aFileName,
749 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
751 aHSequenceOfShape->Clear();
753 // create additional log file
754 STEPControl_Reader aReader;
755 IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);
756 if (status != IFSelect_RetDone)
759 aReader.WS()->MapReader()->SetTraceLevel(2); // increase default trace level
761 Standard_Boolean failsonly = Standard_False;
762 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
765 Standard_Integer nbr = aReader.NbRootsForTransfer();
766 aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
767 for ( Standard_Integer n = 1; n<=nbr; n++) {
768 /*Standard_Boolean ok =*/ aReader.TransferRoot(n);
771 // Collecting resulting entities
772 Standard_Integer nbs = aReader.NbShapes();
774 return IFSelect_RetVoid;
776 for (Standard_Integer i=1; i<=nbs; i++) {
777 aHSequenceOfShape->Append(aReader.Shape(i));
784 //----------------------------------------------------------------------
785 void CImportExport::SaveSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
787 anInteractiveContext->InitCurrent();
788 if (anInteractiveContext->NbCurrents() == 0){
789 AfxMessageBox("No shape selected for export!");
792 Handle(Quantity_HArray1OfColor) anArrayOfColors;
793 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
794 CImportExport::SaveSTEP(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
797 // Return True if no error
798 Standard_Boolean TestFacetedBrep(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
800 Standard_Boolean OneErrorFound = Standard_False;
801 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
803 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
805 TopExp_Explorer Ex(aShape,TopAbs_FACE);
806 while (Ex.More() && !OneErrorFound)
808 // Get the Geom_Surface outside the TopoDS_Face
809 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(Ex.Current()));
810 // check if it is a plane.
811 if (!aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
812 OneErrorFound=Standard_True;
815 TopExp_Explorer Ex2(aShape,TopAbs_EDGE);
816 while (Ex2.More() && !OneErrorFound)
818 // Get the Geom_Curve outside the TopoDS_Face
819 Standard_Real FirstDummy,LastDummy;
820 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(Ex2.Current()),FirstDummy,LastDummy);
821 // check if it is a line.
822 if (!aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
823 OneErrorFound=Standard_True;
827 return !OneErrorFound;
830 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
832 if (aHSequenceOfShape->Length() == 0)
834 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
835 return IFSelect_RetError;
838 IFSelect_ReturnStatus status = IFSelect_RetVoid;
840 CFileSaveSTEPDialog aDlg(NULL);
842 aDlg.m_Cc1ModelType = STEPControl_ManifoldSolidBrep;
844 if (aDlg.DoModal() == IDOK) {
845 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
846 CString C = aDlg.GetPathName();
847 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
849 STEPControl_StepModelType selection = aDlg.m_Cc1ModelType;
851 if(selection == STEPControl_FacetedBrep)
853 if (!TestFacetedBrep(aHSequenceOfShape))
855 MessageBox(0,"At least one shape doesn't contain facetes","CasCade Warning",MB_ICONWARNING);
856 return IFSelect_RetError;
860 status = SaveSTEP(aFileName,aHSequenceOfShape,selection);
863 case IFSelect_RetError:
864 MessageBox(0,"Incorrect Data","ERROR",MB_ICONWARNING);
866 case IFSelect_RetFail:
867 MessageBox(0,"Writing has failed","ERROR",MB_ICONWARNING);
869 case IFSelect_RetVoid:
870 MessageBox(0,"Nothing to transfer","ERROR",MB_ICONWARNING);
873 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
877 //----------------------------------------------------------------------------------------
878 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Standard_CString& aFileName,
879 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
881 const STEPControl_StepModelType aValue /* =TopoDSToCc1Act_ManifoldSolidBrep */ )
886 STEPControl_Writer aWriter;
888 IFSelect_ReturnStatus status;
889 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
891 status = aWriter.Transfer(aHSequenceOfShape->Value(i), aValue);
892 if ( status != IFSelect_RetDone ) return status;
894 status = aWriter.Write(aFileName);
900 //======================================================================
904 //======================================================================
906 void CImportExport::SaveSTL(const Handle(AIS_InteractiveContext)& anInteractiveContext)
908 anInteractiveContext->InitCurrent();
909 if (anInteractiveContext->NbCurrents() == 0){
910 AfxMessageBox("No shape selected for export!");
913 Handle(Quantity_HArray1OfColor) anArrayOfColors;
914 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
915 CImportExport::SaveSTL(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
918 Standard_Boolean CImportExport::SaveSTL(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
920 CFileDialog dlg(FALSE,_T("*.stl"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
921 "stl Files (*.stl)|*.stl;|STL Files (*.STL)|*.STL;||", NULL );
925 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
926 CString initdir = (CASROOTValue + "\\..\\data\\stl");
928 dlg.m_ofn.lpstrInitialDir = initdir;
930 Standard_Boolean result;
932 if (dlg.DoModal() == IDOK) {
933 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
934 CString C = dlg.GetPathName();
935 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
936 TCollection_AsciiString Message;
937 result = SaveSTL(aFileName,aHSequenceOfShape,Message);
940 // Display The Message :
941 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
944 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
945 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
950 Standard_Boolean CImportExport::SaveSTL(const Standard_CString& aFileName,
951 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
952 TCollection_AsciiString& ReturnMessage)
954 Standard_Boolean ReturnValue = Standard_True;
955 if (aHSequenceOfShape->Length() == 0)
957 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
958 return Standard_False;
961 PTColStd_TransientPersistentMap aMap;
962 ReturnMessage += "The Object have be saved in the file ";
963 ReturnMessage += aFileName;
964 ReturnMessage += "\n with the names : ";
968 MKCP.MakeCompound(RES);
970 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
972 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
973 TCollection_AsciiString anObjectName("anObjectName_");
975 ReturnMessage += anObjectName;
976 ReturnMessage += " \n";
978 if ( aShape.IsNull() )
980 ReturnMessage += " Error : Invalid shape \n";
981 ReturnValue = Standard_False;
985 MKCP.Add(RES, aShape);
988 StlAPI_Writer myStlWriter;
989 myStlWriter.Write(RES, aFileName);
995 //======================================================================
999 //======================================================================
1001 void CImportExport::SaveVRML(const Handle(AIS_InteractiveContext)& anInteractiveContext)
1003 anInteractiveContext->InitCurrent();
1004 if (anInteractiveContext->NbCurrents() == 0){
1005 AfxMessageBox("No shape selected for export!");
1008 Handle(Quantity_HArray1OfColor) anArrayOfColors;
1009 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
1010 CImportExport::SaveVRML(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies),
1011 anArrayOfColors, anArrayOfTransparencies);
1014 Standard_Boolean CImportExport::SaveVRML(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
1015 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
1016 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
1018 CFileDialog dlg(FALSE,_T("*.vrml"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
1019 "vrml Files (*.vrml)|*.vrml;|vrm Files (*.vrm)|*.vrm;||", NULL );
1023 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
1024 CString initdir = (CASROOTValue + "\\..\\data\\vrml");
1026 dlg.m_ofn.lpstrInitialDir = initdir;
1028 Standard_Boolean result;
1030 if (dlg.DoModal() == IDOK) {
1031 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
1032 CString C = dlg.GetPathName();
1033 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
1034 TCollection_AsciiString Message;
1035 result = SaveVRML(aFileName,aHSequenceOfShape,anArrayOfColors, anArrayOfTransparencies, Message);
1038 // Display The Message :
1039 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
1042 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
1043 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1048 Standard_Boolean CImportExport::SaveVRML(const Standard_CString& aFileName,
1049 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
1050 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
1051 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies,
1052 TCollection_AsciiString& ReturnMessage)
1054 Standard_Boolean ReturnValue = Standard_True;
1055 if (aHSequenceOfShape->Length() == 0)
1057 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
1058 return Standard_False;
1061 PTColStd_TransientPersistentMap aMap;
1062 ReturnMessage += "The Object has been saved in the file ";
1063 ReturnMessage += aFileName;
1064 ReturnMessage += "\n with the names : ";
1067 VrmlData_Scene scene;
1068 VrmlData_ShapeConvert converter(scene/*, 0.001*/); // from mm to meters
1069 Standard_Integer iShape = 1; // Counter of shapes
1071 for ( int i = 1; i <= aHSequenceOfShape->Length(); i++ )
1074 TopoDS_Shape shape = aHSequenceOfShape->Value( i );
1075 if ( shape.IsNull() )
1077 ReturnMessage += " Error : Invalid shape \n";
1078 ReturnValue = Standard_False;
1083 Quantity_Color color; // yellow
1084 if (!anArrayOfColors.IsNull())
1085 color = anArrayOfColors->Value(i);
1088 Standard_Real transparency = 0.0;
1089 if (!anArrayOfTransparencies.IsNull())
1090 transparency = anArrayOfTransparencies->Value(i);
1092 // Give a name to the shape.
1093 TCollection_AsciiString name("Shape");
1094 name += TCollection_AsciiString(iShape++);
1095 converter.AddShape(shape, name.ToCString());
1096 ReturnMessage += name;
1097 ReturnMessage += '\n';
1099 // Check presence of faces in the shape.
1100 TopExp_Explorer expl(shape, TopAbs_FACE);
1102 converter.Convert(true, false, 0.01); // faces only
1104 converter.Convert(false, true, 0.01); // edges only
1106 // Name of the color & transparency.
1107 // It will be uniquely saved in VRML file.
1108 TCollection_AsciiString cname = Quantity_Color::StringName(color.Name());
1109 cname += transparency;
1111 // Make the appearance (VRML attribute)
1112 Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(scene.FindNode(cname.ToCString()));
1113 if (appearance.IsNull())
1115 // Not found ... create a new one.
1116 Handle(VrmlData_Material) material = new VrmlData_Material(scene, cname.ToCString(), 0.2, 0.2, transparency);
1117 material->SetDiffuseColor(color);
1118 material->SetEmissiveColor(color);
1119 material->SetSpecularColor(color);
1120 scene.AddNode(material, false);
1121 appearance = new VrmlData_Appearance(scene, cname.ToCString());
1122 appearance->SetMaterial(material);
1123 scene.AddNode(appearance, false);
1126 // Apply the material to the shape of entity.
1127 Handle(VrmlData_Group) group = Handle(VrmlData_Group)::DownCast(scene.FindNode(name.ToCString()));
1128 if (!group.IsNull())
1130 VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
1131 for (; itr.More(); itr.Next())
1133 Handle(VrmlData_Node) node = itr.Value();
1134 if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1136 Handle(VrmlData_ShapeNode) shape = Handle(VrmlData_ShapeNode)::DownCast(node);
1137 shape->SetAppearance(appearance);
1139 else if (itr.Value()->DynamicType() == STANDARD_TYPE(VrmlData_Group))
1141 Handle(VrmlData_Group) groupc = Handle(VrmlData_Group)::DownCast(itr.Value());
1142 VrmlData_ListOfNode::Iterator itrc = groupc->NodeIterator();
1143 for (; itrc.More(); itrc.Next())
1145 Handle(VrmlData_Node) nodec = itrc.Value();
1146 if (nodec->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1148 Handle(VrmlData_ShapeNode) shapec = Handle(VrmlData_ShapeNode)::DownCast(nodec);
1149 shapec->SetAppearance(appearance);
1151 } // for of group nodes...
1152 } // if (it is a shape node...
1153 } // for of group nodes...
1154 } // if (!group.IsNull...
1155 } // iterator of shapes
1158 ofstream writer(aFileName);
1162 /* Old approach to store shapes in VRML (without color & transparency).
1163 TopoDS_Compound RES;
1165 MKCP.MakeCompound(RES);
1167 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
1169 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
1170 TCollection_AsciiString anObjectName("anObjectName_");
1172 ReturnMessage += anObjectName;
1173 ReturnMessage += " \n";
1175 if ( aShape.IsNull() )
1177 ReturnMessage += " Error : Invalid shape \n";
1178 ReturnValue = Standard_False;
1182 MKCP.Add(RES, aShape);
1185 VrmlAPI_Writer myVrmlWriter;
1186 myVrmlWriter.Write(RES, aFileName);