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