3945746d61bfd8dc9ec883dfd9da19ffc4d69e09
[occt.git] / samples / mfc / standard / Common / ImportExport / ImportExport.cpp
1 // ImportExport.cpp: implementation of the CImportExport class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "ImportExport.h"
7 #include <OCC_App.h>
8
9 #include "SaveSTEPDlg.h"
10
11 #include "TColStd_SequenceOfAsciiString.hxx"
12 #include "TColStd_SequenceOfExtendedString.hxx"
13 #include "OSD_Timer.hxx"
14
15 #include "IGESControl_Reader.hxx"
16 #include "STEPControl_Controller.hxx"
17
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>
36
37 #include <XSControl_WorkSession.hxx>
38 #include <STEPConstruct_Styles.hxx>
39 #include <TColStd_HSequenceOfTransient.hxx>
40 #include <STEPConstruct.hxx>
41 #include <StepVisual_StyledItem.hxx>
42
43 #ifdef _DEBUG
44 #undef THIS_FILE
45 static char THIS_FILE[]=__FILE__;
46 //#define new DEBUG_NEW
47 #endif
48
49 //////////////////////////////////////////////////////////////////////
50 // Construction/Destruction
51 //////////////////////////////////////////////////////////////////////
52
53 Handle(TopTools_HSequenceOfShape) CImportExport::BuildSequenceFromContext(const Handle(AIS_InteractiveContext)& anInteractiveContext,
54                                                                           Handle(Quantity_HArray1OfColor)&      anArrayOfColors,
55                                                                           Handle(TColStd_HArray1OfReal)&        anArrayOfTransparencies) 
56 {
57     Handle(TopTools_HSequenceOfShape) aSequence;
58     Standard_Integer nb = anInteractiveContext->NbCurrents(), i = 1;
59     if (!nb)
60         return aSequence;
61
62     aSequence               = new TopTools_HSequenceOfShape();
63     anArrayOfColors         = new Quantity_HArray1OfColor(1, nb);
64     anArrayOfTransparencies = new TColStd_HArray1OfReal  (1, nb);
65
66     Handle(AIS_InteractiveObject) picked;
67     for(anInteractiveContext->InitCurrent();anInteractiveContext->MoreCurrent();anInteractiveContext->NextCurrent())
68       {
69         picked = anInteractiveContext->Current();
70         if (anInteractiveContext->Current()->IsKind(STANDARD_TYPE(AIS_Shape)))
71              {
72             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(picked);
73                 TopoDS_Shape aShape = aisShape->Shape();
74             aSequence->Append(aShape);
75             
76             Quantity_Color color;
77             aisShape->Color(color);
78             anArrayOfColors->SetValue(i, color);
79             
80             Standard_Real transparency = aisShape->Transparency();
81             anArrayOfTransparencies->SetValue(i, transparency);
82
83             i++;
84              }
85       }
86       return aSequence;
87 }
88
89 //======================================================================
90 //=                                                                    =
91 //=                      BREP                                          =
92 //=                                                                    =
93 //======================================================================
94
95 int CImportExport::ReadBREP (const Handle(AIS_InteractiveContext)& anInteractiveContext)
96 {
97     Handle(TopTools_HSequenceOfShape) aSequence = CImportExport::ReadBREP();
98         if(aSequence->IsEmpty())
99                 return 1;
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);
106         } 
107         return 0;
108 }
109
110 Handle(TopTools_HSequenceOfShape) CImportExport::ReadBREP()
111 {
112   CFileDialog dlg(TRUE,
113                   NULL,
114                   NULL,
115                   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
116                   L"BREP Files (*.brep , *.rle)|*.brep;  *.BREP; *.rle; *.RLE; |All Files (*.*)|*.*||",
117                   NULL ); 
118
119   CString SHAREPATHValue;
120   SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
121   CString initdir = (SHAREPATHValue + "\\occ");
122
123   dlg.m_ofn.lpstrInitialDir = initdir;
124
125   Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
126
127   if (dlg.DoModal() == IDOK) 
128   {
129     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT));
130     CString filename = dlg.GetPathName();
131     TopoDS_Shape aShape;
132     Standard_Boolean result = ReadBREP (filename, aShape);
133     if (result)
134     {
135       if (!BRepAlgo::IsValid(aShape))
136         MessageBoxW (AfxGetMainWnd()->m_hWnd, L"Warning: The shape is not valid!", L"Cascade Warning", MB_ICONWARNING);
137
138       aSequence->Append(aShape);
139     }
140     else 
141       MessageBoxW (AfxGetMainWnd()->m_hWnd, L"Error: The file was not read", L"Cascade Error", MB_ICONERROR);
142
143     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
144   }
145
146   return aSequence;
147 }
148 //----------------------------------------------------------------------
149
150 Standard_Boolean CImportExport::ReadBREP(CString      aFileName,
151                                         TopoDS_Shape& aShape)
152 {
153   aShape.Nullify();
154
155   std::filebuf aFileBuf;
156   std::istream aStream (&aFileBuf);
157   if (!aFileBuf.open (aFileName, ios::in))
158   {
159     return Standard_False;
160   }
161
162   BRep_Builder aBuilder;
163   BRepTools::Read (aShape, aStream, aBuilder);
164   return !aShape.IsNull();
165 }
166
167 void CImportExport::SaveBREP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
168 {
169         anInteractiveContext->InitCurrent();
170         if (anInteractiveContext->NbCurrents() == 0){
171                 AfxMessageBox (L"No shape selected for export!");
172                 return;
173         }
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();
179         if (aLength == 1){
180                 TopoDS_Shape RES = aHSequenceOfShape->Value(1);
181                 CImportExport::SaveBREP(RES);
182         } else {
183                 TopoDS_Compound RES;
184                 BRep_Builder MKCP;
185                 MKCP.MakeCompound(RES);
186                 for (Standard_Integer i=1;i<=aLength;i++)
187                 {
188                         TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
189                         if ( aShape.IsNull() ) 
190                         {
191                                 continue;
192                         }
193                         MKCP.Add(RES, aShape);
194                 }
195                 CImportExport::SaveBREP(RES);
196         }
197 }
198
199 Standard_Boolean CImportExport::SaveBREP(const TopoDS_Shape& aShape)
200 {
201   CFileDialog dlg (FALSE, L"*.brep",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
202                    L"BREP Files (*.brep)|*.brep;|BREP Files (*.BREP)|*.BREP;||", NULL);
203   
204 CString SHAREPATHValue;
205 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
206 CString initdir = (SHAREPATHValue + "\\occ");
207
208 dlg.m_ofn.lpstrInitialDir = initdir;
209
210   Standard_Boolean result = Standard_False; 
211   if (dlg.DoModal() == IDOK)  
212   { 
213     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT)); 
214     CString filename = dlg.GetPathName(); 
215     result = SaveBREP (filename, aShape);
216     if (!result)  
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)); 
221   } 
222   return result;
223 }
224
225 //----------------------------------------------------------------------------------------
226 Standard_Boolean CImportExport::SaveBREP (CString             aFileName,
227                                           const TopoDS_Shape& aShape)
228 {
229   std::filebuf aFileBuf;
230   std::ostream aStream (&aFileBuf);
231   if (!aFileBuf.open (aFileName, ios::out))
232   {
233     return Standard_False;
234   }
235
236   BRepTools::Write (aShape, aStream); 
237   return Standard_True;
238 }
239
240 //======================================================================
241 //=                                                                    =
242 //=                      IGES                                          =
243 //=                                                                    =
244 //======================================================================
245
246
247
248 void CImportExport::ReadIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
249 {
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)));
253
254 }
255
256 Handle(TopTools_HSequenceOfShape) CImportExport::ReadIGES()// not by reference --> the sequence is created here !!
257 {
258   CFileDialog dlg(TRUE,
259                   NULL,
260                   NULL,
261                   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
262                   L"IGES Files (*.iges , *.igs)|*.iges; *.igs|All Files (*.*)|*.*||",
263                   NULL );
264
265 CString SHAREPATHValue;
266 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
267 CString initdir = (SHAREPATHValue + "\\iges");
268
269 dlg.m_ofn.lpstrInitialDir = initdir;
270   
271   Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();
272   if (dlg.DoModal() == IDOK) 
273   {
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)
279     {
280       MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Error : The file is not read", L"CasCade Error", MB_ICONERROR);
281     }
282
283         SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
284    }
285   return aSequence;
286 }
287 //----------------------------------------------------------------------
288
289 Standard_Integer CImportExport::ReadIGES(const Standard_CString& aFileName,
290                                          Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
291 {
292
293     IGESControl_Reader Reader;
294
295     Standard_Integer status = Reader.ReadFile(aFileName);
296
297     if (status != IFSelect_RetDone) return status;
298     Reader.TransferRoots();
299     TopoDS_Shape aShape = Reader.OneShape();     
300         aHSequenceOfShape->Append(aShape);
301
302     return status;
303 }
304 //----------------------------------------------------------------------
305
306 void CImportExport::SaveIGES(const Handle(AIS_InteractiveContext)& anInteractiveContext)
307 {
308         anInteractiveContext->InitCurrent();
309         if (anInteractiveContext->NbCurrents() == 0){
310                 AfxMessageBox (L"No shape selected for export!");
311                 return;
312         }
313     Handle(Quantity_HArray1OfColor)   anArrayOfColors;
314     Handle(TColStd_HArray1OfReal)     anArrayOfTransparencies;
315     CImportExport::SaveIGES(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
316 }
317
318 Standard_Boolean CImportExport::SaveIGES(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
319 {
320     if (aHSequenceOfShape->Length() == 0)
321     {
322         MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
323         return Standard_False;
324     }
325
326   CFileDialog dlg(FALSE, L"*.iges",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
327                   L"IGES Files (*.iges )|*.iges;|IGES Files (*.igs )| *.igs;||", NULL);
328
329 CString SHAREPATHValue;
330 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
331 CString initdir = (SHAREPATHValue + "\\iges");
332
333 dlg.m_ofn.lpstrInitialDir = initdir;
334   
335   Standard_Boolean result=Standard_False;
336   if (dlg.DoModal() == IDOK)  
337   { 
338     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_WAIT)); 
339
340     TCollection_ExtendedString aFileNameW ((Standard_ExtString )(const wchar_t* )dlg.GetPathName());
341     TCollection_AsciiString    aFileName  (aFileNameW, '?');
342
343     result = SaveIGES (aFileName.ToCString(), aHSequenceOfShape);
344     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
345    }
346     return result;
347 }
348 //----------------------------------------------------------------------
349
350 Standard_Boolean CImportExport::SaveIGES(const Standard_CString& aFileName,
351                                          const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
352 {
353
354     IGESControl_Controller::Init();
355         IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
356                Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
357         
358         for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)  
359                 ICW.AddShape (aHSequenceOfShape->Value(i));                      
360
361         ICW.ComputeModel();
362         Standard_Boolean result = ICW.Write(aFileName );
363     return result;
364 }
365
366 //======================================================================
367
368 //======================================================================
369 //=                                                                    =
370 //=                      STEP                                          =
371 //=                                                                    =
372 //======================================================================
373
374 void CImportExport::ReadSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
375 {
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);
380                 }
381 }
382
383 Handle(TopTools_HSequenceOfShape) CImportExport::ReadSTEP()// not by reference --> the sequence is created here !!
384 {
385   CFileDialog dlg(TRUE,
386                   NULL,
387                   NULL,
388                   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
389                   L"STEP Files (*.stp;*.step)|*.stp; *.step|All Files (*.*)|*.*||",
390                   NULL );
391
392 CString SHAREPATHValue;
393 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
394 CString initdir = (SHAREPATHValue + "\\step");
395
396 dlg.m_ofn.lpstrInitialDir = initdir;
397   
398   Handle(TopTools_HSequenceOfShape) aSequence= new TopTools_HSequenceOfShape();
399   if (dlg.DoModal() == IDOK) 
400   {
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) 
406     {
407        case IFSelect_RetError :
408            MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Not a valid Step file", L"ERROR", MB_ICONWARNING);
409        break;
410        case IFSelect_RetFail :
411            MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Reading has failed", L"ERROR", MB_ICONWARNING);
412        break;
413        case IFSelect_RetVoid :
414             MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Nothing to transfer", L"ERROR", MB_ICONWARNING);
415        break;
416     }
417     SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));       
418   }
419   return aSequence;
420 }
421
422 IFSelect_ReturnStatus CImportExport::ReadSTEP(const Standard_CString& aFileName,
423                                               Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
424 {
425   aHSequenceOfShape->Clear();
426
427   // create additional log file
428   STEPControl_Reader aReader;
429   IFSelect_ReturnStatus status = aReader.ReadFile(aFileName);
430   if (status != IFSelect_RetDone)
431     return status;
432
433   aReader.WS()->MapReader()->SetTraceLevel(2); // increase default trace level
434
435   Standard_Boolean failsonly = Standard_False;
436   aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
437
438   // Root transfers
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);
443   }
444
445   // Collecting resulting entities
446   Standard_Integer nbs = aReader.NbShapes();
447   if (nbs == 0) {
448     return IFSelect_RetVoid;
449   }
450   for (Standard_Integer i=1; i<=nbs; i++) {
451     aHSequenceOfShape->Append(aReader.Shape(i));
452   }
453
454   return status;
455 }
456
457
458 //----------------------------------------------------------------------
459 void CImportExport::SaveSTEP(const Handle(AIS_InteractiveContext)& anInteractiveContext)
460 {
461         anInteractiveContext->InitCurrent();
462         if (anInteractiveContext->NbCurrents() == 0){
463                 AfxMessageBox (L"No shape selected for export!");
464                 return;
465         }
466     Handle(Quantity_HArray1OfColor)   anArrayOfColors;
467     Handle(TColStd_HArray1OfReal)     anArrayOfTransparencies;
468     CImportExport::SaveSTEP(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
469 }
470
471 // Return True if no error
472 Standard_Boolean TestFacetedBrep(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
473 {
474         Standard_Boolean OneErrorFound = Standard_False;
475         for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
476         {
477           TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
478
479           TopExp_Explorer Ex(aShape,TopAbs_FACE);
480           while (Ex.More() && !OneErrorFound)
481                 {
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;
487                 Ex.Next();
488                 }
489           TopExp_Explorer Ex2(aShape,TopAbs_EDGE);
490           while (Ex2.More() && !OneErrorFound)
491                 {
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;
498                 Ex2.Next();
499                 }
500         }
501         return !OneErrorFound;
502 }
503
504 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
505 {
506     if (aHSequenceOfShape->Length() == 0)
507       {
508         MessageBox (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
509         return IFSelect_RetError;
510       }
511
512     IFSelect_ReturnStatus status = IFSelect_RetVoid;
513
514         CFileSaveSTEPDialog aDlg(NULL);
515
516         aDlg.m_Cc1ModelType = STEPControl_AsIs;
517
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, '?');
522
523                 STEPControl_StepModelType selection = aDlg.m_Cc1ModelType;
524
525         if(selection == STEPControl_FacetedBrep)
526
527         if (!TestFacetedBrep(aHSequenceOfShape))
528             {
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;
531             }
532
533
534         status =  SaveSTEP (aFileName.ToCString(), aHSequenceOfShape, selection);
535         switch (status)
536           {
537             case IFSelect_RetError:
538                 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Incorrect Data", L"ERROR", MB_ICONWARNING); 
539             break;
540             case IFSelect_RetFail:
541                 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Writing has failed", L"ERROR", MB_ICONWARNING); 
542             break;
543             case IFSelect_RetVoid:
544                 MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"Nothing to transfer", L"ERROR", MB_ICONWARNING); 
545             break;
546           }
547         SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW)); 
548   } 
549   return status;
550 }
551 //----------------------------------------------------------------------------------------
552 IFSelect_ReturnStatus CImportExport::SaveSTEP(const Standard_CString& aFileName,
553                                               const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
554
555 const STEPControl_StepModelType aValue /* =TopoDSToCc1Act_ManifoldSolidBrep */ )
556
557 {
558     // CREATE THE WRITER
559
560     STEPControl_Writer aWriter;
561
562         IFSelect_ReturnStatus status;
563         for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)  
564         {
565                         status =  aWriter.Transfer(aHSequenceOfShape->Value(i), aValue);
566             if ( status != IFSelect_RetDone ) return status;
567         }     
568     status = aWriter.Write(aFileName);
569     return status;
570 }
571
572
573
574 //======================================================================
575 //=                                                                    =
576 //=                      STL                                           =
577 //=                                                                    =
578 //======================================================================
579
580 void CImportExport::SaveSTL(const Handle(AIS_InteractiveContext)& anInteractiveContext)
581 {
582     anInteractiveContext->InitCurrent();
583         if (anInteractiveContext->NbCurrents() == 0){
584                 AfxMessageBox (L"No shape selected for export!");
585                 return;
586         }
587     Handle(Quantity_HArray1OfColor)   anArrayOfColors;
588     Handle(TColStd_HArray1OfReal)     anArrayOfTransparencies;
589         CImportExport::SaveSTL(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies));
590 }
591
592 Standard_Boolean CImportExport::SaveSTL(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape)
593 {
594   CFileDialog dlg(FALSE, L"*.stl", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
595                       L"stl Files (*.stl)|*.stl;|STL Files (*.STL)|*.STL;||", NULL);
596
597 CString SHAREPATHValue;
598 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
599 CString initdir = (SHAREPATHValue + "\\stl");
600
601 dlg.m_ofn.lpstrInitialDir = initdir;
602
603         Standard_Boolean result = Standard_False;
604
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)); 
614     } 
615   return result;
616 }
617
618 Standard_Boolean CImportExport::SaveSTL(const Standard_CString& aFileName,
619                                           const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
620                                           TCollection_AsciiString& ReturnMessage)
621 {
622         Standard_Boolean ReturnValue = Standard_True;
623     if (aHSequenceOfShape->Length() == 0)
624     {
625         MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
626         return Standard_False;
627     }
628
629     ReturnMessage += "The Object have be saved in the file ";
630     ReturnMessage += aFileName;
631     ReturnMessage += "\n with the names : ";
632
633         TopoDS_Compound RES;
634         BRep_Builder MKCP;
635         MKCP.MakeCompound(RES);
636
637         for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
638         {
639                 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
640                 TCollection_AsciiString anObjectName("anObjectName_");
641                 anObjectName += i;
642                 ReturnMessage += anObjectName;
643                 ReturnMessage += " \n";
644
645                 if ( aShape.IsNull() ) 
646                 {
647                         ReturnMessage += " Error : Invalid shape \n";
648                         ReturnValue = Standard_False;
649                         continue;
650                  }
651
652                 MKCP.Add(RES, aShape);
653         }
654
655         StlAPI_Writer myStlWriter;
656         myStlWriter.Write(RES, aFileName);
657
658     return ReturnValue;
659 }
660
661
662 //======================================================================
663 //=                                                                    =
664 //=                      VRML                                          =
665 //=                                                                    =
666 //======================================================================
667
668 void CImportExport::SaveVRML(const Handle(AIS_InteractiveContext)& anInteractiveContext)
669 {
670    anInteractiveContext->InitCurrent();
671         if (anInteractiveContext->NbCurrents() == 0){
672                 AfxMessageBox (L"No shape selected for export!");
673                 return;
674         }
675     Handle(Quantity_HArray1OfColor) anArrayOfColors;
676     Handle(TColStd_HArray1OfReal)   anArrayOfTransparencies;
677         CImportExport::SaveVRML(BuildSequenceFromContext(anInteractiveContext, anArrayOfColors, anArrayOfTransparencies),
678                             anArrayOfColors, anArrayOfTransparencies);
679 }
680
681 Standard_Boolean CImportExport::SaveVRML(const Handle(TopTools_HSequenceOfShape)& aHSequenceOfShape,
682                                          const Handle(Quantity_HArray1OfColor)&   anArrayOfColors,
683                                          const Handle(TColStd_HArray1OfReal)&     anArrayOfTransparencies)
684 {
685   CFileDialog dlg(FALSE, L"*.vrml", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
686                       L"vrml Files (*.vrml)|*.vrml;|vrm Files (*.vrm)|*.vrm;||", NULL);
687
688 CString SHAREPATHValue;
689 SHAREPATHValue.GetEnvironmentVariable (L"CSF_OCCTDataPath");
690 CString initdir = (SHAREPATHValue + "\\vrml");
691
692 dlg.m_ofn.lpstrInitialDir = initdir;
693   
694   Standard_Boolean result = Standard_False;
695
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)); 
705     } 
706   return result;
707 }
708
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)
714 {
715         Standard_Boolean ReturnValue = Standard_True;
716     if (aHSequenceOfShape->Length() == 0)
717     {
718         MessageBoxW (AfxGetApp()->m_pMainWnd->m_hWnd, L"No Shape in the HSequence!!", L"CasCade Warning", MB_ICONWARNING);
719         return Standard_False;
720     }
721
722     ReturnMessage += "The Object has been saved in the file ";
723     ReturnMessage += aFileName;
724     ReturnMessage += "\n with the names : ";
725
726     // VRML scene.
727     VrmlData_Scene scene;
728     VrmlData_ShapeConvert converter(scene/*, 0.001*/); // from mm to meters 
729     Standard_Integer iShape = 1; // Counter of shapes
730
731     for (int i = 1; i <= aHSequenceOfShape->Length(); i++)
732     {
733         // Shape
734         TopoDS_Shape shape = aHSequenceOfShape->Value(i);
735         if (shape.IsNull())
736         {
737           ReturnMessage += " Error : Invalid shape \n";
738           ReturnValue = Standard_False;
739           continue;
740         }
741
742         // Color
743         Quantity_Color color; // yellow
744         if (!anArrayOfColors.IsNull())
745             color = anArrayOfColors->Value(i);
746
747         // Transparency
748         Standard_Real transparency = 0.0;
749         if (!anArrayOfTransparencies.IsNull())
750             transparency = anArrayOfTransparencies->Value(i);
751
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';
758
759         // Check presence of faces in the shape.
760         TopExp_Explorer expl(shape, TopAbs_FACE);
761         if (expl.More())
762             converter.Convert(true, false, 0.01); // faces only
763         else
764             converter.Convert(false, true, 0.01); // edges only
765
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;
770
771         // Make the appearance (VRML attribute)
772         Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(scene.FindNode(cname.ToCString()));
773         if (appearance.IsNull())
774         {
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);
784         }
785
786         // Apply the material to the shape of entity.
787         Handle(VrmlData_Group) group = Handle(VrmlData_Group)::DownCast(scene.FindNode(name.ToCString()));
788         if (!group.IsNull())
789         {
790             VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
791             for (; itr.More(); itr.Next())
792             {
793                 Handle(VrmlData_Node) node = itr.Value();
794                 if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
795                 {
796                     Handle(VrmlData_ShapeNode) aShape = Handle(VrmlData_ShapeNode)::DownCast(node);
797                     aShape->SetAppearance(appearance);
798                 }
799                 else if (itr.Value()->DynamicType() == STANDARD_TYPE(VrmlData_Group))
800                 {
801                     Handle(VrmlData_Group) groupc = Handle(VrmlData_Group)::DownCast(itr.Value());
802                     VrmlData_ListOfNode::Iterator itrc = groupc->NodeIterator();
803                     for (; itrc.More(); itrc.Next())
804                     {
805                         Handle(VrmlData_Node) nodec = itrc.Value();
806                         if (nodec->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
807                         {
808                             Handle(VrmlData_ShapeNode) shapec = Handle(VrmlData_ShapeNode)::DownCast(nodec);
809                             shapec->SetAppearance(appearance);
810                         }
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
816
817     // Call VRML writer
818     ofstream writer(aFileName);
819     writer<<scene;
820     writer.close();
821
822     /* Old approach to store shapes in VRML (without color & transparency).
823         TopoDS_Compound RES;
824         BRep_Builder MKCP;
825         MKCP.MakeCompound(RES);
826
827         for (Standard_Integer i=1;i<=aHSequenceOfShape->Length();i++)
828         {
829                 TopoDS_Shape aShape= aHSequenceOfShape->Value(i);
830                 TCollection_AsciiString anObjectName("anObjectName_");
831                 anObjectName += i;
832                 ReturnMessage += anObjectName;
833                 ReturnMessage += " \n";
834
835                 if ( aShape.IsNull() ) 
836                 {
837                         ReturnMessage += " Error : Invalid shape \n";
838                         ReturnValue = Standard_False;
839                         continue;
840                  }
841
842                 MKCP.Add(RES, aShape);
843         }
844
845         VrmlAPI_Writer myVrmlWriter;
846         myVrmlWriter.Write(RES, aFileName);
847     */
848
849     return ReturnValue;
850 }
851
852
853
854