General Mouse Function

edited March 2021 in Python scripting

In my quest to bring a mouse function,
that's easy to use and allow's global
storage of mouse clicks .
I need testers and idea's .
Please feel free to upgrade and comment.

Below are 3 scripts and test script
Save each of the 3 script's individually below into C:/Users/your_path/KLayout/pymacros
update in each script to your system path
ie:sys.path.append("C:/Users/your_path/KLayout/pymacros")

Mouse_XY.py gets the mouse clicks
Subfile.py stores the mouse click in a Global Buffer
Settings.py is the Global Buffer

MouseTest.py
Is the test script

####################################################################
# Copy below and save into "C:/Users/your_path/KLayout/pymacros" as Mouse_XY.py
#Tracy Groller
#3/16/2021
#Get Mouse XY
#Save into a Global Buffer for 
#later processing with Settings and Subfile
import pya
import sys
#Import Global 
#Update to your path
sys.path.append("C:/Users/your_path/KLayout/pymacros")
import Settings
import Subfile
Settings.init()  

class GetXYOnMouseClickPluginFactory(pya.PluginFactory):

  def __init__(self):
    super(GetXYOnMouseClickPluginFactory, self).__init__()
    pya.MainWindow.instance()  # (workaround for bug 191)
    self.register(-1000, "mouse_xy", "Get X/Y")
   #Turns off add on to Toolbar reset to visible = True too see the tool bar
    pya.MainWindow.instance().menu().action("@toolbar.mouse_xy").visible = False

  def create_plugin(self, manager, root, view):
    return GetXYOnMouseClickPlugin()

  # Keeps the singleton instance
  instance = None

# Create and store the singleton instance
GetXYOnMouseClickPluginFactory.instance = GetXYOnMouseClickPluginFactory()

# Second thing is the actual plugin
class GetXYOnMouseClickPlugin(pya.Plugin):

  def __init__(self):
    super(GetXYOnMouseClickPlugin, self).__init__()
    pass

  def activated(self):
    pya.MainWindow.instance().message("Click on point to Get X/Y", 10000)

  def deactivated(self):
    pya.MainWindow.instance().message("", 0)

  def mouse_click_event(self, p, buttons, prio):
    # This is where everything happens
    if buttons == pya.ButtonState.RightButton :
          #Right Click ends it process X/Y coords entered
          self.ungrab_mouse()
          pya.MainWindow.instance().cancel()
          pya.MainWindow.instance().menu().action("@toolbar.select").trigger()
    if prio:
      #print("Mouse clicked at X/Y" + str(p))
      pya.MainWindow.instance().message("Mouse Clicked at X/Y = " + str(p), 10000)
      x = p.x
      y = p.y
      #Add points to global buffer
      Subfile.stuff(x,y)
      return True
    return False

####################################################################
# Copy below and save into "C:/Users/your_path/KLayout/pymacros" as Subfile.py
import sys
#Update to your path
sys.path.append("C:/Users/your_path/KLayout/pymacros")
import Settings

def stuff(x,y):
    Settings.myList.append([x,y])

####################################################################
# Copy below and save into "C:/Users/your_path/KLayout/pymacros" as Settings.py
#Global vars settings
# settings.py
def init():
    global myList
    myList = []

####################################################################
# MouseTest main Python Test Script 
# The mouse click's are saved as a buffer within Settings.myList as a list
# Which then can be processed further within this test script.

import pya
import sys
#Import Global 
#Update to your path
sys.path.append("C:/Users/your_path/KLayout/pymacros")
import Settings
import Subfile
import Mouse_XY
#Get Mouse Input
pya.MainWindow.instance().menu().action("@toolbar.mouse_xy").trigger()
print("Mouse X/Y Buffer:",  Settings.myList)
Settings.init()
Settings.mylist = []
print("Reset Mouse X/Y Buffer:",  Settings.myList)
####################################################################
Sign In or Register to comment.