Closest object to a point

hello,

is there a command to find the closest polysurface/text… to a point in rhino scripting (python)?

thanks,
CG

Some implementations has example code, including Python code:

https://developer.rhino3d.com/api/RhinoCommon/search.html?SearchText=ClosestPoint

Example for Surface (scroll down to see Python code):
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Surface_ClosestPoint.htm

// Rolf

thanks rolf!

There is also PointClosestObject:
https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-PointClosestObject

1 Like

that’s what I was looking for, big thanks, jess :smiley:

That interesting wonder if RhCommon also has it :thinking: Can’t find anything … For RC probably just checking distances to bboxes would me the smartest approach.

I don’t think so. If you dig into the rhinoscriptsyntax you’ll find this function:

def PointClosestObject(point, object_ids):
    """Finds the object that is closest to a test point
    Parameters:
      point (point): point to test
      object_id ([guid, ...]): identifiers of one or more objects
    Returns:
      list(guid, point): closest [0] object_id and [1] point on object on success
      None: on failure
    Example:
      import rhinoscriptsyntax as rs
      objs = rs.GetObjects("Select target objects for closest point", 63)
      if objs:
          point = rs.GetPoint("Test point")
          if point:
              results = rs.PointClosestObject(point, objs)
              if results:
                  print "Object id:", results[0]
                  rs.AddPoint( results[1] )
    See Also:
      CurveClosestObject
    """
    object_ids = rhutil.coerceguidlist(object_ids)
    point = rhutil.coerce3dpoint(point, True)
    closest = None
    for id in object_ids:
        geom = rhutil.coercegeometry(id, True)
        point_geometry = geom
        if isinstance(point_geometry, Rhino.Geometry.Point):
            distance = point.DistanceTo( point_geometry.Location )
            if closest is None or distance<closest[0]:
                closest = distance, id, point_geometry.Location
            continue
        point_cloud = geom
        if isinstance(point_cloud, Rhino.Geometry.PointCloud):
            index = point_cloud.ClosestPoint(point)
            if index>=0:
                distance = point.DistanceTo( point_cloud[index].Location )
                if closest is None or distance<closest[0]:
                    closest = distance, id, point_cloud[index].Location
            continue
        curve = geom
        if isinstance(curve, Rhino.Geometry.Curve):
            rc, t = curve.ClosestPoint(point)
            if rc:
                distance = point.DistanceTo( curve.PointAt(t) )
                if closest is None or distance<closest[0]:
                    closest = distance, id, curve.PointAt(t)
            continue
        brep = geom
        if isinstance(brep, Rhino.Geometry.Brep):
            brep_closest = brep.ClosestPoint(point)
            distance = point.DistanceTo( brep_closest )
            if closest is None or distance<closest[0]:
                closest = distance, id, brep_closest
            continue
        mesh = geom
        if isinstance(mesh, Rhino.Geometry.Mesh):
            mesh_closest = mesh.ClosestPoint(point)
            distance = point.DistanceTo( mesh_closest )
            if closest is None or distance<closest[0]:
                closest = distance, id, mesh_closest
            continue
    if closest: return closest[1], closest[2]
1 Like