Salome HOME
6e3e8796b16493de7e94cbdb991ef587f26db77d
[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_Tools.h>
25
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>
31
32 #include <GeomAPI_ShapeIterator.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   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) {
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());
88
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);
98       }
99
100       storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt);
101
102       ResultBaseAlgo aRBA;
103       aRBA.resultBody = aResultBody;
104       aRBA.baseShape = *aBoolObjIt;
105       aRBA.makeShape = *aBoolMSIt;
106       aResultBaseAlgoList.push_back(aRBA);
107       aResultShapesList.push_back((*aBoolMSIt)->shape());
108     }
109
110     myFeature->setResult(aResultBody, aResultIndex++);
111   }
112
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);
117
118   myFeature->removeResults(aResultIndex);
119 }
120
121 //=================================================================================================
122 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
123                                                   ListOfShape& theObjects,
124                                                   ListOfMakeShape& theMakeShapes)
125 {
126   // Getting objects.
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.");
135       return false;
136     }
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);
145           break;
146         }
147       }
148       if(anIt == aCompSolidsObjects.end()) {
149         aCompSolidsObjects[aContextShape].push_back(anObject);
150         aCompSolids.push_back(aContextShape);
151       }
152     } else {
153       if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
154          anObject->shapeType() == GeomAPI_Shape::FACE) {
155         anEdgesAndFaces.push_back(anObject);
156       } else {
157         anObjects.push_back(anObject);
158       }
159     }
160   }
161
162   switch(myOperationType) {
163     case BOOL_CUT: {
164       if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
165         myFeature->setError("Error: Not enough objects for boolean operation.");
166         return false;
167       }
168
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,
176                                                                 theTools,
177                                                                 GeomAlgoAPI_Boolean::BOOL_CUT));
178
179         // Checking that the algorithm worked properly.
180         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
181           myFeature->setError("Error: Boolean algorithm failed.");
182           return false;
183         }
184
185         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
186           theObjects.push_back(anObject);
187           theMakeShapes.push_back(aBoolAlgo);
188         }
189       }
190
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;
196
197         // Collecting solids from compsolids which will not be modified in boolean operation.
198         ListOfShape aShapesToAdd;
199         for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
200              aCompSolidIt.more();
201              aCompSolidIt.next())
202         {
203           GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
204           ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
205           for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
206             if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
207               break;
208             }
209           }
210           if(aUsedShapesIt == aUsedShapes.end()) {
211             aShapesToAdd.push_back(aSolidInCompSolid);
212           }
213         }
214
215         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
216                                                                   theTools,
217                                                                   GeomAlgoAPI_Boolean::BOOL_CUT));
218
219         // Checking that the algorithm worked properly.
220         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
221           myFeature->setError("Error: Boolean algorithm failed.");
222           return false;
223         }
224
225         std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
226         aMakeShapeList->appendAlgo(aBoolAlgo);
227
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.");
234           return false;
235         }
236
237         aMakeShapeList->appendAlgo(aFillerAlgo);
238
239         if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
240           theObjects.push_back(aCompSolid);
241           theMakeShapes.push_back(aMakeShapeList);
242         }
243       }
244       break;
245     }
246     case BOOL_FUSE: {
247       // Set objects.
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());
251
252       // Filter edges and faces in tools.
253       ListOfShape aTools;
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);
258         } else {
259           aTools.push_back(*anIt);
260         }
261       }
262
263       if((anObjects.size() + aTools.size() +
264           aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
265         myFeature->setError("Error: Not enough objects for boolean operation.");
266         return false;
267       }
268
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());
273
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());
282
283         // Collect solids from compsolid which will not be modified in boolean operation.
284         for (GeomAPI_ShapeIterator aCompSolidIt(aCompSolid);
285              aCompSolidIt.more();
286              aCompSolidIt.next())
287         {
288           GeomShapePtr aSolidInCompSolid = aCompSolidIt.current();
289           ListOfShape::iterator anIt = aUsedShapes.begin();
290           for(; anIt != aUsedShapes.end(); anIt++) {
291             if(aSolidInCompSolid->isEqual(*anIt)) {
292               break;
293             }
294           }
295           if(anIt == aUsedShapes.end()) {
296             aShapesToAdd.push_back(aSolidInCompSolid);
297           }
298         }
299       }
300
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());
306
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,
310                                                               aCutTools,
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);
316         }
317       }
318
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,
322                                                               aShapesToAdd,
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);
328         }
329       }
330
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){
336         anObjects.clear();
337         anObjects.push_back(aSolidsToFuse.front());
338         aSolidsToFuse.pop_front();
339         aTools = aSolidsToFuse;
340
341         std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
342                                                           aTools,
343                                                           GeomAlgoAPI_Boolean::BOOL_FUSE));
344
345         // Checking that the algorithm worked properly.
346         if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
347           myFeature->setError("Error: Boolean algorithm failed.");
348           return false;
349         }
350
351         aFusedShape = aFuseAlgo->shape();
352         aMakeShapeList->appendAlgo(aFuseAlgo);
353       }
354
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);
360         }
361
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.");
366           return false;
367         }
368
369         aMakeShapeList->appendAlgo(aFillerAlgo);
370       }
371
372       theMakeShapes.push_back(aMakeShapeList);
373       break;
374     }
375   }
376
377   return true;
378 }
379
380 //=================================================================================================
381 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
382                                 const GeomShapePtr theObject,
383                                 const ListOfShape& theTools,
384                                 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
385 {
386   ListOfShape aTools = theTools;
387   aTools.push_back(theObject);
388
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);
394   }
395 }
396
397 //==================================================================================================
398 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
399   std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
400   const ListOfShape& theTools,
401   const GeomShapePtr theResultShapesCompound)
402 {
403   for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
404     anIt != theResultBaseAlgoList.end();
405     ++anIt)
406   {
407     ResultBaseAlgo& aRCA = *anIt;
408     aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
409       aRCA.baseShape,
410       GeomAPI_Shape::FACE,
411       theResultShapesCompound);
412
413     for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
414     {
415       aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
416         *anIter,
417         GeomAPI_Shape::FACE,
418         theResultShapesCompound);
419     }
420   }
421 }