1 // Copyright (C) 2014-2021 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_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>
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>
39 #include <GeomAPI_ShapeExplorer.h>
40 #include <GeomAPI_ShapeIterator.h>
42 static void explodeCompound(const GeomShapePtr& theShape, ListOfShape& theResult)
44 if (theShape->shapeType() == GeomAPI_Shape::COMPOUND) {
45 GeomAPI_ShapeIterator it(theShape);
46 for (; it.more(); it.next())
47 theResult.push_back(it.current());
49 theResult.push_back(theShape);
52 static void collectSolids(const ListOfShape& theShapes, ListOfShape& theResult)
54 for (ListOfShape::const_iterator it = theShapes.begin(); it != theShapes.end(); ++it)
55 explodeCompound(*it, theResult);
58 //==================================================================================================
59 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
60 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
64 //==================================================================================================
65 void FeaturesPlugin_BooleanFuse::initAttributes()
67 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
69 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
70 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
72 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
74 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
75 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
77 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
80 //==================================================================================================
81 void FeaturesPlugin_BooleanFuse::execute()
84 GeomAPI_ShapeHierarchy anObjectsHierarchy, aToolsHierarchy;
87 bool isSimpleCreation = false;
89 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
90 if (aCreationMethodAttr.get()
91 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
93 isSimpleCreation = true;
97 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
101 if (!isSimpleCreation &&
102 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
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);
112 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
113 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
114 setError(aFeatureError);
118 // version of FUSE feature
119 const std::string aFuseVersion = data()->version();
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);
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();
139 GeomShapePtr anObject = *anObjectsIt;
140 GeomShapePtr aParent = anObjectsHierarchy.parent(anObject, false);
142 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
144 // mark all subs of this parent as precessed to avoid handling twice
145 aParent = anObjectsHierarchy.parent(anObject);
147 ListOfShape aUsed, aNotUsed;
148 anObjectsHierarchy.splitCompound(aParent, aUsed, aNotUsed);
149 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
152 hasSeparateSolids = true;
154 bool isSingleCompsolid = aNbCompsolids == 1 && !hasSeparateSolids;
156 ListOfShape anOriginalShapes = aSolidsToFuse;
157 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
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);
171 if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
172 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
173 anEdgesAndFaces.end());
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));
186 if (GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
187 aSolidsToFuse.push_back(aCutAlgo->shape());
188 aMakeShapeList->appendAlgo(aCutAlgo);
193 if (!aSolidsToFuse.empty()) {
195 anObjects.push_back(aSolidsToFuse.back());
196 aSolidsToFuse.pop_back();
197 aTools = aSolidsToFuse;
200 // Fuse all objects and all tools.
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,
209 GeomAlgoAPI_Tools::BOOL_FUSE));
211 // Checking that the algorithm worked properly.
212 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
217 aShape = aFuseAlgo->shape();
218 aMakeShapeList->appendAlgo(aFuseAlgo);
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);
225 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
227 if (!aShapesToAdd.empty()) {
229 aShapesToAdd.push_back(aShape);
231 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
232 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
233 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
238 aShape = aFillerAlgo->shape();
239 aMakeShapeList->appendAlgo(aFillerAlgo);
242 bool isRemoveEdges = false;
243 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
244 if (removeEdgesAttr.get()) {
245 isRemoveEdges = removeEdgesAttr->value();
249 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
250 new GeomAlgoAPI_UnifySameDomain(aShape));
252 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
257 aShape = aUnifyAlgo->shape();
258 aMakeShapeList->appendAlgo(aUnifyAlgo);
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);
267 int aResultIndex = 0;
269 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
271 ListOfShape anEmptyTools;
272 ModelAPI_Tools::loadModifiedShapes(aResultBody,
277 setResult(aResultBody, aResultIndex);
280 ModelAPI_Tools::loadDeletedShapes(aResultBody,
286 // remove the rest results if there were produced in the previous pass
287 removeResults(aResultIndex);