- BRepAdaptor_Surface surface( TopoDS::Face( theShape ));
- theCenter = surface.Value( 0.5 * ( surface.FirstUParameter() + surface.LastUParameter() ),
- 0.5 * ( surface.FirstVParameter() + surface.LastVParameter() ));
+ const TopoDS_Face& F = TopoDS::Face( theShape );
+ BRepAdaptor_Surface surface( F );
+
+ TopLoc_Location L;
+ Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation( F, L );
+ if ( !triangulation.IsNull() && triangulation->HasUVNodes() )
+ {
+ gp_XY C( 0, 0 );
+ double A = 0;
+ const TColgp_Array1OfPnt2d& uvArray = triangulation->UVNodes();
+ const Poly_Array1OfTriangle& trias = triangulation->Triangles();
+ Standard_Integer n1,n2,n3;
+ for ( int iT = trias.Lower(); iT <= trias.Upper(); ++iT )
+ {
+ trias( iT ).Get( n1,n2,n3 );
+ const gp_Pnt2d& uv1 = uvArray( n1 );
+ const gp_Pnt2d& uv2 = uvArray( n2 );
+ const gp_Pnt2d& uv3 = uvArray( n3 );
+ double a = 0.5 * sqrt(( uv1.X() - uv3.X() ) * ( uv2.Y() - uv1.Y() ) -
+ ( uv1.X() - uv2.X() ) * ( uv3.Y() - uv1.Y() ));
+ C += ( uv1.XY() + uv2.XY() + uv3.XY() ) / 3. * a;
+ A += a;
+ }
+ C /= A;
+ theCenter = surface.Value( C.X(), C.Y() );
+ }
+ else
+ {
+ theCenter = surface.Value( 0.5 * ( surface.FirstUParameter() + surface.LastUParameter() ),
+ 0.5 * ( surface.FirstVParameter() + surface.LastVParameter() ));
+ }