Salome HOME
Merge remote-tracking branch 'remotes/origin/EDF_2020_Lot2'
[modules/shaper.git] / src / FiltersAPI / FiltersAPI_Filter.cpp
1 // Copyright (C) 2014-2020  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 "FiltersAPI_Filter.h"
21
22 #include <ModelAPI_Attribute.h>
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26
27 #include <ModelHighAPI_Dumper.h>
28
29 FiltersAPI_Filter::FiltersAPI_Filter(
30     const std::string& theName,
31     const bool theRevertFilter,
32     const std::list<FiltersAPI_Argument>& theArguments)
33   : myName(theName), myReversed(theRevertFilter), myFilterArguments(theArguments)
34 {
35 }
36
37 FiltersAPI_Filter::FiltersAPI_Filter(const std::string& theName,
38                                      const std::list<AttributePtr>& theArguments)
39   : myName(theName)
40 {
41   std::list<AttributePtr>::const_iterator anArgIt = theArguments.begin();
42   // first attribute is usually for reversing the filter
43   AttributeBooleanPtr aBoolAttr =
44       std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*anArgIt);
45   if (aBoolAttr) {
46     myReversed = aBoolAttr->value();
47     ++anArgIt;
48   }
49
50   for (; anArgIt != theArguments.end(); ++anArgIt) {
51     AttributeSelectionListPtr aSelList =
52         std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*anArgIt);
53     if (aSelList) {
54       int aSize = aSelList->size();
55       for (int i = 0; i < aSize; ++i) {
56         AttributeSelectionPtr aSelection = aSelList->value(i);
57         myFilterArguments.push_back(FiltersAPI_Argument(aSelection));
58       }
59       continue;
60     }
61
62     AttributeSelectionPtr aSelection =
63         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*anArgIt);
64     if (aSelection) {
65       myFilterArguments.push_back(FiltersAPI_Argument(aSelection));
66       continue;
67     }
68
69     AttributeStringPtr aString = std::dynamic_pointer_cast<ModelAPI_AttributeString>(*anArgIt);
70     if (aString) {
71       myFilterArguments.push_back(FiltersAPI_Argument(aString->value()));
72       continue;
73     }
74
75     AttributeBooleanPtr aBoolean = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*anArgIt);
76     if (aBoolean) {
77       myFilterArguments.push_back(FiltersAPI_Argument(aBoolean->value()));
78       continue;
79     }
80   }
81 }
82
83 FiltersAPI_Filter::~FiltersAPI_Filter()
84 {
85 }
86
87 void FiltersAPI_Filter::dump(ModelHighAPI_Dumper& theDumper) const
88 {
89   theDumper << "model.addFilter(name = \"" << myName << "\"";
90   if (myReversed)
91     theDumper << ", exclude = " << myReversed;
92   if (!myFilterArguments.empty()) {
93     theDumper << ", args = [";
94     bool isFirstArg = true;
95     for (std::list<FiltersAPI_Argument>::const_iterator anIt = myFilterArguments.begin();
96          anIt != myFilterArguments.end(); ++anIt) {
97       if (isFirstArg)
98         isFirstArg = false;
99       else
100         theDumper << ", ";
101       anIt->dump(theDumper);
102     }
103     theDumper << "]";
104   }
105   theDumper << ")";
106 }
107
108 // ================================================================================================
109 FilterAPIPtr addFilter(const std::string& name,
110                        const bool exclude,
111                        const std::list<FiltersAPI_Argument>& args)
112 {
113   return FilterAPIPtr(new FiltersAPI_Filter(name, exclude, args));
114 }