Salome HOME
Boost has been removed from code
[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 <memory>
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   std::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 std::shared_ptr<GeomAPI_Shape>& theBasis)
38 {
39   bool isFirstNorm = true;
40   gp_Dir aShapeNormal;
41   TopoDS_Face aBasis = TopoDS::Face(theBasis->impl<TopoDS_Shape>());
42   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(
43     BRep_Tool::Surface(aBasis));
44   if (aPlane.IsNull())  // non-planar shapes is not supported for extrusion yet
45     return;
46
47   const gp_Dir& aNormal = aPlane->Pln().Axis().Direction();  
48   gp_Vec aVec(aNormal);
49   aVec = aVec * mySize;
50
51   BRepPrimAPI_MakePrism* aBuilder = new BRepPrimAPI_MakePrism(aBasis, aVec);
52   if(aBuilder) {
53     setImpl(aBuilder);
54     myDone = aBuilder->IsDone() == Standard_True;
55     if (myDone) {       
56       TopoDS_Shape aResult;
57       if(aBuilder->Shape().ShapeType() == TopAbs_COMPOUND) 
58         aResult = GeomAlgoAPI_DFLoader::refineResult(aBuilder->Shape());
59       else
60         aResult = aBuilder->Shape();
61           // fill data map to keep correct orientation of sub-shapes 
62           for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
63             std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
64         aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
65             myMap.bind(aCurrentShape, aCurrentShape);
66           }   
67       myShape->setImpl(new TopoDS_Shape(aResult));
68       myFirst->setImpl(new TopoDS_Shape(aBuilder->FirstShape()));
69       myLast->setImpl(new TopoDS_Shape(aBuilder-> LastShape()));
70           myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
71         }  
72   }
73 }
74
75 //============================================================================
76 const bool GeomAlgoAPI_Extrusion::isDone() const
77 {return myDone;}
78
79 //============================================================================
80 const bool GeomAlgoAPI_Extrusion::isValid() const
81 {
82         BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
83     return (aChecker.IsValid() == Standard_True);
84 }
85
86 //============================================================================
87 const bool GeomAlgoAPI_Extrusion::hasVolume() const
88 {
89   bool hasVolume(false);
90   if(isValid()) {
91     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
92     GProp_GProps aGProp;
93     BRepGProp::VolumeProperties(aRShape, aGProp);
94     if(aGProp.Mass() > Precision::Confusion()) 
95       hasVolume = true; 
96   }
97   return hasVolume;
98 }
99
100 //============================================================================
101 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Extrusion::shape () const 
102 {
103   return myShape;
104 }
105
106 //============================================================================
107 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Extrusion::firstShape()
108 {
109   return myFirst;
110 }
111
112 //============================================================================
113 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Extrusion::lastShape()
114 {
115   return myLast;
116 }
117
118 //============================================================================
119 void GeomAlgoAPI_Extrusion::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
120 {
121   theMap = myMap;
122 }
123
124 //============================================================================
125 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Extrusion::makeShape() const
126 {
127   return myMkShape;
128 }
129
130 //============================================================================
131 GeomAlgoAPI_Extrusion::~GeomAlgoAPI_Extrusion()
132 {
133   if (myImpl) {    
134         myMap.clear();
135   }
136 }