Customize menu or add a button in SCH or PCB

Is it possible to add a menu or button to run python code instead of running from Scripting Console?

I think there was a macro menu sometime back, but it got kicked out due to maintenance issues… no idea if it’s coming back.
Might need to read the dev mailing list and bugtracker.

Thanks Joan for your explanation!

I’ve found out that it’s possible to add menu items to the interface using the python scripting. It requires wxpython support though, I don’t know if stable builds have it enabled.

The catch is; once you have a script to create the menu item, you have to run it from the scripting console the first time. AFAIK there is no other way to trigger a python script in pcbnew. It would be great if there was a python script file that is run at startup automatically.

Here is an example I’ve been working on. It adds a new menu item to “Tools” menu:

import wx

def doSomething(e):
    print("heey")

def findPcbnewWindow():
    windows = wx.GetTopLevelWindows()
    title = "Pcbnew"
    pcbnew = filter(lambda w: w.GetTitle() == title, windows)
    if len(pcbnew) != 1:
        raise Exception("Cannot find pcbnew window from title matching!")
    return pcbnew[0]

def run():
    win = findPcbnewWindow()
    menuBar = win.MenuBar

    # add the menu item under Tools menu
    toolsMenu = menuBar.GetMenu(menuBar.FindMenu("Tools"))
    somethingMenuItem = toolsMenu.Append(wx.ID_ANY, "Something")
    win.Bind(wx.EVT_MENU, doSomething)

if __name__ == "__main__":
    run()
2 Likes

Thanks hyOzd. I will try it.

there is the option to add a start script when starting python console on the recent pcbnew


still you have to run the pyconsole once…

the only other option I know is to add a general start script (i.e. preloader.py) in the source code file
src\kicad\scripting\python_scripting.cpp
// load pcbnew inside python, and load all the user plugins, TODO: add system wide plugins { char cmd[1024]; PyLOCK lock; //maui preloaders snprintf( cmd, sizeof(cmd), "import sys, traceback\n" "sys.path.append(\".\")\n" "import pcbnew\n" "import preloader\n" "pcbnew.LoadPlugins(\"%s\")", aUserPluginsPath ); PyRun_SimpleString( cmd ); }
and build your copy…
I posted this option feature to developers mailing list a while ago, but they generally listen very little…

Be aware that console “Startup Script” is run every time the console window is opened. So it requires extra precaution to not to run a script twice.