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_CompositeBoolean.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Tools.h>
26 #include <GeomAlgoAPI_Boolean.h>
27 #include <GeomAlgoAPI_CompoundBuilder.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_PaveFiller.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_ShapeExplorer.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) {
82 ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
84 if((*aBoolObjIt)->isEqual((*aBoolMSIt)->shape())) {
85 aResultBody->store((*aBoolMSIt)->shape(), false);
89 aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape());
93 // Store generation history.
94 ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
95 ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
96 for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
97 ++aGenBaseIt, ++aGenMSIt) {
98 storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt);
101 storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt);
104 aRBA.resultBody = aResultBody;
105 aRBA.baseShape = *aBoolObjIt;
106 aRBA.makeShape = *aBoolMSIt;
107 aResultBaseAlgoList.push_back(aRBA);
108 aResultShapesList.push_back((*aBoolMSIt)->shape());
111 myFeature->setResult(aResultBody, aResultIndex++);
114 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
115 // result shape has been deleted, but in another it was modified or stayed.
116 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
117 storeDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
119 myFeature->removeResults(aResultIndex);
122 //=================================================================================================
123 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
124 ListOfShape& theObjects,
125 ListOfMakeShape& theMakeShapes)
128 ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
129 std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
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);
154 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
155 anObject->shapeType() == GeomAPI_Shape::FACE) {
156 anEdgesAndFaces.push_back(anObject);
158 anObjects.push_back(anObject);
163 switch(myOperationType) {
165 if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
166 myFeature->setError("Error: Not enough objects for boolean operation.");
170 // For solids cut each object with all tools.
171 for(ListOfShape::const_iterator
172 anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
173 GeomShapePtr anObject = *anIt;
174 ListOfShape aListWithObject;
175 aListWithObject.push_back(anObject);
176 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
178 GeomAlgoAPI_Boolean::BOOL_CUT));
180 // Checking that the algorithm worked properly.
181 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
182 myFeature->setError("Error: Boolean algorithm failed.");
186 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
187 theObjects.push_back(anObject);
188 theMakeShapes.push_back(aBoolAlgo);
192 // Compsolids handling
193 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
194 anIt != aCompSolidsObjects.cend(); ++anIt) {
195 GeomShapePtr aCompSolid = anIt->first;
196 const ListOfShape& aUsedShapes = anIt->second;
198 // Collecting solids from compsolids which will not be modified in boolean operation.
199 ListOfShape aShapesToAdd;
200 for(GeomAPI_ShapeExplorer
201 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
202 GeomShapePtr aSolidInCompSolid = anExp.current();
203 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
204 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
205 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
209 if(aUsedShapesIt == aUsedShapes.end()) {
210 aShapesToAdd.push_back(aSolidInCompSolid);
214 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
216 GeomAlgoAPI_Boolean::BOOL_CUT));
218 // Checking that the algorithm worked properly.
219 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
220 myFeature->setError("Error: Boolean algorithm failed.");
224 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
225 aMakeShapeList->appendAlgo(aBoolAlgo);
227 // Add result to not used solids from compsolid.
228 aShapesToAdd.push_back(aBoolAlgo->shape());
229 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
230 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
231 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
232 myFeature->setError("Error: PaveFiller algorithm failed.");
236 aMakeShapeList->appendAlgo(aFillerAlgo);
238 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
239 theObjects.push_back(aCompSolid);
240 theMakeShapes.push_back(aMakeShapeList);
247 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
248 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
249 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
251 // Filter edges and faces in tools.
253 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
254 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
255 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
256 anEdgesAndFaces.push_back(*anIt);
258 aTools.push_back(*anIt);
262 if((anObjects.size() + aTools.size() +
263 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
264 myFeature->setError("Error: Not enough objects for boolean operation.");
268 // Collecting all solids which will be fused.
269 ListOfShape aSolidsToFuse;
270 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
271 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
273 // Collecting solids from compsolids which will not be
274 // modified in boolean operation and will be added to result.
275 ListOfShape aShapesToAdd;
276 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
277 anIt != aCompSolidsObjects.end(); anIt++) {
278 GeomShapePtr aCompSolid = anIt->first;
279 ListOfShape& aUsedShapes = anIt->second;
280 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
282 // Collect solids from compsolid which will not be modified in boolean operation.
283 for(GeomAPI_ShapeExplorer
284 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
285 GeomShapePtr aSolidInCompSolid = anExp.current();
286 ListOfShape::iterator anIt = aUsedShapes.begin();
287 for(; anIt != aUsedShapes.end(); anIt++) {
288 if(aSolidInCompSolid->isEqual(*anIt)) {
292 if(anIt == aUsedShapes.end()) {
293 aShapesToAdd.push_back(aSolidInCompSolid);
298 // Cut edges and faces(if we have any) with solids.
299 ListOfShape aCutTools;
300 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
301 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
302 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
304 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
305 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
306 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
308 GeomAlgoAPI_Boolean::BOOL_CUT));
309 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
310 anEdgesAndFaces.clear();
311 anEdgesAndFaces.push_back(aCutAlgo->shape());
312 aMakeShapeList->appendAlgo(aCutAlgo);
316 // If we have compsolids then cut with not used solids all others.
317 if(!aShapesToAdd.empty()) {
318 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
320 GeomAlgoAPI_Boolean::BOOL_CUT));
321 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
322 aSolidsToFuse.clear();
323 aSolidsToFuse.push_back(aCutAlgo->shape());
324 aMakeShapeList->appendAlgo(aCutAlgo);
328 // Fuse all objects and all tools.
329 GeomShapePtr aFusedShape;
330 if(aSolidsToFuse.size() == 1) {
331 aFusedShape = aSolidsToFuse.front();
332 } else if(aSolidsToFuse.size() > 1){
334 anObjects.push_back(aSolidsToFuse.front());
335 aSolidsToFuse.pop_front();
336 aTools = aSolidsToFuse;
338 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
340 GeomAlgoAPI_Boolean::BOOL_FUSE));
342 // Checking that the algorithm worked properly.
343 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
344 myFeature->setError("Error: Boolean algorithm failed.");
348 aFusedShape = aFuseAlgo->shape();
349 aMakeShapeList->appendAlgo(aFuseAlgo);
352 // Combine result with not used solids from compsolid and edges and faces (if we have any).
353 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
354 if(!aShapesToAdd.empty()) {
355 if(aFusedShape.get()) {
356 aShapesToAdd.push_back(aFusedShape);
359 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
360 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
361 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
362 myFeature->setError("Error: PaveFiller algorithm failed.");
366 aMakeShapeList->appendAlgo(aFillerAlgo);
369 theMakeShapes.push_back(aMakeShapeList);
377 //=================================================================================================
378 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
379 const GeomShapePtr theObject,
380 const ListOfShape& theTools,
381 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
383 ListOfShape aTools = theTools;
384 aTools.push_back(theObject);
387 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
388 theResultBody->loadModifiedShapes(theMakeShape, *anIt,
389 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
390 GeomAPI_Shape::EDGE :
391 GeomAPI_Shape::FACE);
395 //==================================================================================================
396 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
397 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
398 const ListOfShape& theTools,
399 const GeomShapePtr theResultShapesCompound)
401 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
402 anIt != theResultBaseAlgoList.end();
405 ResultBaseAlgo& aRCA = *anIt;
406 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
409 theResultShapesCompound);
411 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
413 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
416 theResultShapesCompound);