Salome HOME
b26849dd7b183e6810cd1c0015cdae874caea63d
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Boolean.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Boolean.h"
22
23 #include <GeomAlgoAPI_DFLoader.h>
24 #include <GeomAlgoAPI_ShapeTools.h>
25
26 #include <BOPAlgo_BOP.hxx>
27 #include <TopTools_ListOfShape.hxx>
28
29 //=================================================================================================
30 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const GeomShapePtr theObject,
31                                          const ListOfShape& theTools,
32                                          const OperationType theOperationType)
33 {
34   ListOfShape aListWithObject;
35   aListWithObject.push_back(theObject);
36   build(aListWithObject, theTools, theOperationType);
37 }
38
39 //=================================================================================================
40 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
41                                          const ListOfShape& theTools,
42                                          const OperationType theOperationType)
43 {
44   build(theObjects, theTools, theOperationType);
45 }
46
47
48 //=================================================================================================
49 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
50                                 const ListOfShape& theTools,
51                                 const OperationType theOperationType)
52 {
53   if(theObjects.empty() || theTools.empty()) {
54     return;
55   }
56
57   // Getting objects.
58   TopTools_ListOfShape anObjects;
59   for(ListOfShape::const_iterator
60     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
61   {
62     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
63   }
64
65   // Getting tools.
66   TopTools_ListOfShape aTools;
67   for(ListOfShape::const_iterator
68     aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
69   {
70     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
71   }
72
73   // Creating boolean operation.
74   BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
75   switch (theOperationType) {
76     case BOOL_CUT: {
77       aBuilder->SetOperation(BOPAlgo_CUT);
78       break;
79     }
80     case BOOL_FUSE: {
81       aBuilder->SetOperation(BOPAlgo_FUSE);
82       break;
83     }
84     case BOOL_COMMON: {
85       aBuilder->SetOperation(BOPAlgo_COMMON);
86       break;
87     }
88     default: {
89       return;
90     }
91   }
92   this->setImpl(aBuilder);
93   this->setBuilderType(OCCT_BOPAlgo_Builder);
94   aBuilder->SetArguments(anObjects);
95   aBuilder->SetTools(aTools);
96
97   // Building and getting result.
98   aBuilder->Perform();
99   if (aBuilder->HasErrors())
100     return;
101   TopoDS_Shape aResult = aBuilder->Shape();
102
103   if(aResult.ShapeType() == TopAbs_COMPOUND) {
104     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
105   }
106   if(aResult.ShapeType() == TopAbs_COMPOUND) {
107     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
108     aGeomShape->setImpl(new TopoDS_Shape(aResult));
109     ListOfShape aCompSolids, aFreeSolids;
110     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
111                                                        GeomAPI_Shape::COMPSOLID,
112                                                        aCompSolids,
113                                                        aFreeSolids);
114     aResult = aGeomShape->impl<TopoDS_Shape>();
115   }
116
117   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
118   aShape->setImpl(new TopoDS_Shape(aResult));
119   this->setShape(aShape);
120   this->setDone(true);
121 }