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