Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.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 <BRepAlgoAPI_Cut.hxx>
10 #include <BRepAlgoAPI_Common.hxx>
11 #include <BRepAlgoAPI_Fuse.hxx>
12 #include <BRepCheck_Analyzer.hxx>
13 #include <TopExp_Explorer.hxx>
14 #include <GeomAlgoAPI_DFLoader.h>
15
16 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCut(
17   std::shared_ptr<GeomAPI_Shape> theShape,
18   std::shared_ptr<GeomAPI_Shape> theTool)
19 {
20   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
21   const TopoDS_Shape& aTool = theTool->impl<TopoDS_Shape>();
22
23   BRepAlgoAPI_Cut aCut(aShape, aTool);
24   if (aCut.IsDone()) {
25     std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
26     aResult->setImpl(new TopoDS_Shape(aCut.Shape()));
27     return aResult;
28   }
29   return std::shared_ptr<GeomAPI_Shape>();
30 }
31
32
33 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeFuse(
34   std::shared_ptr<GeomAPI_Shape> theShape,
35   std::shared_ptr<GeomAPI_Shape> theTool)
36 {
37   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
38   const TopoDS_Shape& aTool = theTool->impl<TopoDS_Shape>();
39
40   BRepAlgoAPI_Fuse aFuse(aShape, aTool);
41   if (aFuse.IsDone()) {
42     std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
43     aResult->setImpl(new TopoDS_Shape(aFuse.Shape()));
44     return aResult;
45   }
46   return std::shared_ptr<GeomAPI_Shape>();
47 }
48
49
50 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Boolean::makeCommon(
51   std::shared_ptr<GeomAPI_Shape> theShape,
52   std::shared_ptr<GeomAPI_Shape> theTool)
53 {
54   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
55   const TopoDS_Shape& aTool = theTool->impl<TopoDS_Shape>();
56
57   BRepAlgoAPI_Common aCommon(aShape, aTool);
58   if (aCommon.IsDone()) {
59     std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
60     aResult->setImpl(new TopoDS_Shape(aCommon.Shape()));
61     return aResult;
62   }
63   return std::shared_ptr<GeomAPI_Shape>();
64 }
65
66 //============================================================================
67 GeomAlgoAPI_Boolean::GeomAlgoAPI_Boolean(std::shared_ptr<GeomAPI_Shape> theObject,
68                                          std::shared_ptr<GeomAPI_Shape> theTool,
69                                          int theType)
70 : myOperation(theType), myDone(false), myShape(new GeomAPI_Shape())
71 {
72   build(theObject, theTool);
73 }
74
75
76 //============================================================================
77 void GeomAlgoAPI_Boolean::build(std::shared_ptr<GeomAPI_Shape> theObject,
78                                 std::shared_ptr<GeomAPI_Shape> theTool)
79 {
80   const TopoDS_Shape& anObject = theObject->impl<TopoDS_Shape>();
81   const TopoDS_Shape& aTool    = theTool->impl<TopoDS_Shape>();
82   TopoDS_Shape aResult;
83   switch (myOperation) {
84   case BOOL_FUSE: 
85         {
86           BRepAlgoAPI_Fuse* mkFuse = new BRepAlgoAPI_Fuse(anObject, aTool);
87       if (mkFuse && mkFuse->IsDone()) {
88                 setImpl(mkFuse);
89                 myDone = mkFuse->IsDone() == Standard_True;
90                 myMkShape = new GeomAlgoAPI_MakeShape (mkFuse);
91                 aResult = mkFuse->Shape();//GeomAlgoAPI_DFLoader::refineResult(aFuse->Shape());      
92           }
93           break;
94         }
95   case BOOL_CUT:
96         {
97       BRepAlgoAPI_Cut* mkCut = new BRepAlgoAPI_Cut(anObject, aTool);
98       if (mkCut && mkCut->IsDone()) {
99                 setImpl(mkCut);
100                 myDone = mkCut->IsDone() == Standard_True;
101                 myMkShape = new GeomAlgoAPI_MakeShape (mkCut);
102                 aResult = mkCut->Shape();    
103           }
104           break;
105         }
106   case BOOL_COMMON:
107         {
108       BRepAlgoAPI_Common* mkCom = new BRepAlgoAPI_Common(anObject, aTool);
109       if (mkCom && mkCom->IsDone()) {
110                 setImpl(mkCom);
111                 myDone = mkCom->IsDone() == Standard_True;
112                 myMkShape = new GeomAlgoAPI_MakeShape (mkCom);
113                 aResult = mkCom->Shape(); 
114           }
115           break;
116         }       
117   }
118   if(myDone) {
119         if(aResult.ShapeType() == TopAbs_COMPOUND) 
120       aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
121         myShape->setImpl(new TopoDS_Shape(aResult));
122         std::shared_ptr<GeomAPI_Shape> aGeomResult(new GeomAPI_Shape());
123         aGeomResult->setImpl(new TopoDS_Shape(aResult)); 
124
125         // fill data map to keep correct orientation of sub-shapes 
126         for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
127           std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
128       aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
129           myMap.bind(aCurrentShape, aCurrentShape);
130         }
131   }  
132 }
133
134
135 //============================================================================
136 const bool GeomAlgoAPI_Boolean::isDone() const
137 {return myDone;}
138
139 //============================================================================
140 const bool GeomAlgoAPI_Boolean::isValid() const
141 {
142   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
143   return (aChecker.IsValid() == Standard_True);
144 }
145
146 //============================================================================
147 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Boolean::shape () const 
148 {
149   return myShape;
150 }
151
152 //============================================================================
153 void GeomAlgoAPI_Boolean::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
154 {
155   theMap = myMap;
156 }
157
158 //============================================================================
159 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Boolean::makeShape() const
160 {
161   return myMkShape;
162 }
163
164 //============================================================================
165 GeomAlgoAPI_Boolean::~GeomAlgoAPI_Boolean()
166 {
167   if (myImpl) {    
168         myMap.clear();
169   }
170 }