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 "GeomAlgoAPI_Boolean.h"
22 #include <GeomAlgoAPI_DFLoader.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
25 #include <BOPAlgo_BOP.hxx>
26 #include <TopTools_ListOfShape.hxx>
28 //=================================================================================================
29 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
30 const GeomShapePtr theTool,
31 const OperationType theOperationType)
33 ListOfShape aListWithObject, aListWithTool;
34 aListWithObject.push_back(theObject);
35 aListWithTool.push_back(theTool);
36 build(aListWithObject, aListWithTool, theOperationType);
39 //=================================================================================================
40 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
41 const ListOfShape& theTools,
42 const OperationType theOperationType)
44 ListOfShape aListWithObject;
45 aListWithObject.push_back(theObject);
46 build(aListWithObject, theTools, theOperationType);
49 //=================================================================================================
50 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
51 const ListOfShape& theTools,
52 const OperationType theOperationType)
54 build(theObjects, theTools, theOperationType);
58 //=================================================================================================
59 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
60 const ListOfShape& theTools,
61 const OperationType theOperationType)
63 if(theObjects.empty() || theTools.empty()) {
68 TopTools_ListOfShape anObjects;
69 for(ListOfShape::const_iterator
70 anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
72 anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
76 TopTools_ListOfShape aTools;
77 for(ListOfShape::const_iterator
78 aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
80 aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
83 // Creating boolean operation.
84 BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
85 switch (theOperationType) {
87 aBuilder->SetOperation(BOPAlgo_CUT);
91 aBuilder->SetOperation(BOPAlgo_FUSE);
95 aBuilder->SetOperation(BOPAlgo_COMMON);
102 this->setImpl(aBuilder);
103 this->setBuilderType(OCCT_BOPAlgo_Builder);
104 aBuilder->SetArguments(anObjects);
105 aBuilder->SetTools(aTools);
107 // Building and getting result.
109 if (aBuilder->HasErrors())
111 TopoDS_Shape aResult = aBuilder->Shape();
113 if(aResult.ShapeType() == TopAbs_COMPOUND) {
114 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
116 if(aResult.ShapeType() == TopAbs_COMPOUND) {
117 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
118 aGeomShape->setImpl(new TopoDS_Shape(aResult));
119 ListOfShape aResults;
120 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
121 GeomAPI_Shape::COMPSOLID,
123 aResult = aGeomShape->impl<TopoDS_Shape>();
126 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
127 aShape->setImpl(new TopoDS_Shape(aResult));
128 this->setShape(aShape);