Salome HOME
Eliminate some Visual Studio warnings
[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 <BRepAlgoAPI_BooleanOperation.hxx>
10 #include <BRepAlgoAPI_Common.hxx>
11 #include <BRepAlgoAPI_Cut.hxx>
12 #include <BRepAlgoAPI_Fuse.hxx>
13 #include <BRepCheck_Analyzer.hxx>
14 #include <TopExp_Explorer.hxx>
15 #include <TopTools_ListOfShape.hxx>
16
17 //=================================================================================================
18 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCut(const ListOfShape& theObjects,
19                                                             const ListOfShape& theTools)
20 {
21   GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_CUT);
22   if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
23     return aBoolAlgo.shape();
24   }
25   return std::shared_ptr<GeomAPI_Shape>();
26 }
27
28 //=================================================================================================
29 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeFuse(const ListOfShape& theObjects,
30                                                              const ListOfShape& theTools)
31 {
32   GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_FUSE);
33   if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
34     return aBoolAlgo.shape();
35   }
36   return std::shared_ptr<GeomAPI_Shape>();
37 }
38
39 //=================================================================================================
40 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCommon(const ListOfShape& theObjects,
41                                                                const ListOfShape& theTools)
42 {
43   GeomAlgoAPI_Boolean aBoolAlgo(theObjects, theTools, BOOL_COMMON);
44   if(aBoolAlgo.isDone() && !aBoolAlgo.shape()->isNull() && aBoolAlgo.isValid()) {
45     return aBoolAlgo.shape();
46   }
47   return std::shared_ptr<GeomAPI_Shape>();
48 }
49
50 //=================================================================================================
51 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(const ListOfShape& theObjects,
52                                          const ListOfShape& theTools,
53                                          const OperationType theOperationType)
54 : myDone(false),
55   myShape(new GeomAPI_Shape()),
56   myMap(new GeomAPI_DataMapOfShapeShape()),
57   myMkShape(new GeomAlgoAPI_MakeShape())
58 {
59   build(theObjects, theTools, theOperationType);
60 }
61
62
63 //=================================================================================================
64 void GeomAlgoAPI_Boolean::build(const ListOfShape& theObjects,
65                                 const ListOfShape& theTools,
66                                 const OperationType theOperationType)
67 {
68   if(theObjects.empty() || theTools.empty()) {
69     return;
70   }
71
72   // Getting objects.
73   TopTools_ListOfShape anObjects;
74   for(ListOfShape::const_iterator anObjectsIt = theObjects.begin(); anObjectsIt != theObjects.end(); anObjectsIt++)
75   {
76     anObjects.Append((*anObjectsIt)->impl<TopoDS_Shape>());
77   }
78
79   // Getting tools.
80   TopTools_ListOfShape aTools;
81   for(ListOfShape::const_iterator aToolsIt = theTools.begin(); aToolsIt != theTools.end(); aToolsIt++)
82   {
83     aTools.Append((*aToolsIt)->impl<TopoDS_Shape>());
84   }
85
86   // Creating boolean operation.
87   BRepAlgoAPI_BooleanOperation* anOperation;
88   switch (theOperationType) {
89     case BOOL_CUT: {
90       anOperation = new BRepAlgoAPI_Cut();
91       break;
92     }
93     case BOOL_FUSE: {
94       anOperation = new BRepAlgoAPI_Fuse();
95       break;
96     }
97     case BOOL_COMMON: {
98       anOperation = new BRepAlgoAPI_Common();
99       break;
100     }
101     default: {
102       return;
103     }
104   }
105   myMkShape->setImpl(anOperation);
106   anOperation->SetArguments(anObjects);
107   anOperation->SetTools(aTools);
108
109   // Building and getting result.
110   anOperation->Build();
111   myDone = anOperation->IsDone() == Standard_True;
112   if(!myDone) {
113     return;
114   }
115   TopoDS_Shape aResult = anOperation->Shape();
116
117   // fill data map to keep correct orientation of sub-shapes
118   for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
119     std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
120     aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
121     myMap->bind(aCurrentShape, aCurrentShape);
122   }
123   myShape->setImpl(new TopoDS_Shape(aResult));
124
125 }
126
127 //=================================================================================================
128 const bool GeomAlgoAPI_Boolean::isDone() const
129 {
130   return myDone;
131 }
132
133 //=================================================================================================
134 const bool GeomAlgoAPI_Boolean::isValid() const
135 {
136   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
137   return (aChecker.IsValid() == Standard_True);
138 }
139
140 //=================================================================================================
141 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape() const
142 {
143   return myShape;
144 }
145
146 //=================================================================================================
147 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Boolean::mapOfShapes() const
148 {
149   return myMap;
150 }
151
152 //=================================================================================================
153 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Boolean::makeShape() const
154 {
155   return myMkShape;
156 }