Salome HOME
Update copyrights
[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       GeomShapePtr aBaseShape = anObjects.front();
161       anObjects.pop_front();
162       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
163                                                aBaseShape,
164                                                anObjects,
165                                                aMakeShapeList,
166                                                aShape);
167       setResult(aResultBody, aResultIndex);
168       aResultIndex++;
169
170       aTools = anObjects;
171       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
172       aRBA.resultBody = aResultBody;
173       aRBA.baseShape = aBaseShape;
174       aRBA.makeShape = aMakeShapeList;
175       aResultBaseAlgoList.push_back(aRBA);
176       aResultShapesList.push_back(aShape);
177     }
178   } else {
179     for (ListOfShape::iterator anObjectsIt = anObjects.begin();
180          anObjectsIt != anObjects.end();
181          ++anObjectsIt)
182     {
183       std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
184       ListOfShape aListWithObject;
185       aListWithObject.push_back(anObject);
186       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
187       std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
188       GeomShapePtr aResShape;
189
190       std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
191         GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
192
193       // Resize planes.
194       ListOfShape aToolsWithPlanes = aTools;
195       for (ListOfShape::const_iterator anIt = aPlanes.cbegin();
196            anIt != aPlanes.cend();
197            ++anIt) {
198         GeomShapePtr aPlane = *anIt;
199         GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
200         std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
201           new GeomAlgoAPI_MakeShapeCustom);
202         aMkShCustom->addModified(aPlane, aTool);
203         aMakeShapeList->appendAlgo(aMkShCustom);
204         aToolsWithPlanes.push_back(aTool);
205       }
206
207       aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
208         aToolsWithPlanes,
209         GeomAlgoAPI_Boolean::BOOL_COMMON));
210       aResShape = aBoolAlgo->shape();
211
212       // Checking that the algorithm worked properly.
213       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
214         setError(anError);
215         return;
216       }
217
218       aMakeShapeList->appendAlgo(aBoolAlgo);
219
220       GeomAPI_ShapeIterator aShapeIt(aResShape);
221       if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
222         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
223           document()->createBody(data(), aResultIndex);
224
225         FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
226                                                  anObject,
227                                                  aTools,
228                                                  aMakeShapeList,
229                                                  aResShape);
230         setResult(aResultBody, aResultIndex);
231         aResultIndex++;
232
233         FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
234         aRBA.resultBody = aResultBody;
235         aRBA.baseShape = anObject;
236         aRBA.makeShape = aMakeShapeList;
237         aResultBaseAlgoList.push_back(aRBA);
238         aResultShapesList.push_back(aResShape);
239       }
240     }
241
242     // Compsolids handling
243     for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
244       anIt = aCompSolidsObjects.begin();
245       anIt != aCompSolidsObjects.end();
246       ++anIt)
247     {
248       std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
249       ListOfShape& aUsedInOperationSolids = anIt->second;
250
251       // Collecting solids from compsolids which will not be modified in boolean operation.
252       ListOfShape aNotUsedSolids;
253       for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
254         anExp.more();
255         anExp.next())
256       {
257         std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
258         ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
259         for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
260           if (aSolidInCompSolid->isEqual(*aUsedIt)) {
261             break;
262           }
263         }
264         if (aUsedIt == aUsedInOperationSolids.end()) {
265           aNotUsedSolids.push_back(aSolidInCompSolid);
266         }
267       }
268
269       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
270       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
271         new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
272           aTools,
273           GeomAlgoAPI_Boolean::BOOL_COMMON));
274
275       // Checking that the algorithm worked properly.
276       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
277         setError(anError);
278         return;
279       }
280
281       aMakeShapeList->appendAlgo(aCommonAlgo);
282       GeomShapePtr aResultShape = aCommonAlgo->shape();
283
284       // Add result to not used solids from compsolid.
285       if (!aNotUsedSolids.empty()) {
286         ListOfShape aShapesToAdd = aNotUsedSolids;
287         aShapesToAdd.push_back(aCommonAlgo->shape());
288         std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
289           new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
290         if (!aFillerAlgo->isDone()) {
291           std::string aFeatureError = "Error: PaveFiller algorithm failed.";
292           setError(aFeatureError);
293           return;
294         }
295
296         aMakeShapeList->appendAlgo(aFillerAlgo);
297         aResultShape = aFillerAlgo->shape();
298       }
299
300       GeomAPI_ShapeIterator aShapeIt(aResultShape);
301       if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
302       {
303         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
304           document()->createBody(data(), aResultIndex);
305
306         FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
307                                                  aCompSolid,
308                                                  aTools,
309                                                  aMakeShapeList,
310                                                  aResultShape);
311         setResult(aResultBody, aResultIndex);
312         aResultIndex++;
313
314         FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
315         aRBA.resultBody = aResultBody;
316         aRBA.baseShape = aCompSolid;
317         aRBA.makeShape = aMakeShapeList;
318         aResultBaseAlgoList.push_back(aRBA);
319         aResultShapesList.push_back(aResultShape);
320       }
321     }
322
323     // Compounds handling
324     for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
325       anIt = aCompoundObjects.begin();
326       anIt != aCompoundObjects.end();
327       ++anIt)
328     {
329       std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
330       ListOfShape& aUsedInOperationShapes = anIt->second;
331
332       // Collecting shapes from compound which will not be modified in boolean operation.
333       ListOfShape aNotUsedShapes;
334       for (GeomAPI_ShapeIterator aCompIt(aCompound);
335         aCompIt.more();
336         aCompIt.next())
337       {
338         std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
339         ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
340         for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
341           if (aShapeInCompound->isEqual(*aUsedIt)) {
342             break;
343           }
344         }
345         if (aUsedIt == aUsedInOperationShapes.end()) {
346           aNotUsedShapes.push_back(aShapeInCompound);
347         }
348       }
349
350       std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
351       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
352         new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
353           aTools,
354           GeomAlgoAPI_Boolean::BOOL_COMMON));
355
356       // Checking that the algorithm worked properly.
357       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
358         setError(anError);
359         return;
360       }
361
362       aMakeShapeList->appendAlgo(aCommonAlgo);
363       GeomShapePtr aResultShape = aCommonAlgo->shape();
364
365       // Add result to not used shape from compound.
366       if (!aNotUsedShapes.empty()) {
367         ListOfShape aShapesForResult = aNotUsedShapes;
368         if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
369           for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
370             aShapesForResult.push_back(aResultIt.current());
371           }
372         }
373         else {
374           aShapesForResult.push_back(aResultShape);
375         }
376
377         if (aShapesForResult.size() == 1) {
378           aResultShape = aShapesForResult.front();
379         }
380         else {
381           aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
382         }
383       }
384
385       GeomAPI_ShapeIterator aShapeIt(aResultShape);
386       if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
387         std::shared_ptr<ModelAPI_ResultBody> aResultBody =
388           document()->createBody(data(), aResultIndex);
389
390         FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
391                                                  aCompound,
392                                                  aTools,
393                                                  aMakeShapeList,
394                                                  aResultShape);
395         setResult(aResultBody, aResultIndex);
396         aResultIndex++;
397
398         FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
399         aRBA.resultBody = aResultBody;
400         aRBA.baseShape = aCompound;
401         aRBA.makeShape = aMakeShapeList;
402         aResultBaseAlgoList.push_back(aRBA);
403         aResultShapesList.push_back(aResultShape);
404       }
405     }
406
407   }
408
409   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
410   // result shape has been deleted, but in another it was modified or stayed.
411   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
412   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
413
414   // remove the rest results if there were produced in the previous pass
415   removeResults(aResultIndex);
416 }