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