1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_BooleanSmash.h"
23 #include "FeaturesPlugin_Tools.h"
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_Tools.h>
29 #include <GeomAlgoAPI_Boolean.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAPI_ShapeExplorer.h>
35 #include <GeomAPI_ShapeIterator.h>
37 //==================================================================================================
38 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
39 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
43 //==================================================================================================
44 void FeaturesPlugin_BooleanSmash::initAttributes()
46 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
47 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
50 //==================================================================================================
51 void FeaturesPlugin_BooleanSmash::execute()
53 ListOfShape anObjects, aTools;
54 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
57 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
58 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
59 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
60 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
64 ResultPtr aContext = anObjectAttr->context();
65 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
66 if (aResCompSolidPtr.get())
68 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
70 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
71 anIt = aCompSolidsObjects.begin();
72 for (; anIt != aCompSolidsObjects.end(); anIt++) {
73 if (anIt->first->isEqual(aContextShape)) {
74 aCompSolidsObjects[anIt->first].push_back(anObject);
78 if (anIt == aCompSolidsObjects.end()) {
79 aCompSolidsObjects[aContextShape].push_back(anObject);
83 anObjects.push_back(anObject);
88 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
89 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
90 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
91 GeomShapePtr aTool = aToolAttr->value();
95 aTools.push_back(aTool);
100 if((anObjects.empty() && aCompSolidsObjects.empty())
102 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
103 setError(aFeatureError);
107 // List of original shapes for naming.
108 ListOfShape anOriginalShapes;
109 anOriginalShapes.insert(anOriginalShapes.end(), anObjects.begin(), anObjects.end());
110 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
112 // Collecting all shapes which will be smashed.
113 ListOfShape aShapesToSmash;
114 aShapesToSmash.insert(aShapesToSmash.end(), anObjects.begin(), anObjects.end());
116 // Collecting solids from compsolids which will not be modified in
117 // boolean operation and will be added to result.
118 ListOfShape aShapesToAdd;
119 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
120 anIt = aCompSolidsObjects.begin();
121 anIt != aCompSolidsObjects.end();
124 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
125 ListOfShape& aUsedInOperationSolids = anIt->second;
126 anOriginalShapes.push_back(aCompSolid);
127 aShapesToSmash.insert(aShapesToSmash.end(),
128 aUsedInOperationSolids.begin(),
129 aUsedInOperationSolids.end());
131 // Collect solids from compsolid which will not be modified in boolean operation.
132 for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
136 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
137 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
138 for (; anIt != aUsedInOperationSolids.end(); anIt++) {
139 if (aSolidInCompSolid->isEqual(*anIt)) {
143 if (anIt == aUsedInOperationSolids.end()) {
144 aShapesToAdd.push_back(aSolidInCompSolid);
149 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
150 if (!aShapesToAdd.empty()) {
151 // Cut objects with not used solids.
152 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
153 new GeomAlgoAPI_Boolean(aShapesToSmash,
155 GeomAlgoAPI_Boolean::BOOL_CUT));
157 if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
158 aShapesToSmash.clear();
159 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
160 aMakeShapeList->appendAlgo(anObjectsCutAlgo);
163 // Cut tools with not used solids.
164 std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
165 new GeomAlgoAPI_Boolean(aTools,
167 GeomAlgoAPI_Boolean::BOOL_CUT));
169 if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
171 aTools.push_back(aToolsCutAlgo->shape());
172 aMakeShapeList->appendAlgo(aToolsCutAlgo);
176 // Cut objects with tools.
177 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
178 new GeomAlgoAPI_Boolean(aShapesToSmash,
180 GeomAlgoAPI_Boolean::BOOL_CUT));
182 // Checking that the algorithm worked properly.
183 if (!aBoolAlgo->isDone()) {
184 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
185 setError(aFeatureError);
188 if (aBoolAlgo->shape()->isNull()) {
189 static const std::string aShapeError = "Error: Resulting shape is Null.";
190 setError(aShapeError);
193 if (!aBoolAlgo->isValid()) {
194 std::string aFeatureError = "Error: Resulting shape is not valid.";
195 setError(aFeatureError);
198 aMakeShapeList->appendAlgo(aBoolAlgo);
200 // Put all (cut result, tools and not used solids) to PaveFiller.
201 GeomShapePtr aShape = aBoolAlgo->shape();
202 GeomAPI_ShapeIterator anIt(aShape);
203 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
204 aShapesToAdd.push_back(aShape);
206 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
208 if (aShapesToAdd.size() == 1) {
209 aShape = aShapesToAdd.front();
212 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
213 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
214 if (!aFillerAlgo->isDone()) {
215 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
216 setError(aFeatureError);
219 if (aFillerAlgo->shape()->isNull()) {
220 static const std::string aShapeError = "Error: Resulting shape is Null.";
221 setError(aShapeError);
224 if (!aFillerAlgo->isValid()) {
225 std::string aFeatureError = "Error: Resulting shape is not valid.";
226 setError(aFeatureError);
230 aShape = aFillerAlgo->shape();
231 aMakeShapeList->appendAlgo(aFillerAlgo);
234 std::shared_ptr<GeomAPI_Shape> aFrontShape = anOriginalShapes.front();
235 anOriginalShapes.pop_front();
236 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
238 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
244 setResult(aResultBody, aResultIndex);
247 FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
253 // remove the rest results if there were produced in the previous pass
254 removeResults(aResultIndex);