Skip to content

Commit

Permalink
Part: improve TopoShape::findPlane()
Browse files Browse the repository at this point in the history
Make the returned plane normal consistent with the underlying face
geometry.
  • Loading branch information
realthunder authored and wwmayer committed Dec 14, 2020
1 parent c25ff3a commit 0a35cb5
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/Mod/Part/App/TopoShape.cpp
Expand Up @@ -171,6 +171,8 @@
# include <APIHeaderSection_MakeHeader.hxx>
# include <ShapeAnalysis_FreeBoundsProperties.hxx>
# include <ShapeAnalysis_FreeBoundData.hxx>
# include <BRepLProp_SLProps.hxx>
# include <BRepGProp_Face.hxx>

#if OCC_VERSION_HEX < 0x070300
# include <BRepAlgo_Fuse.hxx>
Expand Down Expand Up @@ -4161,33 +4163,38 @@ bool TopoShape::findPlane(gp_Pln &pln, double tol) const {
if(_Shape.IsNull())
return false;
TopoDS_Shape shape = _Shape;
TopExp_Explorer exp(_Shape,TopAbs_FACE);
TopExp_Explorer exp(_Shape,TopAbs_EDGE);
if(exp.More()) {
auto face = exp.Current();
TopoDS_Shape edge = exp.Current();
exp.Next();
if(!exp.More()) {
BRepAdaptor_Surface adapt(TopoDS::Face(face));
if(adapt.GetType() != GeomAbs_Plane)
return false;
pln = adapt.Plane();
return true;
}
}else{
TopExp_Explorer exp(_Shape,TopAbs_EDGE);
if(exp.More()) {
TopoDS_Shape edge = exp.Current();
exp.Next();
if(!exp.More()) {
// To deal with OCCT bug of wrong edge transformation
shape = BRepBuilderAPI_Copy(edge).Shape();
}
if (!exp.More()) {
// To deal with OCCT bug of wrong edge transformation
shape = BRepBuilderAPI_Copy(_Shape).Shape();
}
}
try {
BRepLib_FindSurface finder(shape,tol,Standard_True);
if (!finder.Found())
return false;
pln = GeomAdaptor_Surface(finder.Surface()).Plane();

// To make the returned plane normal more stable, if the shape has any
// face, use the normal of the first face.
TopExp_Explorer exp(shape, TopAbs_FACE);
if(exp.More()) {
BRepAdaptor_Surface adapt(TopoDS::Face(exp.Current()));
double u = adapt.FirstUParameter()
+ (adapt.LastUParameter() - adapt.FirstUParameter())/2.;
double v = adapt.FirstVParameter()
+ (adapt.LastVParameter() - adapt.FirstVParameter())/2.;
BRepLProp_SLProps prop(adapt,u,v,2,Precision::Confusion());
if(prop.IsNormalDefined()) {
gp_Pnt pnt; gp_Vec vec;
// handles the orientation state of the shape
BRepGProp_Face(TopoDS::Face(exp.Current())).Normal(u,v,pnt,vec);
pln = gp_Pln(pnt, gp_Dir(vec));
}
}
return true;
}catch (Standard_Failure &e) {
// For some reason the above BRepBuilderAPI_Copy failed to copy
Expand Down

0 comments on commit 0a35cb5

Please sign in to comment.