Salome HOME
#1119 Confirmation box for deleting parts
[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
11 #include <BOPAlgo_BOP.hxx>
12 #include <TopTools_ListOfShape.hxx>
13
14 //=================================================================================================
15 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
16                                          const ListOfShape& theTools,
17                                          const OperationType theOperationType)
18 {
19   build(theObjects, theTools, theOperationType);
20 }
21
22
23 //=================================================================================================
24 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
25                                 const ListOfShape& theTools,
26                                 const OperationType theOperationType)
27 {
28   if(theObjects.empty() || theTools.empty()) {
29     return;
30   }
31
32   // Getting objects.
33   TopTools_ListOfShape anObjects;
34   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
35   {
36     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
37   }
38
39   // Getting tools.
40   TopTools_ListOfShape aTools;
41   for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
42   {
43     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
44   }
45
46   // Creating boolean operation.
47   BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
48   switch (theOperationType) {
49     case BOOL_CUT: {
50       aBuilder->SetOperation(BOPAlgo_CUT);
51       break;
52     }
53     case BOOL_FUSE: {
54       aBuilder->SetOperation(BOPAlgo_FUSE);
55       break;
56     }
57     case BOOL_COMMON: {
58       aBuilder->SetOperation(BOPAlgo_COMMON);
59       break;
60     }
61     default: {
62       return;
63     }
64   }
65   this->setImpl(aBuilder);
66   this->setBuilderType(OCCT_BOPAlgo_Builder);
67   aBuilder->SetArguments(anObjects);
68   aBuilder->SetTools(aTools);
69
70   // Building and getting result.
71   aBuilder->Perform();
72   if(aBuilder->ErrorStatus() != 0) {
73     return;
74   }
75   TopoDS_Shape aResult = aBuilder->Shape();
76
77   if(aResult.ShapeType() == TopAbs_COMPOUND) {
78     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
79   }
80
81   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
82   aShape->setImpl(new TopoDS_Shape(aResult));
83   this->setShape(aShape);
84   this->setDone(true);
85 }