1 // Copyright (c) 2020 OPEN CASCADE SAS
3 // This file is part of the examples of the Open CASCADE Technology software library.
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:
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
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
22 #include "DataExchangeSamples.h"
24 #include "MakeBottle.h"
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>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopTools_HSequenceOfShape.hxx>
48 #include <VrmlAPI_Writer.hxx>
50 void DataExchangeSamples::Process (const TCollection_AsciiString& theSampleName)
52 if (IsImportSample(theSampleName))
58 myIsProcessed = Standard_False;
61 ExecuteSample(theSampleName);
65 TraceError(TCollection_AsciiString("Error in sample: ") + theSampleName);
69 void DataExchangeSamples::AppendBottle()
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;
79 void DataExchangeSamples::ExecuteSample (const TCollection_AsciiString& theSampleName)
81 Standard_Boolean anIsSamplePresent = Standard_True;
82 FindSourceCode(theSampleName);
83 if (theSampleName == "BrepExportSample")
87 else if (theSampleName == "StepExportSample")
91 else if (theSampleName == "IgesExportSample")
95 else if (theSampleName == "StlExportSample")
99 else if (theSampleName == "VrmlExportSample")
103 else if (theSampleName == "ImageExportSample")
107 else if (theSampleName == "BrepImportSample")
111 else if (theSampleName == "StepImportSample")
115 else if (theSampleName == "IgesImportSample")
121 myResult << "No function found: " << theSampleName;
122 myCode += TCollection_AsciiString("No function found: ") + theSampleName;
123 anIsSamplePresent = Standard_False;
125 myIsProcessed = anIsSamplePresent;
128 void DataExchangeSamples::BrepExportSample()
130 Standard_Boolean anIsShapeExist = Standard_False;
131 for (Handle(AIS_InteractiveObject) anObject : myObject3d)
133 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
135 anIsShapeExist = Standard_True;
136 if (BRepTools::Write(aShape->Shape(), myFileName.ToCString()))
138 myResult << "A shape was successfully written" << std::endl;
142 myResult << "A shape was not written" << std::endl;
144 break; // write only one shape
149 myResult << "A shape does not exist" << std::endl;
153 void DataExchangeSamples::StepExportSample()
157 myResult << "Unknown step type" << std::endl;
161 IFSelect_ReturnStatus aStatus;
163 if (myStepType == STEPControl_FacetedBrep && !CheckFacetedBrep())
165 myResult << "At least one shape doesn't contain facets" << std::endl;
169 STEPControl_Writer aStepWriter;
170 for (Handle(AIS_InteractiveObject) anObject : myObject3d)
172 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
174 aStatus = aStepWriter.Transfer(aShape->Shape(), myStepType);
175 if (aStatus != IFSelect_RetDone)
177 myResult << "A shape was not transferred successfully" << std::endl;
182 aStatus = aStepWriter.Write(myFileName.ToCString());
186 case IFSelect_RetError:
187 myResult << "Incorrect Data." << std::endl;
189 case IFSelect_RetFail:
190 myResult << "Writing error" << std::endl;
192 case IFSelect_RetVoid:
193 myResult << "Nothing to transfer." << std::endl;
195 case IFSelect_RetStop:
196 case IFSelect_RetDone:
197 myResult << "A STEP file was successfully written" << std::endl;
203 void DataExchangeSamples::IgesExportSample()
205 IGESControl_Controller::Init();
206 IGESControl_Writer anIgesWriter(Interface_Static::CVal("XSTEP.iges.unit"),
207 Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
209 Standard_Boolean anIsShapeExist = Standard_False;
210 for (Handle(AIS_InteractiveObject) anObject : myObject3d)
212 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
214 anIsShapeExist = Standard_True;
215 anIgesWriter.AddShape(aShape->Shape());
220 anIgesWriter.ComputeModel();
221 if (anIgesWriter.Write(myFileName.ToCString()))
223 myResult << "A STEP file was successfully written" << std::endl;
227 myResult << "A STEP file was not written" << std::endl;
232 myResult << "Shapes do not exist" << std::endl;
236 void DataExchangeSamples::StlExportSample()
238 TopoDS_Compound aTopoCompound;
239 BRep_Builder aBuilder;
240 aBuilder.MakeCompound(aTopoCompound);
242 Standard_Boolean anIsShapeExist = Standard_False;
243 for (Handle(AIS_InteractiveObject) anObject : myObject3d)
245 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
247 anIsShapeExist = Standard_True;
248 aBuilder.Add(aTopoCompound, aShape->Shape());
253 StlAPI_Writer aStlWriter;
254 if (aStlWriter.Write(aTopoCompound, myFileName.ToCString()))
256 myResult << "A STL file was successfully written" << std::endl;
260 myResult << "A STL file was not written" << std::endl;
265 myResult << "Shapes do not exist" << std::endl;
269 void DataExchangeSamples::VrmlExportSample()
271 TopoDS_Compound aTopoCompound;
272 BRep_Builder aBrepBuilder;
273 aBrepBuilder.MakeCompound(aTopoCompound);
275 Standard_Boolean anIsShapeExist = Standard_False;
276 for (Handle(AIS_InteractiveObject) anObject : myObject3d)
278 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
280 anIsShapeExist = Standard_True;
281 aBrepBuilder.Add(aTopoCompound, aShape->Shape());
286 VrmlAPI_Writer aVrmlWriter;
287 if (aVrmlWriter.Write(aTopoCompound, myFileName.ToCString()))
289 myResult << "A VRML file was successfully written" << std::endl;
293 myResult << "A VRML file was not written" << std::endl;
298 myResult << "Shapes do not exist" << std::endl;
302 void DataExchangeSamples::ImageExportSample()
306 Standard_Boolean aResult = myView->Dump(myFileName.ToCString());
309 myResult << "An image file was successfully written" << std::endl;
313 myResult << "An image file was not written" << std::endl;
318 void DataExchangeSamples::BrepImportSample()
320 TopoDS_Shape aTopoShape;
321 BRep_Builder aBuilder;
322 Standard_Boolean aResult = BRepTools::Read(aTopoShape, myFileName.ToCString(), aBuilder);
325 Handle(AIS_Shape) anAisShape = new AIS_Shape(aTopoShape);
326 myObject3d.Append(anAisShape);
327 myResult << "A BREP file was read successfully" << std::endl;
331 myResult << "A BREP file was not read successfully" << std::endl;
335 void DataExchangeSamples::StepImportSample()
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)
342 myResult << "A BREP file was not read successfully" << std::endl;
346 bool anIsFailsOnly = false;
347 aReader.PrintCheckLoad(anIsFailsOnly, IFSelect_ItemsByEntity);
349 int aRootsNumber = aReader.NbRootsForTransfer();
350 aReader.PrintCheckTransfer(anIsFailsOnly, IFSelect_ItemsByEntity);
351 for (Standard_Integer i = 1; i <= aRootsNumber; i++)
353 aReader.TransferRoot(i);
356 int aShapesNumber = aReader.NbShapes();
357 for (int i = 1; i <= aShapesNumber; i++)
359 TopoDS_Shape aTopoShape = aReader.Shape(i);
360 Handle(AIS_Shape) anAisShape = new AIS_Shape(aTopoShape);
361 myObject3d.Append(anAisShape);
363 myResult << "A STEP file was read successfully" << std::endl;
366 void DataExchangeSamples::IgesImportSample()
368 IGESControl_Reader aReader;
369 int status = aReader.ReadFile (myFileName.ToCString());
370 if (status != IFSelect_RetDone)
372 myResult << "A IGES file was not read successfully" << std::endl;
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;
382 Standard_Boolean DataExchangeSamples::CheckFacetedBrep()
384 Standard_Boolean anError = Standard_False;
385 for (Handle(AIS_InteractiveObject) anObject : myObject3d)
387 if (Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(anObject))
389 const TopoDS_Shape aTopoShape = aShape->Shape();
390 for (TopExp_Explorer aFaceExplorer(aTopoShape, TopAbs_FACE); aFaceExplorer.More() && !anError; aFaceExplorer.Next())
392 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(aFaceExplorer.Current()));
393 if (!aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
395 anError = Standard_True;
398 for (TopExp_Explorer anEdgeExplorer(aTopoShape, TopAbs_EDGE); anEdgeExplorer.More() && !anError; anEdgeExplorer.Next())
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)))
404 anError = Standard_True;