]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Filter.cpp
Salome HOME
Update Filters data model using the previous filters implementation: filter arguments...
[modules/shaper.git] / src / Model / Model_Filter.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Model_Filter.h"
21
22 #include "ModelAPI_AttributeBoolean.h"
23 #include <Events_InfoMessage.h>
24
25
26 void Model_FiltersFactory::registerFilter(const std::string& theID, ModelAPI_Filter* theFilter)
27 {
28   if (myFilters.find(theID) != myFilters.end()) {
29     Events_InfoMessage("Model_FiltersFactory", "Filter %1 is already registered").arg(theID).send();
30   }
31   else {
32     myFilters[theID] = FilterPtr(theFilter);
33   }
34 }
35
36 bool Model_FiltersFactory::isValid(FeaturePtr theFiltersFeature, GeomShapePtr theShape)
37 {
38   // prepare all filters args
39   ModelAPI_FiltersArgs anArgs;
40   std::map<FilterPtr, bool> aReverseFlags; /// map of all filters to the reverse values
41   static const std::string& anEmptyType("");
42   std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs =
43     theFiltersFeature->data()->attributes(anEmptyType);
44   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
45   for(; anAttr != allAttrs.end(); anAttr++) {
46     const std::string& anAttrID = (*anAttr)->id();
47     if (anAttrID.find(kFilterSeparator) == std::string::npos) { // possible a filter reverse flag
48       std::shared_ptr<ModelAPI_AttributeBoolean> aReverse = theFiltersFeature->boolean(anAttrID);
49       if (aReverse.get() && myFilters.find(anAttrID) != myFilters.end()) {
50         aReverseFlags[myFilters[anAttrID] ] = aReverse->value();
51       }
52     } else { // an argument of a filter
53       anArgs.add(*anAttr);
54     }
55   }
56   // iterate filters and check shape for validity for all of them
57   std::map<FilterPtr, bool>::iterator aFilter = aReverseFlags.begin();
58   for(; aFilter != aReverseFlags.end(); aFilter++) {
59     bool aResult = aFilter->first->isOk(theShape, anArgs);
60     if (aFilter->second)
61       aResult = !aResult;
62     if (!aResult) // one filter is failed => exit immediately
63       return false;
64   }
65   // all filters are passed
66   return true;
67 }
68
69 /// Returns list of filters for the given shape type
70 /// \param theType a shape type
71 std::list<FilterPtr> Model_FiltersFactory::filters(GeomAPI_Shape::ShapeType theType)
72 {
73   std::list<FilterPtr> aResult;
74   std::map<std::string, FilterPtr>::const_iterator anIt;
75   std::list<int> aTypes;
76   std::list<int>::const_iterator aTIt;
77   for (anIt = myFilters.cbegin(); anIt != myFilters.cend(); anIt++) {
78     if (anIt->second->isSupported(theType))
79       aResult.push_back(anIt->second);
80   }
81   return aResult;
82 }
83
84 FilterPtr Model_FiltersFactory::filter(std::string theID)
85 {
86   std::map<std::string, FilterPtr>::iterator aFound = myFilters.find(theID);
87   return aFound == myFilters.end() ? FilterPtr() : aFound->second;
88 }
89
90 std::string Model_FiltersFactory::id(FilterPtr theFilter)
91 {
92   std::map<std::string, FilterPtr>::iterator anIter = myFilters.begin();
93   for(; anIter != myFilters.end(); anIter++) {
94     if (anIter->second == theFilter)
95       return anIter->first;
96   }
97   return ""; // unknown case
98 }