Salome HOME
updated copyright message
[modules/geom.git] / src / STLPlugin / STLPlugin_ExportDriver.cxx
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D, OPEN CASCADE
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 // internal includes
21 #include "STLPlugin_ExportDriver.hxx"
22 #include "STLPlugin_IExport.hxx"
23
24 // KERNEL includes
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
27
28 // GEOM includes
29 #include "GEOM_Function.hxx"
30
31 // OOCT includes
32 #include <BRepBuilderAPI_Copy.hxx>
33 #include <StlAPI_Writer.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <Bnd_Box.hxx>
36 #include <BRepBndLib.hxx>
37 #include <BRepTools.hxx>
38 #include <BRepMesh_IncrementalMesh.hxx>
39
40 #define MAX2(X, Y)      ( Abs(X) > Abs(Y) ? Abs(X) : Abs(Y) )
41 #define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X, Y) , Z ) )
42
43 //=======================================================================
44 //function : GetID
45 //purpose  :
46 //=======================================================================
47 const Standard_GUID& STLPlugin_ExportDriver::GetID()
48 {
49   static Standard_GUID aGUID("88678a6a-885c-477c-b90b-51934572c61d");
50   return aGUID;
51 }
52
53 //=======================================================================
54 //function : STLPlugin_ExportDriver
55 //purpose  :
56 //=======================================================================
57 STLPlugin_ExportDriver::STLPlugin_ExportDriver()
58 {
59 }
60
61 //=======================================================================
62 //function : Execute
63 //purpose  :
64 //=======================================================================
65 Standard_Integer STLPlugin_ExportDriver::Execute(Handle(TFunction_Logbook)& log) const
66 {
67   if (Label().IsNull()) return 0;
68   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
69
70   STLPlugin_IExport aData (aFunction);
71
72   // retrieve the being exported shape
73   TopoDS_Shape aShape;
74   Handle(GEOM_Function) aRefFunction = aData.GetOriginal();
75   if( aRefFunction.IsNull() ) return 0;
76   aShape = aRefFunction->GetValue();
77   if( aShape.IsNull() ) return 0;
78   // set the result of function to be used by next operations
79   aFunction->SetValue( aShape );
80
81   TCollection_AsciiString aFileName = aData.GetFileName();
82   double aDeflection = aData.GetDeflection();
83   bool anIsASCII = aData.GetIsASCII();
84   bool anIsRelative = aData.GetIsRelative();
85
86   MESSAGE( "Export STL into file " << aFileName );
87
88   // Set "C" numeric locale to save numbers correctly
89   Kernel_Utils::Localizer loc;
90
91   try
92   {
93     StlAPI_Writer aWriter;
94     // copy source shape
95     BRepBuilderAPI_Copy aCopy( aShape, Standard_False );
96     TopoDS_Shape aCopyShape = aCopy.Shape();
97     // ASCII mode
98     aWriter.ASCIIMode() = anIsASCII;
99     if ( anIsRelative ) {
100       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
101       Bnd_Box bndBox;
102       BRepBndLib::Add( aShape, bndBox );
103       bndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
104       aDeflection = MAX3( aXmax-aXmin, aYmax-aYmin, aZmax-aZmin ) * aDeflection;
105     }
106     //Compute triangulation
107     BRepTools::Clean( aCopyShape );
108     BRepMesh_IncrementalMesh aMesh( aCopyShape, aDeflection );
109     aWriter.Write( aCopyShape, aFileName.ToCString() );
110     log->SetTouched(Label());
111     return 1;
112   }
113   catch( Standard_Failure& )
114   {
115     //THROW_SALOME_CORBA_EXCEPTION("Exception caught in ExportSTL", SALOME::BAD_PARAM);
116   }
117   return 0;
118 }
119
120 //================================================================================
121 /*!
122  * \brief Returns a name of creation operation and names and values of creation parameters
123  */
124 //================================================================================
125 bool STLPlugin_ExportDriver::
126 GetCreationInformation( std::string&             /*theOperationName*/,
127                         std::vector<GEOM_Param>& /*theParams*/ )
128 {
129   return false;
130 }
131
132 IMPLEMENT_STANDARD_RTTIEXT( STLPlugin_ExportDriver,GEOM_BaseDriver )