Salome HOME
added checkbox in UI for optional fuzzy parameter
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_BooleanFuse.cpp
1 // Copyright (C) 2014-2022  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_BooleanFuse.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_BooleanFuse::FeaturesAPI_BooleanFuse(
32   const std::shared_ptr<ModelAPI_Feature>& theFeature)
33 : ModelHighAPI_Interface(theFeature)
34 {
35   initialize();
36 }
37
38 //==================================================================================================
39 FeaturesAPI_BooleanFuse::FeaturesAPI_BooleanFuse(
40   const std::shared_ptr<ModelAPI_Feature>& theFeature,
41   const std::list<ModelHighAPI_Selection>& theMainObjects,
42   const bool theRemoveEdges,
43   const ModelHighAPI_Double& theFuzzy)
44   : ModelHighAPI_Interface(theFeature)
45 {
46   if (initialize()) {
47     bool aUseFuzzy = (theFuzzy.value() > 0);
48     ModelHighAPI_Double aFuzzy = (aUseFuzzy ? theFuzzy : ModelHighAPI_Double(DEFAULT_FUZZY));
49     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_SIMPLE(), mycreationMethod);
50     fillAttribute(theMainObjects, mymainObjects);
51     fillAttribute(theRemoveEdges, myremoveEdges);
52     fillAttribute(aUseFuzzy, myuseFuzzy);
53     fillAttribute(aFuzzy, myfuzzyParam);
54
55     execute(false);
56   }
57 }
58
59 //==================================================================================================
60 FeaturesAPI_BooleanFuse::FeaturesAPI_BooleanFuse(
61   const std::shared_ptr<ModelAPI_Feature>& theFeature,
62   const std::list<ModelHighAPI_Selection>& theMainObjects,
63   const std::list<ModelHighAPI_Selection>& theToolObjects,
64   const bool theRemoveEdges,
65   const ModelHighAPI_Double& theFuzzy)
66 : ModelHighAPI_Interface(theFeature)
67 {
68   if(initialize()) {
69     bool aUseFuzzy = (theFuzzy.value() > 0);
70     ModelHighAPI_Double aFuzzy = (aUseFuzzy ? theFuzzy : ModelHighAPI_Double(DEFAULT_FUZZY));
71     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED(), mycreationMethod);
72     fillAttribute(theMainObjects, mymainObjects);
73     fillAttribute(theToolObjects, mytoolObjects);
74     fillAttribute(theRemoveEdges, myremoveEdges);
75     fillAttribute(aUseFuzzy, myuseFuzzy);
76     fillAttribute(aFuzzy, myfuzzyParam);
77
78     execute(false);
79   }
80 }
81
82 //==================================================================================================
83 FeaturesAPI_BooleanFuse::~FeaturesAPI_BooleanFuse()
84 {
85 }
86
87
88 //==================================================================================================
89 void FeaturesAPI_BooleanFuse::setMainObjects(
90   const std::list<ModelHighAPI_Selection>& theMainObjects)
91 {
92   fillAttribute(theMainObjects, mymainObjects);
93
94   execute();
95 }
96
97 //==================================================================================================
98 void FeaturesAPI_BooleanFuse::setToolObjects(
99   const std::list<ModelHighAPI_Selection>& theToolObjects)
100 {
101   fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED(), mycreationMethod);
102   fillAttribute(theToolObjects, mytoolObjects);
103
104   execute();
105 }
106
107 //==================================================================================================
108 void FeaturesAPI_BooleanFuse::setRemoveEdges(const bool theRemoveEdges)
109 {
110   fillAttribute(theRemoveEdges, myremoveEdges);
111
112   execute();
113 }
114
115 //==================================================================================================
116 void FeaturesAPI_BooleanFuse::setUseFuzzy(bool theUseFuzzy)
117 {
118   fillAttribute(theUseFuzzy, myuseFuzzy);
119
120   execute();
121 }
122
123 //==================================================================================================
124 void FeaturesAPI_BooleanFuse::setFuzzyValue(const ModelHighAPI_Double& theFuzzy)
125 {
126   fillAttribute(theFuzzy, myfuzzyParam);
127
128   execute();
129 }
130
131 //==================================================================================================
132 void FeaturesAPI_BooleanFuse::setAdvancedMode(const bool theMode)
133 {
134   if (theMode) {
135     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED(), mycreationMethod);
136   } else {
137     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_SIMPLE(), mycreationMethod);
138   }
139
140   execute();
141 }
142
143 //==================================================================================================
144 void FeaturesAPI_BooleanFuse::dump(ModelHighAPI_Dumper& theDumper) const
145 {
146   FeaturePtr aBase = feature();
147
148   theDumper << aBase << " = model.addFuse";
149
150   const std::string& aDocName = theDumper.name(aBase->document());
151   AttributeStringPtr aMode = aBase->string(FeaturesPlugin_BooleanFuse::CREATION_METHOD());
152   AttributeSelectionListPtr anObjects =
153     aBase->selectionList(FeaturesPlugin_BooleanFuse::OBJECT_LIST_ID());
154   AttributeSelectionListPtr aTools =
155     aBase->selectionList(FeaturesPlugin_BooleanFuse::TOOL_LIST_ID());
156   AttributeBooleanPtr aRemoveEdges =
157     aBase->boolean(FeaturesPlugin_BooleanFuse::REMOVE_INTERSECTION_EDGES_ID());
158   bool aUseFuzzy = aBase->boolean(FeaturesPlugin_BooleanFuse::USE_FUZZY_ID())->value();
159   double aFuzzy = aBase->real(FeaturesPlugin_BooleanFuse::FUZZY_PARAM_ID())->value();
160
161   theDumper << "(" << aDocName << ", " << anObjects;
162
163   if (aMode->value() == FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED()) {
164     theDumper << ", " << aTools;
165   }
166
167   if (aRemoveEdges->value()) {
168     theDumper << ", removeEdges = True";
169   }
170
171   if (aUseFuzzy)
172     theDumper << ", fuzzyParam = " << aFuzzy;
173
174   if (!aBase->data()->version().empty())
175     theDumper << ", keepSubResults = True";
176
177   theDumper << ")" << std::endl;
178 }
179
180 //==================================================================================================
181 BooleanFusePtr addFuse(const std::shared_ptr<ModelAPI_Document>& thePart,
182                        const std::list<ModelHighAPI_Selection>& theMainObjects,
183                        const std::pair<std::list<ModelHighAPI_Selection>, bool>& theToolObjects,
184                        const bool theRemoveEdges,
185                        const ModelHighAPI_Double& fuzzyParam,
186                        const bool keepSubResults)
187 {
188   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_BooleanFuse::ID());
189   if (!keepSubResults)
190     aFeature->data()->setVersion("");
191
192   bool aRemoveEdges = theRemoveEdges;
193   if (theToolObjects.first.empty())
194     aRemoveEdges = aRemoveEdges || theToolObjects.second;
195
196   BooleanFusePtr aFuse;
197   if (theToolObjects.first.empty())
198     aFuse.reset(new FeaturesAPI_BooleanFuse(aFeature, theMainObjects, aRemoveEdges, fuzzyParam));
199   else {
200     aFuse.reset(new FeaturesAPI_BooleanFuse(aFeature, theMainObjects, theToolObjects.first,
201                                             aRemoveEdges, fuzzyParam));
202   }
203   return aFuse;
204 }