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_BooleanSmash.h"
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Tools.h>
26 #include <GeomAlgoAPI_Boolean.h>
27 #include <GeomAlgoAPI_CompoundBuilder.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_PaveFiller.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_Tools.h>
33 #include <GeomAPI_ShapeExplorer.h>
34 #include <GeomAPI_ShapeIterator.h>
36 //==================================================================================================
37 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
38 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
42 //==================================================================================================
43 void FeaturesPlugin_BooleanSmash::initAttributes()
45 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
46 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
48 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
51 //==================================================================================================
52 void FeaturesPlugin_BooleanSmash::execute()
55 GeomAPI_ShapeHierarchy anObjectsHistory, aToolsHistory;
58 // Getting objects and tools.
59 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHistory, aPlanes) ||
60 !processAttribute(TOOL_LIST_ID(), aToolsHistory, aPlanes))
65 if (anObjectsHistory.empty() || aToolsHistory.empty()) {
66 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
67 setError(aFeatureError);
71 // Collecting all shapes which will be smashed.
72 ListOfShape aShapesToSmash = anObjectsHistory.objects();
74 // List of original shapes for naming.
75 ListOfShape anOriginalShapes;
76 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToSmash.begin(), aShapesToSmash.end());
77 ListOfShape aTools = aToolsHistory.objects();
78 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
80 // Collecting solids from compsolids which will not be modified in
81 // boolean operation and will be added to result.
82 ListOfShape aShapesToAdd;
83 for (GeomAPI_ShapeHierarchy::iterator anIt = anObjectsHistory.begin();
84 anIt != anObjectsHistory.end();
87 GeomShapePtr aParent = anObjectsHistory.parent(*anIt, false);
89 anOriginalShapes.push_back(aParent);
91 ListOfShape aUsed, aNotUsed;
92 anObjectsHistory.splitCompound(aParent, aUsed, aNotUsed);
93 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
95 // add unused shapes of compounds/compsolids to the history,
96 // to avoid treating them as unused later when constructing a compound containing
97 // the result of Smash and all unused sub-shapes of multi-level compounds
98 for (ListOfShape::iterator aNUIt = aNotUsed.begin(); aNUIt != aNotUsed.end(); ++aNUIt)
99 anObjectsHistory.addObject(*aNUIt);
103 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
104 if (!aShapesToAdd.empty()) {
105 // Cut objects with not used solids.
106 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
107 new GeomAlgoAPI_Boolean(aShapesToSmash,
109 GeomAlgoAPI_Tools::BOOL_CUT));
111 if (GeomAlgoAPI_ShapeTools::area(anObjectsCutAlgo->shape()) > 1.e-27) {
112 aShapesToSmash.clear();
113 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
114 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
117 // Cut tools with not used solids.
118 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
119 new GeomAlgoAPI_Boolean(aTools,
121 GeomAlgoAPI_Tools::BOOL_CUT));
123 if (GeomAlgoAPI_ShapeTools::area(aToolsCutAlgo->shape()) > 1.e-27) {
125 aTools.push_back(aToolsCutAlgo->shape());
126 aMakeShapeList->appendAlgo(aToolsCutAlgo);
130 // Cut objects with tools.
131 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
132 new GeomAlgoAPI_Boolean(aShapesToSmash,
134 GeomAlgoAPI_Tools::BOOL_CUT));
136 // Checking that the algorithm worked properly.
137 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
142 aMakeShapeList->appendAlgo(aBoolAlgo);
144 // Put all (cut result, tools and not used solids) to PaveFiller.
145 GeomShapePtr aShape = aBoolAlgo->shape();
146 GeomAPI_ShapeIterator anIt(aShape);
147 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
148 aShapesToAdd.push_back(aShape);
150 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
152 if (aShapesToAdd.size() == 1) {
153 aShape = aShapesToAdd.front();
156 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
157 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
158 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
163 aShape = aFillerAlgo->shape();
164 aMakeShapeList->appendAlgo(aFillerAlgo);
167 // take into account a version of SMASH feature
168 if (data()->version() == BOP_VERSION_9_4()) {
169 // merge hierarchies of compounds containing objects and tools
170 // and append the result of the FUSE operation
171 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHistory, aToolsHistory, aMakeShapeList);
174 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
176 ModelAPI_Tools::loadModifiedShapes(aResultBody,
182 setResult(aResultBody, aResultIndex);
185 ModelAPI_Tools::loadDeletedShapes(aResultBody,
191 // remove the rest results if there were produced in the previous pass
192 removeResults(aResultIndex);