From 6877aab9e4ae59418c74ce867f59674456838650 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Wed, 8 Feb 2017 09:30:31 +0000
Subject: [PATCH] fuzzy tol for boolean ops

---
 Common/Context.h        |  2 +-
 Common/DefaultOptions.h |  2 ++
 Common/Options.cpp      |  7 +++++++
 Common/Options.h        |  1 +
 Geo/GModelIO_OCC.cpp    | 24 ++++++++++++++++++++----
 5 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/Common/Context.h b/Common/Context.h
index c119ae7339..da389c2f50 100644
--- a/Common/Context.h
+++ b/Common/Context.h
@@ -72,7 +72,7 @@ struct contextGeometryOptions {
   int extrudeSplinePoints, extrudeReturnLateral;
   double normals, tangents, scalingFactor;
   int autoCoherence, highlightOrphans, clip, useTransform;
-  double tolerance, snap[3], transform[3][3], offset[3];
+  double tolerance, toleranceBoolean, snap[3], transform[3][3], offset[3];
   int occFixDegenerated, occFixSmallEdges, occFixSmallFaces;
   int occSewFaces, occConnectFaces, occParallel;
   double occScaling;
diff --git a/Common/DefaultOptions.h b/Common/DefaultOptions.h
index ba954a5cc0..8a416b2b20 100644
--- a/Common/DefaultOptions.h
+++ b/Common/DefaultOptions.h
@@ -919,6 +919,8 @@ StringXNumber GeometryOptions_Number[] = {
     "Display size of tangent vectors (in pixels)" },
   { F|O, "Tolerance" , opt_geometry_tolerance, 1.e-8 ,
     "Geometrical tolerance" },
+  { F|O, "ToleranceBoolean" , opt_geometry_tolerance_boolean, 0. ,
+    "Geometrical tolerance for boolean operations" },
   { F,   "Transform" , opt_geometry_transform , 0. ,
     "Transform model display coordinates (0=no, 1=scale)" },
   { F,   "TransformXX" , opt_geometry_transform00 , 1. ,
diff --git a/Common/Options.cpp b/Common/Options.cpp
index 80d56719f6..183ba74564 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -4406,6 +4406,13 @@ double opt_geometry_tolerance(OPT_ARGS_NUM)
   return CTX::instance()->geom.tolerance;
 }
 
+double opt_geometry_tolerance_boolean(OPT_ARGS_NUM)
+{
+  if(action & GMSH_SET)
+    CTX::instance()->geom.toleranceBoolean = val;
+  return CTX::instance()->geom.toleranceBoolean;
+}
+
 double opt_geometry_normals(OPT_ARGS_NUM)
 {
   if(action & GMSH_SET)
diff --git a/Common/Options.h b/Common/Options.h
index 86a7f0dec7..e193ba597d 100644
--- a/Common/Options.h
+++ b/Common/Options.h
@@ -359,6 +359,7 @@ double opt_geometry_hide_compounds(OPT_ARGS_NUM);
 double opt_geometry_oriented_physicals(OPT_ARGS_NUM);
 double opt_geometry_highlight_orphans(OPT_ARGS_NUM);
 double opt_geometry_tolerance(OPT_ARGS_NUM);
+double opt_geometry_tolerance_boolean(OPT_ARGS_NUM);
 double opt_geometry_normals(OPT_ARGS_NUM);
 double opt_geometry_tangents(OPT_ARGS_NUM);
 double opt_geometry_points(OPT_ARGS_NUM);
diff --git a/Geo/GModelIO_OCC.cpp b/Geo/GModelIO_OCC.cpp
index a658659758..b454fd56ed 100644
--- a/Geo/GModelIO_OCC.cpp
+++ b/Geo/GModelIO_OCC.cpp
@@ -328,8 +328,10 @@ void OCC_Internals::importShape(const std::string &fileName,
     else if(split[2] == ".step" || split[2] == ".stp" ||
             split[2] == ".STEP" || split[2] == ".STP"){
       STEPControl_Reader reader;
-      reader.ReadFile(fileName.c_str());
-      reader.NbRootsForTransfer();
+      if(reader.ReadFile(fileName.c_str()) != IFSelect_RetDone){
+        Msg::Error("Could not read file '%s'", fileName.c_str());
+        return;
+      }
       reader.TransferRoots();
       result = reader.OneShape();
     }
@@ -337,7 +339,6 @@ void OCC_Internals::importShape(const std::string &fileName,
       Msg::Error("Unknown file type '%s'", fileName.c_str());
       return;
     }
-    BRepTools::Clean(result);
     // FIXME: apply healing routine on result?
   }
   catch(Standard_Failure &err){
@@ -354,13 +355,28 @@ void OCC_Internals::importShape(const std::string &fileName,
   // FIXME: if no solids, return faces, etc.
 }
 
+/*
+void OCC_Internals::export(const std::string &fileName)
+{
+  BRep_Builder b;
+  TopoDS_Compound c;
+  b.MakeCompound(c);
+  for(int i = 1; i <= _vmap.Extent(); i++) b.Add(c, _vmap(i));
+  for(int i = 1; i <= _emap.Extent(); i++) b.Add(c, _emap(i));
+  for(int i = 1; i <= _wmap.Extent(); i++) b.Add(c, _wmap(i));
+  for(int i = 1; i <= _fmap.Extent(); i++) b.Add(c, _fmap(i));
+  for(int i = 1; i <= _shmap.Extent(); i++) b.Add(c, _shmap(i));
+  for(int i = 1; i <= _somap.Extent(); i++) b.Add(c, _somap(i));
+}
+*/
+
 void OCC_Internals::applyBooleanOperator(int tag, BooleanOperator op,
                                          std::vector<int> objectTags[4],
                                          std::vector<int> toolTags[4],
                                          std::vector<int> outTags[4],
                                          bool removeObject, bool removeTool)
 {
-  double tolerance = 0.0; // FIXME make this a parameter
+  double tolerance = CTX::instance()->geom.toleranceBoolean;
   bool parallel = CTX::instance()->geom.occParallel;
 
   if(tag > 0 && _tagSolid.IsBound(tag)){
-- 
GitLab