1 // Copyright (C) 2014-2024 CEA, EDF
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 AttributeStringPtr aMethodAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>
74 (data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId())); // #1
76 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()); // #2
77 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()); // #3
79 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(),
80 ModelAPI_AttributeBoolean::typeId()); // #4 ???
82 AttributeBooleanPtr aUseFuzzyAttr; // #5 ???
83 AttributeDoublePtr aValFuzzyAttr; // #6 ???
85 bool badOrder = false;
86 if (aMethodAttr->isInitialized()) { // restoring data from saved study
87 // It is a fix for 37570 Tuleap issue.
88 // We could have studies with aValFuzzyAttr at the 5th position instead of expected 6th.
90 // Fuzzy value (check, is it present at 5th label)
91 aValFuzzyAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>
92 (data()->addAttribute(FUZZY_PARAM_ID(),
93 ModelAPI_AttributeDouble::typeId())); // #5
94 if (aValFuzzyAttr->isInitialized()) {
96 // bad order of attributes in saved study
100 aUseFuzzyAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
101 (data()->addAttribute(FeaturesPlugin_Boolean::USE_FUZZY_ID(),
102 ModelAPI_AttributeBoolean::typeId(),
104 data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(),
105 ModelAPI_AttributeBoolean::typeId(),
111 // good order of attributes
117 aUseFuzzyAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>
118 (data()->addAttribute(FeaturesPlugin_Boolean::USE_FUZZY_ID(),
119 ModelAPI_AttributeBoolean::typeId(),
122 aValFuzzyAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>
123 (data()->addAttribute(FUZZY_PARAM_ID(),
124 ModelAPI_AttributeDouble::typeId(),
128 if (!aUseFuzzyAttr->isInitialized())
129 aUseFuzzyAttr->setValue(false); // Do NOT use the fuzzy parameter by default.
130 if (!aValFuzzyAttr->isInitialized())
131 aValFuzzyAttr->setValue(DEFAULT_FUZZY);
133 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
134 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
136 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
139 //==================================================================================================
140 void FeaturesPlugin_BooleanFuse::execute()
143 GeomAPI_ShapeHierarchy anObjectsHierarchy, aToolsHierarchy;
146 bool isSimpleCreation = false;
148 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
149 if (aCreationMethodAttr.get()
150 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
152 isSimpleCreation = true;
156 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
160 if (!isSimpleCreation &&
161 !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
164 ListOfShape anObjects, aTools, anEdgesAndFaces;
165 // all objects except edges and faces
166 anObjectsHierarchy.objectsByType(anEdgesAndFaces, anObjects,
167 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
168 aToolsHierarchy.objectsByType(anEdgesAndFaces, aTools,
169 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
171 if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
172 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
173 setError(aFeatureError);
177 // Getting fuzzy parameter.
178 // Used as additional tolerance to eliminate tiny results.
179 // Using -1 as fuzzy value in the GeomAlgoAPI means to ignore it during the boolean operation!
180 bool aUseFuzzy = boolean(USE_FUZZY_ID())->value();
181 double aFuzzy = (aUseFuzzy ? real(FUZZY_PARAM_ID())->value() : -1);
183 // version of FUSE feature
184 const std::string aFuseVersion = data()->version();
186 // Collecting all solids which will be fused.
187 // We explode the top-level compounds here because of issue #19931. It performs Fuse operation
188 // on a set of compounds, one of which is treated as self-intersected.
189 // But this problem is eliminated after the exploding, because in this case,
190 // the shapes are intersected, but not self-intersected.
191 ListOfShape aSolidsToFuse;
192 collectSolids(anObjects, aSolidsToFuse);
193 collectSolids(aTools, aSolidsToFuse);
195 // Collecting solids from compsolids which will not be modified
196 // in boolean operation and will be added to result.
197 bool isProcessCompsolid = !isSimpleCreation || !aFuseVersion.empty();
198 ListOfShape aShapesToAdd;
199 int aNbCompsolids = 0; // number of compsolids, which subs is taken into operation
200 bool hasSeparateSolids = false; // are solids or full results exist
201 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjectsHierarchy.begin();
202 isProcessCompsolid && anObjectsIt != anObjectsHierarchy.end();
204 GeomShapePtr anObject = *anObjectsIt;
205 GeomShapePtr aParent = anObjectsHierarchy.parent(anObject, false);
207 if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
209 // mark all subs of this parent as precessed to avoid handling twice
210 aParent = anObjectsHierarchy.parent(anObject);
212 ListOfShape aUsed, aNotUsed;
213 anObjectsHierarchy.splitCompound(aParent, aUsed, aNotUsed);
214 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
217 hasSeparateSolids = true;
219 bool isSingleCompsolid = aNbCompsolids == 1 && !hasSeparateSolids;
221 ListOfShape anOriginalShapes = aSolidsToFuse;
222 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
224 // Cut edges and faces(if we have any) with solids.
225 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
226 GeomShapePtr aCuttedEdgesAndFaces;
227 if (!anEdgesAndFaces.empty()) {
228 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
229 anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT, aFuzzy));
230 if (aCutAlgo->isDone()) {
231 aCuttedEdgesAndFaces = aCutAlgo->shape();
232 aMakeShapeList->appendAlgo(aCutAlgo);
236 if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
237 anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
238 anEdgesAndFaces.end());
241 // If we have compsolids then cut with not used solids all others.
242 if (!aShapesToAdd.empty() && !isSingleCompsolid) {
243 aSolidsToFuse.clear();
244 for (ListOfShape::iterator
245 anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
246 ListOfShape aOneObjectList;
247 aOneObjectList.push_back(*anIt);
248 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
249 new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT, aFuzzy));
251 if (GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
252 aSolidsToFuse.push_back(aCutAlgo->shape());
253 aMakeShapeList->appendAlgo(aCutAlgo);
258 if (!aSolidsToFuse.empty()) {
260 anObjects.push_back(aSolidsToFuse.back());
261 aSolidsToFuse.pop_back();
262 aTools = aSolidsToFuse;
265 // Fuse all objects and all tools.
267 if (anObjects.size() == 1 && aTools.empty()) {
268 aShape = anObjects.front();
269 } else if (anObjects.empty() && aTools.size() == 1) {
270 aShape = aTools.front();
271 } else if ((anObjects.size() + aTools.size()) > 1) {
272 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
274 GeomAlgoAPI_Tools::BOOL_FUSE,
277 // Checking that the algorithm worked properly.
278 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
283 aShape = aFuseAlgo->shape();
284 aMakeShapeList->appendAlgo(aFuseAlgo);
287 // Combine result with not used solids from compsolid and edges and faces (if we have any).
288 if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
289 aShapesToAdd.push_back(aCuttedEdgesAndFaces);
291 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
293 if (!aShapesToAdd.empty()) {
295 aShapesToAdd.push_back(aShape);
297 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
298 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true, aFuzzy));
299 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
304 aShape = aFillerAlgo->shape();
305 aMakeShapeList->appendAlgo(aFillerAlgo);
308 bool isRemoveEdges = false;
309 AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
310 if (removeEdgesAttr.get()) {
311 isRemoveEdges = removeEdgesAttr->value();
315 std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
316 new GeomAlgoAPI_UnifySameDomain(aShape));
318 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
323 aShape = aUnifyAlgo->shape();
324 aMakeShapeList->appendAlgo(aUnifyAlgo);
327 if (aFuseVersion == BOP_VERSION_9_4()) {
328 // merge hierarchies of compounds containing objects and tools
329 // and append the result of the FUSE operation
330 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
333 int aResultIndex = 0;
335 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
337 ListOfShape anEmptyTools;
338 ModelAPI_Tools::loadModifiedShapes(aResultBody,
343 setResult(aResultBody, aResultIndex);
346 ModelAPI_Tools::loadDeletedShapes(aResultBody,
352 // remove the rest results if there were produced in the previous pass
353 removeResults(aResultIndex);