Salome HOME
Make selection naming works wit hsketch faces (added sub-edges in naming structure)
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_FaceBuilder.cpp
1 // File:        GeomAlgoAPI_FaceBuilder.cpp
2 // Created:     23 Apr 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <GeomAlgoAPI_FaceBuilder.h>
6 #include <gp_Pln.hxx>
7 #include <BRepBuilderAPI_MakeFace.hxx>
8 #include <TopoDS_Face.hxx>
9 #include <TopoDS.hxx>
10 #include <BRep_Tool.hxx>
11 #include <Geom_Plane.hxx>
12
13 boost::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::square(
14     boost::shared_ptr<GeomAPI_Pnt> theCenter, boost::shared_ptr<GeomAPI_Dir> theNormal,
15     const double theSize)
16 {
17   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
18   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
19   gp_Pln aPlane(aCenter, aDir);
20   // half of the size in each direction from the center
21   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, -theSize / 2., theSize / 2., -theSize / 2.,
22                                        theSize / 2.);
23   boost::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
24   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
25   return aRes;
26 }
27
28 boost::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_FaceBuilder::plane(
29     boost::shared_ptr<GeomAPI_Shape> theFace)
30 {
31   boost::shared_ptr<GeomAPI_Pln> aResult;
32   if (!theFace)
33     return aResult;  // bad shape
34   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
35   if (aShape.IsNull())
36     return aResult;  // null shape
37   if (aShape.ShapeType() != TopAbs_FACE)
38     return aResult;  // not face
39   TopoDS_Face aFace = TopoDS::Face(aShape);
40   if (aFace.IsNull())
41     return aResult;  // not face
42   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
43   if (aSurf.IsNull())
44     return aResult;  // no surface
45   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurf);
46   if (aPlane.IsNull())
47     return aResult;  // not planar
48   double aA, aB, aC, aD;
49   aPlane->Coefficients(aA, aB, aC, aD);
50   aResult = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
51   return aResult;
52 }