Modify color and arrowheads of AddLineParameter in OutputParams

Here’s some C# code for a component which overrides the preview of the output parameter:

using System;
using System.Collections.Generic;
using System.Drawing;
using Grasshopper.Kernel;
using Rhino.Geometry;

namespace TestComponents
{

  public class LineComponent : GH_Component
  {
    public LineComponent()
      : base("Funky Line", "FLine", "Funky line creator", "Test", "Test")
    { }

    public readonly static Guid FunkyLineId = new Guid("{89F93645-4985-44CA-AED6-1C87DCF6D9EE}");
    public override Guid ComponentGuid
{
  get { return FunkyLineId; }
}

    protected override void RegisterInputParams(GH_InputParamManager pManager)
{
  pManager.AddPointParameter("A", "A", "A", GH_ParamAccess.item);
  pManager.AddPointParameter("B", "B", "B", GH_ParamAccess.item);
}
    protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
  pManager.AddLineParameter("Line", "L", "Funky lines", GH_ParamAccess.item);
  pManager.HideParameter(0);
}

    protected override void BeforeSolveInstance()
{
  _lines.Clear();
  _widths.Clear();
  _colors.Clear();
}
    protected override void SolveInstance(IGH_DataAccess DA)
    {
      Point3d a = Point3d.Unset;
      Point3d b = Point3d.Unset;

      if (!DA.GetData(0, ref a)) return;
      if (!DA.GetData(1, ref b)) return;
      if (!a.IsValid || !b.IsValid)
        return;

      Line line = new Line(a, b);
      DA.SetData(0, line);

      _lines.Add(line);
      _widths.Add(Math.Min(10, (int)(line.Length * 0.1) + 1));
      if (b.X > a.X)
        _colors.Add(Color.HotPink);
      else
        _colors.Add(Color.CornflowerBlue);
    }

    private readonly List<Line> _lines = new List<Line>();
    private readonly List<int> _widths = new List<int>();
    private readonly List<Color> _colors = new List<Color>();

    public override void DrawViewportWires(IGH_PreviewArgs args)
    {
      base.DrawViewportWires(args);

      for (int i = 0; i < _lines.Count; i++)
      {
        args.Display.DrawLine(_lines[i], _colors[i], _widths[i]);
        args.Display.DrawArrowHead(_lines[i].To, _lines[i].Direction, _colors[i], 40, 0);
      }
    }
  }
}
2 Likes