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