Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanFill.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_BooleanFill.h"
21 #include "FeaturesPlugin_Tools.h"
22
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_MakeShapeCustom.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_Partition.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_Tools.h>
35
36 #include <GeomAPI_Face.h>
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAPI_ShapeIterator.h>
39
40 #include <algorithm>
41 #include <map>
42
43 //=================================================================================================
44 FeaturesPlugin_BooleanFill::FeaturesPlugin_BooleanFill()
45   : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FILL)
46 {
47 }
48
49 //=================================================================================================
50 void FeaturesPlugin_BooleanFill::execute()
51 {
52   std::string anError;
53   ListOfShape anObjects, aTools, anEdgesAndFaces, aPlanes;
54   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
55
56   // Getting objects.
57   AttributeSelectionListPtr anObjectsSelList =
58     selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
59   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
60     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
61     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
62     if(!anObject.get()) {
63       return;
64     }
65     ResultPtr aContext = anObjectAttr->context();
66     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
67     if(aResCompSolidPtr.get()
68         && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID) {
69       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
70       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
71         anIt = aCompSolidsObjects.begin();
72       for(; anIt != aCompSolidsObjects.end(); anIt++) {
73         if(anIt->first->isEqual(aContextShape)) {
74           aCompSolidsObjects[anIt->first].push_back(anObject);
75           break;
76         }
77       }
78       if(anIt == aCompSolidsObjects.end()) {
79         aCompSolidsObjects[aContextShape].push_back(anObject);
80       }
81     } else {
82       anObjects.push_back(anObject);
83     }
84   }
85
86   // Getting tools.
87   AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
88   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
89     AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
90     GeomShapePtr aTool = aToolAttr->value();
91     if(!aTool.get()) {
92       // It could be a construction plane.
93       ResultPtr aContext = aToolAttr->context();
94       aPlanes.push_back(aToolAttr->context()->shape());
95     }
96     else {
97       aTools.push_back(aTool);
98     }
99   }
100
101   int aResultIndex = 0;
102
103   if ((anObjects.empty() && aCompSolidsObjects.empty())
104       || (aTools.empty() && aPlanes.empty())) {
105     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
106     setError(aFeatureError);
107     return;
108   }
109
110   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
111   ListOfShape aResultShapesList;
112
113   // For solids cut each object with all tools.
114   for(ListOfShape::iterator
115       anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); anObjectsIt++) {
116     std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
117     ListOfShape aListWithObject;
118     aListWithObject.push_back(anObject);
119     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
120     std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
121     GeomShapePtr aResShape;
122
123     std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
124         GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
125
126     // Resize planes.
127     ListOfShape aToolsWithPlanes = aTools;
128     for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
129                                     anIt != aPlanes.cend();
130                                     ++anIt)
131     {
132       GeomShapePtr aPlane = *anIt;
133       GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
134       std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
135         new GeomAlgoAPI_MakeShapeCustom);
136       aMkShCustom->addModified(aPlane, aTool);
137       aMakeShapeList->appendAlgo(aMkShCustom);
138       aToolsWithPlanes.push_back(aTool);
139     }
140
141     aBoolAlgo.reset(new GeomAlgoAPI_Partition(aListWithObject, aToolsWithPlanes));
142     aResShape = aBoolAlgo->shape();
143     if (aResShape.get() && aResShape->shapeType() == GeomAPI_Shape::COMPOUND) {
144       int aSubResultsNb = 0;
145       GeomAPI_ShapeIterator anIt(aResShape);
146       for(; anIt.more(); anIt.next()) {
147         ++aSubResultsNb;
148       }
149       if(aSubResultsNb == 1) {
150         anIt.init(aResShape);
151         if(anIt.more()) {
152           aResShape = anIt.current();
153         }
154       }
155     }
156
157     // Checking that the algorithm worked properly.
158     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
159       setError(anError);
160       return;
161     }
162
163     aMakeShapeList->appendAlgo(aBoolAlgo);
164
165     std::shared_ptr<ModelAPI_ResultBody> aResultBody =
166         document()->createBody(data(), aResultIndex);
167
168     // tools should be added to the list to fulfill the correct history of modification
169     aListWithObject.insert(aListWithObject.end(), aTools.begin(), aTools.end());
170
171     ListOfShape aUsedTools = aTools;
172     aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
173
174     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aListWithObject, aUsedTools,
175                                              aMakeShapeList, aResShape);
176     setResult(aResultBody, aResultIndex);
177     aResultIndex++;
178
179     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
180     aRBA.resultBody = aResultBody;
181     aRBA.baseShape = anObject;
182     aRBA.makeShape = aMakeShapeList;
183     aResultBaseAlgoList.push_back(aRBA);
184     aResultShapesList.push_back(aResShape);
185   }
186
187   // Compsolids handling
188   for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
189       anIt = aCompSolidsObjects.begin();
190       anIt != aCompSolidsObjects.end(); anIt++) {
191     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
192     ListOfShape& aUsedInOperationSolids = anIt->second;
193
194     // Collecting solids from compsolids which will not be modified in boolean operation.
195     ListOfShape aNotUsedSolids;
196     for(GeomAPI_ShapeExplorer
197         anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
198       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
199       ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
200       for(; anIt != aUsedInOperationSolids.end(); anIt++) {
201         if(aSolidInCompSolid->isEqual(*anIt)) {
202           break;
203         }
204       }
205       if(anIt == aUsedInOperationSolids.end()) {
206         aNotUsedSolids.push_back(aSolidInCompSolid);
207       }
208     }
209
210     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
211     std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
212
213     std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
214       GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
215
216     // Resize planes.
217     ListOfShape aToolsWithPlanes = aTools;
218     for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
219                                     anIt != aPlanes.cend();
220                                     ++anIt)
221     {
222       GeomShapePtr aPlane = *anIt;
223       GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
224       std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
225         new GeomAlgoAPI_MakeShapeCustom);
226       aMkShCustom->addModified(aPlane, aTool);
227       aMakeShapeList->appendAlgo(aMkShCustom);
228       aToolsWithPlanes.push_back(aTool);
229     }
230
231     aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
232
233     // Checking that the algorithm worked properly.
234     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
235       setError(anError);
236       return;
237     }
238
239     aMakeShapeList->appendAlgo(aBoolAlgo);
240     GeomShapePtr aResultShape = aBoolAlgo->shape();
241
242     // Add result to not used solids from compsolid.
243     if(!aNotUsedSolids.empty()) {
244       ListOfShape aShapesToAdd = aNotUsedSolids;
245       aShapesToAdd.push_back(aBoolAlgo->shape());
246       std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
247         new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
248       if(!aFillerAlgo->isDone()) {
249         std::string aFeatureError = "Error: PaveFiller algorithm failed.";
250         setError(aFeatureError);
251         return;
252       }
253
254       aMakeShapeList->appendAlgo(aFillerAlgo);
255       aResultShape = aFillerAlgo->shape();
256     }
257
258     std::shared_ptr<ModelAPI_ResultBody> aResultBody =
259       document()->createBody(data(), aResultIndex);
260
261     ListOfShape aUsedTools = aTools;
262     aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
263
264     ListOfShape aBaseShapes;
265     aBaseShapes.push_back(aCompSolid);
266     // tools should be added to the list to fulfill the correct history of modification
267     aBaseShapes.insert(aBaseShapes.end(), aTools.begin(), aTools.end());
268
269     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, aUsedTools,
270                                              aMakeShapeList, aResultShape);
271     setResult(aResultBody, aResultIndex);
272     aResultIndex++;
273
274     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
275     aRBA.resultBody = aResultBody;
276     aRBA.baseShape = aCompSolid;
277     aRBA.makeShape = aMakeShapeList;
278     aResultBaseAlgoList.push_back(aRBA);
279     aResultShapesList.push_back(aResultShape);
280   }
281
282   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
283   // result shape has been deleted, but in another it was modified or stayed.
284   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
285   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
286
287   // remove the rest results if there were produced in the previous pass
288   removeResults(aResultIndex);
289 }