Salome HOME
c8cee631839272fbef71d96a584e6e4fad907cae
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCommon.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_BooleanCommon.h"
22
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_Boolean.h>
29 #include <GeomAlgoAPI_MakeShapeCustom.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomAPI_ShapeExplorer.h>
35 #include <GeomAlgoAPI_PaveFiller.h>
36 #include <GeomAlgoAPI_CompoundBuilder.h>
37
38
39 //==================================================================================================
40 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
41 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
42 {
43 }
44
45 //==================================================================================================
46 void FeaturesPlugin_BooleanCommon::initAttributes()
47 {
48   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
49
50   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
51   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
52 }
53
54 //==================================================================================================
55 void FeaturesPlugin_BooleanCommon::execute()
56 {
57   ListOfShape anObjects, aTools, aPlanes;
58   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
59   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
60
61   bool isSimpleMode = false;
62
63   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
64   if (aCreationMethodAttr.get()
65       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
66     isSimpleMode = true;
67   }
68
69   // Getting objects.
70   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
71   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
72     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
73     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
74     if (!anObject.get()) {
75       return;
76     }
77     ResultPtr aContext = anObjectAttr->context();
78     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
79     if (aResCompSolidPtr.get())
80     {
81       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
82       GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
83       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
84         aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
85
86       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
87         anIt = aMap.begin();
88       for (; anIt != aMap.end(); anIt++) {
89         if (anIt->first->isEqual(aContextShape)) {
90           aMap[anIt->first].push_back(anObject);
91           break;
92         }
93       }
94       if (anIt == aMap.end()) {
95         aMap[aContextShape].push_back(anObject);
96       }
97
98     }
99     else {
100       anObjects.push_back(anObject);
101     }
102   }
103
104   // Getting tools.
105   if (!isSimpleMode) {
106     AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
107     for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
108       AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
109       GeomShapePtr aTool = aToolAttr->value();
110       if (!aTool.get()) {
111         // It could be a construction plane.
112         ResultPtr aContext = aToolAttr->context();
113         aPlanes.push_back(aToolAttr->context()->shape());
114       } else {
115         aTools.push_back(aTool);
116       }
117     }
118   }
119
120   if ((anObjects.empty() && aCompSolidsObjects.empty())
121       || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
122     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
123     setError(aFeatureError);
124     return;
125   }
126
127   int aResultIndex = 0;
128   GeomAlgoAPI_MakeShapeList aMakeShapeList;
129   GeomAPI_DataMapOfShapeShape aMapOfShapes;
130
131   if (isSimpleMode)
132   {
133     ListOfShape::iterator anObjectsIt = anObjects.begin();
134     GeomShapePtr aShape = *anObjectsIt;
135     for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
136       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
137         new GeomAlgoAPI_Boolean(aShape,
138                                 *anObjectsIt,
139                                 GeomAlgoAPI_Boolean::BOOL_COMMON));
140
141       if (!aCommonAlgo->isDone()) {
142         std::string aFeatureError = "Error: An algorithm failed.";
143         setError(aFeatureError);
144         return;
145       }
146       if (aCommonAlgo->shape()->isNull()) {
147         static const std::string aShapeError = "Error: Resulting shape is Null.";
148         setError(aShapeError);
149         return;
150       }
151       if (!aCommonAlgo->isValid()) {
152         std::string aFeatureError = "Error: Resulting shape is not valid.";
153         setError(aFeatureError);
154         return;
155       }
156
157       aShape = aCommonAlgo->shape();
158       aMakeShapeList.appendAlgo(aCommonAlgo);
159       aMapOfShapes.merge(aCommonAlgo->mapOfSubShapes());
160     }
161
162     GeomAPI_ShapeIterator aShapeIt(aShape);
163     if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
164       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
165         document()->createBody(data(), aResultIndex);
166
167       loadNamingDS(aResultBody, anObjects.front(), anObjects, aShape, aMakeShapeList, aMapOfShapes);
168       setResult(aResultBody, aResultIndex);
169       aResultIndex++;
170     }
171   } else {
172     for (ListOfShape::iterator anObjectsIt = anObjects.begin();
173          anObjectsIt != anObjects.end();
174          ++anObjectsIt)
175     {
176       std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
177       ListOfShape aListWithObject;
178       aListWithObject.push_back(anObject);
179       GeomAlgoAPI_MakeShapeList aMakeShapeList;
180       std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
181       GeomShapePtr aResShape;
182
183       std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
184         GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
185
186       // Resize planes.
187       ListOfShape aToolsWithPlanes = aTools;
188       for (ListOfShape::const_iterator anIt = aPlanes.cbegin();
189            anIt != aPlanes.cend();
190            ++anIt) {
191         GeomShapePtr aPlane = *anIt;
192         GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
193         std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
194           new GeomAlgoAPI_MakeShapeCustom);
195         aMkShCustom->addModified(aPlane, aTool);
196         aMakeShapeList.appendAlgo(aMkShCustom);
197         aToolsWithPlanes.push_back(aTool);
198       }
199
200       aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
201         aToolsWithPlanes,
202         GeomAlgoAPI_Boolean::BOOL_COMMON));
203       aResShape = aBoolAlgo->shape();
204
205       // Checking that the algorithm worked properly.
206       if (!aBoolAlgo->isDone()) {
207         static const std::string aFeatureError = "Error: Boolean algorithm failed.";
208         setError(aFeatureError);
209         return;
210       }
211       if (aResShape->isNull()) {
212         static const std::string aShapeError = "Error: Resulting shape is Null.";
213         setError(aShapeError);
214         return;
215       }
216       if (!aBoolAlgo->isValid()) {
217         std::string aFeatureError = "Error: Resulting shape is not valid.";
218         setError(aFeatureError);
219         return;
220       }
221
222       aMakeShapeList.appendAlgo(aBoolAlgo);
223
224       GeomAPI_ShapeIterator aShapeIt(aResShape);
225       if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
226         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
227           document()->createBody(data(), aResultIndex);
228
229         loadNamingDS(aResultBody, anObject, aTools, aResShape,
230                      aMakeShapeList, *(aBoolAlgo->mapOfSubShapes()));
231         setResult(aResultBody, aResultIndex);
232         aResultIndex++;
233       }
234     }
235
236     // Compsolids handling
237     for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
238       anIt = aCompSolidsObjects.begin();
239       anIt != aCompSolidsObjects.end();
240       ++anIt)
241     {
242       std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
243       ListOfShape& aUsedInOperationSolids = anIt->second;
244
245       // Collecting solids from compsolids which will not be modified in boolean operation.
246       ListOfShape aNotUsedSolids;
247       for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
248         anExp.more();
249         anExp.next())
250       {
251         std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
252         ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
253         for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
254           if (aSolidInCompSolid->isEqual(*aUsedIt)) {
255             break;
256           }
257         }
258         if (aUsedIt == aUsedInOperationSolids.end()) {
259           aNotUsedSolids.push_back(aSolidInCompSolid);
260         }
261       }
262
263       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
264       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
265         new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
266           aTools,
267           GeomAlgoAPI_Boolean::BOOL_COMMON));
268
269       // Checking that the algorithm worked properly.
270       if (!aCommonAlgo->isDone()) {
271         static const std::string aFeatureError = "Error: Boolean algorithm failed.";
272         setError(aFeatureError);
273         return;
274       }
275       if (aCommonAlgo->shape()->isNull()) {
276         static const std::string aShapeError = "Error: Resulting shape is Null.";
277         setError(aShapeError);
278         return;
279       }
280       if (!aCommonAlgo->isValid()) {
281         std::string aFeatureError = "Error: Resulting shape is not valid.";
282         setError(aFeatureError);
283         return;
284       }
285
286       aMakeShapeList->appendAlgo(aCommonAlgo);
287       GeomAPI_DataMapOfShapeShape aMapOfShapes;
288       aMapOfShapes.merge(aCommonAlgo->mapOfSubShapes());
289       GeomShapePtr aResultShape = aCommonAlgo->shape();
290
291       // Add result to not used solids from compsolid.
292       if (!aNotUsedSolids.empty()) {
293         ListOfShape aShapesToAdd = aNotUsedSolids;
294         aShapesToAdd.push_back(aCommonAlgo->shape());
295         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
296           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
297         if (!aFillerAlgo->isDone()) {
298           std::string aFeatureError = "Error: PaveFiller algorithm failed.";
299           setError(aFeatureError);
300           return;
301         }
302
303         aMakeShapeList->appendAlgo(aFillerAlgo);
304         aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
305         aResultShape = aFillerAlgo->shape();
306       }
307
308       GeomAPI_ShapeIterator aShapeIt(aResultShape);
309       if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
310       {
311         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
312           document()->createBody(data(), aResultIndex);
313
314         loadNamingDS(aResultBody,
315           aCompSolid,
316           aTools,
317           aResultShape,
318           *aMakeShapeList,
319           aMapOfShapes);
320         setResult(aResultBody, aResultIndex);
321         aResultIndex++;
322       }
323     }
324
325     // Compounds handling
326     for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
327       anIt = aCompoundObjects.begin();
328       anIt != aCompoundObjects.end();
329       ++anIt)
330     {
331       std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
332       ListOfShape& aUsedInOperationShapes = anIt->second;
333
334       // Collecting shapes from compound which will not be modified in boolean operation.
335       ListOfShape aNotUsedShapes;
336       for (GeomAPI_ShapeIterator aCompIt(aCompound);
337         aCompIt.more();
338         aCompIt.next())
339       {
340         std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
341         ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
342         for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
343           if (aShapeInCompound->isEqual(*aUsedIt)) {
344             break;
345           }
346         }
347         if (aUsedIt == aUsedInOperationShapes.end()) {
348           aNotUsedShapes.push_back(aShapeInCompound);
349         }
350       }
351
352       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
353       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
354         new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
355           aTools,
356           GeomAlgoAPI_Boolean::BOOL_COMMON));
357
358       // Checking that the algorithm worked properly.
359       if (!aCommonAlgo->isDone()) {
360         static const std::string aFeatureError = "Error: Boolean algorithm failed.";
361         setError(aFeatureError);
362         return;
363       }
364       if (aCommonAlgo->shape()->isNull()) {
365         static const std::string aShapeError = "Error: Resulting shape is Null.";
366         setError(aShapeError);
367         return;
368       }
369       if (!aCommonAlgo->isValid()) {
370         std::string aFeatureError = "Error: Resulting shape is not valid.";
371         setError(aFeatureError);
372         return;
373       }
374
375       aMakeShapeList->appendAlgo(aCommonAlgo);
376       GeomAPI_DataMapOfShapeShape aMapOfShapes;
377       aMapOfShapes.merge(aCommonAlgo->mapOfSubShapes());
378       GeomShapePtr aResultShape = aCommonAlgo->shape();
379
380       // Add result to not used shape from compound.
381       if (!aNotUsedShapes.empty()) {
382         ListOfShape aShapesForResult = aNotUsedShapes;
383         if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
384           for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
385             aShapesForResult.push_back(aResultIt.current());
386           }
387         }
388         else {
389           aShapesForResult.push_back(aResultShape);
390         }
391
392         if (aShapesForResult.size() == 1) {
393           aResultShape = aShapesForResult.front();
394         }
395         else {
396           aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
397         }
398       }
399
400       GeomAPI_ShapeIterator aShapeIt(aResultShape);
401       if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
402         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
403           document()->createBody(data(), aResultIndex);
404
405         loadNamingDS(aResultBody,
406           aCompound,
407           aTools,
408           aResultShape,
409           *aMakeShapeList,
410           aMapOfShapes);
411         setResult(aResultBody, aResultIndex);
412         aResultIndex++;
413       }
414     }
415
416   }
417
418   // remove the rest results if there were produced in the previous pass
419   removeResults(aResultIndex);
420 }
421
422 //==================================================================================================
423 void FeaturesPlugin_BooleanCommon::loadNamingDS(ResultBodyPtr theResultBody,
424                                                 const GeomShapePtr theBaseShape,
425                                                 const ListOfShape& theTools,
426                                                 const GeomShapePtr theResultShape,
427                                                 GeomAlgoAPI_MakeShape& theMakeShape,
428                                                 GeomAPI_DataMapOfShapeShape& theMapOfShapes)
429 {
430   //load result
431   if (theBaseShape->isEqual(theResultShape)) {
432     theResultBody->store(theResultShape, false);
433   } else {
434     const int aModifyVTag = 1;
435     const int aModifyETag = 2;
436     const int aModifyFTag = 3;
437     const int aDeletedTag = 4;
438     /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
439     const int aSubsolidsTag = 5;
440
441     theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
442
443     const std::string aModVName = "Modified_Vertex";
444     const std::string aModEName = "Modified_Edge";
445     const std::string aModFName = "Modified_Face";
446
447     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
448                                                aModifyVTag, aModVName, theMapOfShapes, false,
449                                                false, true);
450     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
451                                                aModifyETag, aModEName, theMapOfShapes, false,
452                                                false, true);
453     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
454                                                aModifyFTag, aModFName, theMapOfShapes, false,
455                                                false, true);
456
457     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
458                                      GeomAPI_Shape::VERTEX, aDeletedTag);
459     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
460                                      GeomAPI_Shape::EDGE, aDeletedTag);
461     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
462                                      GeomAPI_Shape::FACE, aDeletedTag);
463
464     for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
465     {
466       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
467                                                  aModifyVTag, aModVName, theMapOfShapes, false,
468                                                  false, true);
469
470       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
471                                                  aModifyETag, aModEName, theMapOfShapes, false,
472                                                  false, true);
473
474       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
475                                                  aModifyFTag, aModFName, theMapOfShapes, false,
476                                                  false, true);
477
478       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX, aDeletedTag);
479       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE, aDeletedTag);
480       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
481     }
482   }
483 }