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