]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_STLExport.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/CEA_2020/Lot1_Export_STL' into CEA_2020_Lot1
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_STLExport.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
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::string& /*theFormatName*/,
42                 const std::shared_ptr<GeomAPI_Shape>& theShape,
43                 const double aDeflection,
44                 const bool anIsRelative, 
45                 const bool anIsASCII,
46                 std::string& theError)
47 {
48   #ifdef _DEBUG
49   std::cout << "Export STl into file " << theFileName << std::endl;
50   #endif
51
52   if (!theShape.get()) {
53     theError = "STl Export failed: An invalid argument";
54     return false;
55   }
56
57   try
58   {
59     // Set "C" numeric locale to save numbers correctly
60     GeomAlgoAPI_Tools::Localizer loc;
61
62     double lDeflection = aDeflection;
63     StlAPI_Writer aWriter;
64     // copy source shape
65     BRepBuilderAPI_Copy aCopy( theShape->impl<TopoDS_Shape>(), Standard_False );
66     TopoDS_Shape aCopyShape = aCopy.Shape();
67     // ASCII mode
68     aWriter.ASCIIMode() = anIsASCII;
69     if ( anIsRelative ) {
70       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
71       Bnd_Box bndBox;
72       BRepBndLib::Add( theShape->impl<TopoDS_Shape>(), bndBox );
73       bndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
74       lDeflection = MAX3( aXmax-aXmin, aYmax-aYmin, aZmax-aZmin ) * aDeflection;
75     }
76     //Compute triangulation
77     BRepTools::Clean( aCopyShape );
78     BRepMesh_IncrementalMesh aMesh( aCopyShape, lDeflection );
79     aWriter.Write( aCopyShape, theFileName.c_str() );
80
81     return true;
82   }
83   catch( Standard_Failure )
84   {
85     theError = "Exception catched in STlExport";
86   }
87   return false;
88 }