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