1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FiltersAPI_Feature.h"
22 #include <ModelAPI_Feature.h>
24 #include <ModelHighAPI_Dumper.h>
25 #include <ModelHighAPI_Tools.h>
27 FiltersAPI_Feature::FiltersAPI_Feature(
28 const std::shared_ptr<ModelAPI_Feature> & theFeature)
29 : ModelHighAPI_Interface(theFeature)
34 FiltersAPI_Feature::~FiltersAPI_Feature()
38 static void separateArguments(const std::list<FiltersAPI_Argument>& theArguments,
39 std::list<ModelHighAPI_Selection>& theSelections,
40 std::list<std::string>& theTextArgs,
41 std::list<bool>& theBoolArgs)
43 std::list<FiltersAPI_Argument>::const_iterator anIt = theArguments.begin();
44 for (; anIt != theArguments.end(); ++anIt) {
45 if (anIt->selection().variantType() != ModelHighAPI_Selection::VT_Empty)
46 theSelections.push_back(anIt->selection());
47 else if (anIt->string().empty())
48 theBoolArgs.push_back(anIt->boolean());
50 theTextArgs.push_back(anIt->string());
54 void FiltersAPI_Feature::setFilters(const std::list<FilterAPIPtr>& theFilters)
56 FiltersFeaturePtr aBase = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(feature());
57 for (std::list<FilterAPIPtr>::const_iterator anIt = theFilters.begin();
58 anIt != theFilters.end(); ++anIt) {
59 aBase->addFilter((*anIt)->name());
60 aBase->setReversed((*anIt)->name(), (*anIt)->isReversed());
62 const std::list<FiltersAPI_Argument>& anArgs = (*anIt)->arguments();
63 if (!anArgs.empty()) {
64 // separate selection arguments and strings
65 std::list<ModelHighAPI_Selection> aSelections;
66 std::list<std::string> aTexts;
67 std::list<bool> aBools;
68 separateArguments(anArgs, aSelections, aTexts, aBools);
70 std::list<AttributePtr> aFilterArgs = aBase->filterArgs((*anIt)->name());
71 std::list<AttributePtr>::iterator aFIt = aFilterArgs.begin();
72 // first boolean argument is always "Reversed" flag
73 AttributeBooleanPtr aReversedFlag =
74 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*aFIt);
77 // fill arguments of the filter
78 for (; aFIt != aFilterArgs.end(); ++aFIt) {
79 AttributeSelectionListPtr aSelList =
80 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aFIt);
82 fillAttribute(aSelections, aSelList);
84 AttributeSelectionPtr aSelection =
85 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aFIt);
86 if (aSelection && aSelections.size() == 1)
87 fillAttribute(aSelections.front(), aSelection);
89 AttributeStringPtr aString =
90 std::dynamic_pointer_cast<ModelAPI_AttributeString>(*aFIt);
92 if (aTexts.size() == 1)
93 fillAttribute(aTexts.front(), aString);
96 AttributeBooleanPtr aBoolean =
97 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*aFIt);
98 if (aBoolean && aBools.size() == 1)
99 fillAttribute(aBools.front(), aBoolean);
108 void FiltersAPI_Feature::dump(ModelHighAPI_Dumper& theDumper) const
110 FiltersFeaturePtr aBase = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(feature());
114 const std::string& aDocName = theDumper.name(aBase->document());
115 theDumper << "model.filters(" << aDocName << ", [";
117 std::list<std::string> aFilters = aBase->filters();
118 for (std::list<std::string>::iterator aFIt = aFilters.begin(); aFIt != aFilters.end(); ++aFIt) {
119 FiltersAPI_Filter aFilter(*aFIt, aBase->filterArgs(*aFIt));
120 if (aFIt != aFilters.begin())
122 aFilter.dump(theDumper);