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