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_BooleanSmash.h"
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeBoolean.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_Tools.h>
28 #include <GeomAlgoAPI_Boolean.h>
29 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_Tools.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAPI_ShapeIterator.h>
39 static const double DEFAULT_FUZZY = 1.e-5;
42 //==================================================================================================
43 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
44 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
48 //==================================================================================================
49 void FeaturesPlugin_BooleanSmash::initAttributes()
51 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
52 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54 data()->addAttribute(USE_FUZZY_ID(), ModelAPI_AttributeBoolean::typeId());
55 data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
56 boolean(USE_FUZZY_ID())->setValue(false); // Do NOT use the fuzzy parameter by default.
57 real(FUZZY_PARAM_ID())->setValue(DEFAULT_FUZZY);
59 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
62 //==================================================================================================
63 void FeaturesPlugin_BooleanSmash::execute()
66 GeomAPI_ShapeHierarchy anObjectsHistory, aToolsHistory;
69 // Getting objects and tools.
70 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHistory, aPlanes) ||
71 !processAttribute(TOOL_LIST_ID(), aToolsHistory, aPlanes))
76 if (anObjectsHistory.empty() || aToolsHistory.empty()) {
77 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
78 setError(aFeatureError);
82 // Collecting all shapes which will be smashed.
83 ListOfShape aShapesToSmash = anObjectsHistory.objects();
85 // List of original shapes for naming.
86 ListOfShape anOriginalShapes;
87 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToSmash.begin(), aShapesToSmash.end());
88 ListOfShape aTools = aToolsHistory.objects();
89 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
91 // Collecting solids from compsolids which will not be modified in
92 // boolean operation and will be added to result.
93 ListOfShape aShapesToAdd;
94 for (GeomAPI_ShapeHierarchy::iterator anIt = anObjectsHistory.begin();
95 anIt != anObjectsHistory.end();
98 GeomShapePtr aParent = anObjectsHistory.parent(*anIt, false);
100 anOriginalShapes.push_back(aParent);
102 ListOfShape aUsed, aNotUsed;
103 anObjectsHistory.splitCompound(aParent, aUsed, aNotUsed);
104 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
106 // add unused shapes of compounds/compsolids to the history,
107 // to avoid treating them as unused later when constructing a compound containing
108 // the result of Smash and all unused sub-shapes of multi-level compounds
109 for (ListOfShape::iterator aNUIt = aNotUsed.begin(); aNUIt != aNotUsed.end(); ++aNUIt)
110 anObjectsHistory.addObject(*aNUIt);
114 // Getting fuzzy parameter.
115 // Used as additional tolerance to eliminate tiny results.
116 // Using -1 as fuzzy value in the GeomAlgoAPI means to ignore it during the boolean operation!
117 bool aUseFuzzy = boolean(USE_FUZZY_ID())->value();
118 double aFuzzy = (aUseFuzzy ? real(FUZZY_PARAM_ID())->value() : -1);
120 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
121 if (!aShapesToAdd.empty()) {
122 // Cut objects with not used solids.
123 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
124 new GeomAlgoAPI_Boolean(aShapesToSmash,
126 GeomAlgoAPI_Tools::BOOL_CUT,
129 if (GeomAlgoAPI_ShapeTools::area(anObjectsCutAlgo->shape()) > 1.e-27) {
130 aShapesToSmash.clear();
131 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
132 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
135 // Cut tools with not used solids.
136 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
137 new GeomAlgoAPI_Boolean(aTools,
139 GeomAlgoAPI_Tools::BOOL_CUT,
142 if (GeomAlgoAPI_ShapeTools::area(aToolsCutAlgo->shape()) > 1.e-27) {
144 aTools.push_back(aToolsCutAlgo->shape());
145 aMakeShapeList->appendAlgo(aToolsCutAlgo);
149 // Cut objects with tools.
150 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
151 new GeomAlgoAPI_Boolean(aShapesToSmash,
153 GeomAlgoAPI_Tools::BOOL_CUT,
156 // Checking that the algorithm worked properly.
157 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
162 aMakeShapeList->appendAlgo(aBoolAlgo);
164 // Put all (cut result, tools and not used solids) to PaveFiller.
165 GeomShapePtr aShape = aBoolAlgo->shape();
166 GeomAPI_ShapeIterator anIt(aShape);
167 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
168 aShapesToAdd.push_back(aShape);
170 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
172 if (aShapesToAdd.size() == 1) {
173 aShape = aShapesToAdd.front();
176 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
177 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true, aFuzzy));
178 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
183 aShape = aFillerAlgo->shape();
184 aMakeShapeList->appendAlgo(aFillerAlgo);
187 // take into account a version of SMASH feature
188 if (data()->version() == BOP_VERSION_9_4()) {
189 // merge hierarchies of compounds containing objects and tools
190 // and append the result of the FUSE operation
191 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHistory, aToolsHistory, aMakeShapeList);
194 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
196 ModelAPI_Tools::loadModifiedShapes(aResultBody,
202 setResult(aResultBody, aResultIndex);
205 ModelAPI_Tools::loadDeletedShapes(aResultBody,
211 // remove the rest results if there were produced in the previous pass
212 removeResults(aResultIndex);