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 const int ModifyVTag = 1;
37 const int ModifyETag = 2;
38 const int ModifyFTag = 3;
39 const int DeletedTag = 4;
40 /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
41 const int SubsolidsTag = 5;
44 //==================================================================================================
45 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
46 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
50 //==================================================================================================
51 void FeaturesPlugin_BooleanCut::execute()
53 ListOfShape anObjects, aTools;
54 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
55 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
58 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
59 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
60 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
61 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
65 ResultPtr aContext = anObjectAttr->context();
66 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
67 if (aResCompSolidPtr.get())
69 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
70 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
71 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
72 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
74 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
76 for (; anIt != aMap.end(); anIt++) {
77 if (anIt->first->isEqual(aContextShape)) {
78 aMap[anIt->first].push_back(anObject);
82 if (anIt == aMap.end()) {
83 aMap[aContextShape].push_back(anObject);
87 anObjects.push_back(anObject);
92 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
93 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
94 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
95 GeomShapePtr aTool = aToolAttr->value();
99 aTools.push_back(aTool);
102 int aResultIndex = 0;
104 if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
106 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
107 setError(aFeatureError);
111 std::vector<ResultBaseAlgo> aResultBaseAlgoList;
112 ListOfShape aResultShapesList;
114 // For solids cut each object with all tools.
115 for(ListOfShape::iterator anObjectsIt = anObjects.begin();
116 anObjectsIt != anObjects.end();
118 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
119 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
120 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
121 new GeomAlgoAPI_Boolean(anObject,
123 GeomAlgoAPI_Boolean::BOOL_CUT));
124 GeomShapePtr aResShape = aCutAlgo->shape();
126 // Checking that the algorithm worked properly.
127 if (!aCutAlgo->isDone()) {
128 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
129 setError(aFeatureError);
132 if(aResShape->isNull()) {
133 static const std::string aShapeError = "Error: Resulting shape is Null.";
134 setError(aShapeError);
137 if (!aCutAlgo->isValid()) {
138 std::string aFeatureError = "Error: Resulting shape is not valid.";
139 setError(aFeatureError);
143 aMakeShapeList->appendAlgo(aCutAlgo);
145 GeomAPI_ShapeIterator aShapeIt(aResShape);
146 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
148 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
149 document()->createBody(data(), aResultIndex);
151 loadNamingDS(aResultBody, anObject, aTools, aResShape,
152 *aMakeShapeList, *(aCutAlgo->mapOfSubShapes()),
154 setResult(aResultBody, aResultIndex);
158 aRBA.resultBody = aResultBody;
159 aRBA.baseShape = anObject;
160 aRBA.makeShape = aMakeShapeList;
161 aResultBaseAlgoList.push_back(aRBA);
162 aResultShapesList.push_back(aResShape);
166 // Compsolids handling
167 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
168 anIt = aCompSolidsObjects.begin();
169 anIt != aCompSolidsObjects.end();
172 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
173 ListOfShape& aUsedInOperationSolids = anIt->second;
175 // Collecting solids from compsolids which will not be modified in boolean operation.
176 ListOfShape aNotUsedSolids;
177 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
181 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
182 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
183 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
184 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
188 if (aUsedIt == aUsedInOperationSolids.end()) {
189 aNotUsedSolids.push_back(aSolidInCompSolid);
193 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
194 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
195 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
197 GeomAlgoAPI_Boolean::BOOL_CUT));
199 // Checking that the algorithm worked properly.
200 if (!aCutAlgo->isDone()) {
201 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
202 setError(aFeatureError);
205 if (aCutAlgo->shape()->isNull()) {
206 static const std::string aShapeError = "Error: Resulting shape is Null.";
207 setError(aShapeError);
210 if (!aCutAlgo->isValid()) {
211 std::string aFeatureError = "Error: Resulting shape is not valid.";
212 setError(aFeatureError);
216 aMakeShapeList->appendAlgo(aCutAlgo);
217 GeomAPI_DataMapOfShapeShape aMapOfShapes;
218 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
219 GeomShapePtr aResultShape = aCutAlgo->shape();
221 // Add result to not used solids from compsolid.
222 if(!aNotUsedSolids.empty()) {
223 ListOfShape aShapesToAdd = aNotUsedSolids;
224 aShapesToAdd.push_back(aCutAlgo->shape());
225 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
226 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
227 if(!aFillerAlgo->isDone()) {
228 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
229 setError(aFeatureError);
233 aMakeShapeList->appendAlgo(aFillerAlgo);
234 aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
235 aResultShape = aFillerAlgo->shape();
238 GeomAPI_ShapeIterator aShapeIt(aResultShape);
239 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
241 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
242 document()->createBody(data(), aResultIndex);
244 loadNamingDS(aResultBody,
251 setResult(aResultBody, aResultIndex);
255 aRBA.resultBody = aResultBody;
256 aRBA.baseShape = aCompSolid;
257 aRBA.makeShape = aMakeShapeList;
258 aResultBaseAlgoList.push_back(aRBA);
259 aResultShapesList.push_back(aResultShape);
263 // Compounds handling
264 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
265 anIt = aCompoundObjects.begin();
266 anIt != aCompoundObjects.end();
269 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
270 ListOfShape& aUsedInOperationShapes = anIt->second;
272 // Collecting shapes from compound which will not be modified in boolean operation.
273 ListOfShape aNotUsedShapes;
274 for (GeomAPI_ShapeIterator aCompIt(aCompound);
278 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
279 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
280 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
281 if (aShapeInCompound->isEqual(*aUsedIt)) {
285 if (aUsedIt == aUsedInOperationShapes.end()) {
286 aNotUsedShapes.push_back(aShapeInCompound);
290 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
291 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
292 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
294 GeomAlgoAPI_Boolean::BOOL_CUT));
296 // Checking that the algorithm worked properly.
297 if (!aCutAlgo->isDone()) {
298 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
299 setError(aFeatureError);
302 if (aCutAlgo->shape()->isNull()) {
303 static const std::string aShapeError = "Error: Resulting shape is Null.";
304 setError(aShapeError);
307 if (!aCutAlgo->isValid()) {
308 std::string aFeatureError = "Error: Resulting shape is not valid.";
309 setError(aFeatureError);
313 aMakeShapeList->appendAlgo(aCutAlgo);
314 GeomAPI_DataMapOfShapeShape aMapOfShapes;
315 aMapOfShapes.merge(aCutAlgo->mapOfSubShapes());
316 GeomShapePtr aResultShape = aCutAlgo->shape();
318 // Add result to not used shape from compound.
319 if (!aNotUsedShapes.empty()) {
320 ListOfShape aShapesForResult = aNotUsedShapes;
321 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
322 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
323 aShapesForResult.push_back(aResultIt.current());
326 aShapesForResult.push_back(aResultShape);
329 if (aShapesForResult.size() == 1) {
330 aResultShape = aShapesForResult.front();
332 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
336 GeomAPI_ShapeIterator aShapeIt(aResultShape);
337 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
338 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
339 document()->createBody(data(), aResultIndex);
341 loadNamingDS(aResultBody,
348 setResult(aResultBody, aResultIndex);
352 aRBA.resultBody = aResultBody;
353 aRBA.baseShape = aCompound;
354 aRBA.makeShape = aMakeShapeList;
355 aResultBaseAlgoList.push_back(aRBA);
356 aResultShapesList.push_back(aResultShape);
360 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
361 // result shape has been deleted, but in another it was modified or stayed.
362 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
363 storeDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
365 // remove the rest results if there were produced in the previous pass
366 removeResults(aResultIndex);
369 //==================================================================================================
370 void FeaturesPlugin_BooleanCut::loadNamingDS(ResultBodyPtr theResultBody,
371 const GeomShapePtr theBaseShape,
372 const ListOfShape& theTools,
373 const GeomShapePtr theResultShape,
374 GeomAlgoAPI_MakeShape& theMakeShape,
375 GeomAPI_DataMapOfShapeShape& theMapOfShapes,
376 const bool theIsStoreAsGenerated)
379 if(theBaseShape->isEqual(theResultShape)) {
380 theResultBody->store(theResultShape, false);
382 theResultBody->storeModified(theBaseShape, theResultShape, SubsolidsTag);
384 const std::string aModVName = "Modified_Vertex";
385 const std::string aModEName = "Modified_Edge";
386 const std::string aModFName = "Modified_Face";
388 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
389 ModifyVTag, aModVName, theMapOfShapes, false,
390 theIsStoreAsGenerated, true);
391 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
392 ModifyETag, aModEName, theMapOfShapes, false,
393 theIsStoreAsGenerated, true);
394 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
395 ModifyFTag, aModFName, theMapOfShapes, false,
396 theIsStoreAsGenerated, true);
398 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
400 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
401 ModifyVTag, aModVName, theMapOfShapes, false,
402 theIsStoreAsGenerated, true);
404 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
405 ModifyETag, aModEName, theMapOfShapes, false,
406 theIsStoreAsGenerated, true);
408 theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
409 ModifyFTag, aModFName, theMapOfShapes, false,
410 theIsStoreAsGenerated, true);
415 //==================================================================================================
416 void FeaturesPlugin_BooleanCut::storeDeletedShapes(
417 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
418 const ListOfShape& theTools,
419 const GeomShapePtr theResultShapesCompound)
421 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
422 anIt != theResultBaseAlgoList.end();
425 ResultBaseAlgo& aRCA = *anIt;
426 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
428 GeomAPI_Shape::VERTEX,
430 theResultShapesCompound);
431 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
435 theResultShapesCompound);
436 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
440 theResultShapesCompound);
442 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
444 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
446 GeomAPI_Shape::VERTEX,
448 theResultShapesCompound);
449 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
453 theResultShapesCompound);
454 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
458 theResultShapesCompound);