Display Conduit - Draw Circle On Mouse Event

Hello,

This is my first time attempting to do a display conduit and I’m a bit stuck and would appreciate some guidance!

I’m attempting to start out by simply drawing a red, filled circle at the location of the mouse when the user has a mouse button down OnMouseDown (in this case the middle mouse button) and removing this circle from the viewport OnMouseUp.

I have debugging statements in place and know that my mouse events are occurring but I’m confused how to implement HUD/viewport drawing within the mouse events or how to call them properly. Perhaps it’s not that but I’m just not understanding how to actually draw/display the graphic on the viewport via the display.conduit.

Thanks for your response!

Here’s the code I have so far:

import Rhino
import scriptcontext as sc
import System
from System.Drawing import Color

class MiddleMouseCallback(Rhino.UI.MouseCallback):
    def OnMouseDown(self, e):
        if e.Button == System.Windows.Forms.MouseButtons.Middle:
            Rhino.RhinoApp.WriteLine("Middle mouse down")
            # Store the mouse location when the middle mouse button is pressed
            self.mouse_down_location = e.Location
        else:
            Rhino.RhinoApp.WriteLine("Other mouse down")
    
    def OnMouseUp(self, e):
        if e.Button == System.Windows.Forms.MouseButtons.Middle:
            Rhino.RhinoApp.WriteLine("Middle mouse up")
        else:
            Rhino.RhinoApp.WriteLine("Other mouse up")

class CircleConduit(Rhino.Display.DisplayConduit):
    def __init__(self, mouse_down_location):
        super(CircleConduit, self).__init__()
        self.mouse_down_location = mouse_down_location

    def DrawForeground(self, e):
        if hasattr(self, 'mouse_down_location'):
            radius = 220
            # Draw the filled circle
            circle = Rhino.Display.DisplayPipeline.DrawCircle(self.mouse_down_location, radius)
            e.Display.DrawCircle(circle, Color.Red, True)

def TestSampleMouseCallback():
    # Initialize the mouse callback
    if sc.sticky.has_key('TestSampleMouseCallback'):
        callback = sc.sticky['TestSampleMouseCallback']
        if callback:
            callback.Enabled = False
            callback = None
            sc.sticky.Remove('TestSampleMouseCallback')
    else:
        callback = MiddleMouseCallback()
        callback.Enabled = True
        sc.sticky['TestSampleMouseCallback'] = callback

if __name__ == "__main__":
    # Call the test function
    TestSampleMouseCallback()

You should be able to find DisplayConduit samples for Python here: Code search results · GitHub in the repository GitHub - mcneel/rhino-developer-samples: Rhino and Grasshopper developer sample code

Looks like you’r missing the enabling of your conduit. So create a conduit, instantiate, and enable. It’ll get called while being active.

Also, you’ll want only one conduit instance, and on it a method that takes the mouse down locations and draw that/those.

And just in case you aren’t fully aware yet of how display conduits work: Rhino - Display Conduits