Align polysurface to plane

I’m trying to align unknown polysurfaces to the XY-plane. I rotate the object and monitor what happens to the dimensions of the bounding box. This gets me quite close to actual alignment to the plane, but never really there. There must be a better way to do this?
Thanks, Johan

Is one or more faces planar? Then you could get the plane using Surface.TryGetPlane(out Plane ), followed by a plane-to-plane transformation using Transform.ChangeBasis(Plane, Plane) to align it to the desired plane.

That just might work!
Dankjewel Menno!