]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_CompositeBoolean.cpp
Salome HOME
670dbe02a845e6f2768217bd3dbceb02ebad399b
[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_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   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_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)) {
205               break;
206             }
207           }
208           if(aUsedShapesIt == aUsedShapes.end()) {
209             aShapesToAdd.push_back(aSolidInCompSolid);
210           }
211         }
212
213         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
214                                                                   theTools,
215                                                                   GeomAlgoAPI_Boolean::BOOL_CUT));
216
217         // Checking that the algorithm worked properly.
218         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
219           myFeature->setError("Error: Boolean algorithm failed.");
220           return false;
221         }
222
223         std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
224         aMakeShapeList->appendAlgo(aBoolAlgo);
225
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.");
232           return false;
233         }
234
235         aMakeShapeList->appendAlgo(aFillerAlgo);
236
237         if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
238           theObjects.push_back(aCompSolid);
239           theMakeShapes.push_back(aMakeShapeList);
240         }
241       }
242       break;
243     }
244     case BOOL_FUSE: {
245       // Set objects.
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());
249
250       // Filter edges and faces in tools.
251       ListOfShape aTools;
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);
256         } else {
257           aTools.push_back(*anIt);
258         }
259       }
260
261       if((anObjects.size() + aTools.size() +
262           aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
263         myFeature->setError("Error: Not enough objects for boolean operation.");
264         return false;
265       }
266
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());
271
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());
280
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)) {
288               break;
289             }
290           }
291           if(anIt == aUsedShapes.end()) {
292             aShapesToAdd.push_back(aSolidInCompSolid);
293           }
294         }
295       }
296
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());
302
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,
306                                                               aCutTools,
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);
312         }
313       }
314
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,
318                                                               aShapesToAdd,
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);
324         }
325       }
326
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){
332         anObjects.clear();
333         anObjects.push_back(aSolidsToFuse.front());
334         aSolidsToFuse.pop_front();
335         aTools = aSolidsToFuse;
336
337         std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
338                                                           aTools,
339                                                           GeomAlgoAPI_Boolean::BOOL_FUSE));
340
341         // Checking that the algorithm worked properly.
342         if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
343           myFeature->setError("Error: Boolean algorithm failed.");
344           return false;
345         }
346
347         aFusedShape = aFuseAlgo->shape();
348         aMakeShapeList->appendAlgo(aFuseAlgo);
349       }
350
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);
356         }
357
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.");
362           return false;
363         }
364
365         aMakeShapeList->appendAlgo(aFillerAlgo);
366       }
367
368       theMakeShapes.push_back(aMakeShapeList);
369       break;
370     }
371   }
372
373   return true;
374 }
375
376 //=================================================================================================
377 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
378                                 const GeomShapePtr theObject,
379                                 const ListOfShape& theTools,
380                                 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape)
381 {
382   ListOfShape aTools = theTools;
383   aTools.push_back(theObject);
384
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);
390   }
391 }
392
393 //==================================================================================================
394 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
395   std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
396   const ListOfShape& theTools,
397   const GeomShapePtr theResultShapesCompound)
398 {
399   for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
400     anIt != theResultBaseAlgoList.end();
401     ++anIt)
402   {
403     ResultBaseAlgo& aRCA = *anIt;
404     aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
405       aRCA.baseShape,
406       GeomAPI_Shape::FACE,
407       theResultShapesCompound);
408
409     for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
410     {
411       aRCA.resultBody->loadDeletedShapes(aRCA.makeShape,
412         *anIter,
413         GeomAPI_Shape::FACE,
414         theResultShapesCompound);
415     }
416   }
417 }