Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Extrusion.cpp
1 // File:        GeomAlgoAPI_Extrusion.cpp
2 // Created:     06 Jun 2014
3 // Author:      Artem ZHIDKOV
4
5 #include <GeomAlgoAPI_Extrusion.h>
6 #include <GeomAlgoAPI_MakeShape.h>
7 #include <GeomAlgoAPI_DFLoader.h>
8 #include <GeomAlgoAPI_MakeShape.h>
9 #include <gp_Pln.hxx>
10 #include <BRepPrimAPI_MakePrism.hxx>
11 #include <BRepBuilderAPI_MakeShape.hxx>
12 #include <Geom_Plane.hxx>
13 #include <Geom_Surface.hxx>
14 #include <TopExp_Explorer.hxx>
15 #include <BRepCheck_Analyzer.hxx>
16 #include <GProp_GProps.hxx>
17 #include <BRepGProp.hxx>
18 #include <TopoDS.hxx>
19 #include <TopTools_ListIteratorOfListOfShape.hxx>
20 #include <Precision.hxx>
21 #include <TDF_TagSource.hxx>
22 #include <boost/shared_ptr.hpp>
23 #include <BRepPrimAPI_MakePrism.hxx>
24 #include <TopoDS_Shape.hxx>
25
26 const double tolerance = Precision::Angular();
27 // Constructor
28 GeomAlgoAPI_Extrusion::GeomAlgoAPI_Extrusion(
29   boost::shared_ptr<GeomAPI_Shape> theBasis, double theSize)
30 : mySize(theSize), myDone(false), myShape(new GeomAPI_Shape()),
31   myFirst(new GeomAPI_Shape()), myLast(new GeomAPI_Shape())
32 {
33   build(theBasis);
34 }
35
36 //============================================================================
37 void GeomAlgoAPI_Extrusion::build(const boost::shared_ptr<GeomAPI_Shape>& theBasis)
38 {
39   bool isFirstNorm = true;
40   gp_Dir aShapeNormal;
41
42   TopoDS_Face aBasis = TopoDS::Face(theBasis->impl<TopoDS_Shape>());
43   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(
44     BRep_Tool::Surface(aBasis));
45   if (aPlane.IsNull())  // non-planar shapes is not supported for extrusion yet
46     return;
47
48   const gp_Dir& aNormal = aPlane->Pln().Axis().Direction();  
49   gp_Vec aVec(aNormal);
50   aVec = aVec * mySize;
51
52   BRepPrimAPI_MakePrism* aBuilder = new BRepPrimAPI_MakePrism(aBasis, aVec);
53   if(aBuilder) {
54     setImpl(aBuilder);
55     myDone = aBuilder->IsDone() == Standard_True;
56     if (myDone) {
57       BRepCheck_Analyzer aChecker(aBuilder->Shape());
58       myDone = aChecker.IsValid() == Standard_True;
59     }
60     if(myDone) {
61       TopoDS_Shape aResult;
62       if(aBuilder->Shape().ShapeType() == TopAbs_COMPOUND) 
63         aResult = GeomAlgoAPI_DFLoader::refineResult(aBuilder->Shape());
64       else
65         aResult = aBuilder->Shape();
66         
67         for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
68            boost::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
69        aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
70            myMap.bind(aCurrentShape, aCurrentShape);
71         }   
72  
73       myShape->setImpl(new TopoDS_Shape(aResult));
74       myFirst->setImpl(new TopoDS_Shape(aBuilder->FirstShape()));
75       myLast->setImpl(new TopoDS_Shape(aBuilder-> LastShape()));
76           myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
77     }
78   }     
79 }
80
81 //============================================================================
82 const bool GeomAlgoAPI_Extrusion::isDone() const
83 {return myDone;}
84
85 //============================================================================
86 const bool GeomAlgoAPI_Extrusion::isValid() const
87 {
88   return myDone;
89 }
90
91 //============================================================================
92 const bool GeomAlgoAPI_Extrusion::hasVolume() const
93 {
94   bool hasVolume(false);
95   if(isValid()) {
96     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
97     GProp_GProps aGProp;
98     BRepGProp::VolumeProperties(aRShape, aGProp);
99     if(aGProp.Mass() > Precision::Confusion()) 
100       hasVolume = true; 
101   }
102   return hasVolume;
103 }
104
105 //============================================================================
106 const boost::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Extrusion::shape () const 
107 {return myShape;}
108
109 //============================================================================
110 /*void GeomAlgoAPI_Extrusion::generated(
111   const boost::shared_ptr<GeomAPI_Shape> theShape, ListOfShape& theHistory)
112 {
113   theHistory.clear();
114   if(myDone) {
115     const TopTools_ListOfShape& aList = implPtr<BRepPrimAPI_MakePrism>()
116       ->Generated(theShape->impl<TopoDS_Shape>());
117     TopTools_ListIteratorOfListOfShape it(aList);
118     for(;it.More();it.Next()) {
119       boost::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
120       aShape->setImpl(&(it.Value()));
121       theHistory.push_back(aShape);
122     }
123   }
124 }
125 */
126 //============================================================================
127 const boost::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Extrusion::firstShape()
128 {
129   return myFirst;
130 }
131
132 //============================================================================
133 const boost::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Extrusion::lastShape()
134 {
135   return myLast;
136 }
137
138 //============================================================================
139 void GeomAlgoAPI_Extrusion::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
140 {
141   theMap = myMap;
142 }
143
144 //============================================================================
145 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Extrusion::makeShape() const
146 {
147   return myMkShape;
148 }
149
150 //============================================================================
151 GeomAlgoAPI_Extrusion::~GeomAlgoAPI_Extrusion()
152 {
153   if (myImpl) {    
154         myMap.clear();
155         //delete myImpl;
156   }
157 }