Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_FaceBuilder.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_FaceBuilder.h"
21
22 #include <GeomAPI_Face.h>
23 #include <GeomAPI_Dir.h>
24 #include <GeomAPI_Pln.h>
25 #include <GeomAPI_Pnt.h>
26 #include <GeomAPI_Shape.h>
27 #include <GeomAPI_Vertex.h>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <GC_MakePlane.hxx>
32 #include <Geom_Plane.hxx>
33 #include <gp_Pln.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Face.hxx>
36
37 //==================================================================================================
38 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::squareFace(
39   const std::shared_ptr<GeomAPI_Pnt> theCenter,
40   const std::shared_ptr<GeomAPI_Dir> theNormal,
41   const double theSize)
42 {
43   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
44   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
45   gp_Pln aPlane(aCenter, aDir);
46   // half of the size in each direction from the center
47   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, -theSize / 2., theSize / 2., -theSize / 2.,
48                                        theSize / 2.);
49   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
50   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
51   return aRes;
52 }
53
54 //==================================================================================================
55 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::squareFace(
56   const std::shared_ptr<GeomAPI_Pln> thePlane,
57   const double theSize)
58 {
59   // half of the size in each direction from the center
60   BRepBuilderAPI_MakeFace aFaceBuilder(thePlane->impl<gp_Pln>(),
61                                        -theSize / 2., theSize / 2.,
62                                        -theSize / 2., theSize / 2.);
63   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
64   const TopoDS_Face& aFace = aFaceBuilder.Face();
65   aRes->setImpl(new TopoDS_Face(aFace));
66   return aRes;
67 }
68
69 //==================================================================================================
70 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFace(
71   const std::shared_ptr<GeomAPI_Pnt> theCenter,
72   const std::shared_ptr<GeomAPI_Dir> theNormal)
73 {
74   const gp_Pnt& aCenter = theCenter->impl<gp_Pnt>();
75   const gp_Dir& aDir = theNormal->impl<gp_Dir>();
76   gp_Pln aPlane(aCenter, aDir);
77   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane);
78   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
79   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
80   return aRes;
81 }
82
83 //==================================================================================================
84 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFace(
85   const std::shared_ptr<GeomAPI_Pln> thePlane,
86   const double theX, const double theY,
87   const double theWidth, const double theHeight)
88 {
89   double aA, aB, aC, aD;
90   thePlane->coefficients(aA, aB, aC, aD);
91   gp_Pln aPlane(aA, aB, aC, aD);
92
93   // half of the size in each direction from the center
94   BRepBuilderAPI_MakeFace aFaceBuilder(aPlane, theX, theX + theWidth,
95                                        theY, theY + theHeight);
96   std::shared_ptr<GeomAPI_Face> aRes(new GeomAPI_Face());
97   aRes->setImpl(new TopoDS_Face(aFaceBuilder.Face()));
98   return aRes;
99 }
100
101 //==================================================================================================
102 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_FaceBuilder::planarFaceByThreeVertices(
103     const std::shared_ptr<GeomAPI_Vertex> theVertex1,
104     const std::shared_ptr<GeomAPI_Vertex> theVertex2,
105     const std::shared_ptr<GeomAPI_Vertex> theVertex3)
106 {
107   gp_Pnt aPnt1 = theVertex1->point()->impl<gp_Pnt>();
108   gp_Pnt aPnt2 = theVertex2->point()->impl<gp_Pnt>();
109   gp_Pnt aPnt3 = theVertex3->point()->impl<gp_Pnt>();
110
111   std::shared_ptr<GeomAPI_Face> aFace;
112   GC_MakePlane aMakePlane(aPnt1, aPnt2, aPnt3);
113   if(!aMakePlane.IsDone()) {
114     return aFace;
115   }
116
117   BRepBuilderAPI_MakeFace aMakeFace(aMakePlane.Value()->Pln());
118   aFace.reset(new GeomAPI_Face());
119   aFace->setImpl(new TopoDS_Face(aMakeFace.Face()));
120   return aFace;
121 }