Salome HOME
Add ExchangeAPI, fillAttribute in ModelHighAPI_Tools, ModelHighAPI_Selection and...
[modules/shaper.git] / src / GeomAPI / GeomAPI_Face.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Face.cpp
4 // Created:     2 Dec 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "GeomAPI_Face.h"
8
9 #include "GeomAPI_Dir.h"
10 #include "GeomAPI_Pln.h"
11 #include "GeomAPI_Pnt.h"
12
13 #include <BRep_Tool.hxx>
14 #include <BRepAdaptor_Surface.hxx>
15 #include <Geom_Surface.hxx>
16 #include <Geom_CylindricalSurface.hxx>
17 #include <Geom_RectangularTrimmedSurface.hxx>
18 #include <TopoDS.hxx>
19 #include <TopoDS_Face.hxx>
20
21 GeomAPI_Face::GeomAPI_Face()
22   : GeomAPI_Shape()
23 {
24 }
25
26 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
27 {
28   if (!theShape->isNull() && theShape->isFace()) {
29     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
30   }
31 }
32
33 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
34 {
35   if (!theFace->isFace())
36     return false;
37
38   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
39   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
40
41   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(TopoDS::Face(aMyShape));
42   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(TopoDS::Face(aInShape));
43
44   // Check that surfaces a the same type
45   if (aMySurf->DynamicType() != aInSurf->DynamicType())
46     return false;
47
48   // Get parameters of surfaces
49   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
50   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
51   double aInUMin, aInUMax, aInVMin, aInVMax;
52   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
53
54   // Check that parameters are the same
55   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
56       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
57       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
58       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
59     return false;
60
61   return true;
62 }
63
64 bool GeomAPI_Face::isCylindrical() const
65 {
66   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
67   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
68   Handle(Geom_RectangularTrimmedSurface) aTrimmed = 
69     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
70   if (!aTrimmed.IsNull())
71     aSurf = aTrimmed->BasisSurface();
72   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
73 }
74
75 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
76 {
77   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
78   BRepAdaptor_Surface aSurfAdapt(TopoDS::Face(aShape));
79
80   if (aSurfAdapt.GetType() != GeomAbs_Plane)
81     return std::shared_ptr<GeomAPI_Pln>();
82
83   // Obtain central point
84   double aUMin, aUMax, aVMin, aVMax;
85   aUMin = aSurfAdapt.FirstUParameter();
86   aUMax = aSurfAdapt.LastUParameter();
87   aVMin = aSurfAdapt.FirstVParameter();
88   aVMax = aSurfAdapt.LastVParameter();
89   gp_Pnt aCentralPnt;
90   gp_Vec aDU, aDV;
91   aSurfAdapt.D1((aUMin+aUMax)*0.5, (aVMin+aVMax)*0.5, aCentralPnt, aDU, aDV);
92   std::shared_ptr<GeomAPI_Pnt> aCenter(
93       new GeomAPI_Pnt(aCentralPnt.X(), aCentralPnt.Y(), aCentralPnt.Z()));
94
95   // Obtain plane direction
96   gp_XYZ aNormalVec = aDU.XYZ().Crossed(aDV.XYZ());
97   if (aNormalVec.SquareModulus() < Precision::Confusion() * Precision::Confusion())
98     return std::shared_ptr<GeomAPI_Pln>();
99   std::shared_ptr<GeomAPI_Dir> aNormal(
100       new GeomAPI_Dir(aNormalVec.X(), aNormalVec.Y(), aNormalVec.Z()));
101
102   std::shared_ptr<GeomAPI_Pln> aResult(new GeomAPI_Pln(aCenter, aNormal));
103   return aResult;
104 }