Detecting deletion of Component

Hi,

I’m making a custom display component for some surface analysis methods in C# and I’m using the DisplayPipeline.DrawForeground event handler to display a floating legend in the active viewport. I managed to deregister the event for some specific cases like if the component is hidden or if the document is closed or unloaded using DocumentContextChanged but I still haven’t figured out how to deregister it in case the component is deleted from the canvas. Is there an attribute or method I’m missing from the GH_Component class or how would one go about doing that? The instance of the legend stays in the viewport if I delete it and is then overlayed with a new one if I put another component on the canvas.

Another bug I’m having is that the legend gets duplicated when I change the active viewport and again dissapears if I go in and out of full-screen. I’m using a check to determine the active viewport before drawing the geometry so it is only drawn there;

private void DrawForeground(Object sender, DrawEventArgs e)
{
if (e.Viewport.Id == e.RhinoDoc.Views.ActiveView.ActiveViewportID)
                    { //draw geometry}
}

I suspect it might be that I’m registering( DrawViewportWires so it triggers when geometry is drawn) and deregistering (BeforeSolveInstance) the event at the wrong parts of the code but I’m pretty green so don’t have good practice habits yet.

If anyone can shed light on this issue it would be super helpful!
Cheers,
Tina

solved for deletion in case it is useful for anyone. Viewport issue still persists.

//when document is closed or unloaded(grasshopper is closed or switching between files)
        public override void DocumentContextChanged(GH_Document document, GH_DocumentContext context) 
        {
            base.DocumentContextChanged(document, context);

            if (context == GH_DocumentContext.Close || context == GH_DocumentContext.Unloaded )
            {
                DisplayPipeline.DrawForeground -= DrawForeground;
            }
        }

        //when component is removed from canvas
  public override void RemovedFromDocument(GH_Document document)
        {
            base.RemovedFromDocument(document);
            DisplayPipeline.DrawForeground -= DrawForeground;
        }
1 Like