Salome HOME
ef2fa323486d823a8b726fc0df6660c9b3a4ff47
[modules/shaper.git] / src / FiltersPlugin / FiltersPlugin_Selection.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 "FiltersPlugin_Selection.h"
21
22 #include <ModelAPI_AttributeBoolean.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_FiltersFactory.h>
25 #include <ModelAPI_Session.h>
26 #include <sstream>
27
28 // identifier of the reverse flag of a filter
29 static const std::string kReverseAttrID("");
30
31 std::string FiltersPlugin_Selection::addFilter(const std::string theFilterID)
32 {
33   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
34   FilterPtr aFilter = aFactory->filter(theFilterID);
35
36   std::string aFilterID = theFilterID;
37   if (aFilter->isMultiple()) { // check that there is already such filter, so, increment ID
38     std::list<std::string> aFilters;
39     data()->allGroups(aFilters);
40     for(int anID = 0; true; anID++) {
41       if (anID != 0) {
42         std::ostringstream aStream;
43         aStream<<"_"<<anID<<"_"<<theFilterID;
44         aFilterID = aStream.str();
45       }
46       std::list<std::string>::iterator aFiltersIDs = aFilters.begin();
47       for(; aFiltersIDs != aFilters.end(); aFiltersIDs++) {
48         if (*aFiltersIDs == aFilterID)
49           break;
50       }
51       if (aFiltersIDs == aFilters.end())
52         break;
53     }
54   }
55
56   if (aFilter.get()) {
57     std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
58       std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
59         kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), aFilterID));
60     aBool->setValue(false); // not reversed by default
61     // to add attributes related to the filter
62     ModelAPI_FiltersArgs anArgs;
63     anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
64     anArgs.setFilter(aFilterID);
65     aFilter->initAttributes(anArgs);
66   }
67   return aFilterID;
68 }
69
70 void FiltersPlugin_Selection::removeFilter(const std::string theFilterID)
71 {
72   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
73   FilterPtr aFilter = aFactory->filter(theFilterID);
74   if (aFilter.get()) {
75     data()->removeAttributes(theFilterID);
76   }
77 }
78
79 std::list<std::string> FiltersPlugin_Selection::filters() const
80 {
81   std::list<std::string> aFilters;
82   data()->allGroups(aFilters);
83   return aFilters;
84 }
85
86 void FiltersPlugin_Selection::setReversed(const std::string theFilterID, const bool theReversed)
87 {
88   std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
89   std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
90   if (aBool.get())
91     aBool->setValue(theReversed);
92 }
93
94 bool FiltersPlugin_Selection::isReversed(const std::string theFilterID)
95 {
96   std::string anAttrID = theFilterID + kFilterSeparator + kReverseAttrID;
97   std::shared_ptr<ModelAPI_AttributeBoolean> aBool = boolean(anAttrID);
98   if (aBool.get())
99     return aBool->value();
100   return false;
101 }
102
103 std::list<AttributePtr> FiltersPlugin_Selection::filterArgs(const std::string theFilterID) const
104 {
105   std::list<AttributePtr> aList;
106   data()->attributesOfGroup(theFilterID, aList);
107   return aList;
108 }
109
110 void FiltersPlugin_Selection::setAttribute(const AttributePtr& theAttr)
111 {
112   myBase = theAttr;
113   if (myBase.get()) { // check that the type of sub-elements is supported by all existing filters
114     std::shared_ptr<ModelAPI_AttributeSelectionList> aSelList =
115       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
116     if (aSelList.get()) {
117       std::string aStrType = aSelList->selectionType();
118       GeomAPI_Shape::ShapeType aType = GeomAPI_Shape::shapeTypeByStr(aStrType);
119       std::list<std::string> aFilters;
120       data()->allGroups(aFilters);
121       ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
122       std::list<std::string>::iterator aFIter = aFilters.begin();
123       for(; aFIter != aFilters.end(); aFIter++) {
124         FilterPtr aFilter = aFactory->filter(*aFIter);
125         if (aFilter.get() && !aFilter->isSupported(aType)) {
126           data()->removeAttributes(*aFIter);
127         }
128       }
129     }
130   }
131 }
132
133 const AttributePtr& FiltersPlugin_Selection::baseAttribute() const
134 {
135   return myBase;
136 }
137
138 void FiltersPlugin_Selection::initAttributes()
139 {
140   ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
141   std::list<std::string> aFilters;
142   data()->allGroups(aFilters);
143   for(std::list<std::string>::iterator aFIt = aFilters.begin(); aFIt != aFilters.end(); aFIt++) {
144     FilterPtr aFilter = aFactory->filter(*aFIt);
145     if (aFilter.get()) {
146       std::shared_ptr<ModelAPI_AttributeBoolean> aBool =
147         std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(data()->addFloatingAttribute(
148           kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), *aFIt));
149       if (!aBool->isInitialized())
150         aBool->setValue(false); // not reversed by default
151       ModelAPI_FiltersArgs anArgs;
152       anArgs.setFeature(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(data()->owner()));
153       anArgs.setFilter(*aFIt);
154       aFilter->initAttributes(anArgs);
155     }
156   }
157 }