HOWTO: Register a python plugin inside pcbnew Tools menu

Ok so you can for example use some wx message box:

import wx
ā€¦
wx.MessageDialog(None,ā€œMy messageā€)

2 Likes

thx,

I had to write:

wx.LogMessage(ā€œMy messageā€)

to see the window
but this is fine for me :wink:

2 Likes

Would it be possible to execute scripts on specific events? Like on import of netlist.
I would like to make the revision on the silkscreen update from the revision in the netlist/schematic

This feature is also planned. Not yet available.

2 Likes

Hi again,
I noticed that when a script is executed, the board get touched and Save button become enabledā€¦
The same script if launched in python scripting console doesnā€™t do the sameā€¦
Is that something expected?
Here a simple example that triggers Save Buttonā€¦

import wx
import pcbnew
from pcbnew import *

class minimal_test( pcbnew.ActionPlugin ):

def defaults( self ):
    """
    Method defaults must be redefined
    self.name should be the menu label to use
    self.category should be the category (not yet used)
    self.description should be a comprehensive description
      of the plugin
    """
    self.name = "minimal test"
    self.category = "Checking PCB"
    self.description = "minimal test"
def Run( self ):
    
        wx.LogMessage("test")

minimal_test().register()

PS
Do you know why this nice tool is not available as default in nightly builds?
I use to build my own copy of kicad, but I discovered that in windows nightly it is not enabled ā€¦

Maurice

@jsreynaud
here a first Action Plugin Script converted from python console to AP :slight_smile:


action_menu_annular_check.py

3 Likes

Hi,

KiCad is unable to detect what your script is trying to do. So it assume that the script is modify the board.
An history in the undo/redo list is added and the board is marked as changed.
Event if you script doesnā€™t modify anything ā€¦

About nightly build Iā€™ll ask to @nickoe.

JS

2 Likes

yes, I noticed in the the cpp code

would be nice to have a return code from the python script (i.e. default = 1 #ā€˜board modifiedā€™) that the script can assign to 0 (board unmodified) so the board can be marked as changed only if the script is doing something that really change it?

that will allow a large amount of users to have this useful option enabled and will give developers a useful feedback ā€¦
Very little amount of users are building kicad from sourceā€¦
In fact this Action Plugin does not add any risk to those already present with the python console, but it is giving a more user friendly way to use python inside Kicad
Looking forward to see it in Nightly :wink:

3 Likes

I look forward too. I am on a mac :slight_smile:

1 Like

I had also ask to @adamwolf for that point.

1 Like

I think I can automatically detect if there are some modifications or not. Iā€™ll try this autodetection on a future patch.

1 Like

it seems that now the Action Plugin is delivered with the Nigtlies :smiley:

Application: kicad
Version: (2017-04-13 revision c4ea54227)-makepkg, release build
Libraries: wxWidgets 3.0.2
libcurl/7.52.1 OpenSSL/1.0.2k zlib/1.2.11 libssh2/1.8.0 nghttp2/1.19.0 librtmp/2.3
Platform: Windows 8 (build 9200), 64-bit edition, 64 bit, Little endian, wxMSW

  • Build Info -
    wxWidgets: 3.0.2 (wchar_t,wx containers,compatible with 2.8)
    Boost: 1.60.0
    Curl: 7.52.1
    KiCad - Compiler: GCC 6.3.0 with C++ ABI 1010
    Settings: USE_WX_GRAPHICS_CONTEXT=OFF
    USE_WX_OVERLAY=OFF
    KICAD_SCRIPTING=ON
    KICAD_SCRIPTING_MODULES=ON
    KICAD_SCRIPTING_WXPYTHON=ON
    KICAD_SCRIPTING_ACTION_MENU=ON
    BUILD_GITHUB_PLUGIN=ON
    KICAD_USE_OCE=ON

From now on, having a larger testing audience is much easier :wink:

I added an other Action Script to my repo:

Maurice

1 Like

Thanks to @skuep, more detail about how to make a ā€œcleanā€ plugin:

While for single file plugins you can put that single file in the plugin directory, multiple files/complex plugin should be created with an other way. You can put all your files inside a sub-directory and put that sub-directory to the plugin directory.

An example:

  • ~/.kicad_plugin/ The main directory
  • MyPlugin/ Your plugin directory
    • _init_.py A single file to register the plugin
    • date_plugin.py The plugin himself
    • ā€¦ any other files

Content of date_plugin.py

import pcbnew
import re
import datetime

class text_by_date( pcbnew.ActionPlugin ):
    def defaults( self ):
        self.name = "Add date on PCB (onto a sub dir)"
        self.category = "Modify PCB"
        self.description = "Automaticaly add date on an existing PCB"
 
     def Run( self ):
         pcb = pcbnew.GetBoard()
         for draw in pcb.m_Drawings:
             if draw.GetClass() == 'PTEXT':
                 txt = re.sub( "\$date\$ [0-9]{4}-[0-9]{2}-[0-9]{2}",
                              "$date$", draw.GetText() )
             if txt == "$date$":
                 draw.SetText( "$date$ %s"%datetime.date.today() )

Content of _init_.py

from date_plugin import *
text_by_date().register()
4 Likes

I can not add this plugin even I have copied the script to those directories, how can I fix it ?
I have refreshed plugin and restart kicad, but external plugin is still not displayedā€¦??

What version of KiCad, OS etc?
The plugin is for Nightly builds, not the ā€œStableā€ 4.0.x series

It is nightly build version. Os is Ubuntu 16.04.
I can t refresh plugin and the script has been copied to plugin directory.
How can I fix it?
Thx

It is kicad version 4.xx but I can not see it now. I m not on my computer.

4.xx is not Nightly. Your two last posts are contradicting each other.

Hereā€™s the version I saw in Help --> About

Version : no-vcs-found-33048e7~59~ubuntu16.04.1,release build

Is that the right one ?