Salome HOME
Issue 2229: additional fix for the interactive creation of the constraint.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Partition.cpp
index 5db097c79ef8062c09e2c5ba19e98cd1f3706ad0..cd880734a65e4c56d03250784d6417e39b525aa8 100644 (file)
@@ -14,7 +14,8 @@
 // License along with this library; if not, write to the Free Software
 // 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<mailto:webmaster.salome@opencascade.com>
 //
 
 #include "GeomAlgoAPI_Partition.h"
@@ -26,6 +27,7 @@
 
 #include <TopExp_Explorer.hxx>
 #include <TopoDS_Builder.hxx>
+#include <TopTools_MapOfShape.hxx>
 
 //=================================================================================================
 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Partition::make(const ListOfShape& theObjects,
@@ -45,6 +47,21 @@ GeomAlgoAPI_Partition::GeomAlgoAPI_Partition(const ListOfShape& theObjects,
   build(theObjects, theTools);
 }
 
+static void prepareShapes(const TopoDS_Shape&   theShape,
+                           TopTools_ListOfShape& theSimpleList)
+{
+  if (theShape.ShapeType() != TopAbs_COMPOUND) {
+      theSimpleList.Append(theShape);
+    return;
+  }
+
+  // explode compound on simple shapes to allow their intersections
+  TopoDS_Iterator It (theShape, Standard_True, Standard_True);
+  for (; It.More(); It.Next()) {
+    TopoDS_Shape curSh = It.Value();
+    prepareShapes(curSh, theSimpleList);
+  }
+}
 
 //=================================================================================================
 void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
@@ -59,11 +76,21 @@ void GeomAlgoAPI_Partition::build(const ListOfShape& theObjects,
   this->setImpl(anOperation);
   this->setBuilderType(OCCT_BOPAlgo_Builder);
 
+  TopTools_MapOfShape ShapesMap;
   // Getting objects.
-  for (ListOfShape::const_iterator
+  for(ListOfShape::const_iterator
        anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++) {
     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
-    anOperation->AddArgument(aShape);
+    // #2240: decompose compounds to get the valid result
+    TopTools_ListOfShape aSimpleShapes;
+    prepareShapes(aShape, aSimpleShapes);
+    TopTools_ListIteratorOfListOfShape aSimpleIter(aSimpleShapes);
+    for (; aSimpleIter.More(); aSimpleIter.Next()) {
+      const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
+      if (ShapesMap.Add(aSimpleSh)) {
+        anOperation->AddArgument(aSimpleSh);
+      }
+    }
   }
 
   // Getting tools.