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_BooleanCut.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_BooleanCut::FeaturesPlugin_BooleanCut()
37 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
41 //==================================================================================================
42 void FeaturesPlugin_BooleanCut::execute()
44 ListOfShape anObjects, aTools;
45 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
46 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
49 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
50 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
51 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
52 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
56 ResultPtr aContext = anObjectAttr->context();
57 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
58 if (aResCompSolidPtr.get())
60 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
61 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
62 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
63 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
65 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
67 for (; anIt != aMap.end(); anIt++) {
68 if (anIt->first->isEqual(aContextShape)) {
69 aMap[anIt->first].push_back(anObject);
73 if (anIt == aMap.end()) {
74 aMap[aContextShape].push_back(anObject);
78 anObjects.push_back(anObject);
83 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
84 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
85 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
86 GeomShapePtr aTool = aToolAttr->value();
90 aTools.push_back(aTool);
95 if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
97 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
98 setError(aFeatureError);
102 // For solids cut each object with all tools.
103 for(ListOfShape::iterator anObjectsIt = anObjects.begin();
104 anObjectsIt != anObjects.end();
106 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
107 GeomAlgoAPI_MakeShapeList aMakeShapeList;
108 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
109 new GeomAlgoAPI_Boolean(anObject,
111 GeomAlgoAPI_Boolean::BOOL_CUT));
112 GeomShapePtr aResShape = aCutAlgo->shape();
114 // Checking that the algorithm worked properly.
115 if (!aCutAlgo->isDone()) {
116 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
117 setError(aFeatureError);
120 if(aResShape->isNull()) {
121 static const std::string aShapeError = "Error: Resulting shape is Null.";
122 setError(aShapeError);
125 if (!aCutAlgo->isValid()) {
126 std::string aFeatureError = "Error: Resulting shape is not valid.";
127 setError(aFeatureError);
131 aMakeShapeList.appendAlgo(aCutAlgo);
133 GeomAPI_ShapeIterator aShapeIt(aResShape);
134 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
136 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
137 document()->createBody(data(), aResultIndex);
139 loadNamingDS(aResultBody, anObject, aTools, aResShape,
140 aMakeShapeList, *(aCutAlgo->mapOfSubShapes()),
142 setResult(aResultBody, aResultIndex);
147 // Compsolids handling
148 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
149 anIt = aCompSolidsObjects.begin();
150 anIt != aCompSolidsObjects.end();
153 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
154 ListOfShape& aUsedInOperationSolids = anIt->second;
156 // Collecting solids from compsolids which will not be modified in boolean operation.
157 ListOfShape aNotUsedSolids;
158 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
162 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
163 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
164 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
165 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
169 if (aUsedIt == aUsedInOperationSolids.end()) {
170 aNotUsedSolids.push_back(aSolidInCompSolid);
174 GeomAlgoAPI_MakeShapeList aMakeShapeList;
175 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
176 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
178 GeomAlgoAPI_Boolean::BOOL_CUT));
180 // Checking that the algorithm worked properly.
181 if (!aCutAlgo->isDone()) {
182 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
183 setError(aFeatureError);
186 if (aCutAlgo->shape()->isNull()) {
187 static const std::string aShapeError = "Error: Resulting shape is Null.";
188 setError(aShapeError);
191 if (!aCutAlgo->isValid()) {
192 std::string aFeatureError = "Error: Resulting shape is not valid.";
193 setError(aFeatureError);
197 aMakeShapeList.appendAlgo(aCutAlgo);
198 GeomAPI_DataMapOfShapeShape aMapOfShapes;
199 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
200 GeomShapePtr aResultShape = aCutAlgo->shape();
202 // Add result to not used solids from compsolid.
203 if(!aNotUsedSolids.empty()) {
204 ListOfShape aShapesToAdd = aNotUsedSolids;
205 aShapesToAdd.push_back(aCutAlgo->shape());
206 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
207 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
208 if(!aFillerAlgo->isDone()) {
209 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
210 setError(aFeatureError);
214 aMakeShapeList.appendAlgo(aFillerAlgo);
215 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
216 aResultShape = aFillerAlgo->shape();
219 GeomAPI_ShapeIterator aShapeIt(aResultShape);
220 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
222 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
223 document()->createBody(data(), aResultIndex);
225 loadNamingDS(aResultBody,
232 setResult(aResultBody, aResultIndex);
237 // Compounds handling
238 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
239 anIt = aCompoundObjects.begin();
240 anIt != aCompoundObjects.end();
243 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
244 ListOfShape& aUsedInOperationShapes = anIt->second;
246 // Collecting shapes from compound which will not be modified in boolean operation.
247 ListOfShape aNotUsedShapes;
248 for (GeomAPI_ShapeIterator aCompIt(aCompound);
252 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
253 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
254 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
255 if (aShapeInCompound->isEqual(*aUsedIt)) {
259 if (aUsedIt == aUsedInOperationShapes.end()) {
260 aNotUsedShapes.push_back(aShapeInCompound);
264 GeomAlgoAPI_MakeShapeList aMakeShapeList;
265 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
266 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
268 GeomAlgoAPI_Boolean::BOOL_CUT));
270 // Checking that the algorithm worked properly.
271 if (!aCutAlgo->isDone()) {
272 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
273 setError(aFeatureError);
276 if (aCutAlgo->shape()->isNull()) {
277 static const std::string aShapeError = "Error: Resulting shape is Null.";
278 setError(aShapeError);
281 if (!aCutAlgo->isValid()) {
282 std::string aFeatureError = "Error: Resulting shape is not valid.";
283 setError(aFeatureError);
287 aMakeShapeList.appendAlgo(aCutAlgo);
288 GeomAPI_DataMapOfShapeShape aMapOfShapes;
289 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
290 GeomShapePtr aResultShape = aCutAlgo->shape();
292 // Add result to not used shape from compound.
293 if (!aNotUsedShapes.empty()) {
294 ListOfShape aShapesForResult = aNotUsedShapes;
295 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
296 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
297 aShapesForResult.push_back(aResultIt.current());
300 aShapesForResult.push_back(aResultShape);
303 if (aShapesForResult.size() == 1) {
304 aResultShape = aShapesForResult.front();
306 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
310 GeomAPI_ShapeIterator aShapeIt(aResultShape);
311 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
312 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
313 document()->createBody(data(), aResultIndex);
315 loadNamingDS(aResultBody,
322 setResult(aResultBody, aResultIndex);
327 // remove the rest results if there were produced in the previous pass
328 removeResults(aResultIndex);
331 //==================================================================================================
332 void FeaturesPlugin_BooleanCut::loadNamingDS(ResultBodyPtr theResultBody,
333 const GeomShapePtr theBaseShape,
334 const ListOfShape& theTools,
335 const GeomShapePtr theResultShape,
336 GeomAlgoAPI_MakeShape& theMakeShape,
337 GeomAPI_DataMapOfShapeShape& theMapOfShapes,
338 const bool theIsStoreAsGenerated)
341 if(theBaseShape->isEqual(theResultShape)) {
342 theResultBody->store(theResultShape, false);
344 const int aModifyVTag = 1;
345 const int aModifyETag = 2;
346 const int aModifyFTag = 3;
347 const int aDeletedTag = 4;
348 /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
349 const int aSubsolidsTag = 5;
351 theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
353 const std::string aModVName = "Modified_Vertex";
354 const std::string aModEName = "Modified_Edge";
355 const std::string aModFName = "Modified_Face";
357 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
358 aModifyVTag, aModVName, theMapOfShapes, false,
359 theIsStoreAsGenerated, true);
360 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
361 aModifyETag, aModEName, theMapOfShapes, false,
362 theIsStoreAsGenerated, true);
363 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
364 aModifyFTag, aModFName, theMapOfShapes, false,
365 theIsStoreAsGenerated, true);
367 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
368 GeomAPI_Shape::VERTEX, aDeletedTag);
369 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
370 GeomAPI_Shape::EDGE, aDeletedTag);
371 theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
372 GeomAPI_Shape::FACE, aDeletedTag);
374 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
376 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
377 aModifyVTag, aModVName, theMapOfShapes, false,
378 theIsStoreAsGenerated, true);
380 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
381 aModifyETag, aModEName, theMapOfShapes, false,
382 theIsStoreAsGenerated, true);
384 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
385 aModifyFTag, aModFName, theMapOfShapes, false,
386 theIsStoreAsGenerated, true);
388 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX, aDeletedTag);
389 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE, aDeletedTag);
390 theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);