1 // Copyright (C) 2014-2020 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 "FeaturesPlugin_Tools.h"
24 #include <ModelAPI_ResultBody.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>
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 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
53 //==================================================================================================
54 void FeaturesPlugin_BooleanSmash::execute()
57 GeomAPI_ShapeHierarchy anObjectsHistory, aToolsHistory;
60 // Getting objects and tools.
61 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHistory, aPlanes) ||
62 !processAttribute(TOOL_LIST_ID(), aToolsHistory, aPlanes))
67 if (anObjectsHistory.empty() || aToolsHistory.empty()) {
68 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
69 setError(aFeatureError);
73 // Collecting all shapes which will be smashed.
74 ListOfShape aShapesToSmash = anObjectsHistory.objects();
76 // List of original shapes for naming.
77 ListOfShape anOriginalShapes;
78 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToSmash.begin(), aShapesToSmash.end());
79 ListOfShape aTools = aToolsHistory.objects();
80 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
82 // Collecting solids from compsolids which will not be modified in
83 // boolean operation and will be added to result.
84 ListOfShape aShapesToAdd;
85 for (GeomAPI_ShapeHierarchy::iterator anIt = anObjectsHistory.begin();
86 anIt != anObjectsHistory.end();
89 GeomShapePtr aParent = anObjectsHistory.parent(*anIt, false);
91 anOriginalShapes.push_back(aParent);
93 ListOfShape aUsed, aNotUsed;
94 anObjectsHistory.splitCompound(aParent, aUsed, aNotUsed);
95 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
97 // add unused shapes of compounds/compsolids to the history,
98 // to avoid treating them as unused later when constructing a compound containing
99 // the result of Smash and all unused sub-shapes of multi-level compounds
100 for (ListOfShape::iterator aNUIt = aNotUsed.begin(); aNUIt != aNotUsed.end(); ++aNUIt)
101 anObjectsHistory.addObject(*aNUIt);
105 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
106 if (!aShapesToAdd.empty()) {
107 // Cut objects with not used solids.
108 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
109 new GeomAlgoAPI_Boolean(aShapesToSmash,
111 GeomAlgoAPI_Tools::BOOL_CUT));
113 if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
114 aShapesToSmash.clear();
115 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
116 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
119 // Cut tools with not used solids.
120 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
121 new GeomAlgoAPI_Boolean(aTools,
123 GeomAlgoAPI_Tools::BOOL_CUT));
125 if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
127 aTools.push_back(aToolsCutAlgo->shape());
128 aMakeShapeList->appendAlgo(aToolsCutAlgo);
132 // Cut objects with tools.
133 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
134 new GeomAlgoAPI_Boolean(aShapesToSmash,
136 GeomAlgoAPI_Tools::BOOL_CUT));
138 // Checking that the algorithm worked properly.
139 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
144 aMakeShapeList->appendAlgo(aBoolAlgo);
146 // Put all (cut result, tools and not used solids) to PaveFiller.
147 GeomShapePtr aShape = aBoolAlgo->shape();
148 GeomAPI_ShapeIterator anIt(aShape);
149 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
150 aShapesToAdd.push_back(aShape);
152 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
154 if (aShapesToAdd.size() == 1) {
155 aShape = aShapesToAdd.front();
158 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
159 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
160 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
165 aShape = aFillerAlgo->shape();
166 aMakeShapeList->appendAlgo(aFillerAlgo);
169 // take into account a version of SMASH feature
170 if (data()->version() == BOP_VERSION_9_4()) {
171 // merge hierarchies of compounds containing objects and tools
172 // and append the result of the FUSE operation
173 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHistory, aToolsHistory, aMakeShapeList);
176 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
178 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
184 setResult(aResultBody, aResultIndex);
187 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
193 // remove the rest results if there were produced in the previous pass
194 removeResults(aResultIndex);