Salome HOME
Issue #555 - Make a number of shifted/rotated copies - selected object does not appea...
[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     const bool theInfinite)
36 {
37   // half of the size in each direction from the center
38   BRepBuilderAPI_MakeFace aFaceBuilder(thePlane->impl<gp_Pln>(),
39                                        -theSize / 2., theSize / 2.,
40                                        -theSize / 2., theSize / 2.);
41   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
42   TopoDS_Shape aFace = aFaceBuilder.Face();
43   if (theInfinite)
44     aFace.Infinite(Standard_True);
45   aRes->setImpl(new TopoDS_Shape(aFace/*aFaceBuilder.Face()*/));
46   return aRes;
47 }
48
49 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_FaceBuilder::plane(
50     std::shared_ptr<GeomAPI_Shape> theFace)
51 {
52   std::shared_ptr<GeomAPI_Pln> aResult;
53   if (!theFace)
54     return aResult;  // bad shape
55   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
56   if (aShape.IsNull())
57     return aResult;  // null shape
58   if (aShape.ShapeType() != TopAbs_FACE)
59     return aResult;  // not face
60   TopoDS_Face aFace = TopoDS::Face(aShape);
61   if (aFace.IsNull())
62     return aResult;  // not face
63   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
64   if (aSurf.IsNull())
65     return aResult;  // no surface
66   GeomLib_IsPlanarSurface isPlanar(aSurf);
67   if(!isPlanar.IsPlanar()) {
68     return aResult;
69   }
70   gp_Pln aPln = isPlanar.Plan();
71   double aA, aB, aC, aD;
72   aPln.Coefficients(aA, aB, aC, aD);
73   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
74   return aResult;
75 }
76
77 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::planarFace(std::shared_ptr<GeomAPI_Pnt> theCenter,
78                                                                    std::shared_ptr<GeomAPI_Dir> theNormal)
79 {
80   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
81   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
82   gp_Pln aPlane(aCenter, aDir);
83   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane);
84   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
85   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
86   return aRes;
87 }
88
89 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::planarFace(std::shared_ptr<GeomAPI_Pln> thePlane,
90              double theX, double theY,
91              double theWidth, double theHeight)
92 {
93   double aA, aB, aC, aD;
94   thePlane->coefficients(aA, aB, aC, aD);
95   gp_Pln aPlane(aA, aB, aC, aD);
96
97   // half of the size in each direction from the center
98   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth, 
99                                        theY, theY + theHeight);
100   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
101   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
102   return aRes;
103 }