Salome HOME
Merge V8_4_BR branch.
[modules/geom.git] / src / STLPlugin / STLPlugin_ExportDriver.cxx
index 7d02d6f8fad6adc31594437fc22b97f214dc6a69..609fca612c5f64e081f63de31ee50150d76c60f5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2014-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #include <BRepBuilderAPI_Copy.hxx>
 #include <StlAPI_Writer.hxx>
 #include <TopoDS_Shape.hxx>
+#include <Bnd_Box.hxx>
+#include <BRepBndLib.hxx>
+#include <BRepTools.hxx>
+#include <BRepMesh_IncrementalMesh.hxx>
+
+#define MAX2(X, Y)      ( Abs(X) > Abs(Y) ? Abs(X) : Abs(Y) )
+#define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X, Y) , Z ) )
 
 //=======================================================================
 //function : GetID
@@ -55,7 +62,7 @@ STLPlugin_ExportDriver::STLPlugin_ExportDriver()
 //function : Execute
 //purpose  :
 //=======================================================================
-Standard_Integer STLPlugin_ExportDriver::Execute( TFunction_Logbook& log ) const
+Standard_Integer STLPlugin_ExportDriver::Execute(Handle(TFunction_Logbook)& log) const
 {
   if (Label().IsNull()) return 0;
   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
@@ -84,34 +91,32 @@ Standard_Integer STLPlugin_ExportDriver::Execute( TFunction_Logbook& log ) const
   try
   {
     StlAPI_Writer aWriter;
-    // set relative mode on false for using custom deflection coefficient
-    aWriter.RelativeMode( ) = anIsRelative;
-    aWriter.ASCIIMode() = anIsASCII;
-    if( anIsRelative )
-      aWriter.SetCoefficient( aDeflection );
-    else
-      aWriter.SetDeflection( aDeflection );
+    // copy source shape
     BRepBuilderAPI_Copy aCopy( aShape, Standard_False );
-    aWriter.Write( aCopy.Shape(), aFileName.ToCString() );
-    log.SetTouched( Label() );
+    TopoDS_Shape aCopyShape = aCopy.Shape();
+    // ASCII mode
+    aWriter.ASCIIMode() = anIsASCII;
+    if ( anIsRelative ) {
+      Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
+      Bnd_Box bndBox;
+      BRepBndLib::Add( aShape, bndBox );
+      bndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
+      aDeflection = MAX3( aXmax-aXmin, aYmax-aYmin, aZmax-aZmin ) * aDeflection;
+    }
+    //Compute triangulation
+    BRepTools::Clean( aCopyShape );
+    BRepMesh_IncrementalMesh aMesh( aCopyShape, aDeflection );
+    aWriter.Write( aCopyShape, aFileName.ToCString() );
+    log->SetTouched(Label());
     return 1;
   }
   catch( Standard_Failure )
   {
-    //THROW_SALOME_CORBA_EXCEPTION("Exception catched in ExportSTL", SALOME::BAD_PARAM);
+    //THROW_SALOME_CORBA_EXCEPTION("Exception caught in ExportSTL", SALOME::BAD_PARAM);
   }
   return 0;
 }
 
-//=======================================================================
-//function : MustExecute
-//purpose  :
-//=======================================================================
-Standard_Boolean STLPlugin_ExportDriver::MustExecute( const TFunction_Logbook& ) const
-{
-  return Standard_True;
-}
-
 //================================================================================
 /*!
  * \brief Returns a name of creation operation and names and values of creation parameters
@@ -124,5 +129,4 @@ GetCreationInformation( std::string&             theOperationName,
   return false;
 }
 
-IMPLEMENT_STANDARD_HANDLE( STLPlugin_ExportDriver,GEOM_BaseDriver );
 IMPLEMENT_STANDARD_RTTIEXT( STLPlugin_ExportDriver,GEOM_BaseDriver );