]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_BooleanFill.cpp
Salome HOME
High level objects history implementation for Fill feature.
[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     ListOfShape aUsedTools = aTools;
170     aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
171
172     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aListWithObject, aUsedTools,
173                                              aMakeShapeList, aResShape);
174     setResult(aResultBody, aResultIndex);
175     aResultIndex++;
176
177     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
178     aRBA.resultBody = aResultBody;
179     aRBA.baseShape = anObject;
180     aRBA.makeShape = aMakeShapeList;
181     aResultBaseAlgoList.push_back(aRBA);
182     aResultShapesList.push_back(aResShape);
183   }
184
185   // Compsolids handling
186   for(std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
187       anIt = aCompSolidsObjects.begin();
188       anIt != aCompSolidsObjects.end(); anIt++) {
189     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
190     ListOfShape& aUsedInOperationSolids = anIt->second;
191
192     // Collecting solids from compsolids which will not be modified in boolean operation.
193     ListOfShape aNotUsedSolids;
194     for(GeomAPI_ShapeExplorer
195         anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
196       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
197       ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
198       for(; anIt != aUsedInOperationSolids.end(); anIt++) {
199         if(aSolidInCompSolid->isEqual(*anIt)) {
200           break;
201         }
202       }
203       if(anIt == aUsedInOperationSolids.end()) {
204         aNotUsedSolids.push_back(aSolidInCompSolid);
205       }
206     }
207
208     std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
209     std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
210
211     std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
212       GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
213
214     // Resize planes.
215     ListOfShape aToolsWithPlanes = aTools;
216     for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
217                                     anIt != aPlanes.cend();
218                                     ++anIt)
219     {
220       GeomShapePtr aPlane = *anIt;
221       GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
222       std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
223         new GeomAlgoAPI_MakeShapeCustom);
224       aMkShCustom->addModified(aPlane, aTool);
225       aMakeShapeList->appendAlgo(aMkShCustom);
226       aToolsWithPlanes.push_back(aTool);
227     }
228
229     aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
230
231     // Checking that the algorithm worked properly.
232     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
233       setError(anError);
234       return;
235     }
236
237     aMakeShapeList->appendAlgo(aBoolAlgo);
238     GeomShapePtr aResultShape = aBoolAlgo->shape();
239
240     // Add result to not used solids from compsolid.
241     if(!aNotUsedSolids.empty()) {
242       ListOfShape aShapesToAdd = aNotUsedSolids;
243       aShapesToAdd.push_back(aBoolAlgo->shape());
244       std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
245         new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
246       if(!aFillerAlgo->isDone()) {
247         std::string aFeatureError = "Error: PaveFiller algorithm failed.";
248         setError(aFeatureError);
249         return;
250       }
251
252       aMakeShapeList->appendAlgo(aFillerAlgo);
253       aResultShape = aFillerAlgo->shape();
254     }
255
256     std::shared_ptr<ModelAPI_ResultBody> aResultBody =
257       document()->createBody(data(), aResultIndex);
258
259     ListOfShape aUsedTools = aTools;
260     aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
261
262     ListOfShape aBaseShapes;
263     aBaseShapes.push_back(aCompSolid);
264     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, aUsedTools,
265                                              aMakeShapeList, aResultShape);
266     setResult(aResultBody, aResultIndex);
267     aResultIndex++;
268
269     FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
270     aRBA.resultBody = aResultBody;
271     aRBA.baseShape = aCompSolid;
272     aRBA.makeShape = aMakeShapeList;
273     aResultBaseAlgoList.push_back(aRBA);
274     aResultShapesList.push_back(aResultShape);
275   }
276
277   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
278   // result shape has been deleted, but in another it was modified or stayed.
279   GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
280   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
281
282   // remove the rest results if there were produced in the previous pass
283   removeResults(aResultIndex);
284 }