1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_Face.cpp
5 // Author: Artem ZHIDKOV
7 #include <GeomAPI_Face.h>
8 #include <GeomAPI_Dir.h>
9 #include <GeomAPI_Pln.h>
10 #include <GeomAPI_Pnt.h>
12 #include <TopoDS_Shape.hxx>
13 #include <TopoDS_Face.hxx>
15 #include <BRep_Tool.hxx>
16 #include <BRepAdaptor_Surface.hxx>
17 #include <Geom_Surface.hxx>
18 #include <Geom_Plane.hxx>
19 #include <GeomLib_IsPlanarSurface.hxx>
21 GeomAPI_Face::GeomAPI_Face()
26 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
28 if (!theShape->isNull() && theShape->isFace()) {
29 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
33 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
35 if (!theFace->isFace())
38 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
39 const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
41 Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(TopoDS::Face(aMyShape));
42 Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(TopoDS::Face(aInShape));
44 // Check that surfaces a the same type
45 if (aMySurf->DynamicType() != aInSurf->DynamicType())
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);
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())
64 bool GeomAPI_Face::isPlanar() const
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 GeomLib_IsPlanarSurface isPlanar(aSurf);
69 return isPlanar.IsPlanar() == Standard_True;
72 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
74 const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
75 BRepAdaptor_Surface aSurfAdapt(TopoDS::Face(aShape));
77 if (aSurfAdapt.GetType() != GeomAbs_Plane)
78 return std::shared_ptr<GeomAPI_Pln>();
80 // Obtain central point
81 double aUMin, aUMax, aVMin, aVMax;
82 aUMin = aSurfAdapt.FirstUParameter();
83 aUMax = aSurfAdapt.LastUParameter();
84 aVMin = aSurfAdapt.FirstVParameter();
85 aVMax = aSurfAdapt.LastVParameter();
88 aSurfAdapt.D1((aUMin+aUMax)*0.5, (aVMin+aVMax)*0.5, aCentralPnt, aDU, aDV);
89 std::shared_ptr<GeomAPI_Pnt> aCenter(
90 new GeomAPI_Pnt(aCentralPnt.X(), aCentralPnt.Y(), aCentralPnt.Z()));
92 // Obtain plane direction
93 gp_XYZ aNormalVec = aDU.XYZ().Crossed(aDV.XYZ());
94 if (aNormalVec.SquareModulus() < Precision::Confusion() * Precision::Confusion())
95 return std::shared_ptr<GeomAPI_Pln>();
96 std::shared_ptr<GeomAPI_Dir> aNormal(
97 new GeomAPI_Dir(aNormalVec.X(), aNormalVec.Y(), aNormalVec.Z()));
99 std::shared_ptr<GeomAPI_Pln> aResult(new GeomAPI_Pln(aCenter, aNormal));