Salome HOME
Improve GeomAlgoAPI_ShapeTools::volume() method to return the volume of the shape...
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_Selection.cpp
index 025d30147b3b9c9883ed66f1303858fa3ad06d90..8dcd52916ffdf0e1ee9939ec8236a524b0c8fad8 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  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
 
 #include "FiltersPlugin_Selection.h"
 
-#include <ModelAPI_Session.h>
 #include <ModelAPI_AttributeBoolean.h>
+#include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_FiltersFactory.h>
+#include <ModelAPI_Session.h>
+#include <sstream>
 
 // identifier of the reverse flag of a filter
 static const std::string kReverseAttrID("");
 
-void FiltersPlugin_Selection::addFilter(const std::string theFilterID)
+std::string FiltersPlugin_Selection::addFilter(const std::string theFilterID)
 {
   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
   FilterPtr aFilter = aFactory->filter(theFilterID);
+
+  std::string aFilterID = theFilterID;
+  if (aFilter->isMultiple()) { // check that there is already such filter, so, increment ID
+    std::list<std::string> aFilters;
+    data()->allGroups(aFilters);
+    for(int anID = 0; true; anID++) {
+      if (anID != 0) {
+        std::ostringstream aStream;
+        aStream<<"_"<<anID<<"_"<<theFilterID;
+        aFilterID = aStream.str();
+      }
+      std::list<std::string>::iterator aFiltersIDs = aFilters.begin();
+      for(; aFiltersIDs != aFilters.end(); aFiltersIDs++) {
+        if (*aFiltersIDs == aFilterID)
+          break;
+      }
+      if (aFiltersIDs == aFilters.end())
+        break;
+    }
+  }
+
   if (aFilter.get()) {
     std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
       std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
-        kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), theFilterID));
+        kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), aFilterID));
     aBool->setValue(false); // not reversed by default
-    // TODO: to add attributes related to the filter
+    // to add attributes related to the filter
+    ModelAPI_FiltersArgs anArgs;
+    anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
+    anArgs.setFilter(aFilterID);
+    aFilter->initAttributes(anArgs);
   }
+  return aFilterID;
 }
 
 void FiltersPlugin_Selection::removeFilter(const std::string theFilterID)
@@ -56,15 +85,19 @@ std::list<std::string> FiltersPlugin_Selection::filters() const
 
 void FiltersPlugin_Selection::setReversed(const std::string theFilterID, const bool theReversed)
 {
-  std::string anAttrID = kReverseAttrID + kFilterSeparator + theFilterID;
+  std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
   std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
+  if (aBool.get())
+    aBool->setValue(theReversed);
 }
 
 bool FiltersPlugin_Selection::isReversed(const std::string theFilterID)
 {
-  std::string anAttrID = kReverseAttrID + kFilterSeparator + theFilterID;
+  std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
   std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
-  return aBool->value();
+  if (aBool.get())
+    return aBool->value();
+  return false;
 }
 
 std::list<AttributePtr> FiltersPlugin_Selection::filterArgs(const std::string theFilterID) const
@@ -73,3 +106,52 @@ std::list<AttributePtr> FiltersPlugin_Selection::filterArgs(const std::string th
   data()->attributesOfGroup(theFilterID, aList);
   return aList;
 }
+
+void FiltersPlugin_Selection::setAttribute(const AttributePtr& theAttr)
+{
+  myBase = theAttr;
+  if (myBase.get()) { // check that the type of sub-elements is supported by all existing filters
+    std::shared_ptr<ModelAPI_AttributeSelectionList> aSelList =
+      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
+    if (aSelList.get()) {
+      std::string aStrType = aSelList->selectionType();
+      GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
+      std::list<std::string> aFilters;
+      data()->allGroups(aFilters);
+      ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
+      std::list<std::string>::iterator aFIter = aFilters.begin();
+      for(; aFIter != aFilters.end(); aFIter++) {
+        FilterPtr aFilter = aFactory->filter(*aFIter);
+        if (aFilter.get() && !aFilter->isSupported(aType)) {
+          data()->removeAttributes(*aFIter);
+        }
+      }
+    }
+  }
+}
+
+const AttributePtr& FiltersPlugin_Selection::baseAttribute() const
+{
+  return myBase;
+}
+
+void FiltersPlugin_Selection::initAttributes()
+{
+  ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
+  std::list<std::string> aFilters;
+  data()->allGroups(aFilters);
+  for(std::list<std::string>::iterator aFIt = aFilters.begin(); aFIt != aFilters.end(); aFIt++) {
+    FilterPtr aFilter = aFactory->filter(*aFIt);
+    if (aFilter.get()) {
+      std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
+        std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
+          kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), *aFIt));
+      if (!aBool->isInitialized())
+        aBool->setValue(false); // not reversed by default
+      ModelAPI_FiltersArgs anArgs;
+      anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
+      anArgs.setFilter(*aFIt);
+      aFilter->initAttributes(anArgs);
+    }
+  }
+}