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 "FeaturesPlugin_Tools.h"
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>
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>
41 #include <GeomAPI_ShapeExplorer.h>
42 #include <GeomAPI_ShapeIterator.h>
44 static void explodeCompound(const GeomShapePtr& theShape, ListOfShape& theResult)
46 if (theShape->shapeType() == GeomAPI_Shape::COMPOUND) {
47 GeomAPI_ShapeIterator it(theShape);
48 for (; it.more(); it.next())
49 theResult.push_back(it.current());
51 theResult.push_back(theShape);
54 static void collectSolids(const ListOfShape& theShapes, ListOfShape& theResult)
56 for (ListOfShape::const_iterator it = theShapes.begin(); it != theShapes.end(); ++it)
57 explodeCompound(*it, theResult);
60 //==================================================================================================
61 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
62 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
66 //==================================================================================================
67 void FeaturesPlugin_BooleanFuse::initAttributes()
69 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
71 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
72 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
74 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
76 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
77 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
79 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
82 //==================================================================================================
83 void FeaturesPlugin_BooleanFuse::execute()
86 GeomAPI_ShapeHierarchy anObjectsHierarchy, aToolsHierarchy;
89 bool isSimpleCreation = false;
91 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
92 if (aCreationMethodAttr.get()
93 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
95 isSimpleCreation = true;
99 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
103 if (!isSimpleCreation &&
104 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
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);
114 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
115 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
116 setError(aFeatureError);
120 // version of FUSE feature
121 const std::string aFuseVersion = data()->version();
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);
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();
141 GeomShapePtr anObject = *anObjectsIt;
142 GeomShapePtr aParent = anObjectsHierarchy.parent(anObject, false);
144 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
146 // mark all subs of this parent as precessed to avoid handling twice
147 aParent = anObjectsHierarchy.parent(anObject);
149 ListOfShape aUsed, aNotUsed;
150 anObjectsHierarchy.splitCompound(aParent, aUsed, aNotUsed);
151 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
154 hasSeparateSolids = true;
156 bool isSingleCompsolid = aNbCompsolids == 1 && !hasSeparateSolids;
158 ListOfShape anOriginalShapes = aSolidsToFuse;
159 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
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);
173 if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
174 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
175 anEdgesAndFaces.end());
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));
188 if (GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
189 aSolidsToFuse.push_back(aCutAlgo->shape());
190 aMakeShapeList->appendAlgo(aCutAlgo);
195 if (!aSolidsToFuse.empty()) {
197 anObjects.push_back(aSolidsToFuse.back());
198 aSolidsToFuse.pop_back();
199 aTools = aSolidsToFuse;
202 // Fuse all objects and all tools.
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,
211 GeomAlgoAPI_Tools::BOOL_FUSE));
213 // Checking that the algorithm worked properly.
214 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
219 aShape = aFuseAlgo->shape();
220 aMakeShapeList->appendAlgo(aFuseAlgo);
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);
227 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
229 if (!aShapesToAdd.empty()) {
231 aShapesToAdd.push_back(aShape);
233 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
234 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
235 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
240 aShape = aFillerAlgo->shape();
241 aMakeShapeList->appendAlgo(aFillerAlgo);
244 bool isRemoveEdges = false;
245 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
246 if (removeEdgesAttr.get()) {
247 isRemoveEdges = removeEdgesAttr->value();
251 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
252 new GeomAlgoAPI_UnifySameDomain(aShape));
254 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
259 aShape = aUnifyAlgo->shape();
260 aMakeShapeList->appendAlgo(aUnifyAlgo);
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);
269 int aResultIndex = 0;
271 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
273 ListOfShape anEmptyTools;
274 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
279 setResult(aResultBody, aResultIndex);
282 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
288 // remove the rest results if there were produced in the previous pass
289 removeResults(aResultIndex);