How to trigger conduit clear on input disconnect?

Hi everyone,

I’m struggling to clear a custom preview from a DisplayConduit when the component’s input is disconnected. My current approach in SolveInstance isn’t working as expected.

I’m using the standard !DA.GetData(0, ref data) || data == null check to check if the input is disconnected but my ClearPreview() function doesn’t seem to execute when the wire is removed, leaving the old preview visible.

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

namespace Toolpaths.Fdm.Components
{
    public class ClearDebug_Component : GH_Component
    {
        public ClearDebug_Component()
          : base("Clear Debug", "ClearDbg",
              "A minimal component to debug preview clearing.",
              "FDM TP", "DEV")
        {
        }

        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddCurveParameter("Curve", "C", "Curve input", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Message", "M", "Debug message", GH_ParamAccess.item);
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Curve curve = null;
            if (!DA.GetData(0, ref curve) || curve == null)
            {
                ClearPreview();
                DA.SetData("Message", "Input is invalid, clearing preview.");
                return;
            }

            // If input is valid
            DA.SetData("Message", "Input is valid.");
        }

        private void ClearPreview()
        {
            // This is the function to be called when input is invalid.
            // In the real component, this would clear the conduit.
        
        }

        protected override System.Drawing.Bitmap Icon => null;
        public override Guid ComponentGuid => new Guid("c1a6c5f1-2a31-4a8e-8b6a-1b3da1b1a0e1");
    }
}

When the input for this component is disconnected, the output message doesn’t update to show that the clear function was called.

What is the correct way to detect an invalid or disconnected input to reliably trigger a cleanup function?

Thanks for any advice.

Hi everyone,

From this " Clear display memory once there are no inputs - Grasshopper Component Display - Grasshopper Developer - McNeel Forum" I understand, that SolveInstance() isn’t called on a component if a required input is disconnected. This means any logic within SolveInstance() to handle a disconnected state won’t execute. The correct method is to use BeforeSolveInstance(), which is always called before each solution, regardless of input validity.

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

  public class ClearDebug_Component : GH_Component
  {
      private TextDot _dot;

      public ClearDebug_Component()
        : base("Clear Debug", "ClearDbg",
            "A minimal component to debug preview clearing.",
            "FDM TP", "DEV")
      {
      }

      protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
      {
          pManager.AddPointParameter("Point", "P", "Point for text dot", GH_ParamAccess.item);
      }

      protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
      {
      }

      protected override void BeforeSolveInstance()
      {
          // Clear the preview data before each solution.
          _dot = null;
      }

      protected override void SolveInstance(IGH_DataAccess DA)
      {
          Point3d point = Point3d.Unset;
          if (!DA.GetData(0, ref point))
          {
              return;
          }

          // If the input is valid, create the preview data.
          _dot = new TextDot("Valid Input", point);
      }

      public override void DrawViewportWires(IGH_PreviewArgs args)
      {
          // Draw whatever is in our preview data.
          // If it was cleared in BeforeSolveInstance, nothing will be drawn.
          if (_dot != null)
          {
              args.Display.DrawDot(_dot, Color.Black, Color.Black, Color.Black);
          }
      }

      protected override System.Drawing.Bitmap Icon => null;
      public override Guid ComponentGuid => new Guid("c1a6c5f1-2a31-4a8e-8b6a-1b3da1b1a0e1");
  }
  1. BeforeSolveInstance() clears the preview data (_dot = null;).
  2. SolveInstance() only runs with a valid input and is responsible for creating the preview data.
  3. DrawViewportWires() draws the current preview data, which will be null if cleared.

This reliably clears the preview when the input is disconnected.