Pick points from Conduit- select Grasshopper Preview points

I found the reference here for the Pick Points from conduit.
https://wiki.mcneel.com/developer/rhinocommonsamples/pickobject

And I built the Code on similar statements:

using System;
using System.Collections;
using System.Collections.Generic;

using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

using Rhino.Input.Custom;
using Rhino.Commands;
using Rhino.Display;
using System.Drawing;
using System.Windows.Forms;


/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
  /// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
  /// <param name="text">String to print.</param>
  private void Print(string text) { /* Implementation hidden. */ }
  /// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
  /// <param name="format">String format.</param>
  /// <param name="args">Formatting parameters.</param>
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  /// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj) { /* Implementation hidden. */ }
  /// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(List<Point3d> pts, bool on, ref object A)
  {

    if (on)
    {
      var pickPoints = PickPoints(pts);
      A = pickPoints;
    }
    else
    {
      return;
    }


  }

  // <Custom additional code> 
  //Class defintion for ConduitPoint
  public class ConduitPoint
  {
    public ConduitPoint(Point3d point)
    {
      Color = System.Drawing.Color.AliceBlue;
      Point = point;
    }
    public System.Drawing.Color Color { get; set; }
    public Point3d Point { get; set; }
  }
  

  //Class Definition to Getpoints from the Conduit
  public class GetConduitPoint: GetPoint
  {
    private List<ConduitPoint> _conduitPoints;

    public GetConduitPoint(List<ConduitPoint> conduitPoints)
    {
      _conduitPoints = conduitPoints;
    }   
    


    //Function to access the mouseDown Event
    protected override void OnMouseDown(GetPointMouseEventArgs e)
    {
      base.OnMouseDown(e);
      var picker = new PickContext();
      picker.View = e.Viewport.ParentView;

      picker.PickStyle = PickStyle.PointPick;

      var xform = e.Viewport.GetPickTransform(e.WindowPoint);
      picker.SetPickTransform(xform);

      foreach (var cp in _conduitPoints)
      {
        double depth;
        double distance;
        if (picker.PickFrustumTest(cp.Point, out depth, out distance))
          cp.Color = System.Drawing.Color.Red;
        else
          cp.Color = System.Drawing.Color.White;
      }
    }
  }
  class PointsConduit : Rhino.Display.DisplayConduit
  {
    private List<ConduitPoint> _conduitPoints;

    public PointsConduit(List<ConduitPoint> conduitPoints)
    {
      _conduitPoints = conduitPoints;
    }

    protected override void DrawForeground(Rhino.Display.DrawEventArgs e)
    {
      if (_conduitPoints != null)
        foreach (var cp in _conduitPoints)
          e.Display.DrawPoint(cp.Point, PointStyle.X, 5, Color.Green);
    }
  }

  //Function to generate Points to access
  private List<Point3d> PickPoints(List<Point3d> cPoints)
  {
    List<Point3d> pickPoints = new List<Point3d>();
    List<ConduitPoint> conduitPoints = new List<ConduitPoint>();
    var conduit = new PointsConduit(conduitPoints);
    conduit.Enabled = true;


    var gcp = new GetConduitPoint(conduitPoints);
    gcp.SetCursor(Rhino.UI.CursorStyle.ArrowCopy);
    while (true)
    {
      gcp.SetCommandPrompt("select conduit point. (<ESC> to exit)");
      gcp.AcceptNothing(true);
      gcp.Get(true);
      doc.Views.Redraw();
      if (gcp.CommandResult() != Rhino.Commands.Result.Success)
      {
        pickPoints.Add(gcp.Point());
        return pickPoints;
      }
    }
  }
  // </Custom additional code> 
}

It does not pick the points, rather it just places a point at the position of cursor. I am sure I am missing something in here.
Any Guidance?

Script is also attached here
Test_PickPreviewPoints.gh (11.0 KB)

I figured out the way to select the points from Conduit within Grasshopper. Here is the snippet for those who are interested in trying it out.

using System;
using System.Collections;
using System.Collections.Generic;

using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

using Grasshopper.Kernel.Special;
using Rhino.Input.Custom;
using Rhino.Commands;
using Rhino.Display;
using System.Drawing;
using System.Windows.Forms;


/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
  /// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
  /// <param name="text">String to print.</param>
  private void Print(string text) { /* Implementation hidden. */ }
  /// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
  /// <param name="format">String format.</param>
  /// <param name="args">Formatting parameters.</param>
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  /// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj) { /* Implementation hidden. */ }
  /// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(List<Point3d> pts, bool on, object clear, ref object A)
  {

    List<Point3d> outPts = new List<Point3d>();
    if (on)
    {
      var selIndices = PickPoints(pts);
      foreach (var index in selIndices)
      {
        outPts.Add(pts[index]);
      }
      stickyData.AddRange(outPts);

      var toggle = this.Component.Params.Input[1].Sources[0];
      ((GH_BooleanToggle) toggle).Value = false;
      toggle.ExpireSolution(true);
    }

    if (stickyData.Count > 0)
    {
      if ((bool) clear) stickyData.Clear();
      A = stickyData;
    }
    else A = null;

  }

  // <Custom additional code> 
  List<Point3d> stickyData = new List<Point3d>();

  public class GetConduitPoint : GetPoint
  {
    private List<Point3d> _conduitPoints;
    private List<Point3d> _selPts;
    public List<int> selIndices;

    public GetConduitPoint(List<Point3d> conduitPoints)
    {
      _conduitPoints = conduitPoints;
      _selPts = new List<Point3d>();
      selIndices = new List<int>();
    }

    protected override void OnMouseDown(GetPointMouseEventArgs e)
    {
      base.OnMouseDown(e);
      var picker = new PickContext();
      picker.View = e.Viewport.ParentView;

      picker.PickStyle = PickStyle.PointPick;

      var xform = e.Viewport.GetPickTransform(e.WindowPoint);
      picker.SetPickTransform(xform);

      for (int index = 0; index < _conduitPoints.Count; index++)
      {
        var cp = _conduitPoints[index];
        double depth;
        double distance;
        if (picker.PickFrustumTest(cp, out depth, out distance))
        {
          _selPts.Add(cp);
          selIndices.Add(index);
        }
      }
    }

    protected override void OnDynamicDraw(GetPointDrawEventArgs e)
    {
      foreach (var pt in _selPts)
        e.Display.DrawPoint(pt, PointStyle.X, 5, System.Drawing.Color.Green);
    }
  }

  private List<int> PickPoints(List<Point3d> conduitPoints)
  {
    var gcp = new GetConduitPoint(conduitPoints);
    gcp.SetCursor(Rhino.UI.CursorStyle.Default);
    while (true)
    {
      gcp.SetCommandPrompt("select conduit point. (<ESC> to exit)");
      gcp.AcceptNothing(true);
      gcp.Get(true);
      doc.Views.Redraw();
      if (gcp.CommandResult() != Rhino.Commands.Result.Success)
      {
        var selIndices = gcp.selIndices;
        return selIndices;
      }
    }
  }



  // </Custom additional code> 
}

PickPreviewPoints.gh (12.0 KB)

2 Likes