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