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