Salome HOME
Merge branch 'master' of newgeom:newgeom
[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, 
22     -theSize / 2., theSize / 2., -theSize / 2., 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) return aResult; // bad shape
33   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
34   if (aShape.IsNull()) return aResult; // null shape
35   TopoDS_Face aFace = TopoDS::Face(aShape);
36   if (aFace.IsNull()) return aResult; // not face
37   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
38   if (aSurf.IsNull()) return aResult; // no surface
39   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurf);
40   if (aPlane.IsNull()) return aResult; // not planar
41   double aA, aB, aC, aD;
42   aPlane->Coefficients(aA, aB, aC, aD);
43   aResult = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
44   return aResult;
45 }