1 // Copyright (C) 2014-2021 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_CompositeBoolean.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_Tools.h>
25 #include <GeomAlgoAPI_Boolean.h>
26 #include <GeomAlgoAPI_CompoundBuilder.h>
27 #include <GeomAlgoAPI_MakeShapeList.h>
28 #include <GeomAlgoAPI_PaveFiller.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30 #include <GeomAlgoAPI_MakeShapeCustom.h>
32 #include <GeomAPI_ShapeIterator.h>
36 //=================================================================================================
37 void FeaturesPlugin_CompositeBoolean::initBooleanAttributes()
39 myFeature->data()->addAttribute(OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
42 //=================================================================================================
43 void FeaturesPlugin_CompositeBoolean::executeCompositeBoolean()
46 ListOfShape aGenBaseShapes;
47 ListOfMakeShape aGenMakeShapes;
48 if(!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
54 for(ListOfMakeShape::const_iterator
55 anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
56 aTools.push_back((*anIt)->shape());
60 ListOfShape aBooleanObjects;
61 ListOfMakeShape aBooleanMakeShapes;
62 if(!makeBoolean(aTools, aBooleanObjects, aBooleanMakeShapes)) {
66 if(myOperationType == BOOL_FUSE) {
67 aTools.splice(aTools.begin(), aBooleanObjects);
68 aBooleanObjects.splice(aBooleanObjects.begin(), aTools, aTools.begin());
73 std::vector<ResultBaseAlgo> aResultBaseAlgoList;
74 ListOfShape aResultShapesList;
75 ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
76 ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
77 for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
78 ++aBoolObjIt, ++aBoolMSIt) {
80 ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
82 if((*aBoolObjIt)->isEqual((*aBoolMSIt)->shape())) {
83 aResultBody->store((*aBoolMSIt)->shape(), false);
87 aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape());
89 // Store generation history.
90 ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
91 ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
92 for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
93 ++aGenBaseIt, ++aGenMSIt) {
94 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMSList(new GeomAlgoAPI_MakeShapeList());
95 aMSList->appendAlgo(*aGenMSIt);
96 aMSList->appendAlgo(*aBoolMSIt);
97 storeGenerationHistory(aResultBody, *aGenBaseIt, aMSList);
100 storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt);
103 aRBA.resultBody = aResultBody;
104 aRBA.baseShape = *aBoolObjIt;
105 aRBA.makeShape = *aBoolMSIt;
106 aResultBaseAlgoList.push_back(aRBA);
107 aResultShapesList.push_back((*aBoolMSIt)->shape());
110 myFeature->setResult(aResultBody, aResultIndex++);
113 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
114 // result shape has been deleted, but in another it was modified or stayed.
115 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
116 storeDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
118 myFeature->removeResults(aResultIndex);
121 //=================================================================================================
122 bool FeaturesPlugin_CompositeBoolean::cutRecursiveCompound(const GeomShapePtr theCompound,
123 const ListOfShape& theTools,
124 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList,
125 GeomShapePtr& theResult)
127 // I. If theCompound is among the selected objects,
128 // cut it by all tools and return result through theResult.
129 // It can be a SOLID, ?COMPOUND?, ?COMPSOLID?
130 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
131 for (int anObjIndex = 0; anObjIndex < anObjectsSelList->size(); anObjIndex++) {
132 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjIndex);
133 GeomShapePtr anObject = anObjectAttr->value();
134 if (theCompound->isEqual(anObject)) {
135 // Cut theCompound itself
136 ListOfShape aListWithObject;
137 aListWithObject.push_back(anObject);
138 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo
139 (new GeomAlgoAPI_Boolean(aListWithObject, theTools, GeomAlgoAPI_Tools::BOOL_CUT));
141 // Checking that the algorithm worked properly.
142 if (!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
143 myFeature->setError("Error: Boolean algorithm failed.");
147 if (GeomAlgoAPI_ShapeTools::area(aBoolAlgo->shape()) > 1.e-27) {
148 theMakeShapeList->appendAlgo(aBoolAlgo);
149 theResult = aBoolAlgo->shape();
156 // II. Iterate the COMPOUND or COMPSOLID
157 // to find and cut Objects among its sub-shapes
158 if (theCompound->shapeType() == GeomAPI_Shape::COMPOUND ||
159 theCompound->shapeType() == GeomAPI_Shape::COMPSOLID) {
162 ListOfShape aShapesToAdd;
163 for (GeomAPI_ShapeIterator it (theCompound); it.more(); it.next()) {
164 GeomShapePtr aSubShape = it.current();
165 GeomShapePtr aResult;
166 if (cutRecursiveCompound(aSubShape, theTools, theMakeShapeList, aResult)) {
168 aShapesToAdd.push_back(aResult);
171 aShapesToAdd.push_back(aSubShape);
176 if (theCompound->shapeType() == GeomAPI_Shape::COMPSOLID) {
178 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo
179 (new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
180 if (!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
181 myFeature->setError("Error: PaveFiller algorithm failed.");
184 theResult = aFillerAlgo->shape();
185 theMakeShapeList->appendAlgo(aFillerAlgo);
189 theResult = GeomAlgoAPI_CompoundBuilder::compound(aShapesToAdd);
190 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aCompMkr (new GeomAlgoAPI_MakeShapeCustom);
191 aCompMkr->setResult(theResult);
192 aCompMkr->addModified(theCompound, theResult); // ??
193 theMakeShapeList->appendAlgo(aCompMkr);
199 return false; // no cuts
202 //=================================================================================================
203 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
204 ListOfShape& theObjects,
205 ListOfMakeShape& theMakeShapes)
208 ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
209 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
210 bool aCompoundsOnly = true;// if there are only compounds, do not use filler restoring compsolids
212 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
214 // collect recursive (complex) compounds, if any
215 ListOfShape aCompounds; // recursive compounds
216 GeomAPI_DataMapOfShapeShape aCompoundsMap; // recursive compounds map
217 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
218 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
219 ResultPtr aContext = anObjectAttr->context();
220 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
221 if (aResCompSolidPtr.get()) {
222 // Root body owner (true)
223 ResultBodyPtr aResRootPtr = ModelAPI_Tools::bodyOwner(aContext, true);
224 if (!aResRootPtr->isSame(aResCompSolidPtr)) {
225 aCompoundsMap.bind(aResRootPtr->shape(), aResRootPtr->shape());
226 aCompounds.push_back(aResRootPtr->shape());
231 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
232 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
233 GeomShapePtr anObject = anObjectAttr->value();
234 if(!anObject.get()) {
235 myFeature->setError("Error: Could not get object.");
238 ResultPtr aContext = anObjectAttr->context();
239 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
240 if(aResCompSolidPtr.get()) {
241 ResultBodyPtr aResRootPtr = ModelAPI_Tools::bodyOwner(aContext, true);
242 if (!aCompoundsMap.isBound(aResRootPtr->shape()) || myOperationType != BOOL_CUT) {
243 // Compsolid or a simple (one-level) compound
245 // TODO: correct FUSE for complex compounds?
246 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
247 std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
248 for(; anIt != aCompSolidsObjects.end(); anIt++) {
249 if(anIt->first->isEqual(aContextShape)) {
250 aCompSolidsObjects[anIt->first].push_back(anObject);
254 if(anIt == aCompSolidsObjects.end()) {
255 aCompSolidsObjects[aContextShape].push_back(anObject);
256 aCompSolids.push_back(aContextShape);
257 if (aContextShape->shapeType() != GeomAPI_Shape::COMPOUND)
258 aCompoundsOnly = false;
262 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
263 anObject->shapeType() == GeomAPI_Shape::FACE) {
264 anEdgesAndFaces.push_back(anObject);
266 anObjects.push_back(anObject);
271 switch(myOperationType) {
273 if((anObjects.empty() && aCompSolidsObjects.empty()
274 && aCompoundsMap.size() < 1) || theTools.empty()) {
275 myFeature->setError("Error: Not enough objects for boolean operation.");
279 // For solids cut each object with all tools.
280 for(ListOfShape::const_iterator
281 anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
282 GeomShapePtr anObject = *anIt;
283 ListOfShape aListWithObject;
284 aListWithObject.push_back(anObject);
285 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
287 GeomAlgoAPI_Tools::BOOL_CUT));
289 // Checking that the algorithm worked properly.
290 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
291 myFeature->setError("Error: Boolean algorithm failed.");
295 if(GeomAlgoAPI_ShapeTools::area(aBoolAlgo->shape()) > 1.e-27) {
296 theObjects.push_back(anObject);
297 theMakeShapes.push_back(aBoolAlgo);
301 // Compsolids handling
302 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
303 anIt != aCompSolidsObjects.cend(); ++anIt) {
304 GeomShapePtr aCompSolid = anIt->first;
305 const ListOfShape& aUsedShapes = anIt->second;
307 // Collecting solids from compsolids which will not be modified in boolean operation.
308 ListOfShape aShapesToAdd;
309 for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
313 GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
314 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
315 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
316 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
320 if(aUsedShapesIt == aUsedShapes.end()) {
321 aShapesToAdd.push_back(aSolidInCompSolid);
325 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
327 GeomAlgoAPI_Tools::BOOL_CUT));
329 // Checking that the algorithm worked properly.
330 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
331 myFeature->setError("Error: Boolean algorithm failed.");
335 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
336 aMakeShapeList->appendAlgo(aBoolAlgo);
338 // Add result to not used solids from compsolid.
339 GeomShapePtr aBoolRes = aBoolAlgo->shape();
340 if (!aShapesToAdd.empty()) {
341 aShapesToAdd.push_back(aBoolRes);
343 { // 23885: if there are no compsolids in input, do not use filler to make compsolids
344 aBoolRes = GeomAlgoAPI_CompoundBuilder::compound(aShapesToAdd);
345 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aCompMkr(new GeomAlgoAPI_MakeShapeCustom);
346 aCompMkr->setResult(aBoolRes);
347 for(ListOfShape::iterator aCS = aCompSolids.begin(); aCS != aCompSolids.end(); aCS++)
348 aCompMkr->addModified(*aCS, aBoolRes);
349 aMakeShapeList->appendAlgo(aCompMkr);
353 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
354 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
355 if (!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid())
357 myFeature->setError("Error: PaveFiller algorithm failed.");
360 aBoolRes = aFillerAlgo->shape();
361 aMakeShapeList->appendAlgo(aFillerAlgo);
365 if(GeomAlgoAPI_ShapeTools::area(aBoolRes) > 1.e-27) {
366 theObjects.push_back(aCompSolid);
367 theMakeShapes.push_back(aMakeShapeList);
371 // Complex (recursive) compounds handling
372 for(ListOfShape::const_iterator anIt = aCompounds.cbegin();
373 anIt != aCompounds.cend(); ++anIt) {
374 GeomShapePtr aCompound = (*anIt);
376 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList (new GeomAlgoAPI_MakeShapeList());
377 cutRecursiveCompound(aCompound, theTools, aMakeShapeList, aRes);
378 theObjects.push_back(aCompound);
379 theMakeShapes.push_back(aMakeShapeList);
386 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
387 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
388 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
390 // Filter edges and faces in tools.
392 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
393 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
394 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
395 anEdgesAndFaces.push_back(*anIt);
397 aTools.push_back(*anIt);
401 if((anObjects.size() + aTools.size() +
402 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
403 myFeature->setError("Error: Not enough objects for boolean operation.");
407 // Collecting all solids which will be fused.
408 ListOfShape aSolidsToFuse;
409 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
410 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
412 // Collecting solids from compsolids which will not be
413 // modified in boolean operation and will be added to result.
414 ListOfShape aShapesToAdd;
415 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
416 anIt != aCompSolidsObjects.end(); anIt++) {
417 GeomShapePtr aCompSolid = anIt->first;
418 ListOfShape& aUsedShapes = anIt->second;
419 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
421 // Collect solids from compsolid which will not be modified in boolean operation.
422 for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
426 GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
427 ListOfShape::iterator aUseIt = aUsedShapes.begin();
428 for(; aUseIt != aUsedShapes.end(); aUseIt++) {
429 if(aSolidInCompSolid->isEqual(*aUseIt)) {
433 if(aUseIt == aUsedShapes.end()) {
434 aShapesToAdd.push_back(aSolidInCompSolid);
439 // Cut edges and faces(if we have any) with solids.
440 ListOfShape aCutTools;
441 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
442 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
443 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
445 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
446 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
447 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
449 GeomAlgoAPI_Tools::BOOL_CUT));
450 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
451 anEdgesAndFaces.clear();
452 anEdgesAndFaces.push_back(aCutAlgo->shape());
453 aMakeShapeList->appendAlgo(aCutAlgo);
457 // If we have compsolids then cut with not used solids all others.
458 if(!aShapesToAdd.empty()) {
459 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
461 GeomAlgoAPI_Tools::BOOL_CUT));
462 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
463 aSolidsToFuse.clear();
464 aSolidsToFuse.push_back(aCutAlgo->shape());
465 aMakeShapeList->appendAlgo(aCutAlgo);
469 // Fuse all objects and all tools.
470 GeomShapePtr aFusedShape;
471 if(aSolidsToFuse.size() == 1) {
472 aFusedShape = aSolidsToFuse.front();
473 } else if(aSolidsToFuse.size() > 1){
475 anObjects.push_back(aSolidsToFuse.front());
476 aSolidsToFuse.pop_front();
477 aTools = aSolidsToFuse;
479 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
481 GeomAlgoAPI_Tools::BOOL_FUSE));
483 // Checking that the algorithm worked properly.
484 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
485 myFeature->setError("Error: Boolean algorithm failed.");
489 aFusedShape = aFuseAlgo->shape();
490 aMakeShapeList->appendAlgo(aFuseAlgo);
493 // Combine result with not used solids from compsolid and edges and faces (if we have any).
494 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
495 if(!aShapesToAdd.empty()) {
496 if(aFusedShape.get()) {
497 aShapesToAdd.push_back(aFusedShape);
500 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
501 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
502 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
503 myFeature->setError("Error: PaveFiller algorithm failed.");
507 aMakeShapeList->appendAlgo(aFillerAlgo);
510 theMakeShapes.push_back(aMakeShapeList);
513 default: // [to avoid compilation warnings]
520 //=================================================================================================
521 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
522 const GeomShapePtr theObject,
523 const ListOfShape& theTools,
524 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
526 ListOfShape aTools = theTools;
527 aTools.push_back(theObject);
529 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
530 theResultBody->loadModifiedShapes(theMakeShape, *anIt,
531 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
532 GeomAPI_Shape::EDGE :
533 GeomAPI_Shape::FACE);
537 //==================================================================================================
538 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
539 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
540 const ListOfShape& theTools,
541 const GeomShapePtr theResultShapesCompound)
543 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
544 anIt != theResultBaseAlgoList.end();
547 ResultBaseAlgo& aRCA = *anIt;
548 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
551 theResultShapesCompound);
553 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
555 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
558 theResultShapesCompound);