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_AttributeDouble.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_PaveFiller.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_Tools.h>
34 #include <GeomAPI_ShapeExplorer.h>
35 #include <GeomAPI_ShapeIterator.h>
38 //==================================================================================================
39 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
40 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
44 //==================================================================================================
45 void FeaturesPlugin_BooleanSmash::initAttributes()
47 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
48 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
50 data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
51 // Initialize the fuzzy parameter with a value below Precision::Confusion() to indicate,
52 // that the internal algorithms should use their default fuzzy value, if none was specified
54 real(FUZZY_PARAM_ID())->setValue(1.e-8);
56 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
59 //==================================================================================================
60 void FeaturesPlugin_BooleanSmash::execute()
63 GeomAPI_ShapeHierarchy anObjectsHistory, aToolsHistory;
66 // Getting objects and tools.
67 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHistory, aPlanes) ||
68 !processAttribute(TOOL_LIST_ID(), aToolsHistory, aPlanes))
73 if (anObjectsHistory.empty() || aToolsHistory.empty()) {
74 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
75 setError(aFeatureError);
79 // Collecting all shapes which will be smashed.
80 ListOfShape aShapesToSmash = anObjectsHistory.objects();
82 // List of original shapes for naming.
83 ListOfShape anOriginalShapes;
84 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToSmash.begin(), aShapesToSmash.end());
85 ListOfShape aTools = aToolsHistory.objects();
86 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
88 // Collecting solids from compsolids which will not be modified in
89 // boolean operation and will be added to result.
90 ListOfShape aShapesToAdd;
91 for (GeomAPI_ShapeHierarchy::iterator anIt = anObjectsHistory.begin();
92 anIt != anObjectsHistory.end();
95 GeomShapePtr aParent = anObjectsHistory.parent(*anIt, false);
97 anOriginalShapes.push_back(aParent);
99 ListOfShape aUsed, aNotUsed;
100 anObjectsHistory.splitCompound(aParent, aUsed, aNotUsed);
101 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
103 // add unused shapes of compounds/compsolids to the history,
104 // to avoid treating them as unused later when constructing a compound containing
105 // the result of Smash and all unused sub-shapes of multi-level compounds
106 for (ListOfShape::iterator aNUIt = aNotUsed.begin(); aNUIt != aNotUsed.end(); ++aNUIt)
107 anObjectsHistory.addObject(*aNUIt);
111 // Getting fuzzy parameter.
112 // Used as additional tolerance to eliminate tiny results.
113 double aFuzzy = real(FUZZY_PARAM_ID())->value();
115 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
116 if (!aShapesToAdd.empty()) {
117 // Cut objects with not used solids.
118 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
119 new GeomAlgoAPI_Boolean(aShapesToSmash,
121 GeomAlgoAPI_Tools::BOOL_CUT,
124 if (GeomAlgoAPI_ShapeTools::area(anObjectsCutAlgo->shape()) > 1.e-27) {
125 aShapesToSmash.clear();
126 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
127 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
130 // Cut tools with not used solids.
131 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
132 new GeomAlgoAPI_Boolean(aTools,
134 GeomAlgoAPI_Tools::BOOL_CUT,
137 if (GeomAlgoAPI_ShapeTools::area(aToolsCutAlgo->shape()) > 1.e-27) {
139 aTools.push_back(aToolsCutAlgo->shape());
140 aMakeShapeList->appendAlgo(aToolsCutAlgo);
144 // Cut objects with tools.
145 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
146 new GeomAlgoAPI_Boolean(aShapesToSmash,
148 GeomAlgoAPI_Tools::BOOL_CUT,
151 // Checking that the algorithm worked properly.
152 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
157 aMakeShapeList->appendAlgo(aBoolAlgo);
159 // Put all (cut result, tools and not used solids) to PaveFiller.
160 GeomShapePtr aShape = aBoolAlgo->shape();
161 GeomAPI_ShapeIterator anIt(aShape);
162 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
163 aShapesToAdd.push_back(aShape);
165 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
167 if (aShapesToAdd.size() == 1) {
168 aShape = aShapesToAdd.front();
171 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
172 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true, aFuzzy));
173 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
178 aShape = aFillerAlgo->shape();
179 aMakeShapeList->appendAlgo(aFillerAlgo);
182 // take into account a version of SMASH feature
183 if (data()->version() == BOP_VERSION_9_4()) {
184 // merge hierarchies of compounds containing objects and tools
185 // and append the result of the FUSE operation
186 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHistory, aToolsHistory, aMakeShapeList);
189 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
191 ModelAPI_Tools::loadModifiedShapes(aResultBody,
197 setResult(aResultBody, aResultIndex);
200 ModelAPI_Tools::loadDeletedShapes(aResultBody,
206 // remove the rest results if there were produced in the previous pass
207 removeResults(aResultIndex);