1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "STLPlugin_ExportDriver.hxx"
22 #include "STLPlugin_IExport.hxx"
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
27 #include <Basics_OCCTVersion.hxx>
30 #include "GEOM_Function.hxx"
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>
41 #define MAX2(X, Y) ( Abs(X) > Abs(Y) ? Abs(X) : Abs(Y) )
42 #define MAX3(X, Y, Z) ( MAX2 ( MAX2(X, Y) , Z ) )
44 //=======================================================================
47 //=======================================================================
48 const Standard_GUID& STLPlugin_ExportDriver::GetID()
50 static Standard_GUID aGUID("88678a6a-885c-477c-b90b-51934572c61d");
54 //=======================================================================
55 //function : STLPlugin_ExportDriver
57 //=======================================================================
58 STLPlugin_ExportDriver::STLPlugin_ExportDriver()
62 //=======================================================================
65 //=======================================================================
66 Standard_Integer STLPlugin_ExportDriver::Execute( TFunction_Logbook& log ) const
68 if (Label().IsNull()) return 0;
69 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
71 STLPlugin_IExport aData (aFunction);
73 // retrieve the being exported shape
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 );
82 TCollection_AsciiString aFileName = aData.GetFileName();
83 double aDeflection = aData.GetDeflection();
84 bool anIsASCII = aData.GetIsASCII();
85 bool anIsRelative = aData.GetIsRelative();
87 MESSAGE( "Export STL into file " << aFileName );
89 // Set "C" numeric locale to save numbers correctly
90 Kernel_Utils::Localizer loc;
94 StlAPI_Writer aWriter;
96 BRepBuilderAPI_Copy aCopy( aShape, Standard_False );
97 TopoDS_Shape aCopyShape = aCopy.Shape();
99 aWriter.ASCIIMode() = anIsASCII;
100 #if OCC_VERSION_LARGE > 0x06080000
101 if ( anIsRelative ) {
102 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
104 BRepBndLib::Add( aShape, bndBox );
105 bndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
106 aDeflection = MAX3( aXmax-aXmin, aYmax-aYmin, aZmax-aZmin ) * aDeflection;
108 //Compute triangulation
109 BRepTools::Clean( aCopyShape );
110 BRepMesh_IncrementalMesh aMesh( aCopyShape, aDeflection );
112 // set relative mode on false for using custom deflection coefficient
113 aWriter.RelativeMode( ) = anIsRelative;
115 aWriter.SetCoefficient( aDeflection );
117 aWriter.SetDeflection( aDeflection );
119 aWriter.Write( aCopyShape, aFileName.ToCString() );
120 log.SetTouched( Label() );
123 catch( Standard_Failure )
125 //THROW_SALOME_CORBA_EXCEPTION("Exception catched in ExportSTL", SALOME::BAD_PARAM);
130 //=======================================================================
131 //function : MustExecute
133 //=======================================================================
134 Standard_Boolean STLPlugin_ExportDriver::MustExecute( const TFunction_Logbook& ) const
136 return Standard_True;
139 //================================================================================
141 * \brief Returns a name of creation operation and names and values of creation parameters
143 //================================================================================
144 bool STLPlugin_ExportDriver::
145 GetCreationInformation( std::string& theOperationName,
146 std::vector<GEOM_Param>& theParams )
151 IMPLEMENT_STANDARD_HANDLE( STLPlugin_ExportDriver,GEOM_BaseDriver );
152 IMPLEMENT_STANDARD_RTTIEXT( STLPlugin_ExportDriver,GEOM_BaseDriver );