]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_Extrusion.cpp
Salome HOME
Issue #110: make only faces are used for extrusion to generate solids only
[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 #include <BRep_Builder.hxx>
14
15 #include <Precision.hxx>
16 const double tolerance = Precision::Angular();
17
18 boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Extrusion::makeExtrusion(
19     boost::shared_ptr<GeomAPI_Shape> theShape, 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, double theSize)
36 {
37   bool isFirstNorm = true;
38   gp_Dir aShapeNormal;
39
40   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
41   TopExp_Explorer aFaceExp(aShape, TopAbs_FACE);
42   TopoDS_Compound aFaces; // use only faces from the shape: make compound for this
43   BRep_Builder aBuilder;
44   aBuilder.MakeCompound(aFaces);
45   for (; aFaceExp.More(); aFaceExp.Next()) {
46     const TopoDS_Face& aFace = (const TopoDS_Face&) aFaceExp.Current();
47     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aFace));
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     aBuilder.Add(aFaces, aFace);
58   }
59   if (aFaces.IsNull())
60     return boost::shared_ptr<GeomAPI_Shape>();
61
62   boost::shared_ptr<GeomAPI_Dir> aDir(
63       new GeomAPI_Dir(aShapeNormal.X(), aShapeNormal.Y(), aShapeNormal.Z()));
64
65   boost::shared_ptr<GeomAPI_Shape> aFacesShape(new (GeomAPI_Shape));
66   aFacesShape->setImpl(new TopoDS_Shape(aFaces));
67   return GeomAlgoAPI_Extrusion::makeExtrusion(aFacesShape, aDir, theSize);
68 }