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 <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_PaveFiller.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_ShapeExplorer.h>
33 #include <GeomAPI_ShapeIterator.h>
35 //==================================================================================================
36 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
37 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
41 //==================================================================================================
42 void FeaturesPlugin_BooleanSmash::initAttributes()
44 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
45 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
48 //==================================================================================================
49 void FeaturesPlugin_BooleanSmash::execute()
51 ListOfShape anObjects, aTools;
52 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
55 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
56 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
57 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
58 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
62 ResultPtr aContext = anObjectAttr->context();
63 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
64 if (aResCompSolidPtr.get())
66 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
68 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
69 anIt = aCompSolidsObjects.begin();
70 for (; anIt != aCompSolidsObjects.end(); anIt++) {
71 if (anIt->first->isEqual(aContextShape)) {
72 aCompSolidsObjects[anIt->first].push_back(anObject);
76 if (anIt == aCompSolidsObjects.end()) {
77 aCompSolidsObjects[aContextShape].push_back(anObject);
81 anObjects.push_back(anObject);
86 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
87 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
88 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
89 GeomShapePtr aTool = aToolAttr->value();
93 aTools.push_back(aTool);
98 if((anObjects.empty() && aCompSolidsObjects.empty())
100 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
101 setError(aFeatureError);
105 // List of original shapes for naming.
106 ListOfShape anOriginalShapes;
107 anOriginalShapes.insert(anOriginalShapes.end(), anObjects.begin(), anObjects.end());
108 anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
110 // Collecting all shapes which will be smashed.
111 ListOfShape aShapesToSmash;
112 aShapesToSmash.insert(aShapesToSmash.end(), anObjects.begin(), anObjects.end());
114 // Collecting solids from compsolids which will not be modified in
115 // boolean operation and will be added to result.
116 ListOfShape aShapesToAdd;
117 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
118 anIt = aCompSolidsObjects.begin();
119 anIt != aCompSolidsObjects.end();
122 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
123 ListOfShape& aUsedInOperationSolids = anIt->second;
124 anOriginalShapes.push_back(aCompSolid);
125 aShapesToSmash.insert(aShapesToSmash.end(),
126 aUsedInOperationSolids.begin(),
127 aUsedInOperationSolids.end());
129 // Collect solids from compsolid which will not be modified in boolean operation.
130 for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
134 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
135 ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
136 for (; anIt != aUsedInOperationSolids.end(); anIt++) {
137 if (aSolidInCompSolid->isEqual(*anIt)) {
141 if (anIt == aUsedInOperationSolids.end()) {
142 aShapesToAdd.push_back(aSolidInCompSolid);
147 GeomAlgoAPI_MakeShapeList aMakeShapeList;
148 GeomAPI_DataMapOfShapeShape aMapOfShapes;
149 if (!aShapesToAdd.empty()) {
150 // Cut objects with not used solids.
151 std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
152 new GeomAlgoAPI_Boolean(aShapesToSmash,
154 GeomAlgoAPI_Boolean::BOOL_CUT));
156 if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
157 aShapesToSmash.clear();
158 aShapesToSmash.push_back(anObjectsCutAlgo->shape());
159 aMakeShapeList.appendAlgo(anObjectsCutAlgo);
160 aMapOfShapes.merge(anObjectsCutAlgo->mapOfSubShapes());
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);
173 aMapOfShapes.merge(aToolsCutAlgo->mapOfSubShapes());
177 // Cut objects with tools.
178 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
179 new GeomAlgoAPI_Boolean(aShapesToSmash,
181 GeomAlgoAPI_Boolean::BOOL_CUT));
183 // Checking that the algorithm worked properly.
184 if (!aBoolAlgo->isDone()) {
185 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
186 setError(aFeatureError);
189 if (aBoolAlgo->shape()->isNull()) {
190 static const std::string aShapeError = "Error: Resulting shape is Null.";
191 setError(aShapeError);
194 if (!aBoolAlgo->isValid()) {
195 std::string aFeatureError = "Error: Resulting shape is not valid.";
196 setError(aFeatureError);
199 aMakeShapeList.appendAlgo(aBoolAlgo);
200 aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
202 // Put all (cut result, tools and not used solids) to PaveFiller.
203 GeomShapePtr aShape = aBoolAlgo->shape();
204 GeomAPI_ShapeIterator anIt(aShape);
205 if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
206 aShapesToAdd.push_back(aShape);
208 aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
210 if (aShapesToAdd.size() == 1) {
211 aShape = aShapesToAdd.front();
214 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
215 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
216 if (!aFillerAlgo->isDone()) {
217 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
218 setError(aFeatureError);
221 if (aFillerAlgo->shape()->isNull()) {
222 static const std::string aShapeError = "Error: Resulting shape is Null.";
223 setError(aShapeError);
226 if (!aFillerAlgo->isValid()) {
227 std::string aFeatureError = "Error: Resulting shape is not valid.";
228 setError(aFeatureError);
232 aShape = aFillerAlgo->shape();
233 aMakeShapeList.appendAlgo(aFillerAlgo);
234 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
237 std::shared_ptr<GeomAPI_Shape> aFrontShape = anOriginalShapes.front();
238 anOriginalShapes.pop_front();
239 std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
240 loadNamingDS(aResultBody,
247 setResult(aResultBody, aResultIndex);
250 // remove the rest results if there were produced in the previous pass
251 removeResults(aResultIndex);
254 //==================================================================================================
255 void FeaturesPlugin_BooleanSmash::loadNamingDS(ResultBodyPtr theResultBody,
256 const GeomShapePtr theBaseShape,
257 const ListOfShape& theTools,
258 const GeomShapePtr theResultShape,
259 GeomAlgoAPI_MakeShape& theMakeShape,
260 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
263 if (theBaseShape->isEqual(theResultShape)) {
264 theResultBody->store(theResultShape, false);
265 } else if (theResultShape->isEqual(theTools.front())) {
266 theResultBody->store(theResultShape, false);
268 const int aModifyVTag = 1;
269 const int aModifyETag = 2;
270 const int aModifyFTag = 3;
271 const int aDeletedTag = 4;
272 /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
273 const int aSubsolidsTag = 5;
275 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
277 const std::string aModVName = "Modified_Vertex";
278 const std::string aModEName = "Modified_Edge";
279 const std::string aModFName = "Modified_Face";
281 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
282 aModifyVTag, aModVName, theMapOfShapes, false,
284 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
285 aModifyETag, aModEName, theMapOfShapes, false,
287 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
288 aModifyFTag, aModFName, theMapOfShapes, false,
291 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
292 GeomAPI_Shape::VERTEX, aDeletedTag);
293 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
294 GeomAPI_Shape::EDGE, aDeletedTag);
295 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
296 GeomAPI_Shape::FACE, aDeletedTag);
298 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
300 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
301 aModifyVTag, aModVName, theMapOfShapes, false,
304 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
305 aModifyETag, aModEName, theMapOfShapes, false,
308 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
309 aModifyFTag, aModFName, theMapOfShapes, false,
312 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX, aDeletedTag);
313 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE, aDeletedTag);
314 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);