1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_BooleanFuse.h"
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>
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>
40 #include <GeomAPI_ShapeExplorer.h>
41 #include <GeomAPI_ShapeIterator.h>
44 static const double DEFAULT_FUZZY = 1.e-5;
47 //==================================================================================================
48 static void explodeCompound(const GeomShapePtr& theShape, ListOfShape& theResult)
50 if (theShape->shapeType() == GeomAPI_Shape::COMPOUND) {
51 GeomAPI_ShapeIterator it(theShape);
52 for (; it.more(); it.next())
53 theResult.push_back(it.current());
55 theResult.push_back(theShape);
58 static void collectSolids(const ListOfShape& theShapes, ListOfShape& theResult)
60 for (ListOfShape::const_iterator it = theShapes.begin(); it != theShapes.end(); ++it)
61 explodeCompound(*it, theResult);
64 //==================================================================================================
65 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
66 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
70 //==================================================================================================
71 void FeaturesPlugin_BooleanFuse::initAttributes()
73 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
75 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
76 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
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);
83 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
85 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
86 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
88 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
91 //==================================================================================================
92 void FeaturesPlugin_BooleanFuse::execute()
95 GeomAPI_ShapeHierarchy anObjectsHierarchy, aToolsHierarchy;
98 bool isSimpleCreation = false;
100 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
101 if (aCreationMethodAttr.get()
102 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
104 isSimpleCreation = true;
108 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
112 if (!isSimpleCreation &&
113 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
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);
123 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
124 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
125 setError(aFeatureError);
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);
135 // version of FUSE feature
136 const std::string aFuseVersion = data()->version();
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);
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();
156 GeomShapePtr anObject = *anObjectsIt;
157 GeomShapePtr aParent = anObjectsHierarchy.parent(anObject, false);
159 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
161 // mark all subs of this parent as precessed to avoid handling twice
162 aParent = anObjectsHierarchy.parent(anObject);
164 ListOfShape aUsed, aNotUsed;
165 anObjectsHierarchy.splitCompound(aParent, aUsed, aNotUsed);
166 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
169 hasSeparateSolids = true;
171 bool isSingleCompsolid = aNbCompsolids == 1 && !hasSeparateSolids;
173 ListOfShape anOriginalShapes = aSolidsToFuse;
174 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
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);
188 if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
189 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
190 anEdgesAndFaces.end());
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));
203 if (GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
204 aSolidsToFuse.push_back(aCutAlgo->shape());
205 aMakeShapeList->appendAlgo(aCutAlgo);
210 if (!aSolidsToFuse.empty()) {
212 anObjects.push_back(aSolidsToFuse.back());
213 aSolidsToFuse.pop_back();
214 aTools = aSolidsToFuse;
217 // Fuse all objects and all tools.
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,
226 GeomAlgoAPI_Tools::BOOL_FUSE,
229 // Checking that the algorithm worked properly.
230 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
235 aShape = aFuseAlgo->shape();
236 aMakeShapeList->appendAlgo(aFuseAlgo);
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);
243 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
245 if (!aShapesToAdd.empty()) {
247 aShapesToAdd.push_back(aShape);
249 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
250 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true, aFuzzy));
251 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
256 aShape = aFillerAlgo->shape();
257 aMakeShapeList->appendAlgo(aFillerAlgo);
260 bool isRemoveEdges = false;
261 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
262 if (removeEdgesAttr.get()) {
263 isRemoveEdges = removeEdgesAttr->value();
267 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
268 new GeomAlgoAPI_UnifySameDomain(aShape));
270 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
275 aShape = aUnifyAlgo->shape();
276 aMakeShapeList->appendAlgo(aUnifyAlgo);
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);
285 int aResultIndex = 0;
287 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
289 ListOfShape anEmptyTools;
290 ModelAPI_Tools::loadModifiedShapes(aResultBody,
295 setResult(aResultBody, aResultIndex);
298 ModelAPI_Tools::loadDeletedShapes(aResultBody,
304 // remove the rest results if there were produced in the previous pass
305 removeResults(aResultIndex);