Salome HOME
Issue #3059 : When “move to the end” of a group, propose to the user a new group...
authormpv <mpv@opencascade.com>
Thu, 27 Feb 2020 09:36:09 +0000 (12:36 +0300)
committermpv <mpv@opencascade.com>
Thu, 27 Feb 2020 09:36:09 +0000 (12:36 +0300)
An additional development to support cases when groups must be not fully divided, but sub-elements are collected by the context principles.

src/CollectionPlugin/CMakeLists.txt
src/CollectionPlugin/CollectionPlugin_Group.cpp
src/CollectionPlugin/Test/TestGroupMoveAndSplit4.py [new file with mode: 0644]

index e13a28446d23377c78b62132680e8888db9017dc..479e865831bec65e21b622ea127e32680b2aa44e 100644 (file)
@@ -163,6 +163,7 @@ ADD_UNIT_TESTS(
                TestGroupMoveAndSplit1.py
                TestGroupMoveAndSplit2.py
                TestGroupMoveAndSplit3.py
+               TestGroupMoveAndSplit4.py
                Test3114.py
                Test18739.py
 )
index e6ae25802b36138aba4726743d50cb45a3675758..6af6734f2f0ba8f640e756290ab3fbb2c12ad850 100644 (file)
@@ -64,6 +64,19 @@ static std::string findName(
 
 }
 
+static int getDepth(AttributeSelectionPtr& theAttr) {
+  if (theAttr->contextFeature().get())
+    return 0;
+  ResultBodyPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theAttr->context());
+  if (aRes.get()) {
+    int aDepth = 0;
+    for(aRes = ModelAPI_Tools::bodyOwner(aRes); aRes.get(); aRes = ModelAPI_Tools::bodyOwner(aRes))
+      aDepth++;
+    return aDepth;
+  }
+  return 0;
+}
+
 bool CollectionPlugin_Group::customAction(const std::string& theActionId)
 {
   if (theActionId == "split") {
@@ -87,13 +100,61 @@ bool CollectionPlugin_Group::customAction(const std::string& theActionId)
         }
       }
     }
-
+    // searching for the depth on the results, minimal for all contextes:
+    // groups will be collected at this depth; the root is 0
+    int aMinDepth = -1;
+    std::map<ObjectPtr, int> aStoredDepth;
     AttributeSelectionListPtr aList = selectionList(LIST_ID());
+    for (int a = aList->size() - 1; a >= 0; a--) {
+      AttributeSelectionPtr anAttr = aList->value(a);
+      if (!anAttr->isInvalid() && anAttr->contextObject().get()) {
+        int aDepth = getDepth(anAttr);
+        aStoredDepth[anAttr->contextObject()] = aDepth;
+        if (aMinDepth == -1 || aDepth < aMinDepth)
+          aMinDepth = aDepth;
+      }
+    }
+    // get common fathers at the minimal depth and set index of group to them
+    std::map<ObjectPtr, ObjectPtr> aFathers;
+    std::map<ObjectPtr, int> aFatherGroup;
+    bool aSplitAnyway = false;
+    for(int aCurrentDepth = 0; aCurrentDepth <= aMinDepth; aCurrentDepth++) {
+      aFathers.clear();
+      aFatherGroup.clear();
+      for (int a = aList->size() - 1; a >= 0; a--) {
+        // start from zero (this group index), then from the end - as in the next iteration
+        AttributeSelectionPtr anAttr = aList->value(a);
+        if (!anAttr->isInvalid() && anAttr->contextObject().get()) {
+          ObjectPtr anObj = anAttr->contextObject();
+          int aDepth = aStoredDepth[anObj];
+          if (!aSplitAnyway && aDepth > aCurrentDepth) {
+            ResultBodyPtr aRes = std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObj);
+            while (aDepth > aCurrentDepth) {
+              aRes = ModelAPI_Tools::bodyOwner(aRes);
+              aDepth--;
+            }
+            anObj = aRes;
+          }
+          aFathers[anAttr->contextObject()] = anObj;
+          if (aFatherGroup.find(anObj) == aFatherGroup.end()) {
+            int aSize = aFatherGroup.size();
+            aFatherGroup[anObj] = aSize; // the first is zero
+          }
+        }
+      }
+      if (aFatherGroup.size() < aList->size() && aFatherGroup.size() != 1)  // already good
+        break;
+      if (aFatherGroup.size() == aList->size()) // no sence to iterate further
+        break;
+      if (aFatherGroup.size() == 1 && aList->size() > 1 && aCurrentDepth == aMinDepth)
+        aSplitAnyway = true;  // split anyway, better that just move
+    }
+
     std::set<int> aRemoved;
     bool aStay = false; // to indicate that the good attribute found stays in the list
     int anIndex = 1; // index of the name assigned to group-feature and result
     // added in the order: 3 2 1 orig=0, so, keep the results to give names later
