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);
149 aSequence->Append(aShape);
152 MessageBox(AfxGetMainWnd()->m_hWnd,"Error: The file was not read","Cascade Error",MB_ICONERROR);
154 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
159 //----------------------------------------------------------------------
161 Standard_Boolean CImportExport::ReadBREP(const Standard_CString& aFileName,
162 TopoDS_Shape& aShape)
164 BRep_Builder aBuilder;
165 Standard_Boolean result = BRepTools::Read(aShape,aFileName,aBuilder);
169 void CImportExport::SaveBREP(const Handle_AIS_InteractiveContext& anInteractiveContext)
171 anInteractiveContext->InitCurrent();
172 if (anInteractiveContext->NbCurrents() == 0){
173 AfxMessageBox("No shape selected for export!");
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();
182 TopoDS_Shape RES = aHSequenceOfShape->Value(1);
183 CImportExport::SaveBREP(RES);
187 MKCP.MakeCompound(RES);
188 for (Standard_Integer i=1;i<=aLength;i++)
190 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
191 if ( aShape.IsNull() )
195 MKCP.Add(RES, aShape);
197 CImportExport::SaveBREP(RES);
201 Standard_Boolean CImportExport::SaveBREP(const TopoDS_Shape& aShape)
203 CFileDialog dlg(FALSE,_T("*.brep"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
204 "BREP Files (*.brep)|*.brep;|BREP Files (*.BREP)|*.BREP;||", NULL );
208 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
209 CString initdir = (CASROOTValue + "\\..\\data\\occ");
211 dlg.m_ofn.lpstrInitialDir = initdir;
213 Standard_Boolean result = Standard_False;
214 if (dlg.DoModal() == IDOK)
216 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
217 CString filename = dlg.GetPathName();
218 Standard_CString aFileName = (Standard_CString)(LPCTSTR)filename;
219 result = SaveBREP(aFileName,aShape);
221 MessageBox(0,"Error : The shape or shapes were not saved.",
222 "CasCade Error",MB_ICONERROR);
223 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
228 //----------------------------------------------------------------------------------------
229 Standard_Boolean CImportExport::SaveBREP(const Standard_CString& aFileName,
230 const TopoDS_Shape& aShape)
232 Standard_Boolean result = BRepTools::Write(aShape,aFileName);
237 //======================================================================
241 //======================================================================
244 TCollection_AsciiString CImportExport::BuildStorageErrorMessage( Storage_Error anError)
247 TCollection_AsciiString aMessage("Storage Status :");
250 aMessage += "no problem \n";
252 case Storage_VSOpenError :
253 aMessage += "OpenError while opening the stream \n";
255 case Storage_VSModeError :
256 aMessage += "the stream is opened with a wrong mode for operation \n";
258 case Storage_VSCloseError :
259 aMessage += "CloseError while closing the stream \n";
261 case Storage_VSAlreadyOpen :
262 aMessage += "stream is already opened \n";
264 case Storage_VSNotOpen :
265 aMessage += "stream not opened \n";
267 case Storage_VSSectionNotFound :
268 aMessage += "the section is not found \n";
270 case Storage_VSWriteError :
271 aMessage += "error during writing \n";
273 case Storage_VSFormatError :
274 aMessage += "wrong format error occured while reading \n";
276 case Storage_VSUnknownType :
277 aMessage += "try to read an unknown type \n";
279 case Storage_VSTypeMismatch :
280 aMessage += "try to read a wrong primitive type (read a char while expecting a real) \n";
282 case Storage_VSInternalError :
283 aMessage += "internal error \n ";
285 case Storage_VSExtCharParityError :
286 aMessage += "problem with 16bit characters, may be an 8bit character is inserted inside a 16bit string \n";
289 aMessage += "Unknown Status ";
297 void CImportExport::ReadCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
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);
304 Handle(TopTools_HSequenceOfShape) CImportExport::ReadCSFDB() // not by reference --> the sequence is created here !!
306 CFileDialog dlg(TRUE,
309 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
310 "CSFDB Files (*.CSFDB , *.csf)|*.csfdb; *.csf; |All Files (*.*)|*.*||",
315 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
316 CString initdir = (CASROOTValue + "\\..\\data\\csfdb");
318 dlg.m_ofn.lpstrInitialDir = initdir;
320 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();;
321 if (dlg.DoModal() == IDOK)
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);
330 // Display The Message :
331 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
336 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
337 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
342 Standard_Boolean CImportExport::ReadCSFDB(const Standard_CString& aFileName,
343 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape, // out parameter
344 TCollection_AsciiString& ReturnMessage) // out parameter
349 // the applicative Schema
350 Handle(ShapeSchema) s = new ShapeSchema;
352 // a Read/Write data object
353 Handle(Storage_Data) d = new Storage_Data;
355 d->ClearErrorStatus();
358 if ( FSD_File::IsGoodFileType( aFileName ) != Storage_VSOk) {
359 ReturnMessage = "Bad file type for ";
360 ReturnMessage += aFileName;
361 ReturnMessage += " \n";
362 return Standard_False;
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;
374 err = d->ErrorStatus() ;
376 if ( err != Storage_VSOk ) {
377 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
378 return Standard_False;
380 // Close the file driver
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";
397 // Read all the root objects
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++ )
408 // Get the persistent application object from the root
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";
416 aPShape = Handle(PTopoDS_HShape)::DownCast(p);
418 if ( !aPShape.IsNull() )
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);
431 ReturnMessage += "Error -> Unable to read\n";
434 return Standard_True;
436 //----------------------------------------------------------------------
437 void CImportExport::SaveCSFDB(const Handle(AIS_InteractiveContext)& anInteractiveContext)
439 anInteractiveContext->InitCurrent();
440 if (anInteractiveContext->NbCurrents() == 0){
441 AfxMessageBox("No shape selected for export!");
444 Handle(Quantity_HArray1OfColor) anArrayOfColors;
445 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
446 CImportExport::SaveCSFDB(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
449 Standard_Boolean CImportExport::SaveCSFDB(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
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);
463 // Display The Message :
464 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
467 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
468 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
473 Standard_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 */)
478 Standard_Boolean ReturnValue = Standard_True;
479 if (aHSequenceOfShape->Length() == 0)
481 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
482 return Standard_False;
488 // the applicative Schema containing
489 // Pesistent Topology and Geometry
490 Handle(ShapeSchema) s = new ShapeSchema;
492 // a Read/Write data object
493 Handle(Storage_Data) d = new Storage_Data;
495 d->ClearErrorStatus();
497 // To Be ReWriten to suppress the Strings,
498 // and provide a CallBack method for dynamic information.
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"));
506 // Open the archive, Write mode
507 Storage_Error err = f.Open(aFileName, Storage_VSWrite);
509 if ( err != Storage_VSOk ) {
510 ReturnMessage += BuildStorageErrorMessage(err);
511 return Standard_False;
514 PTColStd_TransientPersistentMap aMap;
515 ReturnMessage += "The Object have be saved in the file ";
516 ReturnMessage += aFileName;
517 ReturnMessage += "\n with the names : ";
519 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
521 TopoDS_Shape aTShape= aHSequenceOfShape->Value(i);
522 TCollection_AsciiString anObjectName("anObjectName_");
524 ReturnMessage += anObjectName;
525 ReturnMessage += " \n";
527 if ( aTShape.IsNull() )
529 ReturnMessage += " Error : Invalid shape \n";
530 ReturnValue = Standard_False;
534 //Create the persistent Shape
536 Handle(PTopoDS_HShape) aPShape =
537 MgtBRep::Translate(aTShape, aMap, MgtBRep_WithTriangle);
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);
546 // Write the object in the file with the schema
552 if ( d->ErrorStatus() != Storage_VSOk )
554 ReturnMessage += BuildStorageErrorMessage(d->ErrorStatus());
555 return Standard_False;
561 //======================================================================
565 //======================================================================
569 void CImportExport::ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
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)));
577 Handle(TopTools_HSequenceOfShape) CImportExport::ReadIGES()// not by reference --> the sequence is created here !!
579 CFileDialog dlg(TRUE,
582 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
583 "IGES Files (*.iges , *.igs)|*.iges; *.igs|All Files (*.*)|*.*||",
588 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
589 CString initdir = (CASROOTValue + "\\..\\data\\iges");
591 dlg.m_ofn.lpstrInitialDir = initdir;
593 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();
594 if (dlg.DoModal() == IDOK)
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)
602 MessageBox(0,"Error : The file is not read","CasCade Error",MB_ICONERROR);
605 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
609 //----------------------------------------------------------------------
611 Standard_Integer CImportExport::ReadIGES(const Standard_CString& aFileName,
612 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
615 IGESControl_Reader Reader;
617 Standard_Integer status = Reader.ReadFile(aFileName);
619 if (status != IFSelect_RetDone) return status;
620 Reader.TransferRoots();
621 TopoDS_Shape aShape = Reader.OneShape();
622 aHSequenceOfShape->Append(aShape);
626 //----------------------------------------------------------------------
628 void CImportExport::SaveIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
630 anInteractiveContext->InitCurrent();
631 if (anInteractiveContext->NbCurrents() == 0){
632 AfxMessageBox("No shape selected for export!");
635 Handle(Quantity_HArray1OfColor) anArrayOfColors;
636 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
637 CImportExport::SaveIGES(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
640 Standard_Boolean CImportExport::SaveIGES(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
642 if (aHSequenceOfShape->Length() == 0)
644 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
645 return Standard_False;
648 CFileDialog dlg(FALSE,_T("*.iges"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
649 "IGES Files (*.iges )|*.iges;|IGES Files (*.igs )| *.igs;||", NULL );
653 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
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));
662 CString C = dlg.GetPathName();
663 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
666 result = SaveIGES(aFileName,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 "STEP Files (*.stp;*.step)|*.stp; *.step|All Files (*.*)|*.*||",
717 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
718 CString initdir = (CASROOTValue + "\\..\\data\\step");
720 dlg.m_ofn.lpstrInitialDir = initdir;
722 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
723 if (dlg.DoModal() == IDOK)
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)
731 case IFSelect_RetError :
732 MessageBox(0,"Not a valid Step file","ERROR",MB_ICONWARNING);
734 case IFSelect_RetFail :
735 MessageBox(0,"Reading has failed","ERROR",MB_ICONWARNING);
737 case IFSelect_RetVoid :
738 MessageBox(0,"Nothing to transfer","ERROR",MB_ICONWARNING);
741 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
746 IFSelect_ReturnStatus CImportExport::ReadSTEP(const Standard_CString& aFileName,
747 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
749 aHSequenceOfShape->Clear();
751 // create additional log file
752 STEPControl_Reader aReader;
753 IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);
754 if (status != IFSelect_RetDone)
757 aReader.WS()->MapReader()->SetTraceLevel(2); // increase default trace level
759 Standard_Boolean failsonly = Standard_False;
760 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
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);
769 // Collecting resulting entities
770 Standard_Integer nbs = aReader.NbShapes();
772 return IFSelect_RetVoid;
774 for (Standard_Integer i=1; i<=nbs; i++) {
775 aHSequenceOfShape->Append(aReader.Shape(i));
782 //----------------------------------------------------------------------
783 void CImportExport::SaveSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
785 anInteractiveContext->InitCurrent();
786 if (anInteractiveContext->NbCurrents() == 0){
787 AfxMessageBox("No shape selected for export!");
790 Handle(Quantity_HArray1OfColor) anArrayOfColors;
791 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
792 CImportExport::SaveSTEP(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
795 // Return True if no error
796 Standard_Boolean TestFacetedBrep(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
798 Standard_Boolean OneErrorFound = Standard_False;
799 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
801 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
803 TopExp_Explorer Ex(aShape,TopAbs_FACE);
804 while (Ex.More() && !OneErrorFound)
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;
813 TopExp_Explorer Ex2(aShape,TopAbs_EDGE);
814 while (Ex2.More() && !OneErrorFound)
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;
825 return !OneErrorFound;
828 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
830 if (aHSequenceOfShape->Length() == 0)
832 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
833 return IFSelect_RetError;
836 IFSelect_ReturnStatus status;
838 CFileSaveSTEPDialog aDlg(NULL);
840 aDlg.m_Cc1ModelType = STEPControl_ManifoldSolidBrep;
842 if (aDlg.DoModal() == IDOK) {
843 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
844 CString C = aDlg.GetPathName();
845 Standard_CString aFileName = (Standard_CString)(LPCTSTR)C;
847 STEPControl_StepModelType selection = aDlg.m_Cc1ModelType;
849 if(selection == STEPControl_FacetedBrep)
851 if (!TestFacetedBrep(aHSequenceOfShape))
853 MessageBox(0,"At least one shape doesn't contain facetes","CasCade Warning",MB_ICONWARNING);
854 return IFSelect_RetError;
858 status = SaveSTEP(aFileName,aHSequenceOfShape,selection);
861 case IFSelect_RetError:
862 MessageBox(0,"Incorrect Data","ERROR",MB_ICONWARNING);
864 case IFSelect_RetFail:
865 MessageBox(0,"Writing has failed","ERROR",MB_ICONWARNING);
867 case IFSelect_RetVoid:
868 MessageBox(0,"Nothing to transfer","ERROR",MB_ICONWARNING);
871 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
875 //----------------------------------------------------------------------------------------
876 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Standard_CString& aFileName,
877 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
879 const STEPControl_StepModelType aValue /* =TopoDSToCc1Act_ManifoldSolidBrep */ )
884 STEPControl_Writer aWriter;
886 IFSelect_ReturnStatus status;
887 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
889 status = aWriter.Transfer(aHSequenceOfShape->Value(i), aValue);
890 if ( status != IFSelect_RetDone ) return status;
892 status = aWriter.Write(aFileName);
898 //======================================================================
902 //======================================================================
904 void CImportExport::SaveSTL(const Handle(AIS_InteractiveContext)& anInteractiveContext)
906 anInteractiveContext->InitCurrent();
907 if (anInteractiveContext->NbCurrents() == 0){
908 AfxMessageBox("No shape selected for export!");
911 Handle(Quantity_HArray1OfColor) anArrayOfColors;
912 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
913 CImportExport::SaveSTL(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
916 Standard_Boolean CImportExport::SaveSTL(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
918 CFileDialog dlg(FALSE,_T("*.stl"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
919 "stl Files (*.stl)|*.stl;|STL Files (*.STL)|*.STL;||", NULL );
923 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
924 CString initdir = (CASROOTValue + "\\..\\data\\stl");
926 dlg.m_ofn.lpstrInitialDir = initdir;
928 Standard_Boolean result;
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);
938 // Display The Message :
939 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
942 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
943 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
948 Standard_Boolean CImportExport::SaveSTL(const Standard_CString& aFileName,
949 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
950 TCollection_AsciiString& ReturnMessage)
952 Standard_Boolean ReturnValue = Standard_True;
953 if (aHSequenceOfShape->Length() == 0)
955 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
956 return Standard_False;
959 PTColStd_TransientPersistentMap aMap;
960 ReturnMessage += "The Object have be saved in the file ";
961 ReturnMessage += aFileName;
962 ReturnMessage += "\n with the names : ";
966 MKCP.MakeCompound(RES);
968 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
970 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
971 TCollection_AsciiString anObjectName("anObjectName_");
973 ReturnMessage += anObjectName;
974 ReturnMessage += " \n";
976 if ( aShape.IsNull() )
978 ReturnMessage += " Error : Invalid shape \n";
979 ReturnValue = Standard_False;
983 MKCP.Add(RES, aShape);
986 StlAPI_Writer myStlWriter;
987 myStlWriter.Write(RES, aFileName);
993 //======================================================================
997 //======================================================================
999 void CImportExport::SaveVRML(const Handle(AIS_InteractiveContext)& anInteractiveContext)
1001 anInteractiveContext->InitCurrent();
1002 if (anInteractiveContext->NbCurrents() == 0){
1003 AfxMessageBox("No shape selected for export!");
1006 Handle(Quantity_HArray1OfColor) anArrayOfColors;
1007 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
1008 CImportExport::SaveVRML(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies),
1009 anArrayOfColors, anArrayOfTransparencies);
1012 Standard_Boolean CImportExport::SaveVRML(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
1013 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
1014 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
1016 CFileDialog dlg(FALSE,_T("*.vrml"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
1017 "vrml Files (*.vrml)|*.vrml;|vrm Files (*.vrm)|*.vrm;||", NULL );
1021 CString CASROOTValue = ((GetEnvironmentVariable("CASROOT", tchBuf, 80) > 0) ? tchBuf : NULL);
1022 CString initdir = (CASROOTValue + "\\..\\data\\vrml");
1024 dlg.m_ofn.lpstrInitialDir = initdir;
1026 Standard_Boolean result;
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);
1036 // Display The Message :
1037 MessageBox(0,Message.ToCString(),"CasCade ",MB_OK);
1040 MessageBox(0,Message.ToCString(),"CasCade Error",MB_ICONERROR);
1041 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
1046 Standard_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)
1052 Standard_Boolean ReturnValue = Standard_True;
1053 if (aHSequenceOfShape->Length() == 0)
1055 MessageBox(0,"No Shape in the HSequence!!","CasCade Warning",MB_ICONWARNING);
1056 return Standard_False;
1059 PTColStd_TransientPersistentMap aMap;
1060 ReturnMessage += "The Object has been saved in the file ";
1061 ReturnMessage += aFileName;
1062 ReturnMessage += "\n with the names : ";
1065 VrmlData_Scene scene;
1066 VrmlData_ShapeConvert converter(scene/*, 0.001*/); // from mm to meters
1067 Standard_Integer iShape = 1; // Counter of shapes
1069 for ( int i = 1; i <= aHSequenceOfShape->Length(); i++ )
1072 TopoDS_Shape shape = aHSequenceOfShape->Value( i );
1073 if ( shape.IsNull() )
1075 ReturnMessage += " Error : Invalid shape \n";
1076 ReturnValue = Standard_False;
1081 Quantity_Color color; // yellow
1082 if (!anArrayOfColors.IsNull())
1083 color = anArrayOfColors->Value(i);
1086 Standard_Real transparency = 0.0;
1087 if (!anArrayOfTransparencies.IsNull())
1088 transparency = anArrayOfTransparencies->Value(i);
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';
1097 // Check presence of faces in the shape.
1098 TopExp_Explorer expl(shape, TopAbs_FACE);
1100 converter.Convert(true, false, 0.01); // faces only
1102 converter.Convert(false, true, 0.01); // edges only
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;
1109 // Make the appearance (VRML attribute)
1110 Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(scene.FindNode(cname.ToCString()));
1111 if (appearance.IsNull())
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);
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())
1128 VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
1129 for (; itr.More(); itr.Next())
1131 Handle(VrmlData_Node) node = itr.Value();
1132 if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1134 Handle(VrmlData_ShapeNode) shape = Handle(VrmlData_ShapeNode)::DownCast(node);
1135 shape->SetAppearance(appearance);
1137 else if (itr.Value()->DynamicType() == STANDARD_TYPE(VrmlData_Group))
1139 Handle(VrmlData_Group) groupc = Handle(VrmlData_Group)::DownCast(itr.Value());
1140 VrmlData_ListOfNode::Iterator itrc = groupc->NodeIterator();
1141 for (; itrc.More(); itrc.Next())
1143 Handle(VrmlData_Node) nodec = itrc.Value();
1144 if (nodec->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
1146 Handle(VrmlData_ShapeNode) shapec = Handle(VrmlData_ShapeNode)::DownCast(nodec);
1147 shapec->SetAppearance(appearance);
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
1156 ofstream writer(aFileName);
1160 /* Old approach to store shapes in VRML (without color & transparency).
1161 TopoDS_Compound RES;
1163 MKCP.MakeCompound(RES);
1165 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
1167 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
1168 TCollection_AsciiString anObjectName("anObjectName_");
1170 ReturnMessage += anObjectName;
1171 ReturnMessage += " \n";
1173 if ( aShape.IsNull() )
1175 ReturnMessage += " Error : Invalid shape \n";
1176 ReturnValue = Standard_False;
1180 MKCP.Add(RES, aShape);
1183 VrmlAPI_Writer myVrmlWriter;
1184 myVrmlWriter.Write(RES, aFileName);