Salome HOME
Change the paradigm of versioning of Boolean Operations on the Python API level.
[modules/shaper.git] / src / FeaturesAPI / FeaturesAPI_BooleanFuse.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 "FeaturesAPI_BooleanFuse.h"
21
22 #include <ModelHighAPI_Dumper.h>
23 #include <ModelHighAPI_Selection.h>
24 #include <ModelHighAPI_Tools.h>
25
26 //==================================================================================================
27 FeaturesAPI_BooleanFuse::FeaturesAPI_BooleanFuse(
28   const std::shared_ptr<ModelAPI_Feature>& theFeature)
29 : ModelHighAPI_Interface(theFeature)
30 {
31   initialize();
32 }
33
34 //==================================================================================================
35 FeaturesAPI_BooleanFuse::FeaturesAPI_BooleanFuse(
36   const std::shared_ptr<ModelAPI_Feature>& theFeature,
37   const std::list<ModelHighAPI_Selection>& theMainObjects,
38   const bool theRemoveEdges,
39   const int theVersion)
40   : ModelHighAPI_Interface(theFeature)
41 {
42   if (initialize()) {
43     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_SIMPLE(), mycreationMethod);
44     fillAttribute(theVersion, theFeature->integer(FeaturesPlugin_Boolean::VERSION_ID()));
45     fillAttribute(theMainObjects, mymainObjects);
46     fillAttribute(theRemoveEdges, myremoveEdges);
47
48     execute(false);
49   }
50 }
51
52 //==================================================================================================
53 FeaturesAPI_BooleanFuse::FeaturesAPI_BooleanFuse(
54   const std::shared_ptr<ModelAPI_Feature>& theFeature,
55   const std::list<ModelHighAPI_Selection>& theMainObjects,
56   const std::list<ModelHighAPI_Selection>& theToolObjects,
57   const bool theRemoveEdges,
58   const int theVersion)
59 : ModelHighAPI_Interface(theFeature)
60 {
61   if(initialize()) {
62     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED(), mycreationMethod);
63     fillAttribute(theVersion, theFeature->integer(FeaturesPlugin_Boolean::VERSION_ID()));
64     fillAttribute(theMainObjects, mymainObjects);
65     fillAttribute(theToolObjects, mytoolObjects);
66     fillAttribute(theRemoveEdges, myremoveEdges);
67
68     execute(false);
69   }
70 }
71
72 //==================================================================================================
73 FeaturesAPI_BooleanFuse::~FeaturesAPI_BooleanFuse()
74 {
75 }
76
77
78 //==================================================================================================
79 void FeaturesAPI_BooleanFuse::setMainObjects(
80   const std::list<ModelHighAPI_Selection>& theMainObjects)
81 {
82   fillAttribute(theMainObjects, mymainObjects);
83
84   execute();
85 }
86
87 //==================================================================================================
88 void FeaturesAPI_BooleanFuse::setToolObjects(
89   const std::list<ModelHighAPI_Selection>& theToolObjects)
90 {
91   fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED(), mycreationMethod);
92   fillAttribute(theToolObjects, mytoolObjects);
93
94   execute();
95 }
96
97 //==================================================================================================
98 void FeaturesAPI_BooleanFuse::setRemoveEdges(const bool theRemoveEdges)
99 {
100   fillAttribute(theRemoveEdges, myremoveEdges);
101
102   execute();
103 }
104
105 //==================================================================================================
106 void FeaturesAPI_BooleanFuse::setAdvancedMode(const bool theMode)
107 {
108   if (theMode) {
109     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED(), mycreationMethod);
110   } else {
111     fillAttribute(FeaturesPlugin_BooleanFuse::CREATION_METHOD_SIMPLE(), mycreationMethod);
112   }
113
114   execute();
115 }
116
117 //==================================================================================================
118 void FeaturesAPI_BooleanFuse::dump(ModelHighAPI_Dumper& theDumper) const
119 {
120   FeaturePtr aBase = feature();
121
122   theDumper << aBase << " = model.addFuse";
123
124   const std::string& aDocName = theDumper.name(aBase->document());
125   AttributeStringPtr aMode = aBase->string(FeaturesPlugin_BooleanFuse::CREATION_METHOD());
126   AttributeSelectionListPtr anObjects =
127     aBase->selectionList(FeaturesPlugin_BooleanFuse::OBJECT_LIST_ID());
128   AttributeSelectionListPtr aTools =
129     aBase->selectionList(FeaturesPlugin_BooleanFuse::TOOL_LIST_ID());
130   AttributeBooleanPtr aRemoveEdges =
131     aBase->boolean(FeaturesPlugin_BooleanFuse::REMOVE_INTERSECTION_EDGES_ID());
132   AttributeIntegerPtr aVersion =
133     aBase->integer(FeaturesPlugin_BooleanFuse::VERSION_ID());
134
135   theDumper << "(" << aDocName << ", " << anObjects;
136
137   if (aMode->value() == FeaturesPlugin_BooleanFuse::CREATION_METHOD_ADVANCED()) {
138     theDumper << ", " << aTools;
139   }
140
141   if (aRemoveEdges->value()) {
142     theDumper << ", removeEdges = True";
143   }
144
145   if (aVersion && aVersion->isInitialized() &&
146       aVersion->value() == FeaturesPlugin_VersionedBoolean::THE_VERSION_1) {
147     theDumper << ", keepSubResults = True";
148   }
149
150   theDumper << ")" << std::endl;
151 }
152
153 //==================================================================================================
154 static BooleanFusePtr addFuse(const std::shared_ptr<ModelAPI_Document>& thePart,
155                               const std::list<ModelHighAPI_Selection>& theObjects,
156                               const bool theRemoveEdges,
157                               const int theVersion)
158 {
159   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_BooleanFuse::ID());
160   return BooleanFusePtr(new FeaturesAPI_BooleanFuse(aFeature,
161                                                     theObjects,
162                                                     theRemoveEdges,
163                                                     theVersion));
164 }
165
166 //==================================================================================================
167 static BooleanFusePtr addFuse(const std::shared_ptr<ModelAPI_Document>& thePart,
168                               const std::list<ModelHighAPI_Selection>& theMainObjects,
169                               const std::list<ModelHighAPI_Selection>& theToolObjects,
170                               const bool theRemoveEdges,
171                               const int theVersion)
172 {
173   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(FeaturesAPI_BooleanFuse::ID());
174   return BooleanFusePtr(new FeaturesAPI_BooleanFuse(aFeature,
175                                                     theMainObjects,
176                                                     theToolObjects,
177                                                     theRemoveEdges,
178                                                     theVersion));
179 }
180
181 //==================================================================================================
182 BooleanFusePtr addFuse(const std::shared_ptr<ModelAPI_Document>& thePart,
183                        const std::list<ModelHighAPI_Selection>& theMainObjects,
184                        const std::pair<std::list<ModelHighAPI_Selection>, bool>& theToolObjects,
185                        const bool theRemoveEdges,
186                        const bool keepSubResults)
187 {
188   int aVersion = keepSubResults ? FeaturesPlugin_VersionedBoolean::THE_VERSION_1
189                                 : FeaturesPlugin_VersionedBoolean::THE_VERSION_0;
190   bool aRemoveEdges = theRemoveEdges;
191   if (theToolObjects.first.empty())
192     aRemoveEdges = aRemoveEdges || theToolObjects.second;
193   return theToolObjects.first.empty() ?
194       addFuse(thePart, theMainObjects, aRemoveEdges, aVersion) :
195       addFuse(thePart, theMainObjects, theToolObjects.first, aRemoveEdges, aVersion);
196 }