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