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"
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
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>
40 //=================================================================================================
41 FeaturesPlugin_BooleanFill::FeaturesPlugin_BooleanFill()
42 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FILL)
46 //=================================================================================================
47 void FeaturesPlugin_BooleanFill::execute()
49 ListOfShape anObjects, aTools, anEdgesAndFaces, aPlanes;
50 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
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();
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);
74 if(anIt == aCompSolidsObjects.end()) {
75 aCompSolidsObjects[aContextShape].push_back(anObject);
78 anObjects.push_back(anObject);
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();
88 // It could be a construction plane.
89 ResultPtr aContext = aToolAttr->context();
90 aPlanes.push_back(aToolAttr->context()->shape());
93 aTools.push_back(aTool);
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);
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;
116 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
117 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
120 ListOfShape aToolsWithPlanes = aTools;
121 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
122 anIt != aPlanes.cend();
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);
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()) {
142 if(aSubResultsNb == 1) {
143 anIt.init(aResShape);
145 aResShape = anIt.current();
150 // Checking that the algorithm worked properly.
151 if(!aBoolAlgo->isDone()) {
152 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
153 setError(aFeatureError);
156 if(aResShape->isNull()) {
157 static const std::string aShapeError = "Error: Resulting shape is Null.";
158 setError(aShapeError);
161 if(!aBoolAlgo->isValid()) {
162 std::string aFeatureError = "Error: Resulting shape is not valid.";
163 setError(aFeatureError);
167 aMakeShapeList->appendAlgo(aBoolAlgo);
169 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
170 document()->createBody(data(), aResultIndex);
172 ListOfShape aUsedTools = aTools;
173 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
175 loadNamingDS(aResultBody, anObject, aUsedTools, aResShape, aMakeShapeList);
176 setResult(aResultBody, aResultIndex);
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;
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)) {
198 if(anIt == aUsedInOperationSolids.end()) {
199 aNotUsedSolids.push_back(aSolidInCompSolid);
203 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
204 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
206 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
207 GeomAlgoAPI_ShapeTools::getBoundingBox(aUsedInOperationSolids, 1.0);
210 ListOfShape aToolsWithPlanes = aTools;
211 for(ListOfShape::const_iterator anIt = aPlanes.cbegin();
212 anIt != aPlanes.cend();
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);
224 aBoolAlgo.reset(new GeomAlgoAPI_Partition(aUsedInOperationSolids, aToolsWithPlanes));
226 // Checking that the algorithm worked properly.
227 if(!aBoolAlgo->isDone()) {
228 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
229 setError(aFeatureError);
232 if(aBoolAlgo->shape()->isNull()) {
233 static const std::string aShapeError = "Error: Resulting shape is Null.";
234 setError(aShapeError);
237 if(!aBoolAlgo->isValid()) {
238 std::string aFeatureError = "Error: Resulting shape is not valid.";
239 setError(aFeatureError);
243 aMakeShapeList->appendAlgo(aBoolAlgo);
244 GeomShapePtr aResultShape = aBoolAlgo->shape();
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);
258 aMakeShapeList->appendAlgo(aFillerAlgo);
259 aResultShape = aFillerAlgo->shape();
262 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
263 document()->createBody(data(), aResultIndex);
265 ListOfShape aUsedTools = aTools;
266 aUsedTools.insert(aUsedTools.end(), aPlanes.begin(), aPlanes.end());
268 loadNamingDS(aResultBody,
273 setResult(aResultBody, aResultIndex);
277 // remove the rest results if there were produced in the previous pass
278 removeResults(aResultIndex);