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