Salome HOME
Create filter "Belongs To"
[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 struct FilterArgs {
37   FilterPtr myFilter;
38   bool myReverse;
39   std::string myFilterID;
40 };
41
42 bool Model_FiltersFactory::isValid(FeaturePtr theFiltersFeature, GeomShapePtr theShape)
43 {
44   // prepare all filters args
45   ModelAPI_FiltersArgs anArgs;
46   std::list<FilterArgs> aFilters; /// all filters and the reverse values
47
48   std::list<std::string> aGroups;
49   theFiltersFeature->data()->allGroups(aGroups);
50   for(std::list<std::string>::iterator aGIter = aGroups.begin(); aGIter != aGroups.end(); aGIter++)
51   {
52     if (myFilters.find(*aGIter) == myFilters.end())
53       continue;
54     std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
55     theFiltersFeature->data()->attributesOfGroup(*aGIter, anAttrs);
56     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttrs.begin();
57     for(; anAttrIter != anAttrs.end(); anAttrIter++) {
58       std::string anArgID = (*anAttrIter)->id().substr((*aGIter).length() + 2);
59       if (anArgID.empty()) { // reverse flag
60         std::shared_ptr<ModelAPI_AttributeBoolean> aReverse =
61           std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*anAttrIter);
62         FilterArgs aFArgs = { myFilters[*aGIter] , aReverse->value() , *aGIter };
63         aFilters.push_back(aFArgs);
64
65       } else {
66         anArgs.add(*anAttrIter);
67       }
68     }
69   }
70
71   // iterate filters and check shape for validity for all of them
72   std::list<FilterArgs>::iterator aFilter = aFilters.begin();
73   for(; aFilter != aFilters.end(); aFilter++) {
74     anArgs.setFilter(aFilter->myFilterID);
75     bool aResult = aFilter->myFilter->isOk(theShape, anArgs);
76     if (aFilter->myReverse)
77       aResult = !aResult;
78     if (!aResult) // one filter is failed => exit immediately
79       return false;
80   }
81   // all filters are passed
82   return true;
83 }
84
85 /// Returns list of filters for the given shape type
86 /// \param theType a shape type
87 std::list<FilterPtr> Model_FiltersFactory::filters(GeomAPI_Shape::ShapeType theType)
88 {
89   std::list<FilterPtr> aResult;
90   std::map<std::string, FilterPtr>::const_iterator anIt;
91   std::list<int> aTypes;
92   std::list<int>::const_iterator aTIt;
93   for (anIt = myFilters.cbegin(); anIt != myFilters.cend(); anIt++) {
94     if (anIt->second->isSupported(theType))
95       aResult.push_back(anIt->second);
96   }
97   return aResult;
98 }
99
100 FilterPtr Model_FiltersFactory::filter(std::string theID)
101 {
102   std::map<std::string, FilterPtr>::iterator aFound = myFilters.find(theID);
103   return aFound == myFilters.end() ? FilterPtr() : aFound->second;
104 }
105
106 std::string Model_FiltersFactory::id(FilterPtr theFilter)
107 {
108   std::map<std::string, FilterPtr>::iterator anIter = myFilters.begin();
109   for(; anIter != myFilters.end(); anIter++) {
110     if (anIter->second == theFilter)
111       return anIter->first;
112   }
113   return ""; // unknown case
114 }