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) {
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_ShapeExplorer
200 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
201 GeomShapePtr aSolidInCompSolid = anExp.current();
202 ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
203 for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
204 if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
208 if(aUsedShapesIt == aUsedShapes.end()) {
209 aShapesToAdd.push_back(aSolidInCompSolid);
213 std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
215 GeomAlgoAPI_Boolean::BOOL_CUT));
217 // Checking that the algorithm worked properly.
218 if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
219 myFeature->setError("Error: Boolean algorithm failed.");
223 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
224 aMakeShapeList->appendAlgo(aBoolAlgo);
226 // Add result to not used solids from compsolid.
227 aShapesToAdd.push_back(aBoolAlgo->shape());
228 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
229 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
230 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
231 myFeature->setError("Error: PaveFiller algorithm failed.");
235 aMakeShapeList->appendAlgo(aFillerAlgo);
237 if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
238 theObjects.push_back(aCompSolid);
239 theMakeShapes.push_back(aMakeShapeList);
246 theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
247 theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
248 theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
250 // Filter edges and faces in tools.
252 for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
253 if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
254 (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
255 anEdgesAndFaces.push_back(*anIt);
257 aTools.push_back(*anIt);
261 if((anObjects.size() + aTools.size() +
262 aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
263 myFeature->setError("Error: Not enough objects for boolean operation.");
267 // Collecting all solids which will be fused.
268 ListOfShape aSolidsToFuse;
269 aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
270 aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
272 // Collecting solids from compsolids which will not be
273 // modified in boolean operation and will be added to result.
274 ListOfShape aShapesToAdd;
275 for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
276 anIt != aCompSolidsObjects.end(); anIt++) {
277 GeomShapePtr aCompSolid = anIt->first;
278 ListOfShape& aUsedShapes = anIt->second;
279 aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
281 // Collect solids from compsolid which will not be modified in boolean operation.
282 for(GeomAPI_ShapeExplorer
283 anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
284 GeomShapePtr aSolidInCompSolid = anExp.current();
285 ListOfShape::iterator anIt = aUsedShapes.begin();
286 for(; anIt != aUsedShapes.end(); anIt++) {
287 if(aSolidInCompSolid->isEqual(*anIt)) {
291 if(anIt == aUsedShapes.end()) {
292 aShapesToAdd.push_back(aSolidInCompSolid);
297 // Cut edges and faces(if we have any) with solids.
298 ListOfShape aCutTools;
299 aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
300 aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
301 aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
303 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
304 if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
305 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
307 GeomAlgoAPI_Boolean::BOOL_CUT));
308 if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
309 anEdgesAndFaces.clear();
310 anEdgesAndFaces.push_back(aCutAlgo->shape());
311 aMakeShapeList->appendAlgo(aCutAlgo);
315 // If we have compsolids then cut with not used solids all others.
316 if(!aShapesToAdd.empty()) {
317 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
319 GeomAlgoAPI_Boolean::BOOL_CUT));
320 if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
321 aSolidsToFuse.clear();
322 aSolidsToFuse.push_back(aCutAlgo->shape());
323 aMakeShapeList->appendAlgo(aCutAlgo);
327 // Fuse all objects and all tools.
328 GeomShapePtr aFusedShape;
329 if(aSolidsToFuse.size() == 1) {
330 aFusedShape = aSolidsToFuse.front();
331 } else if(aSolidsToFuse.size() > 1){
333 anObjects.push_back(aSolidsToFuse.front());
334 aSolidsToFuse.pop_front();
335 aTools = aSolidsToFuse;
337 std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
339 GeomAlgoAPI_Boolean::BOOL_FUSE));
341 // Checking that the algorithm worked properly.
342 if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
343 myFeature->setError("Error: Boolean algorithm failed.");
347 aFusedShape = aFuseAlgo->shape();
348 aMakeShapeList->appendAlgo(aFuseAlgo);
351 // Combine result with not used solids from compsolid and edges and faces (if we have any).
352 aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
353 if(!aShapesToAdd.empty()) {
354 if(aFusedShape.get()) {
355 aShapesToAdd.push_back(aFusedShape);
358 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
359 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
360 if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
361 myFeature->setError("Error: PaveFiller algorithm failed.");
365 aMakeShapeList->appendAlgo(aFillerAlgo);
368 theMakeShapes.push_back(aMakeShapeList);
376 //=================================================================================================
377 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
378 const GeomShapePtr theObject,
379 const ListOfShape& theTools,
380 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
382 ListOfShape aTools = theTools;
383 aTools.push_back(theObject);
385 for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
386 theResultBody->loadModifiedShapes(theMakeShape, *anIt,
387 (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
388 GeomAPI_Shape::EDGE :
389 GeomAPI_Shape::FACE);
393 //==================================================================================================
394 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
395 std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
396 const ListOfShape& theTools,
397 const GeomShapePtr theResultShapesCompound)
399 for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
400 anIt != theResultBaseAlgoList.end();
403 ResultBaseAlgo& aRCA = *anIt;
404 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
407 theResultShapesCompound);
409 for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
411 aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
414 theResultShapesCompound);