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_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 AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
130 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
131 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
132 GeomShapePtr anObject = anObjectAttr->value();
133 if(!anObject.get()) {
134 myFeature->setError("Error: Could not get object.");
137 ResultPtr aContext = anObjectAttr->context();
138 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
139 if(aResCompSolidPtr.get()) {
140 GeomShapePtr aContextShape = aResCompSolidPtr->shape();
141 std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
142 for(; anIt != aCompSolidsObjects.end(); anIt++) {
143 if(anIt->first->isEqual(aContextShape)) {
144 aCompSolidsObjects[anIt->first].push_back(anObject);
148 if(anIt == aCompSolidsObjects.end()) {
149 aCompSolidsObjects[aContextShape].push_back(anObject);
150 aCompSolids.push_back(aContextShape);
153 if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
154 anObject->shapeType() == GeomAPI_Shape::FACE) {
155 anEdgesAndFaces.push_back(anObject);
157 anObjects.push_back(anObject);
162 switch(myOperationType) {
164 if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
165 myFeature->setError("Error: Not enough objects for boolean operation.");
169 // For solids cut each object with all tools.
170 for(ListOfShape::const_iterator
171 anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
172 GeomShapePtr anObject = *anIt;
173 ListOfShape aListWithObject;
174 aListWithObject.push_back(anObject);
175 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
177 GeomAlgoAPI_Boolean::BOOL_CUT));
179 // Checking that the algorithm worked properly.
180 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
181 myFeature->setError("Error: Boolean algorithm failed.");
185 if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
186 theObjects.push_back(anObject);
187 theMakeShapes.push_back(aBoolAlgo);
191 // Compsolids handling
192 for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
193 anIt != aCompSolidsObjects.cend(); ++anIt) {
194 GeomShapePtr aCompSolid = anIt->first;
195 const ListOfShape& aUsedShapes = anIt->second;
197 // Collecting solids from compsolids which will not be modified in boolean operation.
198 ListOfShape aShapesToAdd;
199 for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
203 GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
204 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
205 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
206 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
210 if(aUsedShapesIt == aUsedShapes.end()) {
211 aShapesToAdd.push_back(aSolidInCompSolid);
215 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
217 GeomAlgoAPI_Boolean::BOOL_CUT));
219 // Checking that the algorithm worked properly.
220 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
221 myFeature->setError("Error: Boolean algorithm failed.");
225 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
226 aMakeShapeList->appendAlgo(aBoolAlgo);
228 // Add result to not used solids from compsolid.
229 aShapesToAdd.push_back(aBoolAlgo->shape());
230 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
231 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
232 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
233 myFeature->setError("Error: PaveFiller algorithm failed.");
237 aMakeShapeList->appendAlgo(aFillerAlgo);
239 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
240 theObjects.push_back(aCompSolid);
241 theMakeShapes.push_back(aMakeShapeList);
248 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
249 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
250 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
252 // Filter edges and faces in tools.
254 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
255 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
256 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
257 anEdgesAndFaces.push_back(*anIt);
259 aTools.push_back(*anIt);
263 if((anObjects.size() + aTools.size() +
264 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
265 myFeature->setError("Error: Not enough objects for boolean operation.");
269 // Collecting all solids which will be fused.
270 ListOfShape aSolidsToFuse;
271 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
272 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
274 // Collecting solids from compsolids which will not be
275 // modified in boolean operation and will be added to result.
276 ListOfShape aShapesToAdd;
277 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
278 anIt != aCompSolidsObjects.end(); anIt++) {
279 GeomShapePtr aCompSolid = anIt->first;
280 ListOfShape& aUsedShapes = anIt->second;
281 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
283 // Collect solids from compsolid which will not be modified in boolean operation.
284 for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
288 GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
289 ListOfShape::iterator anIt = aUsedShapes.begin();
290 for(; anIt != aUsedShapes.end(); anIt++) {
291 if(aSolidInCompSolid->isEqual(*anIt)) {
295 if(anIt == aUsedShapes.end()) {
296 aShapesToAdd.push_back(aSolidInCompSolid);
301 // Cut edges and faces(if we have any) with solids.
302 ListOfShape aCutTools;
303 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
304 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
305 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
307 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
308 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
309 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
311 GeomAlgoAPI_Boolean::BOOL_CUT));
312 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
313 anEdgesAndFaces.clear();
314 anEdgesAndFaces.push_back(aCutAlgo->shape());
315 aMakeShapeList->appendAlgo(aCutAlgo);
319 // If we have compsolids then cut with not used solids all others.
320 if(!aShapesToAdd.empty()) {
321 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
323 GeomAlgoAPI_Boolean::BOOL_CUT));
324 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
325 aSolidsToFuse.clear();
326 aSolidsToFuse.push_back(aCutAlgo->shape());
327 aMakeShapeList->appendAlgo(aCutAlgo);
331 // Fuse all objects and all tools.
332 GeomShapePtr aFusedShape;
333 if(aSolidsToFuse.size() == 1) {
334 aFusedShape = aSolidsToFuse.front();
335 } else if(aSolidsToFuse.size() > 1){
337 anObjects.push_back(aSolidsToFuse.front());
338 aSolidsToFuse.pop_front();
339 aTools = aSolidsToFuse;
341 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
343 GeomAlgoAPI_Boolean::BOOL_FUSE));
345 // Checking that the algorithm worked properly.
346 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
347 myFeature->setError("Error: Boolean algorithm failed.");
351 aFusedShape = aFuseAlgo->shape();
352 aMakeShapeList->appendAlgo(aFuseAlgo);
355 // Combine result with not used solids from compsolid and edges and faces (if we have any).
356 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
357 if(!aShapesToAdd.empty()) {
358 if(aFusedShape.get()) {
359 aShapesToAdd.push_back(aFusedShape);
362 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
363 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
364 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
365 myFeature->setError("Error: PaveFiller algorithm failed.");
369 aMakeShapeList->appendAlgo(aFillerAlgo);
372 theMakeShapes.push_back(aMakeShapeList);
380 //=================================================================================================
381 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
382 const GeomShapePtr theObject,
383 const ListOfShape& theTools,
384 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
386 ListOfShape aTools = theTools;
387 aTools.push_back(theObject);
389 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
390 theResultBody->loadModifiedShapes(theMakeShape, *anIt,
391 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
392 GeomAPI_Shape::EDGE :
393 GeomAPI_Shape::FACE);
397 //==================================================================================================
398 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
399 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
400 const ListOfShape& theTools,
401 const GeomShapePtr theResultShapesCompound)
403 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
404 anIt != theResultBaseAlgoList.end();
407 ResultBaseAlgo& aRCA = *anIt;
408 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
411 theResultShapesCompound);
413 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
415 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
418 theResultShapesCompound);