0031570: Samples - add Qt samples similar to standard MFC samples
[occt.git] / samples / OCCTOverview / code / DataExchangeSamples.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of the examples of the Open CASCADE Technology software library.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
21
22 #include "DataExchangeSamples.h"
23
24 #include "MakeBottle.h"
25
26 #include <AIS_ViewCube.hxx>
27 #include <AIS_Shape.hxx>
28 #include <BRepTools.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRep_Builder.hxx>
31 #include <Geom_Surface.hxx>
32 #include <Geom_Plane.hxx>
33 #include <Geom_Line.hxx>
34 #include <Graphic3d_TextureEnv.hxx>
35 #include <IFSelect_ReturnStatus.hxx>
36 #include <IGESControl_Controller.hxx>
37 #include <IGESControl_Reader.hxx>
38 #include <IGESControl_Writer.hxx>
39 #include <Interface_Static.hxx>
40 #include <STEPControl_Reader.hxx>
41 #include <STEPControl_Writer.hxx>
42 #include <StlAPI_Writer.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopoDS.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopTools_HSequenceOfShape.hxx>
48 #include <VrmlAPI_Writer.hxx>
49
50 void DataExchangeSamples::Process (const TCollection_AsciiString& theSampleName)
51 {
52   if (IsImportSample(theSampleName))
53   {
54     myObject3d.Clear();
55   }
56   myObject2d.Clear();
57   myCode.Clear();
58   myIsProcessed = Standard_False;
59   try
60   {
61     ExecuteSample(theSampleName);
62   }
63   catch (...)
64   {
65     TraceError(TCollection_AsciiString("Error in sample: ") + theSampleName);
66   }
67 }
68
69 void DataExchangeSamples::AppendBottle()
70 {
71   TopoDS_Shape aBottle = MakeBottle(50, 70, 30);
72   Handle(AIS_InteractiveObject) aShape = new AIS_Shape(aBottle);
73   myObject3d.Append(aShape);
74   Handle(AIS_ViewCube) aViewCube = new AIS_ViewCube();
75   myObject3d.Append(aViewCube);
76   myResult << "A bottle shape was created." << std::endl;
77 }
78
79 void DataExchangeSamples::ExecuteSample (const TCollection_AsciiString& theSampleName)
80 {
81   Standard_Boolean anIsSamplePresent = Standard_True;
82   FindSourceCode(theSampleName);
83   if (theSampleName == "BrepExportSample")
84   {
85     BrepExportSample();
86   }
87   else if (theSampleName == "StepExportSample")
88   {
89     StepExportSample();
90   }
91   else if (theSampleName == "IgesExportSample")
92   {
93     IgesExportSample();
94   }
95   else if (theSampleName == "StlExportSample")
96   {
97     StlExportSample();
98   }
99   else if (theSampleName == "VrmlExportSample")
100   {
101     VrmlExportSample();
102   }
103   else if (theSampleName == "ImageExportSample")
104   {
105     ImageExportSample();
106   }
107   else if (theSampleName == "BrepImportSample")
108   {
109     BrepImportSample();
110   }
111   else if (theSampleName == "StepImportSample")
112   {
113     StepImportSample();
114   }
115   else if (theSampleName == "IgesImportSample")
116   {
117     IgesImportSample();
118   }
119   else
120   {
121     myResult << "No function found: " << theSampleName;
122     myCode += TCollection_AsciiString("No function found: ") + theSampleName;
123     anIsSamplePresent = Standard_False;
124   }
125   myIsProcessed = anIsSamplePresent;
126 }
127
128 void DataExchangeSamples::BrepExportSample()
129 {
130   Standard_Boolean anIsShapeExist = Standard_False;
131   for (Handle(AIS_InteractiveObject) anObject : myObject3d)
132   {
133     if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
134     {
135       anIsShapeExist = Standard_True;
136       if (BRepTools::Write(aShape->Shape(), myFileName.ToCString()))
137       {
138         myResult << "A shape was successfully written" << std::endl;
139       }
140       else
141       {
142         myResult << "A shape was not written" << std::endl;
143       }
144       break; // write only one shape
145     }
146   }
147   if (!anIsShapeExist)
148   {
149     myResult << "A shape does not exist" << std::endl;
150   }
151 }
152
153 void DataExchangeSamples::StepExportSample()
154 {
155   if (myStepType < 0)
156   {
157     myResult << "Unknown step type" << std::endl;
158     return;
159   }
160
161   IFSelect_ReturnStatus aStatus;
162
163   if (myStepType == STEPControl_FacetedBrep && !CheckFacetedBrep())
164   {
165     myResult << "At least one shape doesn't contain facets" << std::endl;
166     return;
167   }
168
169   STEPControl_Writer aStepWriter;
170   for (Handle(AIS_InteractiveObject) anObject : myObject3d)
171   {
172     if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
173     {
174       aStatus = aStepWriter.Transfer(aShape->Shape(), myStepType);
175       if (aStatus != IFSelect_RetDone)
176       {
177         myResult << "A shape was not transferred successfully" << std::endl;
178         return;
179       }
180     }
181   }
182   aStatus = aStepWriter.Write(myFileName.ToCString());
183
184   switch (aStatus)
185   {
186     case IFSelect_RetError:
187       myResult << "Incorrect Data." << std::endl;
188       break;
189     case IFSelect_RetFail:
190       myResult << "Writing error" << std::endl;
191       break;
192     case IFSelect_RetVoid:
193       myResult << "Nothing to transfer." << std::endl;
194       break;
195     case IFSelect_RetStop:
196     case IFSelect_RetDone:
197       myResult << "A STEP file was successfully written" << std::endl;
198       break;
199   }
200
201 }
202
203 void DataExchangeSamples::IgesExportSample()
204 {
205   IGESControl_Controller::Init();
206   IGESControl_Writer anIgesWriter(Interface_Static::CVal("XSTEP.iges.unit"),
207                                   Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
208
209   Standard_Boolean anIsShapeExist = Standard_False;
210   for (Handle(AIS_InteractiveObject) anObject : myObject3d)
211   {
212     if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
213     {
214       anIsShapeExist = Standard_True;
215       anIgesWriter.AddShape(aShape->Shape());
216     }
217   }
218   if (anIsShapeExist)
219   {
220     anIgesWriter.ComputeModel();
221     if (anIgesWriter.Write(myFileName.ToCString()))
222     {
223       myResult << "A STEP file was successfully written" << std::endl;
224     }
225     else
226     {
227       myResult << "A STEP file was not written" << std::endl;
228     }
229   }
230   else
231   {
232     myResult << "Shapes do not exist" << std::endl;
233   }
234 }
235
236 void DataExchangeSamples::StlExportSample()
237 {
238   TopoDS_Compound aTopoCompound;
239   BRep_Builder aBuilder;
240   aBuilder.MakeCompound(aTopoCompound);
241
242   Standard_Boolean anIsShapeExist = Standard_False;
243   for (Handle(AIS_InteractiveObject) anObject : myObject3d)
244   {
245     if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
246     {
247       anIsShapeExist = Standard_True;
248       aBuilder.Add(aTopoCompound, aShape->Shape());
249     }
250   }
251   if (anIsShapeExist)
252   {
253     StlAPI_Writer aStlWriter;
254     if (aStlWriter.Write(aTopoCompound, myFileName.ToCString()))
255     {
256       myResult << "A STL file was successfully written" << std::endl;
257     }
258     else
259     {
260       myResult << "A STL file was not written" << std::endl;
261     }
262   }
263   else
264   {
265     myResult << "Shapes do not exist" << std::endl;
266   }
267 }
268
269 void DataExchangeSamples::VrmlExportSample()
270 {
271   TopoDS_Compound aTopoCompound;
272   BRep_Builder aBrepBuilder;
273   aBrepBuilder.MakeCompound(aTopoCompound);
274
275   Standard_Boolean anIsShapeExist = Standard_False;
276   for (Handle(AIS_InteractiveObject) anObject : myObject3d)
277   {
278     if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
279     {
280       anIsShapeExist = Standard_True;
281       aBrepBuilder.Add(aTopoCompound, aShape->Shape());
282     }
283   }
284   if (anIsShapeExist)
285   {
286     VrmlAPI_Writer aVrmlWriter;
287     if (aVrmlWriter.Write(aTopoCompound, myFileName.ToCString()))
288     {
289       myResult << "A VRML file was successfully written" << std::endl;
290     }
291     else
292     {
293       myResult << "A VRML file was not written" << std::endl;
294     }
295   }
296   else
297   {
298     myResult << "Shapes do not exist" << std::endl;
299   }
300 }
301
302 void DataExchangeSamples::ImageExportSample()
303 {
304   if (myView)
305   {
306     Standard_Boolean aResult = myView->Dump(myFileName.ToCString());
307     if (aResult)
308     {
309       myResult << "An image file was successfully written" << std::endl;
310     }
311     else
312     {
313       myResult << "An image file was not written" << std::endl;
314     }
315   }
316 }
317
318 void DataExchangeSamples::BrepImportSample()
319 {
320   TopoDS_Shape aTopoShape;
321   BRep_Builder aBuilder;
322   Standard_Boolean aResult = BRepTools::Read(aTopoShape, myFileName.ToCString(), aBuilder);
323   if (aResult)
324   {
325     Handle(AIS_Shape) anAisShape = new AIS_Shape(aTopoShape);
326     myObject3d.Append(anAisShape);
327     myResult << "A BREP file was read successfully" << std::endl;
328   }
329   else
330   {
331     myResult << "A BREP file was not read successfully" << std::endl;
332   }
333 }
334
335 void DataExchangeSamples::StepImportSample()
336 {
337   Handle(TopTools_HSequenceOfShape) aSequence = new TopTools_HSequenceOfShape();
338   STEPControl_Reader aReader;
339   const IFSelect_ReturnStatus aStatus = aReader.ReadFile(myFileName.ToCString());
340   if (aStatus != IFSelect_RetDone)
341   {
342     myResult << "A BREP file was not read successfully" << std::endl;
343     return;
344   }
345
346   bool anIsFailsOnly = false;
347   aReader.PrintCheckLoad(anIsFailsOnly, IFSelect_ItemsByEntity);
348
349   int aRootsNumber = aReader.NbRootsForTransfer();
350   aReader.PrintCheckTransfer(anIsFailsOnly, IFSelect_ItemsByEntity);
351   for (Standard_Integer i = 1; i <= aRootsNumber; i++)
352   {
353     aReader.TransferRoot(i);
354   }
355
356   int aShapesNumber = aReader.NbShapes();
357   for (int i = 1; i <= aShapesNumber; i++)
358   {
359     TopoDS_Shape aTopoShape = aReader.Shape(i);
360     Handle(AIS_Shape) anAisShape = new AIS_Shape(aTopoShape);
361     myObject3d.Append(anAisShape);
362   }
363   myResult << "A STEP file was read successfully" << std::endl;
364 }
365
366 void DataExchangeSamples::IgesImportSample()
367 {
368   IGESControl_Reader aReader;
369   int status = aReader.ReadFile (myFileName.ToCString());
370   if (status != IFSelect_RetDone)
371   {
372     myResult << "A IGES file was not read successfully" << std::endl;
373     return;
374   }
375   aReader.TransferRoots();
376   TopoDS_Shape aTopoShape = aReader.OneShape();
377   Handle(AIS_Shape) anAisShape = new AIS_Shape (aTopoShape);
378   myObject3d.Append(anAisShape);
379   myResult << "A IGES file was read successfully" << std::endl;
380 }
381
382 Standard_Boolean DataExchangeSamples::CheckFacetedBrep()
383 {
384   Standard_Boolean anError = Standard_False;
385   for (Handle(AIS_InteractiveObject) anObject : myObject3d)
386   {
387     if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
388     {
389       const TopoDS_Shape aTopoShape = aShape->Shape();
390       for (TopExp_Explorer aFaceExplorer(aTopoShape, TopAbs_FACE); aFaceExplorer.More() && !anError; aFaceExplorer.Next())
391       {
392         Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(aFaceExplorer.Current()));
393         if (!aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
394         {
395           anError = Standard_True;
396         }
397       }
398       for (TopExp_Explorer anEdgeExplorer(aTopoShape, TopAbs_EDGE); anEdgeExplorer.More() && !anError; anEdgeExplorer.Next())
399       {
400         Standard_Real fd, ld;
401         Handle(Geom_Curve) curve = BRep_Tool::Curve(TopoDS::Edge(anEdgeExplorer.Current()), fd, ld);
402         if (!curve->IsKind(STANDARD_TYPE(Geom_Line)))
403         {
404           anError = Standard_True;
405         }
406       }
407     }
408   }
409   return !anError;
410 }