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_BooleanCut.h"
23 #include "FeaturesPlugin_Tools.h"
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_Tools.h>
29 #include <GeomAlgoAPI_Boolean.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAPI_ShapeExplorer.h>
35 #include <GeomAPI_ShapeIterator.h>
37 //==================================================================================================
38 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
39 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
43 //==================================================================================================
44 void FeaturesPlugin_BooleanCut::execute()
46 ListOfShape anObjects, aTools;
47 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
48 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
51 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
52 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
53 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
54 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
58 ResultPtr aContext = anObjectAttr->context();
59 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
60 if (aResCompSolidPtr.get())
62 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
63 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
64 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
65 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
67 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
69 for (; anIt != aMap.end(); anIt++) {
70 if (anIt->first->isEqual(aContextShape)) {
71 aMap[anIt->first].push_back(anObject);
75 if (anIt == aMap.end()) {
76 aMap[aContextShape].push_back(anObject);
80 anObjects.push_back(anObject);
85 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
86 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
87 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
88 GeomShapePtr aTool = aToolAttr->value();
92 aTools.push_back(aTool);
97 if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
99 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
100 setError(aFeatureError);
104 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
105 ListOfShape aResultShapesList;
107 // For solids cut each object with all tools.
108 for(ListOfShape::iterator anObjectsIt = anObjects.begin();
109 anObjectsIt != anObjects.end();
111 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
112 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
113 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
114 new GeomAlgoAPI_Boolean(anObject,
116 GeomAlgoAPI_Boolean::BOOL_CUT));
117 GeomShapePtr aResShape = aCutAlgo->shape();
119 // Checking that the algorithm worked properly.
120 if (!aCutAlgo->isDone()) {
121 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
122 setError(aFeatureError);
125 if(aResShape->isNull()) {
126 static const std::string aShapeError = "Error: Resulting shape is Null.";
127 setError(aShapeError);
130 if (!aCutAlgo->isValid()) {
131 std::string aFeatureError = "Error: Resulting shape is not valid.";
132 setError(aFeatureError);
136 aMakeShapeList->appendAlgo(aCutAlgo);
138 GeomAPI_ShapeIterator aShapeIt(aResShape);
139 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
141 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
142 document()->createBody(data(), aResultIndex);
144 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
149 setResult(aResultBody, aResultIndex);
152 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
153 aRBA.resultBody = aResultBody;
154 aRBA.baseShape = anObject;
155 aRBA.makeShape = aMakeShapeList;
156 aResultBaseAlgoList.push_back(aRBA);
157 aResultShapesList.push_back(aResShape);
161 // Compsolids handling
162 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
163 anIt = aCompSolidsObjects.begin();
164 anIt != aCompSolidsObjects.end();
167 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
168 ListOfShape& aUsedInOperationSolids = anIt->second;
170 // Collecting solids from compsolids which will not be modified in boolean operation.
171 ListOfShape aNotUsedSolids;
172 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
176 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
177 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
178 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
179 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
183 if (aUsedIt == aUsedInOperationSolids.end()) {
184 aNotUsedSolids.push_back(aSolidInCompSolid);
188 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
189 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
190 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
192 GeomAlgoAPI_Boolean::BOOL_CUT));
194 // Checking that the algorithm worked properly.
195 if (!aCutAlgo->isDone()) {
196 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
197 setError(aFeatureError);
200 if (aCutAlgo->shape()->isNull()) {
201 static const std::string aShapeError = "Error: Resulting shape is Null.";
202 setError(aShapeError);
205 if (!aCutAlgo->isValid()) {
206 std::string aFeatureError = "Error: Resulting shape is not valid.";
207 setError(aFeatureError);
211 aMakeShapeList->appendAlgo(aCutAlgo);
212 GeomShapePtr aResultShape = aCutAlgo->shape();
214 // Add result to not used solids from compsolid.
215 if(!aNotUsedSolids.empty()) {
216 ListOfShape aShapesToAdd = aNotUsedSolids;
217 aShapesToAdd.push_back(aCutAlgo->shape());
218 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
219 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
220 if(!aFillerAlgo->isDone()) {
221 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
222 setError(aFeatureError);
226 aMakeShapeList->appendAlgo(aFillerAlgo);
227 aResultShape = aFillerAlgo->shape();
230 GeomAPI_ShapeIterator aShapeIt(aResultShape);
231 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
233 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
234 document()->createBody(data(), aResultIndex);
236 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
241 setResult(aResultBody, aResultIndex);
244 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
245 aRBA.resultBody = aResultBody;
246 aRBA.baseShape = aCompSolid;
247 aRBA.makeShape = aMakeShapeList;
248 aResultBaseAlgoList.push_back(aRBA);
249 aResultShapesList.push_back(aResultShape);
253 // Compounds handling
254 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
255 anIt = aCompoundObjects.begin();
256 anIt != aCompoundObjects.end();
259 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
260 ListOfShape& aUsedInOperationShapes = anIt->second;
262 // Collecting shapes from compound which will not be modified in boolean operation.
263 ListOfShape aNotUsedShapes;
264 for (GeomAPI_ShapeIterator aCompIt(aCompound);
268 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
269 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
270 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
271 if (aShapeInCompound->isEqual(*aUsedIt)) {
275 if (aUsedIt == aUsedInOperationShapes.end()) {
276 aNotUsedShapes.push_back(aShapeInCompound);
280 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
281 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
282 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
284 GeomAlgoAPI_Boolean::BOOL_CUT));
286 // Checking that the algorithm worked properly.
287 if (!aCutAlgo->isDone()) {
288 static const std::string aFeatureError = "Error: Boolean algorithm failed.";
289 setError(aFeatureError);
292 if (aCutAlgo->shape()->isNull()) {
293 static const std::string aShapeError = "Error: Resulting shape is Null.";
294 setError(aShapeError);
297 if (!aCutAlgo->isValid()) {
298 std::string aFeatureError = "Error: Resulting shape is not valid.";
299 setError(aFeatureError);
303 aMakeShapeList->appendAlgo(aCutAlgo);
304 GeomShapePtr aResultShape = aCutAlgo->shape();
306 // Add result to not used shape from compound.
307 if (!aNotUsedShapes.empty()) {
308 ListOfShape aShapesForResult = aNotUsedShapes;
309 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
310 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
311 aShapesForResult.push_back(aResultIt.current());
314 aShapesForResult.push_back(aResultShape);
317 if (aShapesForResult.size() == 1) {
318 aResultShape = aShapesForResult.front();
320 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
324 GeomAPI_ShapeIterator aShapeIt(aResultShape);
325 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
326 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
327 document()->createBody(data(), aResultIndex);
329 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
334 setResult(aResultBody, aResultIndex);
337 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
338 aRBA.resultBody = aResultBody;
339 aRBA.baseShape = aCompound;
340 aRBA.makeShape = aMakeShapeList;
341 aResultBaseAlgoList.push_back(aRBA);
342 aResultShapesList.push_back(aResultShape);
346 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
347 // result shape has been deleted, but in another it was modified or stayed.
348 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
349 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
351 // remove the rest results if there were produced in the previous pass
352 removeResults(aResultIndex);