Salome HOME
Issue #2644: Set not valid state if a feature is not accepted by attribute
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.cpp
index 4d8b47998fb6ffa96187727ff3254461fc517678..308e14770edb8d0017fd96ef3200c0d52554cb87 100644 (file)
@@ -35,6 +35,7 @@
 #include <ModelAPI_Events.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_Tools.h>
+#include <ModelAPI_AttributeSelection.h>
 
 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
 #include <SelectMgr_EntityOwner.hxx>
@@ -115,8 +116,44 @@ bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr&
       anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
       myPresentedObject = aResult;
     }
-    else
-      aValid = false; // only results with a shape can be filtered
+    else {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
+      if (aFeature.get()) {
+        // Use feature as a reference to all its results
+        myPresentedObject = aFeature;
+        AttributePtr anAttr = attribute();
+        std::string aType = anAttr->attributeType();
+
+        // Check that results of Feature is acceptable by filters for selection attribute
+        if (aType == ModelAPI_AttributeSelection::typeId()) {
+          AttributeSelectionPtr aSelectAttr =
+            std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
+          aSelectAttr->setValue(myPresentedObject, GeomShapePtr(), true);
+          GeomShapePtr aShape = aSelectAttr->value();
+          if (aShape.get()) {
+            const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
+            Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
+            anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
+          }
+          else
+            aValid = false;
+          aSelectAttr->setValue(ObjectPtr(), GeomShapePtr(), true);
+        }
+        else {
+          ResultPtr aResult = aFeature->firstResult();
+          if (aResult.get()) {
+            GeomShapePtr aShapePtr = ModelAPI_Tools::shape(aResult);
+            if (aShapePtr.get()) {
+              const TopoDS_Shape aTDShape = aShapePtr->impl<TopoDS_Shape>();
+              Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
+              anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
+            }
+          }
+          aValid = !anOwner.IsNull(); // only results with a shape can be filtered
+        }
+      } else
+        aValid = false; // only results with a shape can be filtered
+    }
   }
   // checks the owner by the AIS context activated filters
   if (!anOwner.IsNull()) {
@@ -415,31 +452,42 @@ void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsP
 //********************************************************************
 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
 {
-  std::set<ResultBodyPtr> aCompSolids;
+  std::set<ResultPtr> aFilterOut; // all objects that must be filtered out with their children
   QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
 
   // Collect compsolids.
   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
-  for (; anIt != aLast; anIt++) {
+  for(; anIt != aLast; anIt++) {
     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
     ObjectPtr anObject = aViewerPrs->object();
     ResultBodyPtr aResultCompSolid =
       std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObject);
-    if(aResultCompSolid.get() && aResultCompSolid->numberOfSubs() > 0) {
-      aCompSolids.insert(aResultCompSolid);
+    if (aResultCompSolid.get()) {
+      for(int aSubIndex = 0; aSubIndex < aResultCompSolid->numberOfSubs(); aSubIndex++)
+        aFilterOut.insert(aResultCompSolid->subResult(aSubIndex));
+    } else { // it could be a whole feature selected, so, add all results of this feature
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
+      if (aFeature.get()) {
+        std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
+        for(; aRes != aFeature->results().cend(); aRes++)
+          aFilterOut.insert(*aRes);
+      }
     }
   }
 
   // Filter sub-solids of compsolids.
   anIt = theValues.begin();
-  for (; anIt != aLast; anIt++) {
+  for(; anIt != aLast; anIt++) {
     const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
     ObjectPtr anObject = aViewerPrs->object();
     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
-    ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aResult);
-    if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
-      // Skip sub-solid of compsolid.
-      continue;
+    while(aResult.get()) {
+      if (aFilterOut.find(aResult) != aFilterOut.end()) // skip if parent is filtered out
+        break;
+      aResult = ModelAPI_Tools::bodyOwner(aResult); // iterate all parents
+    }
+    if (aResult.get()) {
+      continue; // skip
     } else {
       aValidatedValues.append(*anIt);
     }