Unaligned Bounding Box

def UnalignedBoundingBox(gmty:var[]..[],coSy:var[]..[])
{
coSy1 = CoordinateSystem.Identity().Transform(coSy);
gmBb1 = BoundingBox.ByGeometry(gmty.Transform(coSy1,CoordinateSystem.Identity())<1>);
gmBb2 = Math.Round(gmBb1.MinPoint.DistanceTo(gmBb1.MaxPoint),3);
gmBb3 = DSCore.List.FirstItem(DSCore.List.SortByKey(gmBb1,gmBb2)["sorted list"]).ToCuboid();
coSy2 = DSCore.List.FirstItem(DSCore.List.SortByKey(coSy1,gmBb2)["sorted list"]);
return gmBb3.Transform(CoordinateSystem.Identity(),coSy2);
};