Salome HOME
Sources formated according to the codeing standards
[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, boost::shared_ptr<GeomAPI_Dir> theDir,
19     double theSize)
20 {
21   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
22   gp_Vec aDir(theDir->impl<gp_Dir>().XYZ() * theSize);
23
24   TopoDS_Shape aPrism = BRepPrimAPI_MakePrism(aShape, aDir);
25   if (aPrism.IsNull())
26     return boost::shared_ptr<GeomAPI_Shape>();
27
28   boost::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape());
29   aResult->setImpl(new TopoDS_Shape(aPrism));
30   return aResult;
31 }
32
33 boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Extrusion::makeExtrusion(
34     boost::shared_ptr<GeomAPI_Shape> theShape, double theSize)
35 {
36   bool isFirstNorm = true;
37   gp_Dir aShapeNormal;
38
39   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
40   TopExp_Explorer aFaceExp(aShape, TopAbs_FACE);
41   for (; aFaceExp.More(); aFaceExp.Next()) {
42     const TopoDS_Face& aFace = (const TopoDS_Face&) aFaceExp.Current();
43     Handle(BRep_TFace) aBFace = Handle(BRep_TFace)::DownCast(aFace.TShape());
44     if (aBFace.IsNull())
45       return boost::shared_ptr<GeomAPI_Shape>();
46
47     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aBFace->Surface());
48     if (aPlane.IsNull())  // non-planar shapes is not supported for extrusion yet
49       continue;
50
51     const gp_Dir& aNormal = aPlane->Pln().Axis().Direction();
52     if (isFirstNorm) {
53       aShapeNormal = aNormal;
54       isFirstNorm = false;
55     } else if (!aShapeNormal.IsEqual(aNormal, tolerance))  // non-planar shapes is not supported for extrusion yet
56       return boost::shared_ptr<GeomAPI_Shape>();
57   }
58
59   boost::shared_ptr<GeomAPI_Dir> aDir(
60       new GeomAPI_Dir(aShapeNormal.X(), aShapeNormal.Y(), aShapeNormal.Z()));
61
62   return GeomAlgoAPI_Extrusion::makeExtrusion(theShape, aDir, theSize);
63 }