-    std::list<ObjectPtr> aResults;
+    std::list<FeaturePtr> aResults;
     for(int aNext = aList->size() - 1; aNext >= 0; aNext--) {
       AttributeSelectionPtr anOldAttr = aList->value(aNext);
       if (anOldAttr->isInvalid() || !anOldAttr->contextObject().get()) {// remove invalids
@@ -104,22 +165,35 @@ bool CollectionPlugin_Group::customAction(const std::string& theActionId)
         aStay = true;
         continue;
       }
+      int aFGroup = aFatherGroup[aFathers[anOldAttr->contextObject()]];
+      if (!aSplitAnyway && aFGroup == 0) // to stay in this first group
+        continue;
+
       aRemoved.insert(aNext);
-      FeaturePtr aNew = aDoc->addFeature(ID(), false);
+      FeaturePtr aNew;
+      if (aSplitAnyway || aFGroup > aResults.size()) {
+        aNew = aDoc->addFeature(ID(), false);
+        aResults.push_front(aNew); // to keep the order
+      } else { // appending in already created new result
+        std::list<FeaturePtr>::reverse_iterator aResIter = aResults.rbegin();
+        for (; aFGroup > 1; aResIter++)
+          aFGroup--;
+        aNew = *aResIter;
+      }
+
       AttributeSelectionListPtr aNewList = aNew->selectionList(LIST_ID());
       aNewList->setSelectionType(aList->selectionType());
       aNewList->append(anOldAttr->contextObject(), anOldAttr->value());
-      aNew->execute();
-      aResults.push_front(aNew); // to keep the order
     }
-    aResults.push_back(data()->owner());
+    aResults.push_back(std::dynamic_pointer_cast<ModelAPI_Feature>(data()->owner()));
     // remove all selections except the first
     aList->remove(aRemoved);
     // set names
     if (aResults.size() > 1) { // rename if there are new groups appeared only
-      std::list<ObjectPtr>::iterator aResIter = aResults.begin();
+      std::list<FeaturePtr>::iterator aResIter = aResults.begin();
       for(int aSuffix = 1; aResIter != aResults.end(); aResIter++) {
         FeaturePtr aFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(*aResIter);
+        aFeat->execute();
         aFeat->data()->setName(findName(name(), aSuffix, aFeatNames));
         if (!aFeat->results().empty() && !results().empty()) {
           int aResSuf = aSuffix - 1;
diff --git a/src/CollectionPlugin/Test/TestGroupMoveAndSplit4.py b/src/CollectionPlugin/Test/TestGroupMoveAndSplit4.py
new file mode 100644 (file)
index 0000000..613eff4
--- /dev/null
@@ -0,0 +1,64 @@
+# Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# 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
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+
+# Check the case when 2 faces in one solid selected in group with thranslation x5, moves to 5 translated groups
+# This is an additional request in the issue #3059
+
+from salome.shaper import model
+from ModelAPI import *
+from GeomAPI import *
+
+model.begin()
+partSet = model.moduleDocument()
+Part_1 = model.addPart(partSet)
+Part_1_doc = Part_1.document()
+Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
+SketchCircle_1 = Sketch_1.addCircle(45.88138045990679, 57.99590690678009, 10.80094646219014)
+SketchLine_1 = Sketch_1.addLine(40.23195449216883, 67.2015807394968, 46.4026355638949, 47.20754569686197)
+SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_1.startPoint(), SketchCircle_1.results()[1])
+SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchCircle_1.results()[1])
+model.do()
+Sketch_1.changeFacesOrder([[SketchCircle_1.results()[1], SketchCircle_1.results()[1], SketchLine_1.result()],
+                           [SketchCircle_1.results()[1], SketchLine_1.result()]
+                          ])
+model.do()
+Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2r-SketchLine_1r"), model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2r-SketchCircle_1_2r-SketchLine_1f")], model.selection(), 10, 0)
+Group_1 = model.addGroup(Part_1_doc, "Faces", [model.selection("FACE", "Extrusion_1_1_1/To_Face"), model.selection("FACE", "Extrusion_1_1_2/To_Face")])
+AngularCopy_1 = model.addMultiRotation(Part_1_doc, [model.selection("COMPSOLID", "Extrusion_1_1")], model.selection("EDGE", "PartSet/OZ"), 5, keepSubResults = True)
+model.do()
+Part_1_doc.moveFeature(Group_1.feature(), AngularCopy_1.feature(), True)
+model.end()
+
+# must be created 5 groups of faces, 2 faces in each
+assert(Part_1_doc.size("Groups") == 5)
+
+for i in range(5):
+  resShape = modelAPI_Result(Part_1_doc.object("Groups", i)).shape()
+  assert(not resShape.isNull())
+  # the group result is a compund, check that this is a compound of two faces
+  aShapeExplorer = GeomAPI_ShapeExplorer(resShape, GeomAPI_Shape.FACE)
+  assert(aShapeExplorer.more())
+  assert(aShapeExplorer.current().isFace())
+  aShapeExplorer.next()
+  assert(aShapeExplorer.more())
+  assert(aShapeExplorer.current().isFace())
+  aShapeExplorer.next()
+  assert(not aShapeExplorer.more())
+
+assert(model.checkPythonDump())