HOWTO: Register a python plugin inside pcbnew Tools menu

suppose to be :
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() )
text_by_date().register()
?

which python do I need for it ?

That’s python I have in my computer,
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type py
py3clean pybuild pydoc pydoc3.5 pygettext3 python python2.7-config python3.5 python3.5m-config python3m-config
py3compile pyclean pydoc2.7 pygettext pygettext3.5 python2 python2-config python3.5-config python3-config python-config
py3versions pycompile pydoc3 pygettext2.7 pyhtmlizer python2.7 python3 python3.5m python3m pyversions
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type python2.7
python2.7 is /usr/bin/python2.7
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type python2
python2 is /usr/bin/python2
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ type python3
python3 is /usr/bin/python3
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$

Do I miss something else ?
Thanks

I don’t know how to get code to format properly here but I will try

#!/usr/bin/python
import pcbnew
import re
import datetime


class text_by_date(pcbnew.ActionPlugin):

    def defaults(self):
        self.name = 'Add date on PCB'
        self.category = 'Modify PCB'
        self.description = 'Automaticaly add date on an existing PCB'

    def Run(self):
        pcb = pcbnew.GetBoard()
        for draw in pcb.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())


text_by_date().register()

It looks ok in the preview window.

If it shows in the plugins menu now, you will probably still get some other errors because of how quickly the code base is changing. The script probably needs rewriting from the beginning.

Just substitute pcb.Drawings() with pcb.GetDrawings().
Like on this example: https://git.launchpad.net/kicad/tree/demos/python_scripts_examples/action_menu_text_by_date.py

:information_source: Undocumented forum tip : for best code formatting, use triple backtick : ```

e.g. typing

```
    if self.num_inputs>4:
        height = (self.num_inputs-1) * 100
        top = int(height/100/2) * 100
```

becomes

    if self.num_inputs>4:
        height = (self.num_inputs-1) * 100
        top = int(height/100/2) * 100

Now, where the heck is the “backtick” key… on my keyboard it’s the top left key above Tab

4 Likes

Many thanks jsreynaud, just tried the modified code and it works fine.

Thanks bobc, I will use your tip the next time I try and post something

I have made some indents as suggested and put the script on ~/.kicad_plugins


but I still can not refresh plugin ??..

Try to run your script in command line directly by running:
python2 action_menu_text_by_date.py

And share the result.

The script in the screenshot still refers to

for draw in pcb.m_Drawings:  

not

for draw in pcb.GetDrawings():  

which generates an error on my machine

Here’s the output

python2 action_menu_text_by_date.py

File “action_menu_text_by_date.py”, line 8
self.name = “Add date on PCB”
^
IndentationError: expected an indented block

Here’s the output :

====

bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:~/.kicad_plugins$ python2 action_menu_text_by_date.py
File “action_menu_text_by_date.py”, line 8
self.name = “Add date on PCB”
^
IndentationError: expected an indented block

========

and from the other location :

bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:/usr/share/kicad/scripting/plugins$ python2 action_menu_text_by_date.py
File “action_menu_text_by_date.py”, line 8
self.name = “Add date on PCB”
^
IndentationError: expected an indented block
bianchi@bianchi-HP-Pavilion-dv6-Notebook-PC:/usr/share/kicad/scripting/plugins$

How to fix it ? I’m not using phyton, so I don’t understand it.

The script is improperly formatted python. Specifically the indentation is not correct (which is very meaningful in python).

The three lines after “def defaults…” must each be indented (the three lines that begin with “self.”. Also, make sure all indentions throughout the file are of the same type: either spaces (usually 4 spaces per each indention) or tabs.


Could the identation be like that ?

The menu can be displayed correctly now, thank you very much everyone :grinning:

When I click the menu I got exception :

How to fix it ?
Use the default code ?

You should add brackets after the GetDrawings: GetDrawings(). It’s a function call.

It’s working now, thanks everyone

1 Like

Hey I’m also trying to get this to run. This is my version info:

Application: kicad
Version: (2017-11-28 revision f992d14)-master, release build
Libraries:
    wxWidgets 3.0.2
    libcurl/7.43.0 SecureTransport zlib/1.2.5
Platform: Mac OS X (Darwin 15.6.0 x86_64), 64 bit, Little endian, wxMac
Build Info:
    wxWidgets: 3.0.2 (UTF-8,STL containers,compatible with 2.8)
    Boost: 1.61.0
    Curl: 7.43.0
    Compiler: Clang 7.3.0 with C++ ABI 1002

Build settings:
    USE_WX_GRAPHICS_CONTEXT=ON
    USE_WX_OVERLAY=ON
    KICAD_SCRIPTING=ON
    KICAD_SCRIPTING_MODULES=ON
    KICAD_SCRIPTING_WXPYTHON=ON
    KICAD_SCRIPTING_ACTION_MENU=ON
    BUILD_GITHUB_PLUGIN=ON
    KICAD_USE_OCE=ON
    KICAD_SPICE=ON

I copied the plugins from MitjaNemec and jsreynaud into my ~/kicad_plugins directory like this:

I can run import replicatelayout in the scripting console but nothing happens (no error, no crash, no hang, just a new line in the console prompt). The external plugins menu remains empty as well. What am I doing wrong?

Do other action plugins work when put into ~/.kicad_plugins ?

Mine works when I put it into the installation folder (on Windows nightly)

C:\Program Files\KiCad\share\kicad\scripting\plugins\replicate_layout

Application: kicad
Version: (2017-11-28 revision f107545a8)-makepkg, release build
Libraries:
wxWidgets 3.0.3
libcurl/7.54.1 OpenSSL/1.0.2l zlib/1.2.11 libssh2/1.8.0 nghttp2/1.23.1 librtmp/2.3
Platform: Windows 7 (build 7601, Service Pack 1), 64-bit edition, 64 bit, Little endian, wxMSW
Build Info:
wxWidgets: 3.0.3 (wchar_t,wx containers,compatible with 2.8)
Boost: 1.60.0
Curl: 7.54.1
Compiler: GCC 7.1.0 with C++ ABI 1011

Build 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
KICAD_SPICE=ON