Salome HOME
fixed bug where "Always create result" was required
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_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 "FeaturesPlugin_BooleanFuse.h"
21
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Validator.h>
30
31 #include <GeomAlgoAPI_Boolean.h>
32 #include <GeomAlgoAPI_MakeShapeList.h>
33 #include <GeomAlgoAPI_PaveFiller.h>
34 #include <GeomAlgoAPI_ShapeBuilder.h>
35 #include <GeomAlgoAPI_ShapeTools.h>
36 #include <GeomAlgoAPI_Tools.h>
37 #include <GeomAlgoAPI_UnifySameDomain.h>
38
39 #include <GeomAPI_ShapeExplorer.h>
40 #include <GeomAPI_ShapeIterator.h>
41
42 static void explodeCompound(const GeomShapePtr& theShape, ListOfShape& theResult)
43 {
44   if (theShape->shapeType() == GeomAPI_Shape::COMPOUND) {
45     GeomAPI_ShapeIterator it(theShape);
46     for (; it.more(); it.next())
47       theResult.push_back(it.current());
48   } else
49     theResult.push_back(theShape);
50 }
51
52 static void collectSolids(const ListOfShape& theShapes, ListOfShape& theResult)
53 {
54   for (ListOfShape::const_iterator it = theShapes.begin(); it != theShapes.end(); ++it)
55     explodeCompound(*it, theResult);
56 }
57
58 //==================================================================================================
59 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
60 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
61 {
62 }
63
64 //==================================================================================================
65 void FeaturesPlugin_BooleanFuse::initAttributes()
66 {
67   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
68
69   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
70   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
71
72   data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
73
74   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
75   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
76
77   initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
78 }
79
80 //==================================================================================================
81 void FeaturesPlugin_BooleanFuse::execute()
82 {
83   std::string anError;
84   GeomAPI_ShapeHierarchy anObjectsHierarchy, aToolsHierarchy;
85   ListOfShape aPlanes;
86
87   bool isSimpleCreation = false;
88
89   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
90   if (aCreationMethodAttr.get()
91       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
92   {
93     isSimpleCreation = true;
94   }
95
96   // Getting objects.
97   if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
98     return;
99
100   // Getting tools.
101   if (!isSimpleCreation &&
102       !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
103     return;
104
105   ListOfShape anObjects, aTools, anEdgesAndFaces;
106   // all objects except edges and faces
107   anObjectsHierarchy.objectsByType(anEdgesAndFaces, anObjects,
108                                    GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
109   aToolsHierarchy.objectsByType(anEdgesAndFaces, aTools,
110                                 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
111
112   if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
113     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
114     setError(aFeatureError);
115     return;
116   }
117
118   // version of FUSE feature
119   const std::string aFuseVersion = data()->version();
120
121   // Collecting all solids which will be fused.
122   // We explode the top-level compounds here because of issue #19931. It performs Fuse operation
123   // on a set of compounds, one of which is treated as self-intersected.
124   // But this problem is eliminated after the exploding, because in this case,
125   // the shapes are intersected, but not self-intersected.
126   ListOfShape aSolidsToFuse;
127   collectSolids(anObjects, aSolidsToFuse);
128   collectSolids(aTools, aSolidsToFuse);
129
130   // Collecting solids from compsolids which will not be modified
131   // in boolean operation and will be added to result.
132   bool isProcessCompsolid = !isSimpleCreation || !aFuseVersion.empty();
133   ListOfShape aShapesToAdd;
134   int aNbCompsolids = 0; // number of compsolids, which subs is taken into operation
135   bool hasSeparateSolids = false; // are solids or full results exist
136   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjectsHierarchy.begin();
137        isProcessCompsolid && anObjectsIt != anObjectsHierarchy.end();
138        ++anObjectsIt) {
139     GeomShapePtr anObject = *anObjectsIt;
140     GeomShapePtr aParent = anObjectsHierarchy.parent(anObject, false);
141
142     if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
143       ++aNbCompsolids;
144       // mark all subs of this parent as precessed to avoid handling twice
145       aParent = anObjectsHierarchy.parent(anObject);
146
147       ListOfShape aUsed, aNotUsed;
148       anObjectsHierarchy.splitCompound(aParent, aUsed, aNotUsed);
149       aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
150     }
151     else
152       hasSeparateSolids = true;
153   }
154   bool isSingleCompsolid = aNbCompsolids == 1 && !hasSeparateSolids;
155
156   ListOfShape anOriginalShapes = aSolidsToFuse;
157   anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
158
159   // Cut edges and faces(if we have any) with solids.
160   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
161   GeomShapePtr aCuttedEdgesAndFaces;
162   if (!anEdgesAndFaces.empty()) {
163     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
164       anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
165     if (aCutAlgo->isDone()) {
166       aCuttedEdgesAndFaces = aCutAlgo->shape();
167       aMakeShapeList->appendAlgo(aCutAlgo);
168     }
169   }
170
171   if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
172     anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
173                             anEdgesAndFaces.end());
174   }
175
176   // If we have compsolids then cut with not used solids all others.
177   if (!aShapesToAdd.empty() && !isSingleCompsolid) {
178     aSolidsToFuse.clear();
179     for (ListOfShape::iterator
180          anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
181       ListOfShape aOneObjectList;
182       aOneObjectList.push_back(*anIt);
183       std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
184         new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
185
186       if (GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
187         aSolidsToFuse.push_back(aCutAlgo->shape());
188         aMakeShapeList->appendAlgo(aCutAlgo);
189       }
190     }
191   }
192
193   if (!aSolidsToFuse.empty()) {
194     anObjects.clear();
195     anObjects.push_back(aSolidsToFuse.back());
196     aSolidsToFuse.pop_back();
197     aTools = aSolidsToFuse;
198   }
199
200   // Fuse all objects and all tools.
201   GeomShapePtr aShape;
202   if (anObjects.size() == 1 && aTools.empty()) {
203     aShape = anObjects.front();
204   } else if (anObjects.empty() && aTools.size() == 1) {
205     aShape = aTools.front();
206   } else if ((anObjects.size() + aTools.size()) > 1) {
207     std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
208       aTools,
209       GeomAlgoAPI_Tools::BOOL_FUSE));
210
211     // Checking that the algorithm worked properly.
212     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
213       setError(anError);
214       return;
215     }
216
217     aShape = aFuseAlgo->shape();
218     aMakeShapeList->appendAlgo(aFuseAlgo);
219   }
220
221   // Combine result with not used solids from compsolid and edges and faces (if we have any).
222   if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
223     aShapesToAdd.push_back(aCuttedEdgesAndFaces);
224   } else {
225     aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
226   }
227   if (!aShapesToAdd.empty()) {
228     if (aShape.get()) {
229       aShapesToAdd.push_back(aShape);
230     }
231     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
232       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
233     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
234       setError(anError);
235       return;
236     }
237
238     aShape = aFillerAlgo->shape();
239     aMakeShapeList->appendAlgo(aFillerAlgo);
240   }
241
242   bool isRemoveEdges = false;
243   AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
244   if (removeEdgesAttr.get()) {
245     isRemoveEdges = removeEdgesAttr->value();
246   }
247
248   if (isRemoveEdges) {
249     std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
250       new GeomAlgoAPI_UnifySameDomain(aShape));
251
252     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
253       setError(anError);
254       return;
255     }
256
257     aShape = aUnifyAlgo->shape();
258     aMakeShapeList->appendAlgo(aUnifyAlgo);
259   }
260
261   if (aFuseVersion == BOP_VERSION_9_4()) {
262     // merge hierarchies of compounds containing objects and tools
263     // and append the result of the FUSE operation
264     aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
265   }
266
267   int aResultIndex = 0;
268
269   ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
270
271   ListOfShape anEmptyTools;
272   ModelAPI_Tools::loadModifiedShapes(aResultBody,
273                                      anOriginalShapes,
274                                      anEmptyTools,
275                                      aMakeShapeList,
276                                      aShape);
277   setResult(aResultBody, aResultIndex);
278   aResultIndex++;
279
280   ModelAPI_Tools::loadDeletedShapes(aResultBody,
281                                     GeomShapePtr(),
282                                     anOriginalShapes,
283                                     aMakeShapeList,
284                                     aShape);
285
286   // remove the rest results if there were produced in the previous pass
287   removeResults(aResultIndex);
288 }