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