1 // Copyright (C) 2014-2020 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::makeBoolean(const ListOfShape& theTools,
123 ListOfShape& theObjects,
124 ListOfMakeShape& theMakeShapes)
127 ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
128 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
129 bool aCompoundsOnly = true;// if there are only compounds, do not use filler restoring compsolids
130 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
131 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
132 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
133 GeomShapePtr anObject = anObjectAttr->value();
134 if(!anObject.get()) {
135 myFeature->setError("Error: Could not get object.");
138 ResultPtr aContext = anObjectAttr->context();
139 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
140 if(aResCompSolidPtr.get()) {
141 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
142 std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
143 for(; anIt != aCompSolidsObjects.end(); anIt++) {
144 if(anIt->first->isEqual(aContextShape)) {
145 aCompSolidsObjects[anIt->first].push_back(anObject);
149 if(anIt == aCompSolidsObjects.end()) {
150 aCompSolidsObjects[aContextShape].push_back(anObject);
151 aCompSolids.push_back(aContextShape);
152 if (aContextShape->shapeType() != GeomAPI_Shape::COMPOUND)
153 aCompoundsOnly = false;
156 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
157 anObject->shapeType() == GeomAPI_Shape::FACE) {
158 anEdgesAndFaces.push_back(anObject);
160 anObjects.push_back(anObject);
165 switch(myOperationType) {
167 if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
168 myFeature->setError("Error: Not enough objects for boolean operation.");
172 // For solids cut each object with all tools.
173 for(ListOfShape::const_iterator
174 anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
175 GeomShapePtr anObject = *anIt;
176 ListOfShape aListWithObject;
177 aListWithObject.push_back(anObject);
178 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
180 GeomAlgoAPI_Tools::BOOL_CUT));
182 // Checking that the algorithm worked properly.
183 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
184 myFeature->setError("Error: Boolean algorithm failed.");
188 if(GeomAlgoAPI_ShapeTools::area(aBoolAlgo->shape()) > 1.e-27) {
189 theObjects.push_back(anObject);
190 theMakeShapes.push_back(aBoolAlgo);
194 // Compsolids handling
195 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
196 anIt != aCompSolidsObjects.cend(); ++anIt) {
197 GeomShapePtr aCompSolid = anIt->first;
198 const ListOfShape& aUsedShapes = anIt->second;
200 // Collecting solids from compsolids which will not be modified in boolean operation.
201 ListOfShape aShapesToAdd;
202 for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
206 GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
207 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
208 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
209 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
213 if(aUsedShapesIt == aUsedShapes.end()) {
214 aShapesToAdd.push_back(aSolidInCompSolid);
218 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
220 GeomAlgoAPI_Tools::BOOL_CUT));
222 // Checking that the algorithm worked properly.
223 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
224 myFeature->setError("Error: Boolean algorithm failed.");
228 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
229 aMakeShapeList->appendAlgo(aBoolAlgo);
231 // Add result to not used solids from compsolid.
232 GeomShapePtr aBoolRes = aBoolAlgo->shape();
233 if (!aShapesToAdd.empty()) {
234 aShapesToAdd.push_back(aBoolRes);
236 { // 23885: if there are no compsolids in input, do not use filler to make compsolids
237 aBoolRes = GeomAlgoAPI_CompoundBuilder::compound(aShapesToAdd);
238 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aCompMkr(new GeomAlgoAPI_MakeShapeCustom);
239 aCompMkr->setResult(aBoolRes);
240 for(ListOfShape::iterator aCS = aCompSolids.begin(); aCS != aCompSolids.end(); aCS++)
241 aCompMkr->addModified(*aCS, aBoolRes);
242 aMakeShapeList->appendAlgo(aCompMkr);
246 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
247 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
248 if (!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid())
250 myFeature->setError("Error: PaveFiller algorithm failed.");
253 aBoolRes = aFillerAlgo->shape();
254 aMakeShapeList->appendAlgo(aFillerAlgo);
258 if(GeomAlgoAPI_ShapeTools::area(aBoolRes) > 1.e-27) {
259 theObjects.push_back(aCompSolid);
260 theMakeShapes.push_back(aMakeShapeList);
267 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
268 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
269 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
271 // Filter edges and faces in tools.
273 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
274 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
275 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
276 anEdgesAndFaces.push_back(*anIt);
278 aTools.push_back(*anIt);
282 if((anObjects.size() + aTools.size() +
283 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
284 myFeature->setError("Error: Not enough objects for boolean operation.");
288 // Collecting all solids which will be fused.
289 ListOfShape aSolidsToFuse;
290 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
291 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
293 // Collecting solids from compsolids which will not be
294 // modified in boolean operation and will be added to result.
295 ListOfShape aShapesToAdd;
296 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
297 anIt != aCompSolidsObjects.end(); anIt++) {
298 GeomShapePtr aCompSolid = anIt->first;
299 ListOfShape& aUsedShapes = anIt->second;
300 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
302 // Collect solids from compsolid which will not be modified in boolean operation.
303 for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
307 GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
308 ListOfShape::iterator aUseIt = aUsedShapes.begin();
309 for(; aUseIt != aUsedShapes.end(); aUseIt++) {
310 if(aSolidInCompSolid->isEqual(*aUseIt)) {
314 if(aUseIt == aUsedShapes.end()) {
315 aShapesToAdd.push_back(aSolidInCompSolid);
320 // Cut edges and faces(if we have any) with solids.
321 ListOfShape aCutTools;
322 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
323 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
324 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
326 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
327 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
328 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
330 GeomAlgoAPI_Tools::BOOL_CUT));
331 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
332 anEdgesAndFaces.clear();
333 anEdgesAndFaces.push_back(aCutAlgo->shape());
334 aMakeShapeList->appendAlgo(aCutAlgo);
338 // If we have compsolids then cut with not used solids all others.
339 if(!aShapesToAdd.empty()) {
340 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
342 GeomAlgoAPI_Tools::BOOL_CUT));
343 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::area(aCutAlgo->shape()) > 1.e-27) {
344 aSolidsToFuse.clear();
345 aSolidsToFuse.push_back(aCutAlgo->shape());
346 aMakeShapeList->appendAlgo(aCutAlgo);
350 // Fuse all objects and all tools.
351 GeomShapePtr aFusedShape;
352 if(aSolidsToFuse.size() == 1) {
353 aFusedShape = aSolidsToFuse.front();
354 } else if(aSolidsToFuse.size() > 1){
356 anObjects.push_back(aSolidsToFuse.front());
357 aSolidsToFuse.pop_front();
358 aTools = aSolidsToFuse;
360 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
362 GeomAlgoAPI_Tools::BOOL_FUSE));
364 // Checking that the algorithm worked properly.
365 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
366 myFeature->setError("Error: Boolean algorithm failed.");
370 aFusedShape = aFuseAlgo->shape();
371 aMakeShapeList->appendAlgo(aFuseAlgo);
374 // Combine result with not used solids from compsolid and edges and faces (if we have any).
375 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
376 if(!aShapesToAdd.empty()) {
377 if(aFusedShape.get()) {
378 aShapesToAdd.push_back(aFusedShape);
381 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
382 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
383 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
384 myFeature->setError("Error: PaveFiller algorithm failed.");
388 aMakeShapeList->appendAlgo(aFillerAlgo);
391 theMakeShapes.push_back(aMakeShapeList);
394 default: // [to avoid compilation warnings]
401 //=================================================================================================
402 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
403 const GeomShapePtr theObject,
404 const ListOfShape& theTools,
405 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
407 ListOfShape aTools = theTools;
408 aTools.push_back(theObject);
410 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
411 theResultBody->loadModifiedShapes(theMakeShape, *anIt,
412 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
413 GeomAPI_Shape::EDGE :
414 GeomAPI_Shape::FACE);
418 //==================================================================================================
419 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
420 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
421 const ListOfShape& theTools,
422 const GeomShapePtr theResultShapesCompound)
424 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
425 anIt != theResultBaseAlgoList.end();
428 ResultBaseAlgo& aRCA = *anIt;
429 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
432 theResultShapesCompound);
434 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
436 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
439 theResultShapesCompound);