Salome HOME
Some fixes.
[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     int aTag = 1;
81
82     ResultBodyPtr aResultBody = myFeature->document()->createBody(myFeature->data(), aResultIndex);
83
84     if((*aBoolObjIt)->isEqual((*aBoolMSIt)->shape())) {
85       aResultBody->store((*aBoolMSIt)->shape(), false);
86     }
87     else
88     {
89       aResultBody->storeModified(*aBoolObjIt, (*aBoolMSIt)->shape(), aTag);
90
91       aTag += 5000;
92
93       // Store generation history.
94       ListOfShape::const_iterator aGenBaseIt = aGenBaseShapes.cbegin();
95       ListOfMakeShape::const_iterator aGenMSIt = aGenMakeShapes.cbegin();
96       for(; aGenBaseIt != aGenBaseShapes.cend() && aGenMSIt != aGenMakeShapes.cend();
97           ++aGenBaseIt, ++aGenMSIt) {
98         storeGenerationHistory(aResultBody, *aGenBaseIt, *aGenMSIt, aTag);
99       }
100
101       int aModTag = aTag;
102       storeModificationHistory(aResultBody, *aBoolObjIt, aTools, *aBoolMSIt, aModTag);
103
104       ResultBaseAlgo aRBA;
105       aRBA.resultBody = aResultBody;
106       aRBA.baseShape = *aBoolObjIt;
107       aRBA.makeShape = *aBoolMSIt;
108       aRBA.delTag = aModTag;
109       aResultBaseAlgoList.push_back(aRBA);
110       aResultShapesList.push_back((*aBoolMSIt)->shape());
111     }
112
113     myFeature->setResult(aResultBody, aResultIndex++);
114   }
115
116   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
117   // result shape has been deleted, but in another it was modified or stayed.
118   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
119   storeDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
120
121   myFeature->removeResults(aResultIndex);
122 }
123
124 //=================================================================================================
125 bool FeaturesPlugin_CompositeBoolean::makeBoolean(const ListOfShape& theTools,
126                                                   ListOfShape& theObjects,
127                                                   ListOfMakeShape& theMakeShapes)
128 {
129   // Getting objects.
130   ListOfShape anObjects, anEdgesAndFaces, aCompSolids;
131   std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
132   AttributeSelectionListPtr anObjectsSelList = myFeature->selectionList(OBJECTS_ID());
133   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
134     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
135     GeomShapePtr anObject = anObjectAttr->value();
136     if(!anObject.get()) {
137       myFeature->setError("Error: Could not get object.");
138       return false;
139     }
140     ResultPtr aContext = anObjectAttr->context();
141     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
142     if(aResCompSolidPtr.get()) {
143       GeomShapePtr aContextShape = aResCompSolidPtr->shape();
144       std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
145       for(; anIt != aCompSolidsObjects.end(); anIt++) {
146         if(anIt->first->isEqual(aContextShape)) {
147           aCompSolidsObjects[anIt->first].push_back(anObject);
148           break;
149         }
150       }
151       if(anIt == aCompSolidsObjects.end()) {
152         aCompSolidsObjects[aContextShape].push_back(anObject);
153         aCompSolids.push_back(aContextShape);
154       }
155     } else {
156       if(anObject->shapeType() == GeomAPI_Shape::EDGE ||
157          anObject->shapeType() == GeomAPI_Shape::FACE) {
158         anEdgesAndFaces.push_back(anObject);
159       } else {
160         anObjects.push_back(anObject);
161       }
162     }
163   }
164
165   switch(myOperationType) {
166     case BOOL_CUT: {
167       if((anObjects.empty() && aCompSolidsObjects.empty()) || theTools.empty()) {
168         myFeature->setError("Error: Not enough objects for boolean operation.");
169         return false;
170       }
171
172       // For solids cut each object with all tools.
173       for(ListOfShape::const_iterator
174           anIt = anObjects.cbegin(); anIt != anObjects.cend(); ++anIt) {
175         GeomShapePtr anObject = *anIt;
176         ListOfShape aListWithObject;
177         aListWithObject.push_back(anObject);
178         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aListWithObject,
179                                                                 theTools,
180                                                                 GeomAlgoAPI_Boolean::BOOL_CUT));
181
182         // Checking that the algorithm worked properly.
183         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
184           myFeature->setError("Error: Boolean algorithm failed.");
185           return false;
186         }
187
188         if(GeomAlgoAPI_ShapeTools::volume(aBoolAlgo->shape()) > 1.e-27) {
189           theObjects.push_back(anObject);
190           theMakeShapes.push_back(aBoolAlgo);
191         }
192       }
193
194       // Compsolids handling
195       for(std::map<GeomShapePtr, ListOfShape>::const_iterator anIt = aCompSolidsObjects.cbegin();
196           anIt != aCompSolidsObjects.cend(); ++anIt) {
197         GeomShapePtr aCompSolid = anIt->first;
198         const ListOfShape& aUsedShapes = anIt->second;
199
200         // Collecting solids from compsolids which will not be modified in boolean operation.
201         ListOfShape aShapesToAdd;
202         for(GeomAPI_ShapeExplorer
203             anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
204           GeomShapePtr aSolidInCompSolid = anExp.current();
205           ListOfShape::const_iterator aUsedShapesIt = aUsedShapes.cbegin();
206           for(; aUsedShapesIt != aUsedShapes.cend(); ++aUsedShapesIt) {
207             if(aSolidInCompSolid->isEqual(*aUsedShapesIt)) {
208               break;
209             }
210           }
211           if(aUsedShapesIt == aUsedShapes.end()) {
212             aShapesToAdd.push_back(aSolidInCompSolid);
213           }
214         }
215
216         std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(new GeomAlgoAPI_Boolean(aUsedShapes,
217                                                                   theTools,
218                                                                   GeomAlgoAPI_Boolean::BOOL_CUT));
219
220         // Checking that the algorithm worked properly.
221         if(!aBoolAlgo->isDone() || aBoolAlgo->shape()->isNull() || !aBoolAlgo->isValid()) {
222           myFeature->setError("Error: Boolean algorithm failed.");
223           return false;
224         }
225
226         std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
227         aMakeShapeList->appendAlgo(aBoolAlgo);
228
229         // Add result to not used solids from compsolid.
230         aShapesToAdd.push_back(aBoolAlgo->shape());
231         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
232           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
233         if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
234           myFeature->setError("Error: PaveFiller algorithm failed.");
235           return false;
236         }
237
238         aMakeShapeList->appendAlgo(aFillerAlgo);
239
240         if(GeomAlgoAPI_ShapeTools::volume(aFillerAlgo->shape()) > 1.e-27) {
241           theObjects.push_back(aCompSolid);
242           theMakeShapes.push_back(aMakeShapeList);
243         }
244       }
245       break;
246     }
247     case BOOL_FUSE: {
248       // Set objects.
249       theObjects.insert(theObjects.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
250       theObjects.insert(theObjects.end(), anObjects.begin(), anObjects.end());
251       theObjects.insert(theObjects.end(), aCompSolids.begin(), aCompSolids.end());
252
253       // Filter edges and faces in tools.
254       ListOfShape aTools;
255       for(ListOfShape::const_iterator anIt = theTools.cbegin(); anIt != theTools.cend(); ++anIt) {
256         if((*anIt)->shapeType() == GeomAPI_Shape::EDGE ||
257            (*anIt)->shapeType() == GeomAPI_Shape::FACE) {
258           anEdgesAndFaces.push_back(*anIt);
259         } else {
260           aTools.push_back(*anIt);
261         }
262       }
263
264       if((anObjects.size() + aTools.size() +
265           aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
266         myFeature->setError("Error: Not enough objects for boolean operation.");
267         return false;
268       }
269
270       // Collecting all solids which will be fused.
271       ListOfShape aSolidsToFuse;
272       aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
273       aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
274
275       // Collecting solids from compsolids which will not be
276       // modified in boolean operation and will be added to result.
277       ListOfShape aShapesToAdd;
278       for(std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
279           anIt != aCompSolidsObjects.end(); anIt++) {
280         GeomShapePtr aCompSolid = anIt->first;
281         ListOfShape& aUsedShapes = anIt->second;
282         aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedShapes.begin(), aUsedShapes.end());
283
284         // Collect solids from compsolid which will not be modified in boolean operation.
285         for(GeomAPI_ShapeExplorer
286             anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
287           GeomShapePtr aSolidInCompSolid = anExp.current();
288           ListOfShape::iterator anIt = aUsedShapes.begin();
289           for(; anIt != aUsedShapes.end(); anIt++) {
290             if(aSolidInCompSolid->isEqual(*anIt)) {
291               break;
292             }
293           }
294           if(anIt == aUsedShapes.end()) {
295             aShapesToAdd.push_back(aSolidInCompSolid);
296           }
297         }
298       }
299
300       // Cut edges and faces(if we have any) with solids.
301       ListOfShape aCutTools;
302       aCutTools.insert(aCutTools.end(), anObjects.begin(), anObjects.end());
303       aCutTools.insert(aCutTools.end(), aCompSolids.begin(), aCompSolids.end());
304       aCutTools.insert(aCutTools.end(), aTools.begin(), aTools.end());
305
306       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
307       if(!anEdgesAndFaces.empty() && !aCutTools.empty()) {
308         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
309                                                               aCutTools,
310                                                               GeomAlgoAPI_Boolean::BOOL_CUT));
311         if(aCutAlgo->isDone() && !aCutAlgo->shape()->isNull() && aCutAlgo->isValid()) {
312           anEdgesAndFaces.clear();
313           anEdgesAndFaces.push_back(aCutAlgo->shape());
314           aMakeShapeList->appendAlgo(aCutAlgo);
315         }
316       }
317
318       // If we have compsolids then cut with not used solids all others.
319       if(!aShapesToAdd.empty()) {
320         std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(aSolidsToFuse,
321                                                               aShapesToAdd,
322                                                               GeomAlgoAPI_Boolean::BOOL_CUT));
323         if(aCutAlgo->isDone() && GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
324           aSolidsToFuse.clear();
325           aSolidsToFuse.push_back(aCutAlgo->shape());
326           aMakeShapeList->appendAlgo(aCutAlgo);
327         }
328       }
329
330       // Fuse all objects and all tools.
331       GeomShapePtr aFusedShape;
332       if(aSolidsToFuse.size() == 1) {
333         aFusedShape = aSolidsToFuse.front();
334       } else if(aSolidsToFuse.size() > 1){
335         anObjects.clear();
336         anObjects.push_back(aSolidsToFuse.front());
337         aSolidsToFuse.pop_front();
338         aTools = aSolidsToFuse;
339
340         std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
341                                                           aTools,
342                                                           GeomAlgoAPI_Boolean::BOOL_FUSE));
343
344         // Checking that the algorithm worked properly.
345         if(!aFuseAlgo->isDone() || aFuseAlgo->shape()->isNull() || !aFuseAlgo->isValid()) {
346           myFeature->setError("Error: Boolean algorithm failed.");
347           return false;
348         }
349
350         aFusedShape = aFuseAlgo->shape();
351         aMakeShapeList->appendAlgo(aFuseAlgo);
352       }
353
354       // Combine result with not used solids from compsolid and edges and faces (if we have any).
355       aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
356       if(!aShapesToAdd.empty()) {
357         if(aFusedShape.get()) {
358           aShapesToAdd.push_back(aFusedShape);
359         }
360
361         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
362           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
363         if(!aFillerAlgo->isDone() || aFillerAlgo->shape()->isNull() || !aFillerAlgo->isValid()) {
364           myFeature->setError("Error: PaveFiller algorithm failed.");
365           return false;
366         }
367
368         aMakeShapeList->appendAlgo(aFillerAlgo);
369       }
370
371       theMakeShapes.push_back(aMakeShapeList);
372       break;
373     }
374   }
375
376   return true;
377 }
378
379 //=================================================================================================
380 void FeaturesPlugin_CompositeBoolean::storeModificationHistory(ResultBodyPtr theResultBody,
381                                 const GeomShapePtr theObject,
382                                 const ListOfShape& theTools,
383                                 const std::shared_ptr<GeomAlgoAPI_MakeShape> theMakeShape,
384                                 int& theTag)
385 {
386   int aModTag = theTag;
387   int anEdgesAndFacesTag = ++aModTag;
388   int aDelTag = ++anEdgesAndFacesTag;
389   theTag = aDelTag;
390
391   const std::string aModName = "Modfied";
392
393   ListOfShape aTools = theTools;
394   aTools.push_back(theObject);
395
396   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMap = theMakeShape->mapOfSubShapes();
397
398   int aTag;
399   std::string aName;
400   for(ListOfShape::const_iterator anIt = aTools.begin(); anIt != aTools.end(); anIt++) {
401     if((*anIt)->shapeType() == GeomAPI_Shape::EDGE) {
402       aTag = anEdgesAndFacesTag;
403       aName = aModName + "_Edge";
404     }
405     else if((*anIt)->shapeType() == GeomAPI_Shape::FACE) {
406       aTag = anEdgesAndFacesTag;
407       aName = aModName + "_Face";
408     } else {
409       aTag = aModTag;
410       aName = aModName;
411     }
412     theResultBody->loadAndOrientModifiedShapes(theMakeShape.get(), *anIt,
413       (*anIt)->shapeType() == GeomAPI_Shape::EDGE ?
414       GeomAPI_Shape::EDGE : GeomAPI_Shape::FACE, aTag, aName, *aMap.get(), false, false, true);
415   }
416 }
417
418 //==================================================================================================
419 void FeaturesPlugin_CompositeBoolean::storeDeletedShapes(
420   std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
421   const ListOfShape& theTools,
422   const GeomShapePtr theResultShapesCompound)
423 {
424   for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
425     anIt != theResultBaseAlgoList.end();
426     ++anIt)
427   {
428     ResultBaseAlgo& aRCA = *anIt;
429     aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
430       aRCA.baseShape,
431       GeomAPI_Shape::FACE,
432       aRCA.delTag,
433       theResultShapesCompound);
434
435     for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
436     {
437       aRCA.resultBody->loadDeletedShapes(aRCA.makeShape.get(),
438         *anIter,
439         GeomAPI_Shape::FACE,
440         aRCA.delTag,
441         theResultShapesCompound);
442     }
443   }
444 }