Salome HOME
Merge remote-tracking branch 'remotes/origin/Filters_Development_2'
[modules/shaper.git] / src / Model / Model_FiltersFactory.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_FiltersFactory.h"
21
22 #include "ModelAPI_AttributeBoolean.h"
23 #include "ModelAPI_AttributeSelectionList.h"
24 #include <Events_InfoMessage.h>
25
26
27 void Model_FiltersFactory::registerFilter(const std::string& theID, ModelAPI_Filter* theFilter)
28 {
29   if (myFilters.find(theID) != myFilters.end()) {
30     Events_InfoMessage("Model_FiltersFactory", "Filter %1 is already registered").arg(theID).send();
31   }
32   else {
33     myFilters[theID] = FilterPtr(theFilter);
34   }
35 }
36
37 struct FilterArgs {
38   FilterPtr myFilter;
39   bool myReverse;
40   std::string myFilterID;
41 };
42
43 bool Model_FiltersFactory::isValid(FeaturePtr theFiltersFeature,
44                                    ResultPtr theResult,
45                                    GeomShapePtr theShape)
46 {
47   // check that the shape type corresponds to the attribute list type
48   AttributePtr aBase =
49     std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theFiltersFeature)->baseAttribute();
50   if (aBase.get()) {
51     std::shared_ptr<ModelAPI_AttributeSelectionList> aList =
52       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aBase);
53     std::string aStrType = aList->selectionType();
54     GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
55     if (theShape->shapeType() != aType)
56       return false;
57   }
58   // prepare all filters args
59   ModelAPI_FiltersArgs anArgs;
60   std::list<FilterArgs> aFilters; /// all filters and the reverse values
61
62   std::list<std::string> aGroups;
63   theFiltersFeature->data()->allGroups(aGroups);
64   for(std::list<std::string>::iterator aGIter = aGroups.begin(); aGIter != aGroups.end(); aGIter++)
65   {
66     if (myFilters.find(*aGIter) == myFilters.end())
67       continue;
68     std::list<std::shared_ptr<ModelAPI_Attribute> > anAttrs;
69     theFiltersFeature->data()->attributesOfGroup(*aGIter, anAttrs);
70     std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttrIter = anAttrs.begin();
71     for(; anAttrIter != anAttrs.end(); anAttrIter++) {
72       std::string anArgID = (*anAttrIter)->id().substr((*aGIter).length() + 2);
73       if (anArgID.empty()) { // reverse flag
74         std::shared_ptr<ModelAPI_AttributeBoolean> aReverse =
75           std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*anAttrIter);
76         FilterArgs aFArgs = { myFilters[*aGIter] , aReverse->value() , *aGIter };
77         aFilters.push_back(aFArgs);
78
79       } else {
80         anArgs.add(*anAttrIter);
81       }
82     }
83   }
84
85   // iterate filters and check shape for validity for all of them
86   std::list<FilterArgs>::iterator aFilter = aFilters.begin();
87   for(; aFilter != aFilters.end(); aFilter++) {
88     anArgs.setFilter(aFilter->myFilterID);
89     bool aResult = aFilter->myFilter->isOk(theShape, theResult, anArgs);
90     if (aFilter->myReverse)
91       aResult = !aResult;
92     if (!aResult) // one filter is failed => exit immediately
93       return false;
94   }
95   // all filters are passed
96   return true;
97 }
98
99 /// Returns list of filters for the given shape type
100 /// \param theType a shape type
101 std::list<FilterPtr> Model_FiltersFactory::filters(GeomAPI_Shape::ShapeType theType)
102 {
103   std::list<FilterPtr> aResult;
104   std::map<std::string, FilterPtr>::const_iterator anIt;
105   std::list<int> aTypes;
106   std::list<int>::const_iterator aTIt;
107   for (anIt = myFilters.cbegin(); anIt != myFilters.cend(); anIt++) {
108     if (anIt->second->isSupported(theType))
109       aResult.push_back(anIt->second);
110   }
111   return aResult;
112 }
113
114 FilterPtr Model_FiltersFactory::filter(std::string theID)
115 {
116   std::map<std::string, FilterPtr>::iterator aFound = myFilters.find(theID);
117   return aFound == myFilters.end() ? FilterPtr() : aFound->second;
118 }
119
120 std::string Model_FiltersFactory::id(FilterPtr theFilter)
121 {
122   std::map<std::string, FilterPtr>::iterator anIter = myFilters.begin();
123   for(; anIter != myFilters.end(); anIter++) {
124     if (anIter->second == theFilter)
125       return anIter->first;
126   }
127   return ""; // unknown case
128 }