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 "Model_FiltersFactory.h"
22 #include "ModelAPI_AttributeBoolean.h"
23 #include "ModelAPI_AttributeSelectionList.h"
24 #include <Events_InfoMessage.h>
27 void Model_FiltersFactory::registerFilter(const std::string& theID, ModelAPI_Filter* theFilter)
29 if (myFilters.find(theID) != myFilters.end()) {
30 Events_InfoMessage("Model_FiltersFactory", "Filter %1 is already registered").arg(theID).send();
33 myFilters[theID] = FilterPtr(theFilter);
40 std::string myFilterID;
43 bool Model_FiltersFactory::isValid(FeaturePtr theFiltersFeature, GeomShapePtr theShape)
45 // check that the shape type corresponds to the attribute list type
47 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theFiltersFeature)->baseAttribute();
49 std::shared_ptr<ModelAPI_AttributeSelectionList> aList =
50 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aBase);
51 std::string aStrType = aList->selectionType();
52 GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
53 if (theShape->shapeType() != aType)
56 // prepare all filters args
57 ModelAPI_FiltersArgs anArgs;
58 std::list<FilterArgs> aFilters; /// all filters and the reverse values
60 std::list<std::string> aGroups;
61 theFiltersFeature->data()->allGroups(aGroups);
62 for(std::list<std::string>::iterator aGIter = aGroups.begin(); aGIter != aGroups.end(); aGIter++)
64 if (myFilters.find(*aGIter) == myFilters.end())
66 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
67 theFiltersFeature->data()->attributesOfGroup(*aGIter, anAttrs);
68 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttrs.begin();
69 for(; anAttrIter != anAttrs.end(); anAttrIter++) {
70 std::string anArgID = (*anAttrIter)->id().substr((*aGIter).length() + 2);
71 if (anArgID.empty()) { // reverse flag
72 std::shared_ptr<ModelAPI_AttributeBoolean> aReverse =
73 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*anAttrIter);
74 FilterArgs aFArgs = { myFilters[*aGIter] , aReverse->value() , *aGIter };
75 aFilters.push_back(aFArgs);
78 anArgs.add(*anAttrIter);
83 // iterate filters and check shape for validity for all of them
84 std::list<FilterArgs>::iterator aFilter = aFilters.begin();
85 for(; aFilter != aFilters.end(); aFilter++) {
86 anArgs.setFilter(aFilter->myFilterID);
87 bool aResult = aFilter->myFilter->isOk(theShape, anArgs);
88 if (aFilter->myReverse)
90 if (!aResult) // one filter is failed => exit immediately
93 // all filters are passed
97 /// Returns list of filters for the given shape type
98 /// \param theType a shape type
99 std::list<FilterPtr> Model_FiltersFactory::filters(GeomAPI_Shape::ShapeType theType)
101 std::list<FilterPtr> aResult;
102 std::map<std::string, FilterPtr>::const_iterator anIt;
103 std::list<int> aTypes;
104 std::list<int>::const_iterator aTIt;
105 for (anIt = myFilters.cbegin(); anIt != myFilters.cend(); anIt++) {
106 if (anIt->second->isSupported(theType))
107 aResult.push_back(anIt->second);
112 FilterPtr Model_FiltersFactory::filter(std::string theID)
114 std::map<std::string, FilterPtr>::iterator aFound = myFilters.find(theID);
115 return aFound == myFilters.end() ? FilterPtr() : aFound->second;
118 std::string Model_FiltersFactory::id(FilterPtr theFilter)
120 std::map<std::string, FilterPtr>::iterator anIter = myFilters.begin();
121 for(; anIter != myFilters.end(); anIter++) {
122 if (anIter->second == theFilter)
123 return anIter->first;
125 return ""; // unknown case