1 // Copyright (C) 2014-2019 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 static const int THE_SMASH_VERSION_1 = 20190506;
40 //==================================================================================================
41 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
42 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
46 //==================================================================================================
47 void FeaturesPlugin_BooleanSmash::initAttributes()
49 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
50 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
52 initVersion(THE_SMASH_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
55 //==================================================================================================
56 void FeaturesPlugin_BooleanSmash::execute()
59 ObjectHierarchy anObjectsHistory, aToolsHistory;
62 // Getting objects and tools.
63 if (!processAttribute(OBJECT_LIST_ID(), anObjectsHistory, aPlanes) ||
64 !processAttribute(TOOL_LIST_ID(), aToolsHistory, aPlanes))
69 if (anObjectsHistory.IsEmpty() || aToolsHistory.IsEmpty()) {
70 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
71 setError(aFeatureError);
75 // Collecting all shapes which will be smashed.
76 ListOfShape aShapesToSmash = anObjectsHistory.Objects();
78 // List of original shapes for naming.
79 ListOfShape anOriginalShapes;
80 anOriginalShapes.insert(anOriginalShapes.end(), aShapesToSmash.begin(), aShapesToSmash.end());
81 ListOfShape aTools = aToolsHistory.Objects();
82 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
84 // Collecting solids from compsolids which will not be modified in
85 // boolean operation and will be added to result.
86 ListOfShape aShapesToAdd;
87 for (ObjectHierarchy::Iterator anIt = anObjectsHistory.Begin();
88 anIt != anObjectsHistory.End();
91 GeomShapePtr aParent = anObjectsHistory.Parent(*anIt, false);
93 anOriginalShapes.push_back(aParent);
95 ListOfShape aUsed, aNotUsed;
96 anObjectsHistory.SplitCompound(aParent, aUsed, aNotUsed);
97 aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
99 // add unused shapes of compounds/compsolids to the history,
100 // to avoid treating them as unused later when constructing a compound containing
101 // the result of Smash and all unused sub-shapes of multi-level compounds
102 for (ListOfShape::iterator anIt = aNotUsed.begin(); anIt != aNotUsed.end(); ++anIt)
103 anObjectsHistory.AddObject(*anIt);
107 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
108 if (!aShapesToAdd.empty()) {
109 // Cut objects with not used solids.
110 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
111 new GeomAlgoAPI_Boolean(aShapesToSmash,
113 GeomAlgoAPI_Tools::BOOL_CUT));
115 if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
116 aShapesToSmash.clear();
117 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
118 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
121 // Cut tools with not used solids.
122 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
123 new GeomAlgoAPI_Boolean(aTools,
125 GeomAlgoAPI_Tools::BOOL_CUT));
127 if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
129 aTools.push_back(aToolsCutAlgo->shape());
130 aMakeShapeList->appendAlgo(aToolsCutAlgo);
134 // Cut objects with tools.
135 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
136 new GeomAlgoAPI_Boolean(aShapesToSmash,
138 GeomAlgoAPI_Tools::BOOL_CUT));
140 // Checking that the algorithm worked properly.
141 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
146 aMakeShapeList->appendAlgo(aBoolAlgo);
148 // Put all (cut result, tools and not used solids) to PaveFiller.
149 GeomShapePtr aShape = aBoolAlgo->shape();
150 GeomAPI_ShapeIterator anIt(aShape);
151 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
152 aShapesToAdd.push_back(aShape);
154 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
156 if (aShapesToAdd.size() == 1) {
157 aShape = aShapesToAdd.front();
160 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
161 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
162 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
167 aShape = aFillerAlgo->shape();
168 aMakeShapeList->appendAlgo(aFillerAlgo);
171 // take into account a version of SMASH feature
172 int aSmashVersion = version();
173 if (aSmashVersion == THE_SMASH_VERSION_1) {
174 // merge hierarchies of compounds containing objects and tools
175 // and append the result of the FUSE operation
176 aShape = keepUnusedSubsOfCompound(aShape, anObjectsHistory, aToolsHistory, aMakeShapeList);
179 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
181 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
187 setResult(aResultBody, aResultIndex);
190 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
196 // remove the rest results if there were produced in the previous pass
197 removeResults(aResultIndex);