Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_FaceBuilder.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_FaceBuilder.h"
22
23 #include <GeomAPI_Face.h>
24 #include <GeomAPI_Dir.h>
25 #include <GeomAPI_Pln.h>
26 #include <GeomAPI_Pnt.h>
27 #include <GeomAPI_Shape.h>
28 #include <GeomAPI_Vertex.h>
29
30 #include <BRep_Tool.hxx>
31 #include <BRepBuilderAPI_MakeFace.hxx>
32 #include <GC_MakePlane.hxx>
33 #include <Geom_Plane.hxx>
34 #include <gp_Pln.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Face.hxx>
37
38 //==================================================================================================
39 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::squareFace(
40   const std::shared_ptr<GeomAPI_Pnt> theCenter,
41   const std::shared_ptr<GeomAPI_Dir> theNormal,
42   const double theSize)
43 {
44   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
45   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
46   gp_Pln aPlane(aCenter, aDir);
47   // half of the size in each direction from the center
48   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, -theSize / 2., theSize / 2., -theSize / 2.,
49                                        theSize / 2.);
50   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
51   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
52   return aRes;
53 }
54
55 //==================================================================================================
56 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::squareFace(
57   const std::shared_ptr<GeomAPI_Pln> thePlane,
58   const double theSize)
59 {
60   // half of the size in each direction from the center
61   BRepBuilderAPI_MakeFace aFaceBuilder(thePlane->impl<gp_Pln>(),
62                                        -theSize / 2., theSize / 2.,
63                                        -theSize / 2., theSize / 2.);
64   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
65   const TopoDS_Face& aFace = aFaceBuilder.Face();
66   aRes->setImpl(new TopoDS_Face(aFace));
67   return aRes;
68 }
69
70 //==================================================================================================
71 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFace(
72   const std::shared_ptr<GeomAPI_Pnt> theCenter,
73   const std::shared_ptr<GeomAPI_Dir> theNormal)
74 {
75   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
76   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
77   gp_Pln aPlane(aCenter, aDir);
78   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane);
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::planarFace(
86   const std::shared_ptr<GeomAPI_Pln> thePlane,
87   const double theX, const double theY,
88   const double theWidth, const double theHeight)
89 {
90   double aA, aB, aC, aD;
91   thePlane->coefficients(aA, aB, aC, aD);
92   gp_Pln aPlane(aA, aB, aC, aD);
93
94   // half of the size in each direction from the center
95   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth,
96                                        theY, theY + theHeight);
97   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
98   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
99   return aRes;
100 }
101
102 //==================================================================================================
103 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(
104     const std::shared_ptr<GeomAPI_Vertex> theVertex1,
105     const std::shared_ptr<GeomAPI_Vertex> theVertex2,
106     const std::shared_ptr<GeomAPI_Vertex> theVertex3)
107 {
108   gp_Pnt aPnt1 = theVertex1->point()->impl<gp_Pnt>();
109   gp_Pnt aPnt2 = theVertex2->point()->impl<gp_Pnt>();
110   gp_Pnt aPnt3 = theVertex3->point()->impl<gp_Pnt>();
111
112   std::shared_ptr<GeomAPI_Face> aFace;
113   GC_MakePlane aMakePlane(aPnt1, aPnt2, aPnt3);
114   if(!aMakePlane.IsDone()) {
115     return aFace;
116   }
117
118   BRepBuilderAPI_MakeFace aMakeFace(aMakePlane.Value()->Pln());
119   aFace.reset(new GeomAPI_Face());
120   aFace->setImpl(new TopoDS_Face(aMakeFace.Face()));
121   return aFace;
122 }
123
124 //==================================================================================================
125 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFaceByFaceAndVertex(
126     const std::shared_ptr<GeomAPI_Face> theFace,
127     const std::shared_ptr<GeomAPI_Vertex> theVertex)
128 {
129   gp_Pln aPln = theFace->getPlane()->impl<gp_Pln>();
130   gp_Pnt aPnt = theVertex->point()->impl<gp_Pnt>();
131
132   std::shared_ptr<GeomAPI_Face> aFace;
133   GC_MakePlane aMakePlane(aPln, aPnt);
134   if(!aMakePlane.IsDone()) {
135     return aFace;
136   }
137
138   BRepBuilderAPI_MakeFace aMakeFace(aMakePlane.Value()->Pln());
139   aFace.reset(new GeomAPI_Face());
140   aFace->setImpl(new TopoDS_Face(aMakeFace.Face()));
141   return aFace;
142 }