1 // ImportExport.cpp: implementation of the CImportExport class.
3 //////////////////////////////////////////////////////////////////////
6 #include "ImportExport.h"
9 #include "SaveSTEPDlg.h"
11 #include "TColStd_SequenceOfAsciiString.hxx"
12 #include "TColStd_SequenceOfExtendedString.hxx"
13 #include "OSD_Timer.hxx"
15 #include "IGESControl_Reader.hxx"
16 #include "STEPControl_Controller.hxx"
18 #include <BRepAlgo.hxx>
19 #include <IGESControl_Controller.hxx>
20 #include <IGESControl_Writer.hxx>
21 #include <Interface_Static.hxx>
22 #include <STEPControl_Reader.hxx>
23 #include <Geom_Curve.hxx>
24 #include <STEPControl_Writer.hxx>
25 #include <TopoDS_Compound.hxx>
26 #include <Geom_Line.hxx>
27 #include <StlAPI_Writer.hxx>
28 #include <VrmlAPI_Writer.hxx>
29 #include <VrmlData_Scene.hxx>
30 #include <VrmlData_ShapeConvert.hxx>
31 #include <VrmlData_Appearance.hxx>
32 #include <VrmlData_Material.hxx>
33 #include <VrmlData_Group.hxx>
34 #include <VrmlData_ListOfNode.hxx>
35 #include <VrmlData_ShapeNode.hxx>
37 #include <XSControl_WorkSession.hxx>
38 #include <STEPConstruct_Styles.hxx>
39 #include <TColStd_HSequenceOfTransient.hxx>
40 #include <STEPConstruct.hxx>
41 #include <StepVisual_StyledItem.hxx>
45 static char THIS_FILE[]=__FILE__;
46 //#define new DEBUG_NEW
49 //////////////////////////////////////////////////////////////////////
50 // Construction/Destruction
51 //////////////////////////////////////////////////////////////////////
53 Handle(TopTools_HSequenceOfShape) CImportExport::BuildSequenceFromContext(const Handle(AIS_InteractiveContext)& anInteractiveContext,
54 Handle(Quantity_HArray1OfColor)& anArrayOfColors,
55 Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
57 Handle(TopTools_HSequenceOfShape) aSequence;
58 Standard_Integer nb = anInteractiveContext->NbCurrents(), i = 1;
62 aSequence = new TopTools_HSequenceOfShape();
63 anArrayOfColors = new Quantity_HArray1OfColor(1, nb);
64 anArrayOfTransparencies = new TColStd_HArray1OfReal (1, nb);
66 Handle(AIS_InteractiveObject) picked;
67 for(anInteractiveContext->InitCurrent();anInteractiveContext->MoreCurrent();anInteractiveContext->NextCurrent())
69 picked = anInteractiveContext->Current();
70 if (anInteractiveContext->Current()->IsKind(STANDARD_TYPE(AIS_Shape)))
72 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(picked);
73 TopoDS_Shape aShape = aisShape->Shape();
74 aSequence->Append(aShape);
77 aisShape->Color(color);
78 anArrayOfColors->SetValue(i, color);
80 Standard_Real transparency = aisShape->Transparency();
81 anArrayOfTransparencies->SetValue(i, transparency);
89 //======================================================================
93 //======================================================================
95 int CImportExport::ReadBREP (const Handle(AIS_InteractiveContext)& anInteractiveContext)
97 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadBREP();
98 if(aSequence->IsEmpty())
100 Handle(AIS_Shape) aShape;
101 for(int i=1;i<= aSequence->Length();i++){
102 aShape = new AIS_Shape(aSequence->Value(i));
103 anInteractiveContext->SetDisplayMode(aShape, 1, Standard_False);
104 anInteractiveContext->Display(aShape, Standard_False);
105 anInteractiveContext->SetCurrentObject(aShape, Standard_False);
110 Handle(TopTools_HSequenceOfShape) CImportExport::ReadBREP()
112 CFileDialog dlg(TRUE,
115 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
116 L"BREP Files (*.brep , *.rle)|*.brep; *.BREP; *.rle; *.RLE; |All Files (*.*)|*.*||",
119 CString SHAREPATHValue;
120 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
121 CString initdir = (SHAREPATHValue + "\\occ");
123 dlg.m_ofn.lpstrInitialDir = initdir;
125 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
127 if (dlg.DoModal() == IDOK)
129 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
130 CString filename = dlg.GetPathName();
132 Standard_Boolean result = ReadBREP (filename, aShape);
135 if (!BRepAlgo::IsValid(aShape))
136 MessageBoxW (AfxGetMainWnd()->m_hWnd, L"Warning: The shape is not valid!", L"Cascade Warning", MB_ICONWARNING);
138 aSequence->Append(aShape);
141 MessageBoxW (AfxGetMainWnd()->m_hWnd, L"Error: The file was not read", L"Cascade Error", MB_ICONERROR);
143 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
148 //----------------------------------------------------------------------
150 Standard_Boolean CImportExport::ReadBREP(CString aFileName,
151 TopoDS_Shape& aShape)
155 std::filebuf aFileBuf;
156 std::istream aStream (&aFileBuf);
157 if (!aFileBuf.open (aFileName, ios::in))
159 return Standard_False;
162 BRep_Builder aBuilder;
163 BRepTools::Read (aShape, aStream, aBuilder);
164 return !aShape.IsNull();
167 void CImportExport::SaveBREP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
169 anInteractiveContext->InitCurrent();
170 if (anInteractiveContext->NbCurrents() == 0){
171 AfxMessageBox (L"No shape selected for export!");
174 Handle(TopTools_HSequenceOfShape) aHSequenceOfShape;
175 Handle(Quantity_HArray1OfColor) anArrayOfColors;
176 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
177 aHSequenceOfShape = BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies);
178 int aLength = aHSequenceOfShape->Length();
180 TopoDS_Shape RES = aHSequenceOfShape->Value(1);
181 CImportExport::SaveBREP(RES);
185 MKCP.MakeCompound(RES);
186 for (Standard_Integer i=1;i<=aLength;i++)
188 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
189 if ( aShape.IsNull() )
193 MKCP.Add(RES, aShape);
195 CImportExport::SaveBREP(RES);
199 Standard_Boolean CImportExport::SaveBREP(const TopoDS_Shape& aShape)
201 CFileDialog dlg (FALSE, L"*.brep",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
202 L"BREP Files (*.brep)|*.brep;|BREP Files (*.BREP)|*.BREP;||", NULL);
204 CString SHAREPATHValue;
205 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
206 CString initdir = (SHAREPATHValue + "\\occ");
208 dlg.m_ofn.lpstrInitialDir = initdir;
210 Standard_Boolean result = Standard_False;
211 if (dlg.DoModal() == IDOK)
213 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
214 CString filename = dlg.GetPathName();
215 result = SaveBREP (filename, aShape);
217 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd,
218 L"Error : The shape or shapes were not saved.",
219 L"CasCade Error", MB_ICONERROR);
220 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
225 //----------------------------------------------------------------------------------------
226 Standard_Boolean CImportExport::SaveBREP (CString aFileName,
227 const TopoDS_Shape& aShape)
229 std::filebuf aFileBuf;
230 std::ostream aStream (&aFileBuf);
231 if (!aFileBuf.open (aFileName, ios::out))
233 return Standard_False;
236 BRepTools::Write (aShape, aStream);
237 return Standard_True;
240 //======================================================================
244 //======================================================================
248 void CImportExport::ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
250 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadIGES();
251 for(int i=1;i<= aSequence->Length();i++)
252 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)));
256 Handle(TopTools_HSequenceOfShape) CImportExport::ReadIGES()// not by reference --> the sequence is created here !!
258 CFileDialog dlg(TRUE,
261 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
262 L"IGES Files (*.iges , *.igs)|*.iges; *.igs|All Files (*.*)|*.*||",
265 CString SHAREPATHValue;
266 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
267 CString initdir = (SHAREPATHValue + "\\iges");
269 dlg.m_ofn.lpstrInitialDir = initdir;
271 Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();
272 if (dlg.DoModal() == IDOK)
274 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
275 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
276 TCollection_AsciiString aFileName (aFileNameW, '?');
277 Standard_Integer status = ReadIGES (aFileName.ToCString(), aSequence);
278 if (status != IFSelect_RetDone)
280 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : The file is not read", L"CasCade Error", MB_ICONERROR);
283 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
287 //----------------------------------------------------------------------
289 Standard_Integer CImportExport::ReadIGES(const Standard_CString& aFileName,
290 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
293 IGESControl_Reader Reader;
295 Standard_Integer status = Reader.ReadFile(aFileName);
297 if (status != IFSelect_RetDone) return status;
298 Reader.TransferRoots();
299 TopoDS_Shape aShape = Reader.OneShape();
300 aHSequenceOfShape->Append(aShape);
304 //----------------------------------------------------------------------
306 void CImportExport::SaveIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
308 anInteractiveContext->InitCurrent();
309 if (anInteractiveContext->NbCurrents() == 0){
310 AfxMessageBox (L"No shape selected for export!");
313 Handle(Quantity_HArray1OfColor) anArrayOfColors;
314 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
315 CImportExport::SaveIGES(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
318 Standard_Boolean CImportExport::SaveIGES(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
320 if (aHSequenceOfShape->Length() == 0)
322 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
323 return Standard_False;
326 CFileDialog dlg(FALSE, L"*.iges",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
327 L"IGES Files (*.iges )|*.iges;|IGES Files (*.igs )| *.igs;||", NULL);
329 CString SHAREPATHValue;
330 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
331 CString initdir = (SHAREPATHValue + "\\iges");
333 dlg.m_ofn.lpstrInitialDir = initdir;
335 Standard_Boolean result=Standard_False;
336 if (dlg.DoModal() == IDOK)
338 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
340 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
341 TCollection_AsciiString aFileName (aFileNameW, '?');
343 result = SaveIGES (aFileName.ToCString(), aHSequenceOfShape);
344 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
348 //----------------------------------------------------------------------
350 Standard_Boolean CImportExport::SaveIGES(const Standard_CString& aFileName,
351 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
354 IGESControl_Controller::Init();
355 IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
356 Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
358 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
359 ICW.AddShape (aHSequenceOfShape->Value(i));
362 Standard_Boolean result = ICW.Write(aFileName );
366 //======================================================================
368 //======================================================================
372 //======================================================================
374 void CImportExport::ReadSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
376 Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadSTEP();
377 if (!aSequence.IsNull()) {
378 for(int i=1;i<= aSequence->Length();i++)
379 anInteractiveContext->Display(new AIS_Shape(aSequence->Value(i)), Standard_False);
383 Handle(TopTools_HSequenceOfShape) CImportExport::ReadSTEP()// not by reference --> the sequence is created here !!
385 CFileDialog dlg(TRUE,
388 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
389 L"STEP Files (*.stp;*.step)|*.stp; *.step|All Files (*.*)|*.*||",
392 CString SHAREPATHValue;
393 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
394 CString initdir = (SHAREPATHValue + "\\step");
396 dlg.m_ofn.lpstrInitialDir = initdir;
398 Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
399 if (dlg.DoModal() == IDOK)
401 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
402 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
403 TCollection_AsciiString aFileName (aFileNameW, '?');
404 IFSelect_ReturnStatus ReturnStatus = ReadSTEP (aFileName.ToCString(), aSequence);
405 switch (ReturnStatus)
407 case IFSelect_RetError :
408 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Not a valid Step file", L"ERROR", MB_ICONWARNING);
410 case IFSelect_RetFail :
411 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Reading has failed", L"ERROR", MB_ICONWARNING);
413 case IFSelect_RetVoid :
414 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Nothing to transfer", L"ERROR", MB_ICONWARNING);
417 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
422 IFSelect_ReturnStatus CImportExport::ReadSTEP(const Standard_CString& aFileName,
423 Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
425 aHSequenceOfShape->Clear();
427 // create additional log file
428 STEPControl_Reader aReader;
429 IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);
430 if (status != IFSelect_RetDone)
433 aReader.WS()->MapReader()->SetTraceLevel(2); // increase default trace level
435 Standard_Boolean failsonly = Standard_False;
436 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
439 Standard_Integer nbr = aReader.NbRootsForTransfer();
440 aReader.PrintCheckTransfer (failsonly, IFSelect_ItemsByEntity);
441 for ( Standard_Integer n = 1; n<=nbr; n++) {
442 /*Standard_Boolean ok =*/ aReader.TransferRoot(n);
445 // Collecting resulting entities
446 Standard_Integer nbs = aReader.NbShapes();
448 return IFSelect_RetVoid;
450 for (Standard_Integer i=1; i<=nbs; i++) {
451 aHSequenceOfShape->Append(aReader.Shape(i));
458 //----------------------------------------------------------------------
459 void CImportExport::SaveSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
461 anInteractiveContext->InitCurrent();
462 if (anInteractiveContext->NbCurrents() == 0){
463 AfxMessageBox (L"No shape selected for export!");
466 Handle(Quantity_HArray1OfColor) anArrayOfColors;
467 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
468 CImportExport::SaveSTEP(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
471 // Return True if no error
472 Standard_Boolean TestFacetedBrep(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
474 Standard_Boolean OneErrorFound = Standard_False;
475 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
477 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
479 TopExp_Explorer Ex(aShape,TopAbs_FACE);
480 while (Ex.More() && !OneErrorFound)
482 // Get the Geom_Surface outside the TopoDS_Face
483 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(Ex.Current()));
484 // check if it is a plane.
485 if (!aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
486 OneErrorFound=Standard_True;
489 TopExp_Explorer Ex2(aShape,TopAbs_EDGE);
490 while (Ex2.More() && !OneErrorFound)
492 // Get the Geom_Curve outside the TopoDS_Face
493 Standard_Real FirstDummy,LastDummy;
494 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(Ex2.Current()),FirstDummy,LastDummy);
495 // check if it is a line.
496 if (!aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
497 OneErrorFound=Standard_True;
501 return !OneErrorFound;
504 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
506 if (aHSequenceOfShape->Length() == 0)
508 MessageBox (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
509 return IFSelect_RetError;
512 IFSelect_ReturnStatus status = IFSelect_RetVoid;
514 CFileSaveSTEPDialog aDlg(NULL);
516 aDlg.m_Cc1ModelType = STEPControl_AsIs;
518 if (aDlg.DoModal() == IDOK) {
519 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
520 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )aDlg.GetPathName());
521 TCollection_AsciiString aFileName (aFileNameW, '?');
523 STEPControl_StepModelType selection = aDlg.m_Cc1ModelType;
525 if(selection == STEPControl_FacetedBrep)
527 if (!TestFacetedBrep(aHSequenceOfShape))
529 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"At least one shape doesn't contain facetes", L"CasCade Warning", MB_ICONWARNING);
530 return IFSelect_RetError;
534 status = SaveSTEP (aFileName.ToCString(), aHSequenceOfShape, selection);
537 case IFSelect_RetError:
538 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Incorrect Data", L"ERROR", MB_ICONWARNING);
540 case IFSelect_RetFail:
541 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Writing has failed", L"ERROR", MB_ICONWARNING);
543 case IFSelect_RetVoid:
544 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Nothing to transfer", L"ERROR", MB_ICONWARNING);
547 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
551 //----------------------------------------------------------------------------------------
552 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Standard_CString& aFileName,
553 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
555 const STEPControl_StepModelType aValue /* =TopoDSToCc1Act_ManifoldSolidBrep */ )
560 STEPControl_Writer aWriter;
562 IFSelect_ReturnStatus status;
563 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
565 status = aWriter.Transfer(aHSequenceOfShape->Value(i), aValue);
566 if ( status != IFSelect_RetDone ) return status;
568 status = aWriter.Write(aFileName);
574 //======================================================================
578 //======================================================================
580 void CImportExport::SaveSTL(const Handle(AIS_InteractiveContext)& anInteractiveContext)
582 anInteractiveContext->InitCurrent();
583 if (anInteractiveContext->NbCurrents() == 0){
584 AfxMessageBox (L"No shape selected for export!");
587 Handle(Quantity_HArray1OfColor) anArrayOfColors;
588 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
589 CImportExport::SaveSTL(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
592 Standard_Boolean CImportExport::SaveSTL(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
594 CFileDialog dlg(FALSE, L"*.stl", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
595 L"stl Files (*.stl)|*.stl;|STL Files (*.STL)|*.STL;||", NULL);
597 CString SHAREPATHValue;
598 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
599 CString initdir = (SHAREPATHValue + "\\stl");
601 dlg.m_ofn.lpstrInitialDir = initdir;
603 Standard_Boolean result = Standard_False;
605 if (dlg.DoModal() == IDOK) {
606 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
607 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
608 TCollection_AsciiString aFileName (aFileNameW, '?');
609 TCollection_AsciiString Message;
610 result = SaveSTL (aFileName.ToCString(), aHSequenceOfShape, Message);
611 CString aMsg (Message.ToCString());
612 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, aMsg, result ? L"CasCade" : L"CasCade Error", result ? MB_OK : MB_ICONERROR);
613 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
618 Standard_Boolean CImportExport::SaveSTL(const Standard_CString& aFileName,
619 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
620 TCollection_AsciiString& ReturnMessage)
622 Standard_Boolean ReturnValue = Standard_True;
623 if (aHSequenceOfShape->Length() == 0)
625 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
626 return Standard_False;
629 ReturnMessage += "The Object have be saved in the file ";
630 ReturnMessage += aFileName;
631 ReturnMessage += "\n with the names : ";
635 MKCP.MakeCompound(RES);
637 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
639 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
640 TCollection_AsciiString anObjectName("anObjectName_");
642 ReturnMessage += anObjectName;
643 ReturnMessage += " \n";
645 if ( aShape.IsNull() )
647 ReturnMessage += " Error : Invalid shape \n";
648 ReturnValue = Standard_False;
652 MKCP.Add(RES, aShape);
655 StlAPI_Writer myStlWriter;
656 myStlWriter.Write(RES, aFileName);
662 //======================================================================
666 //======================================================================
668 void CImportExport::SaveVRML(const Handle(AIS_InteractiveContext)& anInteractiveContext)
670 anInteractiveContext->InitCurrent();
671 if (anInteractiveContext->NbCurrents() == 0){
672 AfxMessageBox (L"No shape selected for export!");
675 Handle(Quantity_HArray1OfColor) anArrayOfColors;
676 Handle(TColStd_HArray1OfReal) anArrayOfTransparencies;
677 CImportExport::SaveVRML(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies),
678 anArrayOfColors, anArrayOfTransparencies);
681 Standard_Boolean CImportExport::SaveVRML(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
682 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
683 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies)
685 CFileDialog dlg(FALSE, L"*.vrml", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
686 L"vrml Files (*.vrml)|*.vrml;|vrm Files (*.vrm)|*.vrm;||", NULL);
688 CString SHAREPATHValue;
689 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
690 CString initdir = (SHAREPATHValue + "\\vrml");
692 dlg.m_ofn.lpstrInitialDir = initdir;
694 Standard_Boolean result = Standard_False;
696 if (dlg.DoModal() == IDOK) {
697 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
698 TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
699 TCollection_AsciiString aFileName (aFileNameW, '?');
700 TCollection_AsciiString Message;
701 result = SaveVRML (aFileName.ToCString(), aHSequenceOfShape, anArrayOfColors, anArrayOfTransparencies, Message);
702 CString aMsg (Message.ToCString());
703 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, aMsg, result ? L"CasCade" : L"CasCade Error", result ? MB_OK : MB_ICONERROR);
704 SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
709 Standard_Boolean CImportExport::SaveVRML(const Standard_CString& aFileName,
710 const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
711 const Handle(Quantity_HArray1OfColor)& anArrayOfColors,
712 const Handle(TColStd_HArray1OfReal)& anArrayOfTransparencies,
713 TCollection_AsciiString& ReturnMessage)
715 Standard_Boolean ReturnValue = Standard_True;
716 if (aHSequenceOfShape->Length() == 0)
718 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
719 return Standard_False;
722 ReturnMessage += "The Object has been saved in the file ";
723 ReturnMessage += aFileName;
724 ReturnMessage += "\n with the names : ";
727 VrmlData_Scene scene;
728 VrmlData_ShapeConvert converter(scene/*, 0.001*/); // from mm to meters
729 Standard_Integer iShape = 1; // Counter of shapes
731 for (int i = 1; i <= aHSequenceOfShape->Length(); i++)
734 TopoDS_Shape shape = aHSequenceOfShape->Value(i);
737 ReturnMessage += " Error : Invalid shape \n";
738 ReturnValue = Standard_False;
743 Quantity_Color color; // yellow
744 if (!anArrayOfColors.IsNull())
745 color = anArrayOfColors->Value(i);
748 Standard_Real transparency = 0.0;
749 if (!anArrayOfTransparencies.IsNull())
750 transparency = anArrayOfTransparencies->Value(i);
752 // Give a name to the shape.
753 TCollection_AsciiString name("Shape");
754 name += TCollection_AsciiString(iShape++);
755 converter.AddShape(shape, name.ToCString());
756 ReturnMessage += name;
757 ReturnMessage += '\n';
759 // Check presence of faces in the shape.
760 TopExp_Explorer expl(shape, TopAbs_FACE);
762 converter.Convert(true, false, 0.01); // faces only
764 converter.Convert(false, true, 0.01); // edges only
766 // Name of the color & transparency.
767 // It will be uniquely saved in VRML file.
768 TCollection_AsciiString cname = Quantity_Color::StringName(color.Name());
769 cname += transparency;
771 // Make the appearance (VRML attribute)
772 Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(scene.FindNode(cname.ToCString()));
773 if (appearance.IsNull())
775 // Not found ... create a new one.
776 Handle(VrmlData_Material) material = new VrmlData_Material(scene, cname.ToCString(), 0.2, 0.2, transparency);
777 material->SetDiffuseColor(color);
778 material->SetEmissiveColor(color);
779 material->SetSpecularColor(color);
780 scene.AddNode(material, false);
781 appearance = new VrmlData_Appearance(scene, cname.ToCString());
782 appearance->SetMaterial(material);
783 scene.AddNode(appearance, false);
786 // Apply the material to the shape of entity.
787 Handle(VrmlData_Group) group = Handle(VrmlData_Group)::DownCast(scene.FindNode(name.ToCString()));
790 VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
791 for (; itr.More(); itr.Next())
793 Handle(VrmlData_Node) node = itr.Value();
794 if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
796 Handle(VrmlData_ShapeNode) aShape = Handle(VrmlData_ShapeNode)::DownCast(node);
797 aShape->SetAppearance(appearance);
799 else if (itr.Value()->DynamicType() == STANDARD_TYPE(VrmlData_Group))
801 Handle(VrmlData_Group) groupc = Handle(VrmlData_Group)::DownCast(itr.Value());
802 VrmlData_ListOfNode::Iterator itrc = groupc->NodeIterator();
803 for (; itrc.More(); itrc.Next())
805 Handle(VrmlData_Node) nodec = itrc.Value();
806 if (nodec->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
808 Handle(VrmlData_ShapeNode) shapec = Handle(VrmlData_ShapeNode)::DownCast(nodec);
809 shapec->SetAppearance(appearance);
811 } // for of group nodes...
812 } // if (it is a shape node...
813 } // for of group nodes...
814 } // if (!group.IsNull...
815 } // iterator of shapes
818 ofstream writer(aFileName);
822 /* Old approach to store shapes in VRML (without color & transparency).
825 MKCP.MakeCompound(RES);
827 for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
829 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
830 TCollection_AsciiString anObjectName("anObjectName_");
832 ReturnMessage += anObjectName;
833 ReturnMessage += " \n";
835 if ( aShape.IsNull() )
837 ReturnMessage += " Error : Invalid shape \n";
838 ReturnValue = Standard_False;
842 MKCP.Add(RES, aShape);
845 VrmlAPI_Writer myVrmlWriter;
846 myVrmlWriter.Write(RES, aFileName);