Salome HOME
Initial (compilation) porting to SALOME 8.5.0 current on Debian 8
[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 ListOfShape& theObjects,
31                                          const ListOfShape& theTools,
32                                          const OperationType theOperationType)
33 {
34   build(theObjects, theTools, theOperationType);
35 }
36
37
38 //=================================================================================================
39 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
40                                 const ListOfShape& theTools,
41                                 const OperationType theOperationType)
42 {
43   if(theObjects.empty() || theTools.empty()) {
44     return;
45   }
46
47   // Getting objects.
48   TopTools_ListOfShape anObjects;
49   for(ListOfShape::const_iterator
50     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
51   {
52     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
53   }
54
55   // Getting tools.
56   TopTools_ListOfShape aTools;
57   for(ListOfShape::const_iterator
58     aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
59   {
60     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
61   }
62
63   // Creating boolean operation.
64   BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
65   switch (theOperationType) {
66     case BOOL_CUT: {
67       aBuilder->SetOperation(BOPAlgo_CUT);
68       break;
69     }
70     case BOOL_FUSE: {
71       aBuilder->SetOperation(BOPAlgo_FUSE);
72       break;
73     }
74     case BOOL_COMMON: {
75       aBuilder->SetOperation(BOPAlgo_COMMON);
76       break;
77     }
78     default: {
79       return;
80     }
81   }
82   this->setImpl(aBuilder);
83   this->setBuilderType(OCCT_BOPAlgo_Builder);
84   aBuilder->SetArguments(anObjects);
85   aBuilder->SetTools(aTools);
86
87   // Building and getting result.
88   aBuilder->Perform();
89   if (aBuilder->HasErrors())
90     return;
91   TopoDS_Shape aResult = aBuilder->Shape();
92
93   if(aResult.ShapeType() == TopAbs_COMPOUND) {
94     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
95   }
96   if(aResult.ShapeType() == TopAbs_COMPOUND) {
97     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
98     aGeomShape->setImpl(new TopoDS_Shape(aResult));
99     ListOfShape aCompSolids, aFreeSolids;
100     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
101                                                        GeomAPI_Shape::COMPSOLID,
102                                                        aCompSolids,
103                                                        aFreeSolids);
104     aResult = aGeomShape->impl<TopoDS_Shape>();
105   }
106
107   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
108   aShape->setImpl(new TopoDS_Shape(aResult));
109   this->setShape(aShape);
110   this->setDone(true);
111 }