Salome HOME
Hide scroll bar in SALOME
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Boolean.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Boolean.cpp
4 // Created:     02 Sept 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "GeomAlgoAPI_Boolean.h"
8
9 #include <GeomAlgoAPI_DFLoader.h>
10 #include <GeomAlgoAPI_ShapeTools.h>
11
12 #include <BOPAlgo_BOP.hxx>
13 #include <TopTools_ListOfShape.hxx>
14
15 //=================================================================================================
16 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
17                                          const ListOfShape& theTools,
18                                          const OperationType theOperationType)
19 {
20   build(theObjects, theTools, theOperationType);
21 }
22
23
24 //=================================================================================================
25 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
26                                 const ListOfShape& theTools,
27                                 const OperationType theOperationType)
28 {
29   if(theObjects.empty() || theTools.empty()) {
30     return;
31   }
32
33   // Getting objects.
34   TopTools_ListOfShape anObjects;
35   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
36   {
37     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
38   }
39
40   // Getting tools.
41   TopTools_ListOfShape aTools;
42   for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
43   {
44     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
45   }
46
47   // Creating boolean operation.
48   BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
49   switch (theOperationType) {
50     case BOOL_CUT: {
51       aBuilder->SetOperation(BOPAlgo_CUT);
52       break;
53     }
54     case BOOL_FUSE: {
55       aBuilder->SetOperation(BOPAlgo_FUSE);
56       break;
57     }
58     case BOOL_COMMON: {
59       aBuilder->SetOperation(BOPAlgo_COMMON);
60       break;
61     }
62     default: {
63       return;
64     }
65   }
66   this->setImpl(aBuilder);
67   this->setBuilderType(OCCT_BOPAlgo_Builder);
68   aBuilder->SetArguments(anObjects);
69   aBuilder->SetTools(aTools);
70
71   // Building and getting result.
72   aBuilder->Perform();
73   if(aBuilder->ErrorStatus() != 0) {
74     return;
75   }
76   TopoDS_Shape aResult = aBuilder->Shape();
77
78   if(aResult.ShapeType() == TopAbs_COMPOUND) {
79     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
80   }
81   if(aResult.ShapeType() == TopAbs_COMPOUND) {
82     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
83     aGeomShape->setImpl(new TopoDS_Shape(aResult));
84     ListOfShape aCompSolids, aFreeSolids;
85     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
86                                                        GeomAPI_Shape::COMPSOLID,
87                                                        aCompSolids,
88                                                        aFreeSolids);
89     aResult = aGeomShape->impl<TopoDS_Shape>();
90   }
91
92   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
93   aShape->setImpl(new TopoDS_Shape(aResult));
94   this->setShape(aShape);
95   this->setDone(true);
96 }