Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_CompoundBuilder.cpp
index 6e8516840baa1822e6a9d7b9e7b342ad034b8762..db0d6fb0d036c7f76c2dcfcd306f5ece40750f1e 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_CompoundBuilder.h>
@@ -23,6 +22,7 @@
 #include <TopoDS_Compound.hxx>
 #include <TopTools_IndexedMapOfShape.hxx>
 #include <TopExp.hxx>
+#include <TopExp_Explorer.hxx>
 
 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_CompoundBuilder::compound(
     std::list<std::shared_ptr<GeomAPI_Shape> > theShapes)
@@ -42,6 +42,20 @@ std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_CompoundBuilder::compound(
   return aRes;
 }
 
+// Returns true if transformations are equal with the given precision
+static bool isEqual(const gp_Trsf& theT1, const gp_Trsf& theT2, const double thePrecision)
+{
+  for(int aRow = 1; aRow < 4; aRow++) {
+    for(int aCol = 1; aCol < 5; aCol++) {
+      double aDiff = theT1.Value(aRow, aCol) - theT2.Value(aRow, aCol);
+      if (aDiff < 0) aDiff = -aDiff;
+      if (aDiff > thePrecision)
+        return false;
+    }
+  }
+  return true;
+}
+
 int GeomAlgoAPI_CompoundBuilder::id(
       std::shared_ptr<GeomAPI_Shape> theContext, std::shared_ptr<GeomAPI_Shape> theSub)
 {
@@ -52,6 +66,18 @@ int GeomAlgoAPI_CompoundBuilder::id(
     TopTools_IndexedMapOfShape aSubShapesMap;
     TopExp::MapShapes(aMainShape, aSubShapesMap);
     anID = aSubShapesMap.FindIndex(aSubShape);
+    if (anID == 0) { // try to search shape with the same location if TopLoc_Location is different
+      TopExp_Explorer anExp(aMainShape, aSubShape.ShapeType());
+      for(; anExp.More(); anExp.Next()) {
+        if (anExp.Current().TShape() == aSubShape.TShape()) {
+          const TopLoc_Location aLoc1 = anExp.Current().Location();
+          if (isEqual(aLoc1.Transformation(), aSubShape.Location().Transformation(), 1.e-7)) {
+            anID = aSubShapesMap.FindIndex(anExp.Current());
+            break;
+          }
+        }
+      }
+    }
   }
 
   return anID;