Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_FaceBuilder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_FaceBuilder.cpp
4 // Created:     23 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <GeomAlgoAPI_FaceBuilder.h>
8 #include <gp_Pln.hxx>
9 #include <BRepBuilderAPI_MakeFace.hxx>
10 #include <TopoDS_Face.hxx>
11 #include <TopoDS.hxx>
12 #include <BRep_Tool.hxx>
13 #include <Geom_Plane.hxx>
14
15
16 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::square(
17     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
18     const double theSize)
19 {
20   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
21   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
22   gp_Pln aPlane(aCenter, aDir);
23   // half of the size in each direction from the center
24   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, -theSize / 2., theSize / 2., -theSize / 2.,
25                                        theSize / 2.);
26   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
27   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
28   return aRes;
29 }
30
31 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::square(
32     std::shared_ptr<GeomAPI_Pln> thePlane,
33     const double theSize)
34 {
35   // half of the size in each direction from the center
36   BRepBuilderAPI_MakeFace aFaceBuilder(thePlane->impl<gp_Pln>(),
37                                        -theSize / 2., theSize / 2.,
38                                        -theSize / 2., theSize / 2.);
39   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
40   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
41   return aRes;
42 }
43
44 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_FaceBuilder::plane(
45     std::shared_ptr<GeomAPI_Shape> theFace)
46 {
47   std::shared_ptr<GeomAPI_Pln> aResult;
48   if (!theFace)
49     return aResult;  // bad shape
50   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
51   if (aShape.IsNull())
52     return aResult;  // null shape
53   if (aShape.ShapeType() != TopAbs_FACE)
54     return aResult;  // not face
55   TopoDS_Face aFace = TopoDS::Face(aShape);
56   if (aFace.IsNull())
57     return aResult;  // not face
58   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
59   if (aSurf.IsNull())
60     return aResult;  // no surface
61   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurf);
62   if (aPlane.IsNull())
63     return aResult;  // not planar
64   double aA, aB, aC, aD;
65   aPlane->Coefficients(aA, aB, aC, aD);
66   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
67   return aResult;
68 }
69
70 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::planarFace(std::shared_ptr<GeomAPI_Pln> thePlane,
71              double theX, double theY,
72              double theWidth, double theHeight)
73 {
74   double aA, aB, aC, aD;
75   thePlane->coefficients(aA, aB, aC, aD);
76   gp_Pln aPlane(aA, aB, aC, aD);
77
78   // half of the size in each direction from the center
79   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth, 
80                                        theY, theY + theHeight);
81   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
82   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
83   return aRes;
84 }