Clear custom display when deleting GhPython component

Hello,

I created several components which create component specific custom display using GhPython.
And with the help of several users of the forum I’ve been able to manually clear them individually. :smile:

The way I’ve done it is by creating component specific entry in the sticky context using UUID. Unfortunately, it means that I can’t clear the entry of a component I just deleted as I don’t know what was the generated UUID

I was wondering If anyone knew how to automatically clear custom display when deleting ? Maybe by overriding a method linked to the deletion of the component ? I honestly don’t know :sweat_smile:

Thank you in advance !

.

Ps: Here’s an exemple of the type of scripts I’m using to plot a force vector and its magnitude

    from ghpythonlib.componentbase import executingcomponent as component
    import Grasshopper, GhPython
    import System
    import Rhino
    import rhinoscriptsyntax as rs

    from Rhino.Geometry import Line, Vector3d as V3, Point3d as Pt3
    from Rhino.Display import CustomDisplay
    from scriptcontext import sticky
    from System.Drawing.Color import Green
    import Rhino
    from uuid import uuid4

    class Forces_component(component):
        
        def __init__(self):
            component.__init__(self)
            self.ACTIVE_DOC = Rhino.RhinoDoc.ActiveDoc
            DisplayGuid = "CustomDisplayForces" + str(uuid4())
            self.TextDotGuid = "TextDot" + str(uuid4())
            
            if DisplayGuid not in sticky:
                sticky[DisplayGuid] = CustomDisplay(True)
                self.custom_display = sticky[DisplayGuid]
            else:
                self.custom_display = sticky[DisplayGuid]
            
            if self.TextDotGuid not in sticky:
                sticky[self.TextDotGuid] = []
                
            self.custom_display.Clear()
            
        def RunScript(self, Pt, V, plot):
            
            self.custom_display.Clear()
            self.clear_textdots(sticky[self.TextDotGuid])
            
            if plot:
                sticky[self.TextDotGuid] = []
                attr = Rhino.DocObjects.ObjectAttributes()
                attr.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject
                attr.ObjectColor = Green
                for vector in V:
                    for point in Pt:
                        if vector.Length < 50e3:
                            v = 2*vector/vector.Length
                        else:
                            v = 2*vector/50e3
                        self.custom_display.AddVector(point-v,v,Green)
                        tdot = self.create_textdot("{}kN".format(vector.Length/1000.), 
                                                                 point-1.3*v, -1)
                        id = self.ACTIVE_DOC.Objects.AddTextDot(tdot, attr)
                        sticky[self.TextDotGuid].append(id)

        def create_textdot(self, text, point):
            textdot = Rhino.Geometry.TextDot(text, point)
            textdot.FontHeight = 18
            return textdot
        
        def clear_textdots(self, object_ids):
            for id in object_ids:
                if self.ACTIVE_DOC.Objects.Delete(id, True):
                    pass

The right way would be to override DrawViewportWires and/or DrawViewportMeshes:

https://developer.rhino3d.com/api/grasshopper/html/M_Grasshopper_Kernel_IGH_PreviewObject_DrawViewportMeshes.htm

Here an example:


apologies regarding the missing images: the Discourse team mistakenly deleted them…

You can also overrding the __del__(self): and __exit__(self): funtions.

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

Hi @piac,

Could you please update the images in that post. Apparently, discourse screw something up again.

A while ago happened again. There are hundreds of affected posts where solutions were images

I no longer have those images, sorry.

I think I have saved that thread as a pdf somewhere for offline access. I will try to find and send it to you. This is very valuable thread at least IMHO. Would you agree on creating it as a wiki page? Like the ones on github.

1 Like

@piac thank you for your answers !
Unfortunately I wasn’t able to make it work :sweat_smile:

@ivelin.peychev If you find it, could you share the pdf :smiley: ? I would be glad to be able to read it with the images !

Alas, it turns out I have saved another thread :frowning: