Hello,
I’m trying to get the current mouse canvas position via python and while I think I’m calling the correct methods I can’t figure out how to cast the IronPython.Runtime.Types.ReflectedProperty or IronPython.Runtime.BuiltinFunction (.GetValue method). The method I’m calling says it returns a PointF struct but I’m not sure how to get the actual XY point values as calling .X or .Y on the position (or and index 0, or 1) does not seem to yield a number value I would expect from a point2d for instance.
Getting the windows cursor position is working just fine but I need the GH canvas position.
Thanks so much for the help!
Graph Space:
Code Thus Far:
import Grasshopper as gh
import System.Drawing as sd
import System.Windows.Forms as Forms
# Function to add a panel at the specified location
def add_panel(x, y):
# Create a Panel component at the specified (x, y) location
panel = ghenv.Component.OnPingDocument().AddObject("Panel", x, y, False)
# Output the (x, y) coordinates of the created panel in canvas space
return ghenv.Component.Attributes.Bounds.Location
# Get the current component position in the Grasshopper canvas space
current_component_position = ghenv.Component.Attributes.Bounds.Location
# Output the current component position in the O output
O = current_component_position
# Get the cursor position from Windows
Wp = Forms.Cursor.Position
# Get the cursor position from the GH Canvas Mouse Event
mouse_loc_x = gh.GUI.GH_CanvasMouseEvent.CanvasX.GetValue
mouse_loc_y = gh.GUI.GH_CanvasMouseEvent.CanvasY.GetValue
mouse_loc = (mouse_loc_x,mouse_loc_y)
cursor_position = mouse_loc
# Get The X,Y Canvas Position Of The Mouse Cursor
Cp = cursor_position
print Cp
## Check if the "P" key is pressed
#if ghenv.Component.OnPingDocument().KeyDown.IsKeyDown(Forms.Keys.P):
# # "P" key is pressed, create a Panel component at the Cp position
# ghenv.Component.OnPingDocument().CreateDefaultAttributes()
# panel_guid = ghenv.Component.OnPingDocument().AddObject("Panel", Cp.X, Cp.Y, False)
#
# # Output the (x, y) coordinates of the created panel in canvas space
# add_panel(Cp.X, Cp.Y)
20230823_Add_Panel_On_Wire_Drag_01a.gh (6.7 KB)
8 posts - 4 participants
