Salome HOME
Fix for GeomAlgoAPI_FaceBuidler method name change.
[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 #include <GeomLib_IsPlanarSurface.hxx>
15
16
17 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::square(
18     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
19     const double theSize)
20 {
21   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
22   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
23   gp_Pln aPlane(aCenter, aDir);
24   // half of the size in each direction from the center
25   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, -theSize / 2., theSize / 2., -theSize / 2.,
26                                        theSize / 2.);
27   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
28   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
29   return aRes;
30 }
31
32 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::square(
33     std::shared_ptr<GeomAPI_Pln> thePlane,
34     const double theSize)
35 {
36   // half of the size in each direction from the center
37   BRepBuilderAPI_MakeFace aFaceBuilder(thePlane->impl<gp_Pln>(),
38                                        -theSize / 2., theSize / 2.,
39                                        -theSize / 2., theSize / 2.);
40   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
41   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
42   return aRes;
43 }
44
45 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_FaceBuilder::plane(
46     std::shared_ptr<GeomAPI_Shape> theFace)
47 {
48   std::shared_ptr<GeomAPI_Pln> aResult;
49   if (!theFace)
50     return aResult;  // bad shape
51   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
52   if (aShape.IsNull())
53     return aResult;  // null shape
54   if (aShape.ShapeType() != TopAbs_FACE)
55     return aResult;  // not face
56   TopoDS_Face aFace = TopoDS::Face(aShape);
57   if (aFace.IsNull())
58     return aResult;  // not face
59   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
60   if (aSurf.IsNull())
61     return aResult;  // no surface
62   GeomLib_IsPlanarSurface isPlanar(aSurf);
63   if(!isPlanar.IsPlanar()) {
64     return aResult;
65   }
66   gp_Pln aPln = isPlanar.Plan();
67   double aA, aB, aC, aD;
68   aPln.Coefficients(aA, aB, aC, aD);
69   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
70   return aResult;
71 }
72
73 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::planarFace(std::shared_ptr<GeomAPI_Ax1> theAxis)
74 {
75   return planarFace(theAxis->origin(), theAxis->dir());
76 }
77
78 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::planarFace(std::shared_ptr<GeomAPI_Pnt> theCenter,
79                                                                    std::shared_ptr<GeomAPI_Dir> theNormal)
80 {
81   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
82   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
83   gp_Pln aPlane(aCenter, aDir);
84   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane);
85   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
86   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
87   return aRes;
88 }
89
90 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::planarFace(std::shared_ptr<GeomAPI_Pln> thePlane,
91              double theX, double theY,
92              double theWidth, double theHeight)
93 {
94   double aA, aB, aC, aD;
95   thePlane->coefficients(aA, aB, aC, aD);
96   gp_Pln aPlane(aA, aB, aC, aD);
97
98   // half of the size in each direction from the center
99   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth, 
100                                        theY, theY + theHeight);
101   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
102   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
103   return aRes;
104 }