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