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");
}
- BeforeSolveInstance() clears the preview data (_dot = null;).
- SolveInstance() only runs with a valid input and is responsible for creating the preview data.
- DrawViewportWires() draws the current preview data, which will be null if cleared.
This reliably clears the preview when the input is disconnected.