import rhinoscriptsyntax as rs
import scriptcontext as sc
def main():
idBrep = rs.GetObject("Select surface", rs.filter.surface, preselect=True)
if idBrep is None: return
face = rs.coercesurface(idBrep)
b, cyl = face.TryGetCylinder(1.e-7)
if not b: return
s = 'cyl.IsValid'; print s+':',eval(s)
sc.doc.Objects.AddPoint(cyl.Center)
srf = cyl.ToRevSurface()
if srf is not None: sc.doc.Objects.AddSurface(srf)
sc.doc.Views.Redraw()
if __name__ == '__main__': main()
If Surface.TryGetCylinder is successful, it returns an infinite cylinder. This is by design. When a surface has a cylindrical shape, even if its ends are not circles on the cylinder, it returns the axis and radius. That’s what the “portion” means in the function comments.
Note, you can detect infinite cylinders using Cylinder.IsFinite in the WIP, or by verifying that Cylinder.Height1 and Cylinder.Height2 are equal.
If you are trying to reverse engineer a cylinder, then after calling Surface.TryGetCylinder to verify the surface looks like a cylinder, then just calculate a plane-aligned bounding box to obtain the cylinder’s height.
The issue isn’t concerning the height properties. For the surface I uploaded, the cylinder’s circle’s plane origin is not at the correct location. This image is viewing directly at the circle’s plane with the target surface on the right: