Salome HOME
Refresh menu size after chnges in preferences
[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
7 #include <gp_Pln.hxx>
8
9 #include <BRepPrimAPI_MakePrism.hxx>
10 #include <Geom_Plane.hxx>
11 #include <Geom_Surface.hxx>
12 #include <TopExp_Explorer.hxx>
13
14 #include <Precision.hxx>
15 const double tolerance = Precision::Angular();
16
17 boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Extrusion::makeExtrusion(
18                         boost::shared_ptr<GeomAPI_Shape> theShape,
19                         boost::shared_ptr<GeomAPI_Dir>   theDir,
20                         double                           theSize)
21 {
22   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
23   gp_Vec aDir(theDir->impl<gp_Dir>().XYZ() * theSize);
24
25   TopoDS_Shape aPrism = BRepPrimAPI_MakePrism(aShape, aDir);
26   if (aPrism.IsNull())
27     return boost::shared_ptr<GeomAPI_Shape>();
28
29   boost::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
30   aResult->setImpl(new TopoDS_Shape(aPrism));
31   return aResult;
32 }
33
34 boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Extrusion::makeExtrusion(
35                         boost::shared_ptr<GeomAPI_Shape> theShape,
36                         double                           theSize)
37 {
38   bool isFirstNorm = true;
39   gp_Dir aShapeNormal;
40
41   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
42   TopExp_Explorer aFaceExp(aShape, TopAbs_FACE);
43   for ( ; aFaceExp.More(); aFaceExp.Next())
44   {
45     const TopoDS_Face& aFace = (const TopoDS_Face&)aFaceExp.Current();
46     Handle(BRep_TFace) aBFace = Handle(BRep_TFace)::DownCast(aFace.TShape());
47     if (aBFace.IsNull())
48       return boost::shared_ptr<GeomAPI_Shape>();
49
50     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aBFace->Surface());
51     if (aPlane.IsNull()) // non-planar shapes is not supported for extrusion yet
52       continue;
53
54     const gp_Dir& aNormal = aPlane->Pln().Axis().Direction();
55     if (isFirstNorm)
56     {
57       aShapeNormal = aNormal;
58       isFirstNorm = false;
59     }
60     else if (!aShapeNormal.IsEqual(aNormal, tolerance)) // non-planar shapes is not supported for extrusion yet
61       return boost::shared_ptr<GeomAPI_Shape>();
62   }
63
64   boost::shared_ptr<GeomAPI_Dir> aDir(
65     new GeomAPI_Dir(aShapeNormal.X(), aShapeNormal.Y(), aShapeNormal.Z()));
66
67   return GeomAlgoAPI_Extrusion::makeExtrusion(theShape, aDir, theSize);
68 }