Pick Point

Demonstrates how to pick and select a point object.

partial class Examples
{
  public static Result PickPoint(RhinoDoc doc)
  {
    // this creates a point where the mouse is clicked.
    var gp = new GetPoint();
    gp.SetCommandPrompt("Click for new point");
    gp.Get();
    if (gp.CommandResult() != Result.Success)
      return gp.CommandResult();
    var point3d = gp.Point();
    doc.Objects.AddPoint(point3d);
    doc.Views.Redraw();

    // selects a point that already exists
    ObjRef obj_ref;
    var rc = RhinoGet.GetOneObject("Select point", false, ObjectType.Point, out obj_ref);
    if (rc != Result.Success)
      return rc;
    var point = obj_ref.Point();
    RhinoApp.WriteLine("Point: x:{0}, y:{1}, z:{2}",
      point.Location.X,
      point.Location.Y,
      point.Location.Z);
    doc.Objects.UnselectAll();

    // selects multiple points that already exist
    ObjRef[] obj_refs;
    rc = RhinoGet.GetMultipleObjects("Select point", false, ObjectType.Point, out obj_refs);
    if (rc != Result.Success)
      return rc;
    foreach (var o_ref in obj_refs)
    {
      point = o_ref.Point();
      RhinoApp.WriteLine("Point: x:{0}, y:{1}, z:{2}",
        point.Location.X,
        point.Location.Y,
        point.Location.Z);
    }
    doc.Objects.UnselectAll();

    // also selects a point that already exists.
    // Used when RhinoGet doesn't provide enough control
    var go = new GetObject();
    go.SetCommandPrompt("Select point");
    go.GeometryFilter = ObjectType.Point;
    go.GetMultiple(1, 0);
    if (go.CommandResult() != Result.Success)
      return go.CommandResult();
    foreach (var o_ref in  go.Objects())
    {
      point = o_ref.Point();
      if (point != null)
        RhinoApp.WriteLine("Point: x:{0}, y:{1}, z:{2}",
          point.Location.X,
          point.Location.Y,
          point.Location.Z);
    }

    doc.Views.Redraw();
    return Result.Success;
  }
}
from Rhino import RhinoApp
from Rhino.DocObjects import ObjectType
from Rhino.Commands import Result
from Rhino.Input import RhinoGet
from Rhino.Input.Custom import GetPoint, GetObject
from scriptcontext import doc
import rhinoscriptsyntax as rs

def RunCommand():
    # creates a point where the mouse is clicked
    # using RhinoCommon
    gp = GetPoint()
    gp.SetCommandPrompt("Click for point")
    gp.Get()
    if gp.CommandResult() != Result.Success:
        return gp.CommandResult()
    point3d = gp.Point()
    doc.Objects.AddPoint(point3d)
    doc.Views.Redraw()

    # creates a point where the mouse is clicked
    # using the RhinoScript syntax
    point3d = rs.GetPoint("Click for point")
    if point3d == None: return Result.Nothing
    rs.AddPoint(point3d)
    doc.Objects.AddPoint(point3d)

    # selects a point that already exists
    # using RhinoCommon
    rc, obj_ref = RhinoGet.GetOneObject("Select point", False, ObjectType.Point)
    if rc != Result.Success:
        return rc
    point = obj_ref.Point()
    RhinoApp.WriteLine("Point: x:{0}, y:{1}, z:{2}", point.Location.X, point.Location.Y, point.Location.Z)
    doc.Objects.UnselectAll()

    # also selects a point that already exists
    # using RhinoCommon
    # Used when RhinoGet doesn't provide enough control
    go = GetObject()
    go.SetCommandPrompt("Select point")
    go.GeometryFilter = ObjectType.Point
    go.GetMultiple(1, 0)
    if go.CommandResult() != Result.Success:
        return go.CommandResult()
    for o_ref in  go.Objects():
        point = o_ref.Point()
        if point != None:
            RhinoApp.WriteLine("Point: x:{0}, y:{1}, z:{2}", point.Location.X, point.Location.Y, point.Location.Z)
    doc.Objects.UnselectAll()

    # selects a point that already exists
    # using RhinoScript syntax
    point_id = rs.GetObject("Select point", rs.filter.point)
    if point_id == None: return Result.Nothing
    print("point id: {0}".format(point_id))
    rs.UnselectAllObjects()

    # selects multiple points that already exist
    rc, obj_refs = RhinoGet.GetMultipleObjects("Select point", False, ObjectType.Point)
    if rc != Result.Success:
        return rc
    for o_ref in obj_refs:
        point = o_ref.Point()
        RhinoApp.WriteLine("Point: x:{0}, y:{1}, z:{2}", point.Location.X, point.Location.Y, point.Location.Z)
    doc.Objects.UnselectAll()

    # selects multiple poins that already exists
    # using the RhinoScript syntax
    point_ids = rs.GetObjects("Select point", rs.filter.point)
    for p_id in point_ids:
        print("point id: {0}".format(p_id))

    return Result.Success

if __name__ == "__main__":
    RunCommand()