1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_BooleanCut.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_MakeShapeList.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_Tools.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAPI_ShapeIterator.h>
38 //==================================================================================================
39 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
40 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
44 //==================================================================================================
45 void FeaturesPlugin_BooleanCut::execute()
47 ListOfShape anObjects, aTools;
48 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
49 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
52 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
53 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
54 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
55 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
59 ResultPtr aContext = anObjectAttr->context();
60 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
61 if (aResCompSolidPtr.get())
63 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
64 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
65 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
66 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
68 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
70 for (; anIt != aMap.end(); anIt++) {
71 if (anIt->first->isEqual(aContextShape)) {
72 aMap[anIt->first].push_back(anObject);
76 if (anIt == aMap.end()) {
77 aMap[aContextShape].push_back(anObject);
81 anObjects.push_back(anObject);
86 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
87 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
88 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
89 GeomShapePtr aTool = aToolAttr->value();
93 aTools.push_back(aTool);
98 if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
100 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
101 setError(aFeatureError);
105 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
106 ListOfShape aResultShapesList;
109 // For solids cut each object with all tools.
110 for(ListOfShape::iterator anObjectsIt = anObjects.begin();
111 anObjectsIt != anObjects.end();
113 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
114 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
115 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
116 new GeomAlgoAPI_Boolean(anObject,
118 GeomAlgoAPI_Boolean::BOOL_CUT));
119 GeomShapePtr aResShape = aCutAlgo->shape();
121 // Checking that the algorithm worked properly.
122 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
127 aMakeShapeList->appendAlgo(aCutAlgo);
129 GeomAPI_ShapeIterator aShapeIt(aResShape);
130 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
132 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
133 document()->createBody(data(), aResultIndex);
135 ListOfShape anObjectList;
136 anObjectList.push_back(anObject);
137 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
142 setResult(aResultBody, aResultIndex);
145 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
146 aRBA.resultBody = aResultBody;
147 aRBA.baseShape = anObject;
148 aRBA.makeShape = aMakeShapeList;
149 aResultBaseAlgoList.push_back(aRBA);
150 aResultShapesList.push_back(aResShape);
154 // Compsolids handling
155 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
156 anIt = aCompSolidsObjects.begin();
157 anIt != aCompSolidsObjects.end();
160 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
161 ListOfShape& aUsedInOperationSolids = anIt->second;
163 // Collecting solids from compsolids which will not be modified in boolean operation.
164 ListOfShape aNotUsedSolids;
165 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
169 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
170 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
171 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
172 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
176 if (aUsedIt == aUsedInOperationSolids.end()) {
177 aNotUsedSolids.push_back(aSolidInCompSolid);
181 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
182 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
183 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
185 GeomAlgoAPI_Boolean::BOOL_CUT));
187 // Checking that the algorithm worked properly.
188 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
193 aMakeShapeList->appendAlgo(aCutAlgo);
194 GeomShapePtr aResultShape = aCutAlgo->shape();
196 // Add result to not used solids from compsolid.
197 if(!aNotUsedSolids.empty()) {
198 ListOfShape aShapesToAdd = aNotUsedSolids;
199 aShapesToAdd.push_back(aCutAlgo->shape());
200 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
201 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
202 if(!aFillerAlgo->isDone()) {
203 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
204 setError(aFeatureError);
208 aMakeShapeList->appendAlgo(aFillerAlgo);
209 aResultShape = aFillerAlgo->shape();
212 GeomAPI_ShapeIterator aShapeIt(aResultShape);
213 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
215 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
216 document()->createBody(data(), aResultIndex);
218 ListOfShape anObjectList;
219 anObjectList.push_back(aCompSolid);
220 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
225 setResult(aResultBody, aResultIndex);
228 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
229 aRBA.resultBody = aResultBody;
230 aRBA.baseShape = aCompSolid;
231 aRBA.makeShape = aMakeShapeList;
232 aResultBaseAlgoList.push_back(aRBA);
233 aResultShapesList.push_back(aResultShape);
237 // Compounds handling
238 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
239 anIt = aCompoundObjects.begin();
240 anIt != aCompoundObjects.end();
243 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
244 ListOfShape& aUsedInOperationShapes = anIt->second;
246 // Collecting shapes from compound which will not be modified in boolean operation.
247 ListOfShape aNotUsedShapes;
248 for (GeomAPI_ShapeIterator aCompIt(aCompound);
252 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
253 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
254 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
255 if (aShapeInCompound->isEqual(*aUsedIt)) {
259 if (aUsedIt == aUsedInOperationShapes.end()) {
260 aNotUsedShapes.push_back(aShapeInCompound);
264 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
265 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
266 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
268 GeomAlgoAPI_Boolean::BOOL_CUT));
270 // Checking that the algorithm worked properly.
271 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
276 aMakeShapeList->appendAlgo(aCutAlgo);
277 GeomShapePtr aResultShape = aCutAlgo->shape();
279 // Add result to not used shape from compound.
280 if (!aNotUsedShapes.empty()) {
281 ListOfShape aShapesForResult = aNotUsedShapes;
282 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
283 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
284 aShapesForResult.push_back(aResultIt.current());
287 aShapesForResult.push_back(aResultShape);
290 if (aShapesForResult.size() == 1) {
291 aResultShape = aShapesForResult.front();
293 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
297 GeomAPI_ShapeIterator aShapeIt(aResultShape);
298 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
299 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
300 document()->createBody(data(), aResultIndex);
302 ListOfShape anObjectList;
303 anObjectList.push_back(aCompound);
304 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
309 setResult(aResultBody, aResultIndex);
312 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
313 aRBA.resultBody = aResultBody;
314 aRBA.baseShape = aCompound;
315 aRBA.makeShape = aMakeShapeList;
316 aResultBaseAlgoList.push_back(aRBA);
317 aResultShapesList.push_back(aResultShape);
321 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
322 // result shape has been deleted, but in another it was modified or stayed.
323 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
324 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
326 // remove the rest results if there were produced in the previous pass
327 removeResults(aResultIndex);