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 //==================================================================================================
45 static void explodeCompound(const GeomShapePtr& theShape, ListOfShape& theResult)
47 if (theShape->shapeType() == GeomAPI_Shape::COMPOUND) {
48 GeomAPI_ShapeIterator it(theShape);
49 for (; it.more(); it.next())
50 theResult.push_back(it.current());
52 theResult.push_back(theShape);
55 static void collectSolids(const ListOfShape& theShapes, ListOfShape& theResult)
57 for (ListOfShape::const_iterator it = theShapes.begin(); it != theShapes.end(); ++it)
58 explodeCompound(*it, theResult);
61 //==================================================================================================
62 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
63 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
67 //==================================================================================================
68 void FeaturesPlugin_BooleanFuse::initAttributes()
70 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
72 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
73 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
75 data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
76 // Initialize the fuzzy parameter with a value below Precision::Confusion() to indicate,
77 // that the internal algorithms should use their default fuzzy value, if none was specified
79 real(FUZZY_PARAM_ID())->setValue(1.e-8);
81 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
83 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
84 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
86 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
89 //==================================================================================================
90 void FeaturesPlugin_BooleanFuse::execute()
93 GeomAPI_ShapeHierarchy anObjectsHierarchy, aToolsHierarchy;
96 bool isSimpleCreation = false;
98 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
99 if (aCreationMethodAttr.get()
100 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
102 isSimpleCreation = true;
106 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
110 if (!isSimpleCreation &&
111 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
114 ListOfShape anObjects, aTools, anEdgesAndFaces;
115 // all objects except edges and faces
116 anObjectsHierarchy.objectsByType(anEdgesAndFaces, anObjects,
117 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
118 aToolsHierarchy.objectsByType(anEdgesAndFaces, aTools,
119 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
121 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
122 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
123 setError(aFeatureError);
127 // Getting fuzzy parameter.
128 // Used as additional tolerance to eliminate tiny results.
129 double aFuzzy = real(FUZZY_PARAM_ID())->value();
131 // version of FUSE feature
132 const std::string aFuseVersion = data()->version();
134 // Collecting all solids which will be fused.
135 // We explode the top-level compounds here because of issue #19931. It performs Fuse operation
136 // on a set of compounds, one of which is treated as self-intersected.
137 // But this problem is eliminated after the exploding, because in this case,
138 // the shapes are intersected, but not self-intersected.
139 ListOfShape aSolidsToFuse;
140 collectSolids(anObjects, aSolidsToFuse);
141 collectSolids(aTools, aSolidsToFuse);
143 // Collecting solids from compsolids which will not be modified
144 // in boolean operation and will be added to result.
145 bool isProcessCompsolid = !isSimpleCreation || !aFuseVersion.empty();
146 ListOfShape aShapesToAdd;
147 int aNbCompsolids = 0; // number of compsolids, which subs is taken into operation
148 bool hasSeparateSolids = false; // are solids or full results exist
149 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjectsHierarchy.begin();
150 isProcessCompsolid && anObjectsIt != anObjectsHierarchy.end();
152 GeomShapePtr anObject = *anObjectsIt;
153 GeomShapePtr aParent = anObjectsHierarchy.parent(anObject, false);
155 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
157 // mark all subs of this parent as precessed to avoid handling twice
158 aParent = anObjectsHierarchy.parent(anObject);
160 ListOfShape aUsed, aNotUsed;
161 anObjectsHierarchy.splitCompound(aParent, aUsed, aNotUsed);
162 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
165 hasSeparateSolids = true;
167 bool isSingleCompsolid = aNbCompsolids == 1 && !hasSeparateSolids;
169 ListOfShape anOriginalShapes = aSolidsToFuse;
170 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
172 // Cut edges and faces(if we have any) with solids.
173 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
174 GeomShapePtr aCuttedEdgesAndFaces;
175 if (!anEdgesAndFaces.empty()) {
176 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
177 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT, aFuzzy));
178 if (aCutAlgo->isDone()) {
179 aCuttedEdgesAndFaces = aCutAlgo->shape();
180 aMakeShapeList->appendAlgo(aCutAlgo);
184 if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
185 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
186 anEdgesAndFaces.end());
189 // If we have compsolids then cut with not used solids all others.
190 if (!aShapesToAdd.empty() && !isSingleCompsolid) {
191 aSolidsToFuse.clear();
192 for (ListOfShape::iterator
193 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
194 ListOfShape aOneObjectList;
195 aOneObjectList.push_back(*anIt);
196 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
197 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT, aFuzzy));
199 if (GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
200 aSolidsToFuse.push_back(aCutAlgo->shape());
201 aMakeShapeList->appendAlgo(aCutAlgo);
206 if (!aSolidsToFuse.empty()) {
208 anObjects.push_back(aSolidsToFuse.back());
209 aSolidsToFuse.pop_back();
210 aTools = aSolidsToFuse;
213 // Fuse all objects and all tools.
215 if (anObjects.size() == 1 && aTools.empty()) {
216 aShape = anObjects.front();
217 } else if (anObjects.empty() && aTools.size() == 1) {
218 aShape = aTools.front();
219 } else if ((anObjects.size() + aTools.size()) > 1) {
220 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
222 GeomAlgoAPI_Tools::BOOL_FUSE,
225 // Checking that the algorithm worked properly.
226 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
231 aShape = aFuseAlgo->shape();
232 aMakeShapeList->appendAlgo(aFuseAlgo);
235 // Combine result with not used solids from compsolid and edges and faces (if we have any).
236 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
237 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
239 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
241 if (!aShapesToAdd.empty()) {
243 aShapesToAdd.push_back(aShape);
245 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
246 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true, aFuzzy));
247 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
252 aShape = aFillerAlgo->shape();
253 aMakeShapeList->appendAlgo(aFillerAlgo);
256 bool isRemoveEdges = false;
257 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
258 if (removeEdgesAttr.get()) {
259 isRemoveEdges = removeEdgesAttr->value();
263 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
264 new GeomAlgoAPI_UnifySameDomain(aShape));
266 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
271 aShape = aUnifyAlgo->shape();
272 aMakeShapeList->appendAlgo(aUnifyAlgo);
275 if (aFuseVersion == BOP_VERSION_9_4()) {
276 // merge hierarchies of compounds containing objects and tools
277 // and append the result of the FUSE operation
278 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
281 int aResultIndex = 0;
283 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
285 ListOfShape anEmptyTools;
286 ModelAPI_Tools::loadModifiedShapes(aResultBody,
291 setResult(aResultBody, aResultIndex);
294 ModelAPI_Tools::loadDeletedShapes(aResultBody,
300 // remove the rest results if there were produced in the previous pass
301 removeResults(aResultIndex);