Salome HOME
Make plane as close as possible to selected shape by its size
[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 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::square(
16     std::shared_ptr<GeomAPI_Pnt> theCenter, std::shared_ptr<GeomAPI_Dir> theNormal,
17     const double theSize)
18 {
19   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
20   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
21   gp_Pln aPlane(aCenter, aDir);
22   // half of the size in each direction from the center
23   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, -theSize / 2., theSize / 2., -theSize / 2.,
24                                        theSize / 2.);
25   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
26   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
27   return aRes;
28 }
29
30 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_FaceBuilder::plane(
31     std::shared_ptr<GeomAPI_Shape> theFace)
32 {
33   std::shared_ptr<GeomAPI_Pln> aResult;
34   if (!theFace)
35     return aResult;  // bad shape
36   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
37   if (aShape.IsNull())
38     return aResult;  // null shape
39   if (aShape.ShapeType() != TopAbs_FACE)
40     return aResult;  // not face
41   TopoDS_Face aFace = TopoDS::Face(aShape);
42   if (aFace.IsNull())
43     return aResult;  // not face
44   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
45   if (aSurf.IsNull())
46     return aResult;  // no surface
47   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurf);
48   if (aPlane.IsNull())
49     return aResult;  // not planar
50   double aA, aB, aC, aD;
51   aPlane->Coefficients(aA, aB, aC, aD);
52   aResult = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
53   return aResult;
54 }
55
56
57 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_FaceBuilder::
58   planarFace(std::shared_ptr<GeomAPI_Pln> thePlane,
59              double theX, double theY,
60              double theWidth, double theHeight)
61 {
62   double aA, aB, aC, aD;
63   thePlane->coefficients(aA, aB, aC, aD);
64   gp_Pln aPlane(aA, aB, aC, aD);
65
66   // half of the size in each direction from the center
67   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth, 
68                                        theY, theY + theHeight);
69   std::shared_ptr<GeomAPI_Shape> aRes(new GeomAPI_Shape);
70   aRes->setImpl(new TopoDS_Shape(aFaceBuilder.Face()));
71   return aRes;
72 }