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 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
140 setResult(aResultBody, aResultIndex);
143 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
144 aRBA.resultBody = aResultBody;
145 aRBA.baseShape = anObject;
146 aRBA.makeShape = aMakeShapeList;
147 aResultBaseAlgoList.push_back(aRBA);
148 aResultShapesList.push_back(aResShape);
152 // Compsolids handling
153 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
154 anIt = aCompSolidsObjects.begin();
155 anIt != aCompSolidsObjects.end();
158 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
159 ListOfShape& aUsedInOperationSolids = anIt->second;
161 // Collecting solids from compsolids which will not be modified in boolean operation.
162 ListOfShape aNotUsedSolids;
163 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
167 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
168 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
169 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
170 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
174 if (aUsedIt == aUsedInOperationSolids.end()) {
175 aNotUsedSolids.push_back(aSolidInCompSolid);
179 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
180 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
181 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
183 GeomAlgoAPI_Boolean::BOOL_CUT));
185 // Checking that the algorithm worked properly.
186 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
191 aMakeShapeList->appendAlgo(aCutAlgo);
192 GeomShapePtr aResultShape = aCutAlgo->shape();
194 // Add result to not used solids from compsolid.
195 if(!aNotUsedSolids.empty()) {
196 ListOfShape aShapesToAdd = aNotUsedSolids;
197 aShapesToAdd.push_back(aCutAlgo->shape());
198 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
199 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
200 if(!aFillerAlgo->isDone()) {
201 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
202 setError(aFeatureError);
206 aMakeShapeList->appendAlgo(aFillerAlgo);
207 aResultShape = aFillerAlgo->shape();
210 GeomAPI_ShapeIterator aShapeIt(aResultShape);
211 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
213 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
214 document()->createBody(data(), aResultIndex);
216 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
221 setResult(aResultBody, aResultIndex);
224 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
225 aRBA.resultBody = aResultBody;
226 aRBA.baseShape = aCompSolid;
227 aRBA.makeShape = aMakeShapeList;
228 aResultBaseAlgoList.push_back(aRBA);
229 aResultShapesList.push_back(aResultShape);
233 // Compounds handling
234 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
235 anIt = aCompoundObjects.begin();
236 anIt != aCompoundObjects.end();
239 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
240 ListOfShape& aUsedInOperationShapes = anIt->second;
242 // Collecting shapes from compound which will not be modified in boolean operation.
243 ListOfShape aNotUsedShapes;
244 for (GeomAPI_ShapeIterator aCompIt(aCompound);
248 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
249 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
250 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
251 if (aShapeInCompound->isEqual(*aUsedIt)) {
255 if (aUsedIt == aUsedInOperationShapes.end()) {
256 aNotUsedShapes.push_back(aShapeInCompound);
260 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
261 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
262 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
264 GeomAlgoAPI_Boolean::BOOL_CUT));
266 // Checking that the algorithm worked properly.
267 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
272 aMakeShapeList->appendAlgo(aCutAlgo);
273 GeomShapePtr aResultShape = aCutAlgo->shape();
275 // Add result to not used shape from compound.
276 if (!aNotUsedShapes.empty()) {
277 ListOfShape aShapesForResult = aNotUsedShapes;
278 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
279 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
280 aShapesForResult.push_back(aResultIt.current());
283 aShapesForResult.push_back(aResultShape);
286 if (aShapesForResult.size() == 1) {
287 aResultShape = aShapesForResult.front();
289 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
293 GeomAPI_ShapeIterator aShapeIt(aResultShape);
294 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
295 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
296 document()->createBody(data(), aResultIndex);
298 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
303 setResult(aResultBody, aResultIndex);
306 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
307 aRBA.resultBody = aResultBody;
308 aRBA.baseShape = aCompound;
309 aRBA.makeShape = aMakeShapeList;
310 aResultBaseAlgoList.push_back(aRBA);
311 aResultShapesList.push_back(aResultShape);
315 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
316 // result shape has been deleted, but in another it was modified or stayed.
317 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
318 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
320 // remove the rest results if there were produced in the previous pass
321 removeResults(aResultIndex);