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