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_Filter.h"
22 #include "ModelAPI_AttributeBoolean.h"
23 #include <Events_InfoMessage.h>
26 void Model_FiltersFactory::registerFilter(const std::string& theID, ModelAPI_Filter* theFilter)
28 if (myFilters.find(theID) != myFilters.end()) {
29 Events_InfoMessage("Model_FiltersFactory", "Filter %1 is already registered").arg(theID).send();
32 myFilters[theID] = FilterPtr(theFilter);
36 bool Model_FiltersFactory::isValid(FeaturePtr theFiltersFeature, GeomShapePtr theShape)
38 // prepare all filters args
39 ModelAPI_FiltersArgs anArgs;
40 std::map<FilterPtr, bool> aReverseFlags; /// map of all filters to the reverse values
42 std::list<std::string> aGroups;
43 theFiltersFeature->data()->allGroups(aGroups);
44 for(std::list<std::string>::iterator aGIter = aGroups.begin(); aGIter != aGroups.end(); aGIter++)
46 if (myFilters.find(*aGIter) == myFilters.end())
48 std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
49 theFiltersFeature->data()->attributesOfGroup(*aGIter, anAttrs);
50 std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttrs.begin();
51 for(; anAttrIter != anAttrs.end(); anAttrIter++) {
52 std::string anArgID = (*anAttrIter)->id().substr((*aGIter).length() + 2);
53 if (anArgID.empty()) { // reverse flag
54 std::shared_ptr<ModelAPI_AttributeBoolean> aReverse =
55 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*anAttrIter);
56 aReverseFlags[myFilters[*aGIter] ] = aReverse->value();
58 anArgs.add(*anAttrIter);
63 // iterate filters and check shape for validity for all of them
64 std::map<FilterPtr, bool>::iterator aFilter = aReverseFlags.begin();
65 for(; aFilter != aReverseFlags.end(); aFilter++) {
66 bool aResult = aFilter->first->isOk(theShape, anArgs);
69 if (!aResult) // one filter is failed => exit immediately
72 // all filters are passed
76 /// Returns list of filters for the given shape type
77 /// \param theType a shape type
78 std::list<FilterPtr> Model_FiltersFactory::filters(GeomAPI_Shape::ShapeType theType)
80 std::list<FilterPtr> aResult;
81 std::map<std::string, FilterPtr>::const_iterator anIt;
82 std::list<int> aTypes;
83 std::list<int>::const_iterator aTIt;
84 for (anIt = myFilters.cbegin(); anIt != myFilters.cend(); anIt++) {
85 if (anIt->second->isSupported(theType))
86 aResult.push_back(anIt->second);
91 FilterPtr Model_FiltersFactory::filter(std::string theID)
93 std::map<std::string, FilterPtr>::iterator aFound = myFilters.find(theID);
94 return aFound == myFilters.end() ? FilterPtr() : aFound->second;
97 std::string Model_FiltersFactory::id(FilterPtr theFilter)
99 std::map<std::string, FilterPtr>::iterator anIter = myFilters.begin();
100 for(; anIter != myFilters.end(); anIter++) {
101 if (anIter->second == theFilter)
102 return anIter->first;
104 return ""; // unknown case