Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_UnifySameDomain.cpp
index 795a0fac19512a83a9fa9597b84b26200da83bfd..ee75472232e23c6fdacfd0604aea33a98f1e6762 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "GeomAlgoAPI_UnifySameDomain.h"
 
 #include <GeomAlgoAPI_CompoundBuilder.h>
+#include <GeomAlgoAPI_DFLoader.h>
 #include <GeomAlgoAPI_ShapeTools.h>
 
-#include <ShapeUpgrade_UnifySameDomain.hxx>
+#include <BRep_Tool.hxx>
 #include <TopExp_Explorer.hxx>
 #include <TopoDS_Shape.hxx>
+#include <Precision.hxx>
+#include <TopoDS.hxx>
+#include <TopoDS_Edge.hxx>
+#include <Bnd_Box.hxx>
+#include <BRepBndLib.hxx>
+#include <ShapeUpgrade_UnifySameDomain.hxx>
 
 //==================================================================================================
 GeomAlgoAPI_UnifySameDomain::GeomAlgoAPI_UnifySameDomain(const ListOfShape& theShapes)
@@ -48,24 +54,39 @@ void GeomAlgoAPI_UnifySameDomain::build(const ListOfShape& theShapes)
 
   // Make compound.
   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(theShapes);
-  ListOfShape aCombined, aFree;
+  ListOfShape aResults;
   GeomAlgoAPI_ShapeTools::combineShapes(
     aCompound,
     GeomAPI_Shape::SHELL,
-    aCombined,
-    aFree);
+    aResults);
 
-  if(aFree.size() > 0 || aCombined.size() > 1) {
+  if(aResults.size() > 1 ||
+     (aResults.size() == 1 && aResults.front()->shapeType() > GeomAPI_Shape::SHELL)) {
     return;
   }
 
-  const TopoDS_Shape& aShell = aCombined.front()->impl<TopoDS_Shape>();
+  const TopoDS_Shape& aShell = aResults.front()->impl<TopoDS_Shape>();
 
   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
   aShape->setImpl(new TopoDS_Shape(aShell));
   build(aShape, true);
 }
 
+// calculates maximum possible tolerance on edges of shape
+static Standard_Real defineLinearTolerance(const TopoDS_Shape& theShape)
+{
+  Standard_Real aMaxTol = Precision::Confusion();
+
+  TopExp_Explorer Explo(theShape, TopAbs_EDGE);
+  for (; Explo.More(); Explo.Next())
+  {
+    const TopoDS_Edge& anEdge = TopoDS::Edge(Explo.Current());
+    aMaxTol = Max(aMaxTol, BRep_Tool::Tolerance(anEdge));
+  }
+
+  return aMaxTol;
+}
+
 //==================================================================================================
 void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
                                         const bool theIsToSimplifyShell)
@@ -74,13 +95,19 @@ void GeomAlgoAPI_UnifySameDomain::build(const GeomShapePtr& theShape,
   this->setImpl(aUnifyAlgo);
 
   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
-  aUnifyAlgo->Initialize(aShape);
+  aUnifyAlgo->Initialize(aShape, Standard_True, Standard_True, Standard_True);
+  aUnifyAlgo->SetLinearTolerance(defineLinearTolerance(aShape));
+  aUnifyAlgo->SetAngularTolerance(1.e-5); // for #32443
   aUnifyAlgo->Build();
 
   TopoDS_Shape aResult = aUnifyAlgo->Shape();
   if (aResult.IsNull()) {
     return;
   }
+  // take off the compound if it consists of single sub-shape
+  if (aResult.ShapeType() == TopAbs_COMPOUND) {
+    aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
+  }
 
   if (theIsToSimplifyShell && aResult.ShapeType() == TopAbs_SHELL) {
     int aNb = 0;