Salome HOME
Issue #1834: Fix length of lines
[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 
36     anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
37   {
38     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
39   }
40
41   // Getting tools.
42   TopTools_ListOfShape aTools;
43   for(ListOfShape::const_iterator 
44     aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
45   {
46     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
47   }
48
49   // Creating boolean operation.
50   BOPAlgo_BOP* aBuilder = new BOPAlgo_BOP();
51   switch (theOperationType) {
52     case BOOL_CUT: {
53       aBuilder->SetOperation(BOPAlgo_CUT);
54       break;
55     }
56     case BOOL_FUSE: {
57       aBuilder->SetOperation(BOPAlgo_FUSE);
58       break;
59     }
60     case BOOL_COMMON: {
61       aBuilder->SetOperation(BOPAlgo_COMMON);
62       break;
63     }
64     default: {
65       return;
66     }
67   }
68   this->setImpl(aBuilder);
69   this->setBuilderType(OCCT_BOPAlgo_Builder);
70   aBuilder->SetArguments(anObjects);
71   aBuilder->SetTools(aTools);
72
73   // Building and getting result.
74   aBuilder->Perform();
75   if(aBuilder->ErrorStatus() != 0) {
76     return;
77   }
78   TopoDS_Shape aResult = aBuilder->Shape();
79
80   if(aResult.ShapeType() == TopAbs_COMPOUND) {
81     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
82   }
83   if(aResult.ShapeType() == TopAbs_COMPOUND) {
84     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
85     aGeomShape->setImpl(new TopoDS_Shape(aResult));
86     ListOfShape aCompSolids, aFreeSolids;
87     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
88                                                        GeomAPI_Shape::COMPSOLID,
89                                                        aCompSolids,
90                                                        aFreeSolids);
91     aResult = aGeomShape->impl<TopoDS_Shape>();
92   }
93
94   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
95   aShape->setImpl(new TopoDS_Shape(aResult));
96   this->setShape(aShape);
97   this->setDone(true);
98 }