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 <GeomAlgoAPI_Tools.h>
36 #include <GeomAPI_ShapeExplorer.h>
37 #include <GeomAPI_ShapeIterator.h>
39 //==================================================================================================
40 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
41 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
45 //==================================================================================================
46 void FeaturesPlugin_BooleanCut::execute()
48 ListOfShape anObjects, aTools;
49 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
50 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
53 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
54 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
55 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
56 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
60 ResultPtr aContext = anObjectAttr->context();
61 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
62 if (aResCompSolidPtr.get())
64 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
65 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
66 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
67 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
69 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
71 for (; anIt != aMap.end(); anIt++) {
72 if (anIt->first->isEqual(aContextShape)) {
73 aMap[anIt->first].push_back(anObject);
77 if (anIt == aMap.end()) {
78 aMap[aContextShape].push_back(anObject);
82 anObjects.push_back(anObject);
87 AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
88 for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
89 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
90 GeomShapePtr aTool = aToolAttr->value();
94 aTools.push_back(aTool);
99 if((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
101 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
102 setError(aFeatureError);
106 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
107 ListOfShape aResultShapesList;
110 // For solids cut each object with all tools.
111 for(ListOfShape::iterator anObjectsIt = anObjects.begin();
112 anObjectsIt != anObjects.end();
114 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
115 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
116 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
117 new GeomAlgoAPI_Boolean(anObject,
119 GeomAlgoAPI_Boolean::BOOL_CUT));
120 GeomShapePtr aResShape = aCutAlgo->shape();
122 // Checking that the algorithm worked properly.
123 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
128 aMakeShapeList->appendAlgo(aCutAlgo);
130 GeomAPI_ShapeIterator aShapeIt(aResShape);
131 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX)
133 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
134 document()->createBody(data(), aResultIndex);
136 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
141 setResult(aResultBody, aResultIndex);
144 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
145 aRBA.resultBody = aResultBody;
146 aRBA.baseShape = anObject;
147 aRBA.makeShape = aMakeShapeList;
148 aResultBaseAlgoList.push_back(aRBA);
149 aResultShapesList.push_back(aResShape);
153 // Compsolids handling
154 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
155 anIt = aCompSolidsObjects.begin();
156 anIt != aCompSolidsObjects.end();
159 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
160 ListOfShape& aUsedInOperationSolids = anIt->second;
162 // Collecting solids from compsolids which will not be modified in boolean operation.
163 ListOfShape aNotUsedSolids;
164 for(GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
168 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
169 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
170 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
171 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
175 if (aUsedIt == aUsedInOperationSolids.end()) {
176 aNotUsedSolids.push_back(aSolidInCompSolid);
180 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
181 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
182 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
184 GeomAlgoAPI_Boolean::BOOL_CUT));
186 // Checking that the algorithm worked properly.
187 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
192 aMakeShapeList->appendAlgo(aCutAlgo);
193 GeomShapePtr aResultShape = aCutAlgo->shape();
195 // Add result to not used solids from compsolid.
196 if(!aNotUsedSolids.empty()) {
197 ListOfShape aShapesToAdd = aNotUsedSolids;
198 aShapesToAdd.push_back(aCutAlgo->shape());
199 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
200 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
201 if(!aFillerAlgo->isDone()) {
202 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
203 setError(aFeatureError);
207 aMakeShapeList->appendAlgo(aFillerAlgo);
208 aResultShape = aFillerAlgo->shape();
211 GeomAPI_ShapeIterator aShapeIt(aResultShape);
212 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
214 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
215 document()->createBody(data(), aResultIndex);
217 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
222 setResult(aResultBody, aResultIndex);
225 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
226 aRBA.resultBody = aResultBody;
227 aRBA.baseShape = aCompSolid;
228 aRBA.makeShape = aMakeShapeList;
229 aResultBaseAlgoList.push_back(aRBA);
230 aResultShapesList.push_back(aResultShape);
234 // Compounds handling
235 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
236 anIt = aCompoundObjects.begin();
237 anIt != aCompoundObjects.end();
240 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
241 ListOfShape& aUsedInOperationShapes = anIt->second;
243 // Collecting shapes from compound which will not be modified in boolean operation.
244 ListOfShape aNotUsedShapes;
245 for (GeomAPI_ShapeIterator aCompIt(aCompound);
249 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
250 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
251 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
252 if (aShapeInCompound->isEqual(*aUsedIt)) {
256 if (aUsedIt == aUsedInOperationShapes.end()) {
257 aNotUsedShapes.push_back(aShapeInCompound);
261 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
262 std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
263 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
265 GeomAlgoAPI_Boolean::BOOL_CUT));
267 // Checking that the algorithm worked properly.
268 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCutAlgo, getKind(), anError)) {
273 aMakeShapeList->appendAlgo(aCutAlgo);
274 GeomShapePtr aResultShape = aCutAlgo->shape();
276 // Add result to not used shape from compound.
277 if (!aNotUsedShapes.empty()) {
278 ListOfShape aShapesForResult = aNotUsedShapes;
279 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
280 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
281 aShapesForResult.push_back(aResultIt.current());
284 aShapesForResult.push_back(aResultShape);
287 if (aShapesForResult.size() == 1) {
288 aResultShape = aShapesForResult.front();
290 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
294 GeomAPI_ShapeIterator aShapeIt(aResultShape);
295 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
296 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
297 document()->createBody(data(), aResultIndex);
299 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
304 setResult(aResultBody, aResultIndex);
307 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
308 aRBA.resultBody = aResultBody;
309 aRBA.baseShape = aCompound;
310 aRBA.makeShape = aMakeShapeList;
311 aResultBaseAlgoList.push_back(aRBA);
312 aResultShapesList.push_back(aResultShape);
316 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
317 // result shape has been deleted, but in another it was modified or stayed.
318 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
319 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
321 // remove the rest results if there were produced in the previous pass
322 removeResults(aResultIndex);