Salome HOME
Issue #1834: Fix length of lines
[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
9 #include <GeomAPI_Face.h>
10 #include <GeomAPI_Dir.h>
11 #include <GeomAPI_Pln.h>
12 #include <GeomAPI_Pnt.h>
13 #include <GeomAPI_Shape.h>
14 #include <GeomAPI_Vertex.h>
15
16 #include <BRep_Tool.hxx>
17 #include <BRepBuilderAPI_MakeFace.hxx>
18 #include <GC_MakePlane.hxx>
19 #include <Geom_Plane.hxx>
20 #include <gp_Pln.hxx>
21 #include <TopoDS.hxx>
22 #include <TopoDS_Face.hxx>
23
24 //==================================================================================================
25 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::squareFace(
26   const std::shared_ptr<GeomAPI_Pnt> theCenter,
27   const std::shared_ptr<GeomAPI_Dir> theNormal,
28   const double theSize)
29 {
30   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
31   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
32   gp_Pln aPlane(aCenter, aDir);
33   // half of the size in each direction from the center
34   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, -theSize / 2., theSize / 2., -theSize / 2.,
35                                        theSize / 2.);
36   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
37   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
38   return aRes;
39 }
40
41 //==================================================================================================
42 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::squareFace(
43   const std::shared_ptr<GeomAPI_Pln> thePlane,
44   const double theSize)
45 {
46   // half of the size in each direction from the center
47   BRepBuilderAPI_MakeFace aFaceBuilder(thePlane->impl<gp_Pln>(),
48                                        -theSize / 2., theSize / 2.,
49                                        -theSize / 2., theSize / 2.);
50   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
51   const TopoDS_Face& aFace = aFaceBuilder.Face();
52   aRes->setImpl(new TopoDS_Face(aFace));
53   return aRes;
54 }
55
56 //==================================================================================================
57 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFace(
58   const std::shared_ptr<GeomAPI_Pnt> theCenter,
59   const std::shared_ptr<GeomAPI_Dir> theNormal)
60 {
61   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
62   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
63   gp_Pln aPlane(aCenter, aDir);
64   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane);
65   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
66   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
67   return aRes;
68 }
69
70 //==================================================================================================
71 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFace(
72   const std::shared_ptr<GeomAPI_Pln> thePlane,
73   const double theX, const double theY,
74   const double theWidth, const double theHeight)
75 {
76   double aA, aB, aC, aD;
77   thePlane->coefficients(aA, aB, aC, aD);
78   gp_Pln aPlane(aA, aB, aC, aD);
79
80   // half of the size in each direction from the center
81   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth, 
82                                        theY, theY + theHeight);
83   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
84   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
85   return aRes;
86 }
87
88 //==================================================================================================
89 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(
90     const std::shared_ptr<GeomAPI_Vertex> theVertex1,
91     const std::shared_ptr<GeomAPI_Vertex> theVertex2,
92     const std::shared_ptr<GeomAPI_Vertex> theVertex3)
93 {
94   gp_Pnt aPnt1 = theVertex1->point()->impl<gp_Pnt>();
95   gp_Pnt aPnt2 = theVertex2->point()->impl<gp_Pnt>();
96   gp_Pnt aPnt3 = theVertex3->point()->impl<gp_Pnt>();
97
98   std::shared_ptr<GeomAPI_Face> aFace;
99   GC_MakePlane aMakePlane(aPnt1, aPnt2, aPnt3);
100   if(!aMakePlane.IsDone()) {
101     return aFace;
102   }
103
104   BRepBuilderAPI_MakeFace aMakeFace(aMakePlane.Value()->Pln());
105   aFace.reset(new GeomAPI_Face());
106   aFace->setImpl(new TopoDS_Face(aMakeFace.Face()));
107   return aFace;
108 }
109
110 //==================================================================================================
111 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFaceByFaceAndVertex(
112     const std::shared_ptr<GeomAPI_Face> theFace,
113     const std::shared_ptr<GeomAPI_Vertex> theVertex)
114 {
115   gp_Pln aPln = theFace->getPlane()->impl<gp_Pln>();
116   gp_Pnt aPnt = theVertex->point()->impl<gp_Pnt>();
117
118   std::shared_ptr<GeomAPI_Face> aFace;
119   GC_MakePlane aMakePlane(aPln, aPnt);
120   if(!aMakePlane.IsDone()) {
121     return aFace;
122   }
123
124   BRepBuilderAPI_MakeFace aMakeFace(aMakePlane.Value()->Pln());
125   aFace.reset(new GeomAPI_Face());
126   aFace->setImpl(new TopoDS_Face(aMakeFace.Face()));
127   return aFace;
128 }