Salome HOME
1c742269f323a939476c79e3e4dcbf5ec7a8e362
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_STLExport.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_STLExport.h"
21
22 #include "GeomAlgoAPI_Tools.h"
23
24 #include <TopoDS_Shape.hxx>
25
26 // OOCT includes
27 #include <BRepBuilderAPI_Copy.hxx>
28 #include <StlAPI_Writer.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <Bnd_Box.hxx>
31 #include <BRepBndLib.hxx>
32 #include <BRepTools.hxx>
33 #include <BRepMesh_IncrementalMesh.hxx>
34
35
36
37 #define MAX2(X, Y)      ( Abs(X) > Abs(Y) ? Abs(X) : Abs(Y) )
38 #define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X, Y) , Z ) )
39
40 bool STLExport(const std::string& theFileName,
41                const std::shared_ptr<GeomAPI_Shape>& theShape,
42                const double theDeflection,
43                const bool theIsRelative,
44                const bool theIsASCII,
45                std::string& theError)
46 {
47   #ifdef _DEBUG
48   std::cout << "Export STl into file " << theFileName << std::endl;
49   #endif
50
51   if (!theShape.get()) {
52     theError = "STl Export failed: An invalid argument";
53     return false;
54   }
55
56   try
57   {
58
59     double aDeflection = theDeflection;
60     StlAPI_Writer aWriter;
61     // copy source shape
62     BRepBuilderAPI_Copy aCopy( theShape->impl<TopoDS_Shape>(), Standard_False );
63     TopoDS_Shape aCopyShape = aCopy.Shape();
64     // ASCII mode
65     aWriter.ASCIIMode() = theIsASCII;
66     if ( theIsRelative ) {
67       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
68       Bnd_Box aBndBox;
69       BRepBndLib::Add( theShape->impl<TopoDS_Shape>(), aBndBox );
70       aBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
71       aDeflection = MAX3( aXmax-aXmin, aYmax-aYmin, aZmax-aZmin ) * theDeflection;
72     }
73     //Compute triangulation
74     BRepTools::Clean( aCopyShape );
75     BRepMesh_IncrementalMesh aMesh( aCopyShape, aDeflection );
76
77     if (!aWriter.Write( aCopyShape, theFileName.c_str())) {
78       theError = "STL Export failed";
79       return false;
80     }
81     return true;
82   }
83   catch( Standard_Failure )
84   {
85     theError = "Exception catched in STlExport";
86   }
87   return false;
88 }