Salome HOME
Issue #1063: Problem of dynamic cast on Linux for Selection validators is solved
[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 <BRepCheck_Analyzer.hxx>
16 #include <TopExp_Explorer.hxx>
17 #include <TopTools_ListOfShape.hxx>
18
19 //=================================================================================================
20 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
21                                          const ListOfShape& theTools,
22                                          const OperationType theOperationType)
23 : myDone(false)
24 {
25   build(theObjects, theTools, theOperationType);
26 }
27
28
29 //=================================================================================================
30 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
31                                 const ListOfShape& theTools,
32                                 const OperationType theOperationType)
33 {
34   if(theObjects.empty() || theTools.empty()) {
35     return;
36   }
37
38   // Getting objects.
39   TopTools_ListOfShape anObjects;
40   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
41   {
42     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
43   }
44
45   // Getting tools.
46   TopTools_ListOfShape aTools;
47   for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
48   {
49     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
50   }
51
52   // Creating boolean operation.
53   BRepAlgoAPI_BooleanOperation* anOperation;
54   switch (theOperationType) {
55     case BOOL_CUT: {
56       anOperation = new BRepAlgoAPI_Cut();
57       break;
58     }
59     case BOOL_FUSE: {
60       anOperation = new BRepAlgoAPI_Fuse();
61       break;
62     }
63     case BOOL_COMMON: {
64       anOperation = new BRepAlgoAPI_Common();
65       break;
66     }
67     default: {
68       return;
69     }
70   }
71   myMkShape.reset(new GeomAlgoAPI_MakeShape(anOperation));
72   anOperation->SetArguments(anObjects);
73   anOperation->SetTools(aTools);
74
75   // Building and getting result.
76   anOperation->Build();
77   myDone = anOperation->IsDone() == Standard_True;
78   if(!myDone) {
79     return;
80   }
81   TopoDS_Shape aResult = anOperation->Shape();
82
83   if(aResult.ShapeType() == TopAbs_COMPOUND) {
84     aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
85   }
86
87   // fill data map to keep correct orientation of sub-shapes
88   myMap.reset(new GeomAPI_DataMapOfShapeShape());
89   for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
90     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
91     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
92     myMap->bind(aCurrentShape, aCurrentShape);
93   }
94   myShape.reset(new GeomAPI_Shape());
95   myShape->setImpl(new TopoDS_Shape(aResult));
96
97 }
98
99 //=================================================================================================
100 const bool GeomAlgoAPI_Boolean::isDone() const
101 {
102   return myDone;
103 }
104
105 //=================================================================================================
106 const bool GeomAlgoAPI_Boolean::isValid() const
107 {
108   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
109   return (aChecker.IsValid() == Standard_True);
110 }
111
112 //=================================================================================================
113 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape() const
114 {
115   return myShape;
116 }
117
118 //=================================================================================================
119 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Boolean::mapOfShapes() const
120 {
121   return myMap;
122 }
123
124 //=================================================================================================
125 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Boolean::makeShape() const
126 {
127   return myMkShape;
128 }