1 // Copyright (C) 2014-2021 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>
28 // identifier of the reverse flag of a filter
29 static const std::string kReverseAttrID("");
31 std::string FiltersPlugin_Selection::addFilter(const std::string theFilterID)
33 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
34 FilterPtr aFilter = aFactory->filter(theFilterID);
36 std::string aFilterID = theFilterID;
37 if (aFilter->isMultiple()) { // check that there is already such filter, so, increment ID
38 std::list<std::string> aFilters;
39 data()->allGroups(aFilters);
40 for(int anID = 0; true; anID++) {
42 std::ostringstream aStream;
43 aStream<<"_"<<anID<<"_"<<theFilterID;
44 aFilterID = aStream.str();
46 std::list<std::string>::iterator aFiltersIDs = aFilters.begin();
47 for(; aFiltersIDs != aFilters.end(); aFiltersIDs++) {
48 if (*aFiltersIDs == aFilterID)
51 if (aFiltersIDs == aFilters.end())
57 std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
58 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
59 kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), aFilterID));
60 aBool->setValue(false); // not reversed by default
61 // to add attributes related to the filter
62 ModelAPI_FiltersArgs anArgs;
63 anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
64 anArgs.setFilter(aFilterID);
65 aFilter->initAttributes(anArgs);
70 void FiltersPlugin_Selection::removeFilter(const std::string theFilterID)
72 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
73 FilterPtr aFilter = aFactory->filter(theFilterID);
75 data()->removeAttributes(theFilterID);
79 std::list<std::string> FiltersPlugin_Selection::filters() const
81 std::list<std::string> aFilters;
82 data()->allGroups(aFilters);
86 void FiltersPlugin_Selection::setReversed(const std::string theFilterID, const bool theReversed)
88 std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
89 std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
91 aBool->setValue(theReversed);
94 bool FiltersPlugin_Selection::isReversed(const std::string theFilterID)
96 std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
97 std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
99 return aBool->value();
103 std::list<AttributePtr> FiltersPlugin_Selection::filterArgs(const std::string theFilterID) const
105 std::list<AttributePtr> aList;
106 data()->attributesOfGroup(theFilterID, aList);
110 void FiltersPlugin_Selection::setAttribute(const AttributePtr& theAttr)
113 if (myBase.get()) { // check that the type of sub-elements is supported by all existing filters
114 std::shared_ptr<ModelAPI_AttributeSelectionList> aSelList =
115 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
116 if (aSelList.get()) {
117 std::string aStrType = aSelList->selectionType();
118 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
119 std::list<std::string> aFilters;
120 data()->allGroups(aFilters);
121 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
122 std::list<std::string>::iterator aFIter = aFilters.begin();
123 for(; aFIter != aFilters.end(); aFIter++) {
124 FilterPtr aFilter = aFactory->filter(*aFIter);
125 if (aFilter.get() && !aFilter->isSupported(aType)) {
126 data()->removeAttributes(*aFIter);
133 const AttributePtr& FiltersPlugin_Selection::baseAttribute() const
138 void FiltersPlugin_Selection::initAttributes()
140 ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
141 std::list<std::string> aFilters;
142 data()->allGroups(aFilters);
143 for(std::list<std::string>::iterator aFIt = aFilters.begin(); aFIt != aFilters.end(); aFIt++) {
144 FilterPtr aFilter = aFactory->filter(*aFIt);
146 std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
147 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
148 kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), *aFIt));
149 if (!aBool->isInitialized())
150 aBool->setValue(false); // not reversed by default
151 ModelAPI_FiltersArgs anArgs;
152 anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
153 anArgs.setFilter(*aFIt);
154 aFilter->initAttributes(anArgs);