Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCommon.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_BooleanCommon.h"
21
22 #include "FeaturesPlugin_Tools.h"
23
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Tools.h>
28
29 #include <GeomAlgoAPI_Boolean.h>
30 #include <GeomAlgoAPI_MakeShapeCustom.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAPI_Face.h>
34 #include <GeomAPI_ShapeIterator.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAlgoAPI_PaveFiller.h>
37 #include <GeomAlgoAPI_CompoundBuilder.h>
38 #include <GeomAlgoAPI_Tools.h>
39
40
41 //==================================================================================================
42 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
43 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
44 {
45 }
46
47 //==================================================================================================
48 void FeaturesPlugin_BooleanCommon::initAttributes()
49 {
50   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
51
52   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
53   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54 }
55
56 //==================================================================================================
57 void FeaturesPlugin_BooleanCommon::execute()
58 {
59   ListOfShape anObjects, aTools, aPlanes;
60   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
61   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
62
63   bool isSimpleMode = false;
64
65   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
66   if (aCreationMethodAttr.get()
67       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
68     isSimpleMode = true;
69   }
70
71   // Getting objects.
72   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
73   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
74     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
75     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
76     if (!anObject.get()) {
77       return;
78     }
79     ResultPtr aContext = anObjectAttr->context();
80     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
81     if (!isSimpleMode
82         && aResCompSolidPtr.get())
83     {
84       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
85       GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
86       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
87         aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
88
89       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
90         anIt = aMap.begin();
91       for (; anIt != aMap.end(); anIt++) {
92         if (anIt->first->isEqual(aContextShape)) {
93           aMap[anIt->first].push_back(anObject);
94           break;
95         }
96       }
97       if (anIt == aMap.end()) {
98         aMap[aContextShape].push_back(anObject);
99       }
100
101     }
102     else {
103       anObjects.push_back(anObject);
104     }
105   }
106
107   // Getting tools.
108   if (!isSimpleMode) {
109     AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
110     for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
111       AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
112       GeomShapePtr aTool = aToolAttr->value();
113       if (!aTool.get()) {
114         // It could be a construction plane.
115         ResultPtr aContext = aToolAttr->context();
116         aPlanes.push_back(aToolAttr->context()->shape());
117       } else {
118         aTools.push_back(aTool);
119       }
120     }
121   }
122
123   if ((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
124       || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
125     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
126     setError(aFeatureError);
127     return;
128   }
129
130   int aResultIndex = 0;
131   std::string anError;
132   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
133   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
134   ListOfShape aResultShapesList;
135
136   if (isSimpleMode)
137   {
138     ListOfShape::iterator anObjectsIt = anObjects.begin();
139     GeomShapePtr aShape = *anObjectsIt;
140     for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
141       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
142         new GeomAlgoAPI_Boolean(aShape,
143                                 *anObjectsIt,
144                                 GeomAlgoAPI_Boolean::BOOL_COMMON));
145
146       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
147         setError(anError);
148         return;
149       }
150
151       aShape = aCommonAlgo->shape();
152       aMakeShapeList->appendAlgo(aCommonAlgo);
153     }
154
155     GeomAPI_ShapeIterator aShapeIt(aShape);
156     if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
157       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
158         document()->createBody(data(), aResultIndex);
159
160       ListOfShape anEmptyTools;
161       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
162                                                anObjects,
163                                                anEmptyTools,
164                                                aMakeShapeList,
165                                                aShape);
166       GeomShapePtr aBaseShape = anObjects.front();
167       anObjects.pop_front();
168       setResult(aResultBody, aResultIndex);
169       aResultIndex++;
170
171       aTools = anObjects;
172       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
173       aRBA.resultBody = aResultBody;
174       aRBA.baseShape = aBaseShape;
175       aRBA.makeShape = aMakeShapeList;
176       aResultBaseAlgoList.push_back(aRBA);
177       aResultShapesList.push_back(aShape);
178     }
179   } else {
180     for (ListOfShape::iterator anObjectsIt = anObjects.begin();
181          anObjectsIt != anObjects.end();
182          ++anObjectsIt)
183     {
184       std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
185       ListOfShape aListWithObject;
186       aListWithObject.push_back(anObject);
187       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
188       std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
189       GeomShapePtr aResShape;
190
191       std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
192         GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
193
194       // Resize planes.
195       ListOfShape aToolsWithPlanes = aTools;
196       for (ListOfShape::const_iterator anIt = aPlanes.cbegin();
197            anIt != aPlanes.cend();
198            ++anIt) {
199         GeomShapePtr aPlane = *anIt;
200         GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
201         std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
202           new GeomAlgoAPI_MakeShapeCustom);
203         aMkShCustom->addModified(aPlane, aTool);
204         aMakeShapeList->appendAlgo(aMkShCustom);
205         aToolsWithPlanes.push_back(aTool);
206       }
207
208       aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
209         aToolsWithPlanes,
210         GeomAlgoAPI_Boolean::BOOL_COMMON));
211       aResShape = aBoolAlgo->shape();
212
213       // Checking that the algorithm worked properly.
214       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
215         setError(anError);
216         return;
217       }
218
219       aMakeShapeList->appendAlgo(aBoolAlgo);
220
221       GeomAPI_ShapeIterator aShapeIt(aResShape);
222       if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
223         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
224           document()->createBody(data(), aResultIndex);
225         ListOfShape anObjectList;
226         anObjectList.push_back(anObject);
227         FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
228                                                  anObjectList,
229                                                  aTools,
230                                                  aMakeShapeList,
231                                                  aResShape);
232         setResult(aResultBody, aResultIndex);
233         aResultIndex++;
234
235         FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
236         aRBA.resultBody = aResultBody;
237         aRBA.baseShape = anObject;
238         aRBA.makeShape = aMakeShapeList;
239         aResultBaseAlgoList.push_back(aRBA);
240         aResultShapesList.push_back(aResShape);
241       }
242     }
243
244     // Compsolids handling
245     for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
246       anIt = aCompSolidsObjects.begin();
247       anIt != aCompSolidsObjects.end();
248       ++anIt)
249     {
250       std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
251       ListOfShape& aUsedInOperationSolids = anIt->second;
252
253       // Collecting solids from compsolids which will not be modified in boolean operation.
254       ListOfShape aNotUsedSolids;
255       for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
256         anExp.more();
257         anExp.next())
258       {
259         std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
260         ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
261         for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
262           if (aSolidInCompSolid->isEqual(*aUsedIt)) {
263             break;
264           }
265         }
266         if (aUsedIt == aUsedInOperationSolids.end()) {
267           aNotUsedSolids.push_back(aSolidInCompSolid);
268         }
269       }
270
271       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
272       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
273         new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
274           aTools,
275           GeomAlgoAPI_Boolean::BOOL_COMMON));
276
277       // Checking that the algorithm worked properly.
278       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
279         setError(anError);
280         return;
281       }
282
283       aMakeShapeList->appendAlgo(aCommonAlgo);
284       GeomShapePtr aResultShape = aCommonAlgo->shape();
285
286       // Add result to not used solids from compsolid.
287       if (!aNotUsedSolids.empty()) {
288         ListOfShape aShapesToAdd = aNotUsedSolids;
289         aShapesToAdd.push_back(aCommonAlgo->shape());
290         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
291           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
292         if (!aFillerAlgo->isDone()) {
293           std::string aFeatureError = "Error: PaveFiller algorithm failed.";
294           setError(aFeatureError);
295           return;
296         }
297
298         aMakeShapeList->appendAlgo(aFillerAlgo);
299         aResultShape = aFillerAlgo->shape();
300       }
301
302       GeomAPI_ShapeIterator aShapeIt(aResultShape);
303       if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
304       {
305         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
306           document()->createBody(data(), aResultIndex);
307
308         ListOfShape aCompSolidList;
309         aCompSolidList.push_back(aCompSolid);
310         FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
311                                                  aCompSolidList,
312                                                  aTools,
313                                                  aMakeShapeList,
314                                                  aResultShape);
315         setResult(aResultBody, aResultIndex);
316         aResultIndex++;
317
318         FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
319         aRBA.resultBody = aResultBody;
320         aRBA.baseShape = aCompSolid;
321         aRBA.makeShape = aMakeShapeList;
322         aResultBaseAlgoList.push_back(aRBA);
323         aResultShapesList.push_back(aResultShape);
324       }
325     }
326
327     // Compounds handling
328     for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
329       anIt = aCompoundObjects.begin();
330       anIt != aCompoundObjects.end();
331       ++anIt)
332     {
333       std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
334       ListOfShape& aUsedInOperationShapes = anIt->second;
335
336       // Collecting shapes from compound which will not be modified in boolean operation.
337       ListOfShape aNotUsedShapes;
338       for (GeomAPI_ShapeIterator aCompIt(aCompound);
339         aCompIt.more();
340         aCompIt.next())
341       {
342         std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
343         ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
344         for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
345           if (aShapeInCompound->isEqual(*aUsedIt)) {
346             break;
347           }
348         }
349         if (aUsedIt == aUsedInOperationShapes.end()) {
350           aNotUsedShapes.push_back(aShapeInCompound);
351         }
352       }
353
354       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
355       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
356         new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
357           aTools,
358           GeomAlgoAPI_Boolean::BOOL_COMMON));
359
360       // Checking that the algorithm worked properly.
361       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
362         setError(anError);
363         return;
364       }
365
366       aMakeShapeList->appendAlgo(aCommonAlgo);
367       GeomShapePtr aResultShape = aCommonAlgo->shape();
368
369       // Add result to not used shape from compound.
370       if (!aNotUsedShapes.empty()) {
371         ListOfShape aShapesForResult = aNotUsedShapes;
372         if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
373           for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
374             aShapesForResult.push_back(aResultIt.current());
375           }
376         }
377         else {
378           aShapesForResult.push_back(aResultShape);
379         }
380
381         if (aShapesForResult.size() == 1) {
382           aResultShape = aShapesForResult.front();
383         }
384         else {
385           aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
386         }
387       }
388
389       GeomAPI_ShapeIterator aShapeIt(aResultShape);
390       if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
391         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
392           document()->createBody(data(), aResultIndex);
393
394         ListOfShape aCompoundList;
395         aCompoundList.push_back(aCompound);
396         FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
397                                                  aCompoundList,
398                                                  aTools,
399                                                  aMakeShapeList,
400                                                  aResultShape);
401         setResult(aResultBody, aResultIndex);
402         aResultIndex++;
403
404         FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
405         aRBA.resultBody = aResultBody;
406         aRBA.baseShape = aCompound;
407         aRBA.makeShape = aMakeShapeList;
408         aResultBaseAlgoList.push_back(aRBA);
409         aResultShapesList.push_back(aResultShape);
410       }
411     }
412
413   }
414
415   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
416   // result shape has been deleted, but in another it was modified or stayed.
417   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
418   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
419
420   // remove the rest results if there were produced in the previous pass
421   removeResults(aResultIndex);
422 }