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