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 //==================================================================================================
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());
51 //==================================================================================================
52 void FeaturesPlugin_BooleanSmash::execute()
55 ListOfShape anObjects, aTools;
56 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
59 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
60 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
61 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
62 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
66 ResultPtr aContext = anObjectAttr->context();
67 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
68 if (aResCompSolidPtr.get())
70 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
72 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
73 anIt = aCompSolidsObjects.begin();
74 for (; anIt != aCompSolidsObjects.end(); anIt++) {
75 if (anIt->first->isEqual(aContextShape)) {
76 aCompSolidsObjects[anIt->first].push_back(anObject);
80 if (anIt == aCompSolidsObjects.end()) {
81 aCompSolidsObjects[aContextShape].push_back(anObject);
85 anObjects.push_back(anObject);
90 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
91 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
92 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
93 GeomShapePtr aTool = aToolAttr->value();
97 aTools.push_back(aTool);
100 int aResultIndex = 0;
102 if((anObjects.empty() && aCompSolidsObjects.empty())
104 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
105 setError(aFeatureError);
109 // List of original shapes for naming.
110 ListOfShape anOriginalShapes;
111 anOriginalShapes.insert(anOriginalShapes.end(), anObjects.begin(), anObjects.end());
112 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
114 // Collecting all shapes which will be smashed.
115 ListOfShape aShapesToSmash;
116 aShapesToSmash.insert(aShapesToSmash.end(), anObjects.begin(), anObjects.end());
118 // Collecting solids from compsolids which will not be modified in
119 // boolean operation and will be added to result.
120 ListOfShape aShapesToAdd;
121 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
122 anIt = aCompSolidsObjects.begin();
123 anIt != aCompSolidsObjects.end();
126 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
127 ListOfShape& aUsedInOperationSolids = anIt->second;
128 anOriginalShapes.push_back(aCompSolid);
129 aShapesToSmash.insert(aShapesToSmash.end(),
130 aUsedInOperationSolids.begin(),
131 aUsedInOperationSolids.end());
133 // Collect solids from compsolid which will not be modified in boolean operation.
134 for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
138 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
139 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
140 for (; anIt != aUsedInOperationSolids.end(); anIt++) {
141 if (aSolidInCompSolid->isEqual(*anIt)) {
145 if (anIt == aUsedInOperationSolids.end()) {
146 aShapesToAdd.push_back(aSolidInCompSolid);
151 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
152 if (!aShapesToAdd.empty()) {
153 // Cut objects with not used solids.
154 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
155 new GeomAlgoAPI_Boolean(aShapesToSmash,
157 GeomAlgoAPI_Tools::BOOL_CUT));
159 if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
160 aShapesToSmash.clear();
161 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
162 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
165 // Cut tools with not used solids.
166 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
167 new GeomAlgoAPI_Boolean(aTools,
169 GeomAlgoAPI_Tools::BOOL_CUT));
171 if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
173 aTools.push_back(aToolsCutAlgo->shape());
174 aMakeShapeList->appendAlgo(aToolsCutAlgo);
178 // Cut objects with tools.
179 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
180 new GeomAlgoAPI_Boolean(aShapesToSmash,
182 GeomAlgoAPI_Tools::BOOL_CUT));
184 // Checking that the algorithm worked properly.
185 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
190 aMakeShapeList->appendAlgo(aBoolAlgo);
192 // Put all (cut result, tools and not used solids) to PaveFiller.
193 GeomShapePtr aShape = aBoolAlgo->shape();
194 GeomAPI_ShapeIterator anIt(aShape);
195 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
196 aShapesToAdd.push_back(aShape);
198 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
200 if (aShapesToAdd.size() == 1) {
201 aShape = aShapesToAdd.front();
204 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
205 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
206 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
211 aShape = aFillerAlgo->shape();
212 aMakeShapeList->appendAlgo(aFillerAlgo);
215 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
217 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
223 setResult(aResultBody, aResultIndex);
226 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
232 // remove the rest results if there were produced in the previous pass
233 removeResults(aResultIndex);