]> SALOME platform Git repositories - modules/shaper.git/blob - src/FiltersAPI/FiltersAPI_Feature.cpp
Salome HOME
Implement filter "F12: Topologically connected Faces"
[modules/shaper.git] / src / FiltersAPI / FiltersAPI_Feature.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 "FiltersAPI_Feature.h"
21
22 #include <ModelAPI_Feature.h>
23
24 #include <ModelHighAPI_Dumper.h>
25 #include <ModelHighAPI_Tools.h>
26
27 FiltersAPI_Feature::FiltersAPI_Feature(
28     const std::shared_ptr<ModelAPI_Feature> & theFeature)
29 : ModelHighAPI_Interface(theFeature)
30 {
31   initialize();
32 }
33
34 FiltersAPI_Feature::~FiltersAPI_Feature()
35 {
36 }
37
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)
42 {
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());
49     else
50       theTextArgs.push_back(anIt->string());
51   }
52 }
53
54 void FiltersAPI_Feature::setFilters(const std::list<FilterAPIPtr>& theFilters)
55 {
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());
61
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);
69
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);
75       if (aReversedFlag)
76         ++aFIt;
77       // fill arguments of the filter
78       for (; aFIt != aFilterArgs.end(); ++aFIt) {
79         AttributeSelectionListPtr aSelList =
80             std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aFIt);
81         if (aSelList)
82           fillAttribute(aSelections, aSelList);
83         else {
84           AttributeSelectionPtr aSelection =
85               std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aFIt);
86           if (aSelection && aSelections.size() == 1)
87             fillAttribute(aSelections.front(), aSelection);
88           else {
89             AttributeStringPtr aString =
90                 std::dynamic_pointer_cast<ModelAPI_AttributeString>(*aFIt);
91             if (aString) {
92               if (aTexts.size() == 1)
93                 fillAttribute(aTexts.front(), aString);
94             }
95             else {
96               AttributeBooleanPtr aBoolean =
97                   std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(*aFIt);
98               if (aBoolean && aBools.size() == 1)
99                 fillAttribute(aBools.front(), aBoolean);
100             }
101           }
102         }
103       }
104     }
105   }
106 }
107
108 void FiltersAPI_Feature::dump(ModelHighAPI_Dumper& theDumper) const
109 {
110   FiltersFeaturePtr aBase = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(feature());
111   if (!aBase)
112     return;
113
114   const std::string& aDocName = theDumper.name(aBase->document());
115   theDumper << "model.filters(" << aDocName << ", [";
116
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())
121       theDumper << ", ";
122     aFilter.dump(theDumper);
123   }
124
125   theDumper << "])";
126 }