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