1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "FeaturesPlugin_BooleanFill.h"
22 #include "FeaturesPlugin_Tools.h"
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_Tools.h>
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>
37 #include <GeomAPI_Face.h>
38 #include <GeomAPI_ShapeExplorer.h>
39 #include <GeomAPI_ShapeIterator.h>
44 //=================================================================================================
45 FeaturesPlugin_BooleanFill::FeaturesPlugin_BooleanFill()
46 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FILL)
50 //=================================================================================================
51 void FeaturesPlugin_BooleanFill::execute()
54 ListOfShape anObjects, aTools, anEdgesAndFaces, aPlanes;
55 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
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();
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);
79 if(anIt == aCompSolidsObjects.end()) {
80 aCompSolidsObjects[aContextShape].push_back(anObject);
83 anObjects.push_back(anObject);
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();
93 // It could be a construction plane.
94 ResultPtr aContext = aToolAttr->context();
95 aPlanes.push_back(aToolAttr->context()->shape());
98 aTools.push_back(aTool);
102 int aResultIndex = 0;
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);
111 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
112 ListOfShape aResultShapesList;
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;
124 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
125 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
128 ListOfShape aToolsWithPlanes = aTools;
129 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
130 anIt != aPlanes.cend();
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);
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()) {
150 if(aSubResultsNb == 1) {
151 anIt.init(aResShape);
153 aResShape = anIt.current();
158 // Checking that the algorithm worked properly.
159 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
164 aMakeShapeList->appendAlgo(aBoolAlgo);
166 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
167 document()->createBody(data(), aResultIndex);
169 // tools should be added to the list to fulfill the correct history of modification
170 aListWithObject.insert(aListWithObject.end(), aTools.begin(), aTools.end());
172 ListOfShape aUsedTools = aTools;
173 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
175 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aListWithObject, aUsedTools,
176 aMakeShapeList, aResShape);
177 setResult(aResultBody, aResultIndex);
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);
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;
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)) {
206 if(anIt == aUsedInOperationSolids.end()) {
207 aNotUsedSolids.push_back(aSolidInCompSolid);
211 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
212 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
214 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
215 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
218 ListOfShape aToolsWithPlanes = aTools;
219 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
220 anIt != aPlanes.cend();
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);
232 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
234 // Checking that the algorithm worked properly.
235 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
240 aMakeShapeList->appendAlgo(aBoolAlgo);
241 GeomShapePtr aResultShape = aBoolAlgo->shape();
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);
255 aMakeShapeList->appendAlgo(aFillerAlgo);
256 aResultShape = aFillerAlgo->shape();
259 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
260 document()->createBody(data(), aResultIndex);
262 ListOfShape aUsedTools = aTools;
263 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
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());
270 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, aBaseShapes, aUsedTools,
271 aMakeShapeList, aResultShape);
272 setResult(aResultBody, aResultIndex);
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);
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);
288 // remove the rest results if there were produced in the previous pass
289 removeResults(aResultIndex);