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