1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FiltersPlugin_Selection.h"
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_FiltersFactory.h>
25 #include <ModelAPI_Session.h>
27 // identifier of the reverse flag of a filter
28 static const std::string kReverseAttrID("");
30 void FiltersPlugin_Selection::addFilter(const std::string theFilterID)
32 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
33 FilterPtr aFilter = aFactory->filter(theFilterID);
35 std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
36 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
37 kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), theFilterID));
38 aBool->setValue(false); // not reversed by default
39 // to add attributes related to the filter
40 ModelAPI_FiltersArgs anArgs;
41 anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
42 anArgs.setFilter(theFilterID);
43 aFilter->initAttributes(anArgs);
47 void FiltersPlugin_Selection::removeFilter(const std::string theFilterID)
49 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
50 FilterPtr aFilter = aFactory->filter(theFilterID);
52 data()->removeAttributes(theFilterID);
56 std::list<std::string> FiltersPlugin_Selection::filters() const
58 std::list<std::string> aFilters;
59 data()->allGroups(aFilters);
63 void FiltersPlugin_Selection::setReversed(const std::string theFilterID, const bool theReversed)
65 std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
66 std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
68 aBool->setValue(theReversed);
71 bool FiltersPlugin_Selection::isReversed(const std::string theFilterID)
73 std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
74 std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
76 return aBool->value();
80 std::list<AttributePtr> FiltersPlugin_Selection::filterArgs(const std::string theFilterID) const
82 std::list<AttributePtr> aList;
83 data()->attributesOfGroup(theFilterID, aList);
87 void FiltersPlugin_Selection::setAttribute(const AttributePtr& theAttr)
90 if (myBase.get()) { // check that the type of sub-elements is supported by all existing filters
91 std::shared_ptr<ModelAPI_AttributeSelectionList> aSelList =
92 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
94 std::string aStrType = aSelList->selectionType();
95 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
96 std::list<std::string> aFilters;
97 data()->allGroups(aFilters);
98 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
99 std::list<std::string>::iterator aFIter = aFilters.begin();
100 for(; aFIter != aFilters.end(); aFIter++) {
101 FilterPtr aFilter = aFactory->filter(*aFIter);
102 if (aFilter.get() && !aFilter->isSupported(aType)) {
103 data()->removeAttributes(*aFIter);
110 const AttributePtr& FiltersPlugin_Selection::baseAttribute() const
115 void FiltersPlugin_Selection::initAttributes()
117 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
118 std::list<std::string> aFilters;
119 data()->allGroups(aFilters);
120 for(std::list<std::string>::iterator aFIt = aFilters.begin(); aFIt != aFilters.end(); aFIt++) {
121 FilterPtr aFilter = aFactory->filter(*aFIt);
123 std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
124 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
125 kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), *aFIt));
126 if (!aBool->isInitialized())
127 aBool->setValue(false); // not reversed by default
128 ModelAPI_FiltersArgs anArgs;
129 anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
130 anArgs.setFilter(*aFIt);
131 aFilter->initAttributes(anArgs);