]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/CollectionPlugin/CollectionPlugin_GroupAddition.cpp
Salome HOME
Task 2.5. Combination operations on Groups (issue #2935)
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_GroupAddition.cpp
index 1c3795dc3799d401c9b410635a8cae13ae62b6d0..c27826ce850af6f1e8bdd791ae540b7a57b10ecf 100644 (file)
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_ResultGroup.h>
 
+#include <GeomAPI_ShapeIterator.h>
+#include <GeomAlgoAPI_CompoundBuilder.h>
+
 CollectionPlugin_GroupAddition::CollectionPlugin_GroupAddition()
 {
 }
 
 void CollectionPlugin_GroupAddition::initAttributes()
 {
-  data()->addAttribute(CollectionPlugin_Group::LIST_ID(),
+  data()->addAttribute(CollectionPlugin_GroupAddition::LIST_ID(),
                        ModelAPI_AttributeSelectionList::typeId());
 }
 
-void CollectionPlugin_GroupAddition::execute()
+static void explodeCompound(const GeomShapePtr& theCompound, ListOfShape& theSubs)
+{
+  if (theCompound->isCompound()) {
+    GeomAPI_ShapeIterator anIt(theCompound);
+    for (; anIt.more(); anIt.next())
+      explodeCompound(anIt.current(), theSubs);
+  }
+  else
+    theSubs.push_back(theCompound);
+}
+
+static void keepUniqueShapes(ListOfShape& theShapes)
 {
-  if (results().empty() || firstResult()->isDisabled()) { // just create result if not exists
-    ResultPtr aGroup = document()->createGroup(data());
-    setResult(aGroup);
+  ListOfShape::iterator anIt = theShapes.begin();
+  while (anIt != theShapes.end()) {
+    GeomShapePtr aCurrent = *anIt;
+    ListOfShape::iterator anIt2 = theShapes.begin();
+    for (; anIt2 != anIt; ++anIt2)
+      if (aCurrent->isEqual(*anIt2))
+        break;
+    if (anIt2 != anIt) {
+      // the same shape is found
+      ListOfShape::iterator aRemoveIt = anIt++;
+      theShapes.erase(aRemoveIt);
+    }
+    else
+      ++anIt;
   }
 }
+
+void CollectionPlugin_GroupAddition::execute()
+{
+  ResultGroupPtr aGroup = document()->createGroup(data());
+  // clean the result of the operation
+  aGroup->store(GeomShapePtr());
+
+  // collect all unique sub-shapes
+  GeomShapePtr aCompound = aGroup->shape();
+  ListOfShape aSubs;
+  explodeCompound(aCompound, aSubs);
+  keepUniqueShapes(aSubs);
+  aCompound = aSubs.empty() ? GeomShapePtr() : GeomAlgoAPI_CompoundBuilder::compound(aSubs);
+  aGroup->store(aCompound);
+
+  setResult(aGroup);
+}