Salome HOME
46294f036e12ed26e562c612754131c3627cb797
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_BooleanCommon.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 "FeaturesAPI_BooleanCommon.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Selection.h>
24 #include <ModelHighAPI_Tools.h>
25
26
27 static const double DEFAULT_FUZZY = 1.e-5;
28
29
30 //==================================================================================================
31 FeaturesAPI_BooleanCommon::FeaturesAPI_BooleanCommon(
32   const std::shared_ptr<ModelAPI_Feature>& theFeature)
33 : ModelHighAPI_Interface(theFeature)
34 {
35   initialize();
36 }
37
38 //==================================================================================================
39 FeaturesAPI_BooleanCommon::FeaturesAPI_BooleanCommon(
40   const std::shared_ptr<ModelAPI_Feature>& theFeature,
41   const std::list<ModelHighAPI_Selection>& theMainObjects,
42   const ModelHighAPI_Double& theFuzzy)
43 : ModelHighAPI_Interface(theFeature)
44 {
45   if(initialize()) {
46     fillAttribute(FeaturesPlugin_BooleanCommon::CREATION_METHOD_SIMPLE(), VAR_NAME(creationMethod));
47     fillAttribute(theMainObjects, VAR_NAME(mainObjects));
48     fillAttribute(theFuzzy, VAR_NAME(fuzzyParam));
49
50     // Get the evaluated fuzzy parameter from the attribute!!
51     bool aUseFuzzy = (fuzzyParam()->value() > 0);
52     fillAttribute(aUseFuzzy, VAR_NAME(useFuzzy));
53     if (!aUseFuzzy) {
54       ModelHighAPI_Double aFuzzy(DEFAULT_FUZZY);
55       fillAttribute(aFuzzy, VAR_NAME(fuzzyParam));
56     }
57
58     execute(false);
59   }
60 }
61
62 //==================================================================================================
63 FeaturesAPI_BooleanCommon::FeaturesAPI_BooleanCommon(
64   const std::shared_ptr<ModelAPI_Feature>& theFeature,
65   const std::list<ModelHighAPI_Selection>& theMainObjects,
66   const std::list<ModelHighAPI_Selection>& theToolObjects,
67   const ModelHighAPI_Double& theFuzzy)
68 : ModelHighAPI_Interface(theFeature)
69 {
70   if(initialize()) {
71     fillAttribute(FeaturesPlugin_BooleanCommon::CREATION_METHOD_ADVANCED(), VAR_NAME(creationMethod));
72     fillAttribute(theMainObjects, VAR_NAME(mainObjects));
73     fillAttribute(theToolObjects, VAR_NAME(toolObjects));
74     fillAttribute(theFuzzy, VAR_NAME(fuzzyParam));
75
76     // Get the evaluated fuzzy parameter from the attribute!!
77     bool aUseFuzzy = (fuzzyParam()->value() > 0);
78     fillAttribute(aUseFuzzy, VAR_NAME(useFuzzy));
79     if (!aUseFuzzy) {
80       ModelHighAPI_Double aFuzzy(DEFAULT_FUZZY);
81       fillAttribute(aFuzzy, VAR_NAME(fuzzyParam));
82     }
83
84     execute(false);
85   }
86 }
87
88 //==================================================================================================
89 FeaturesAPI_BooleanCommon::~FeaturesAPI_BooleanCommon()
90 {
91 }
92
93
94 //==================================================================================================
95 void FeaturesAPI_BooleanCommon::setMainObjects(
96   const std::list<ModelHighAPI_Selection>& theMainObjects)
97 {
98   fillAttribute(theMainObjects, VAR_NAME(mainObjects));
99
100   execute();
101 }
102
103 //==================================================================================================
104 void FeaturesAPI_BooleanCommon::setToolObjects(
105   const std::list<ModelHighAPI_Selection>& theToolObjects)
106 {
107   fillAttribute(FeaturesPlugin_BooleanCommon::CREATION_METHOD_ADVANCED(), VAR_NAME(creationMethod));
108   fillAttribute(theToolObjects, VAR_NAME(toolObjects));
109
110   execute();
111 }
112
113 //==================================================================================================
114 void FeaturesAPI_BooleanCommon::setUseFuzzy(bool theUseFuzzy)
115 {
116   fillAttribute(theUseFuzzy, VAR_NAME(useFuzzy));
117
118   execute();
119 }
120
121 //==================================================================================================
122 void FeaturesAPI_BooleanCommon::setFuzzyValue(const ModelHighAPI_Double& theFuzzy)
123 {
124   fillAttribute(theFuzzy, VAR_NAME(fuzzyParam));
125
126   execute();
127 }
128
129 //==================================================================================================
130 void FeaturesAPI_BooleanCommon::setAdvancedMode(const bool theMode)
131 {
132   if (theMode) {
133     fillAttribute(FeaturesPlugin_BooleanCommon::CREATION_METHOD_ADVANCED(), VAR_NAME(creationMethod));
134   } else {
135     fillAttribute(FeaturesPlugin_BooleanCommon::CREATION_METHOD_SIMPLE(), VAR_NAME(creationMethod));
136   }
137
138   execute();
139 }
140
141 //==================================================================================================
142 void FeaturesAPI_BooleanCommon::dump(ModelHighAPI_Dumper& theDumper) const
143 {
144   FeaturePtr aBase = feature();
145
146   theDumper << aBase << " = model.addCommon";
147
148   const std::string& aDocName = theDumper.name(aBase->document());
149   AttributeStringPtr aMode = aBase->string(FeaturesPlugin_BooleanCommon::CREATION_METHOD());
150   AttributeSelectionListPtr anObjects =
151     aBase->selectionList(FeaturesPlugin_BooleanCommon::OBJECT_LIST_ID());
152   AttributeSelectionListPtr aTools =
153     aBase->selectionList(FeaturesPlugin_BooleanCommon::TOOL_LIST_ID());
154   bool aUseFuzzy = aBase->boolean(FeaturesPlugin_BooleanCommon::USE_FUZZY_ID())->value();
155   AttributeDoublePtr aFuzzy = aBase->real(FeaturesPlugin_BooleanCommon::FUZZY_PARAM_ID());
156
157   theDumper << "(" << aDocName << ", " << anObjects;
158
159   if (aMode->value() == FeaturesPlugin_BooleanCommon::CREATION_METHOD_ADVANCED()) {
160     theDumper << ", " << aTools;
161   }
162
163   if (aUseFuzzy)
164     theDumper << ", fuzzyParam = " << aFuzzy;
165
166   if (!aBase->data()->version().empty())
167     theDumper << ", keepSubResults = True";
168
169   theDumper << ")" << std::endl;
170 }
171
172 //==================================================================================================
173 BooleanCommonPtr addCommon(const std::shared_ptr<ModelAPI_Document>& thePart,
174                            const std::list<ModelHighAPI_Selection>& theMainObjects,
175                            const std::list<ModelHighAPI_Selection>& theToolObjects,
176                            const ModelHighAPI_Double& theFuzzy,
177                            const bool keepSubResults)
178 {
179   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_BooleanCommon::ID());
180   if (!keepSubResults)
181     aFeature->data()->setVersion("");
182   BooleanCommonPtr aCommon;
183   if (theToolObjects.empty())
184     aCommon.reset(new FeaturesAPI_BooleanCommon(aFeature, theMainObjects, theFuzzy));
185   else
186     aCommon.reset(new FeaturesAPI_BooleanCommon(aFeature, theMainObjects, theToolObjects, theFuzzy));
187   return aCommon;
188 }