Salome HOME
Issue #2255: Wrong result of ExtrusionCut if a solid has no common part with the...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CompositeBoolean.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_CompositeBoolean.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultCompSolid.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_PaveFiller.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31
32 #include <GeomAPI_ShapeExplorer.h>
33
34 #include <map>
35
36 //=================================================================================================
37 void FeaturesPlugin_CompositeBoolean::initBooleanAttributes()
38 {
39   myFeature->data()->addAttribute(OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
40 }
41
42 //=================================================================================================
43 void FeaturesPlugin_CompositeBoolean::executeCompositeBoolean()
44 {
45   // Make generation.
46   ListOfShape aGenBaseShapes;
47   ListOfMakeShape aGenMakeShapes;
48   if(!makeGeneration(aGenBaseShapes, aGenMakeShapes)) {
49     return;
50   }
51
52   // Getting tools.
53   ListOfShape aTools;
54   for(ListOfMakeShape::const_iterator
55       anIt = aGenMakeShapes.cbegin(); anIt != aGenMakeShapes.cend(); ++anIt) {
56     aTools.push_back((*anIt)->shape());
57   }
58
59   // Make boolean.
60   ListOfShape aBooleanObjects;
61   ListOfMakeShape aBooleanMakeShapes;
62   if(!makeBoolean(aTools, aBooleanObjects, aBooleanMakeShapes)) {
63     return;
64   }
65
66   if(myOperationType == BOOL_FUSE) {
67     aTools.splice(aTools.begin(), aBooleanObjects);
68     aBooleanObjects.splice(aBooleanObjects.begin(), aTools, aTools.begin());
69   }
70
71   // Store result.
72   int aResultIndex = 0;
73   ListOfShape::const_iterator aBoolObjIt = aBooleanObjects.cbegin();
74   ListOfMakeShape::const_iterator aBoolMSIt = aBooleanMakeShapes.cbegin();
75   for(; aBoolObjIt != aBooleanObjects.cend() && aBoolMSIt != aBooleanMakeShapes.cend();
76       ++aBoolObjIt, ++aBoolMSIt) {
77
78     int aTag = 1;
79
80     ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
81
82     if((*aBoolObjIt)->isEqual((*aBoolMSIt)->shape())) {
83       aResultBody->store((*aBoolMSIt)->shape(), false);
84     }
85     else
86     {
87       aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape(), aTag);
88
89       aTag += 5000;
90
91       // Store generation history.
92       ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
93       ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
94       for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
95           ++aGenBaseIt, ++aGenMSIt) {
96         storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt, aTag);
97       }
98
99       int aModTag = aTag;
100       storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt, aModTag);
101     }
102
103     myFeature->setResult(aResultBody, aResultIndex++);
104   }
105
106   myFeature->removeResults(aResultIndex);
107 }
108
109 //=================================================================================================
110 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
111                                                   ListOfShape& theObjects,
112                                                   ListOfMakeShape& theMakeShapes)
113 {
114   // Getting objects.
115   ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
116   std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
117   AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
118   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
119     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
120     GeomShapePtr anObject = anObjectAttr->value();
121     if(!anObject.get()) {
122       myFeature->setError("Error: Could not get object.");
123       return false;
124     }
125     ResultPtr aContext = anObjectAttr->context();
126     ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
127     if(aResCompSolidPtr.get()) {
128       GeomShapePtr aContextShape = aResCompSolidPtr->shape();
129       std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
130       for(; anIt != aCompSolidsObjects.end(); anIt++) {
131         if(anIt->first->isEqual(aContextShape)) {
132           aCompSolidsObjects[anIt->first].push_back(anObject);
133           break;
134         }
135       }
136       if(anIt == aCompSolidsObjects.end()) {
137         aCompSolidsObjects[aContextShape].push_back(anObject);
138         aCompSolids.push_back(aContextShape);
139       }
140     } else {
141       if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
142          anObject->shapeType() == GeomAPI_Shape::FACE) {
143         anEdgesAndFaces.push_back(anObject);
144       } else {
145         anObjects.push_back(anObject);
146       }
147     }
148   }
149
150   switch(myOperationType) {
151     case BOOL_CUT: {
152       if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
153         myFeature->setError("Error: Not enough objects for boolean operation.");
154         return false;
155       }
156
157       // For solids cut each object with all tools.
158       for(ListOfShape::const_iterator
159           anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
160         GeomShapePtr anObject = *anIt;
161         ListOfShape aListWithObject;
162         aListWithObject.push_back(anObject);
163         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
164                                                                 theTools,
165                                                                 GeomAlgoAPI_Boolean::BOOL_CUT));
166
167         // Checking that the algorithm worked properly.
168         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
169           myFeature->setError("Error: Boolean algorithm failed.");
170           return false;
171         }
172
173         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
174           theObjects.push_back(anObject);
175           theMakeShapes.push_back(aBoolAlgo);
176         }
177       }
178
179       // Compsolids handling
180       for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
181           anIt != aCompSolidsObjects.cend(); ++anIt) {
182         GeomShapePtr aCompSolid = anIt->first;
183         const ListOfShape& aUsedShapes = anIt->second;
184
185         // Collecting solids from compsolids which will not be modified in boolean operation.
186         ListOfShape aShapesToAdd;
187         for(GeomAPI_ShapeExplorer
188             anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
189           GeomShapePtr aSolidInCompSolid = anExp.current();
190           ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
191           for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
192             if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
193               break;
194             }
195           }
196           if(aUsedShapesIt == aUsedShapes.end()) {
197             aShapesToAdd.push_back(aSolidInCompSolid);
198           }
199         }
200
201         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
202                                                                   theTools,
203                                                                   GeomAlgoAPI_Boolean::BOOL_CUT));
204
205         // Checking that the algorithm worked properly.
206         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
207           myFeature->setError("Error: Boolean algorithm failed.");
208           return false;
209         }
210
211         std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
212         aMakeShapeList->appendAlgo(aBoolAlgo);
213
214         // Add result to not used solids from compsolid.
215         aShapesToAdd.push_back(aBoolAlgo->shape());
216         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
217           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
218         if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
219           myFeature->setError("Error: PaveFiller algorithm failed.");
220           return false;
221         }
222
223         aMakeShapeList->appendAlgo(aFillerAlgo);
224
225         if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
226           theObjects.push_back(aCompSolid);
227           theMakeShapes.push_back(aMakeShapeList);
228         }
229       }
230       break;
231     }
232     case BOOL_FUSE: {
233       // Set objects.
234       theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
235       theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
236       theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
237
238       // Filter edges and faces in tools.
239       ListOfShape aTools;
240       for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
241         if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
242            (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
243           anEdgesAndFaces.push_back(*anIt);
244         } else {
245           aTools.push_back(*anIt);
246         }
247       }
248
249       if((anObjects.size() + aTools.size() +
250           aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
251         myFeature->setError("Error: Not enough objects for boolean operation.");
252         return false;
253       }
254
255       // Collecting all solids which will be fused.
256       ListOfShape aSolidsToFuse;
257       aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
258       aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
259
260       // Collecting solids from compsolids which will not be
261       // modified in boolean operation and will be added to result.
262       ListOfShape aShapesToAdd;
263       for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
264           anIt != aCompSolidsObjects.end(); anIt++) {
265         GeomShapePtr aCompSolid = anIt->first;
266         ListOfShape& aUsedShapes = anIt->second;
267         aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
268
269         // Collect solids from compsolid which will not be modified in boolean operation.
270         for(GeomAPI_ShapeExplorer
271             anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
272           GeomShapePtr aSolidInCompSolid = anExp.current();
273           ListOfShape::iterator anIt = aUsedShapes.begin();
274           for(; anIt != aUsedShapes.end(); anIt++) {
275             if(aSolidInCompSolid->isEqual(*anIt)) {
276               break;
277             }
278           }
279           if(anIt == aUsedShapes.end()) {
280             aShapesToAdd.push_back(aSolidInCompSolid);
281           }
282         }
283       }
284
285       // Cut edges and faces(if we have any) with solids.
286       ListOfShape aCutTools;
287       aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
288       aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
289       aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
290
291       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
292       if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
293         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
294                                                               aCutTools,
295                                                               GeomAlgoAPI_Boolean::BOOL_CUT));
296         if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
297           anEdgesAndFaces.clear();
298           anEdgesAndFaces.push_back(aCutAlgo->shape());
299           aMakeShapeList->appendAlgo(aCutAlgo);
300         }
301       }
302
303       // If we have compsolids then cut with not used solids all others.
304       if(!aShapesToAdd.empty()) {
305         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
306                                                               aShapesToAdd,
307                                                               GeomAlgoAPI_Boolean::BOOL_CUT));
308         if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
309           aSolidsToFuse.clear();
310           aSolidsToFuse.push_back(aCutAlgo->shape());
311           aMakeShapeList->appendAlgo(aCutAlgo);
312         }
313       }
314
315       // Fuse all objects and all tools.
316       GeomShapePtr aFusedShape;
317       if(aSolidsToFuse.size() == 1) {
318         aFusedShape = aSolidsToFuse.front();
319       } else if(aSolidsToFuse.size() > 1){
320         anObjects.clear();
321         anObjects.push_back(aSolidsToFuse.front());
322         aSolidsToFuse.pop_front();
323         aTools = aSolidsToFuse;
324
325         std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
326                                                           aTools,
327                                                           GeomAlgoAPI_Boolean::BOOL_FUSE));
328
329         // Checking that the algorithm worked properly.
330         if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
331           myFeature->setError("Error: Boolean algorithm failed.");
332           return false;
333         }
334
335         aFusedShape = aFuseAlgo->shape();
336         aMakeShapeList->appendAlgo(aFuseAlgo);
337       }
338
339       // Combine result with not used solids from compsolid and edges and faces (if we have any).
340       aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
341       if(!aShapesToAdd.empty()) {
342         if(aFusedShape.get()) {
343           aShapesToAdd.push_back(aFusedShape);
344         }
345
346         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
347           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
348         if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
349           myFeature->setError("Error: PaveFiller algorithm failed.");
350           return false;
351         }
352
353         aMakeShapeList->appendAlgo(aFillerAlgo);
354       }
355
356       theMakeShapes.push_back(aMakeShapeList);
357       break;
358     }
359   }
360
361   return true;
362 }
363
364 //=================================================================================================
365 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
366                                 const GeomShapePtr theObject,
367                                 const ListOfShape& theTools,
368                                 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
369                                 int& theTag)
370 {
371   int aModTag = theTag;
372   int anEdgesAndFacesTag = ++aModTag;
373   int aDelTag = ++anEdgesAndFacesTag;
374   theTag = aDelTag;
375
376   const std::string aModName = "Modfied";
377
378   ListOfShape aTools = theTools;
379   aTools.push_back(theObject);
380
381   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMap = theMakeShape->mapOfSubShapes();
382
383   int aTag;
384   std::string aName;
385   for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
386     if((*anIt)->shapeType() == GeomAPI_Shape::EDGE) {
387       aTag = anEdgesAndFacesTag;
388       aName = aModName + "_Edge";
389     }
390     else if((*anIt)->shapeType() == GeomAPI_Shape::FACE) {
391       aTag = anEdgesAndFacesTag;
392       aName = aModName + "_Face";
393     } else {
394       aTag = aModTag;
395       aName = aModName;
396     }
397     theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt,
398       (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
399       GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE, aTag, aName, *aMap.get(), false, false, true);
400     theResultBody->loadDeletedShapes(theMakeShape.get(), *anIt, GeomAPI_Shape::FACE, aDelTag);
401   }
402 }