Running Python Plugin with cmd?

Ah, ok I got confused because I used those to create regex matchers.

DATED = re.compile(r'.*\.(aac|mp3|pdf)$')

Yeah they are commonly used for regexes because regexes often contain backslashes themselves, and it is nice to not have to escape them.

To be honest:
I´m really confused by all those responses. I have no idea about coding in python and just searched on google.

I want to be abel to start a plugin without opening Kicad.

The follwoing Plugin already worked in KiCad.

import pcbnew
import os

class ZoneFillerPlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Zone Filler"
        self.category = "Modify PCB"
        self.description = "Fill all zones on the PCB"

    def Run(self):
        board = pcbnew.GetBoard()
        filler = pcbnew.ZONE_FILLER(board)
        zones = board.Zones()
        filler.Fill(zones)

ZoneFillerPlugin().register()

If you have no idea about coding in Python, this is perhaps a hard place to start.

The plugin you found is an action plugin, it is designed to be run from within KiCad. You need to make further modifications to it to run outside of KiCad, but if you don’t know Python, your best bet is to see if someone else is willing to do that work for you (I do not have time at the moment to make a complete version and test it)

If your goal is just to fill zones from the command line, maybe someone has a standalone script already that does this.

2 Likes

Ok thanks for the explanation!

For me, using the CMD-line in the Python Console is bothersome so, for non-Kicad-specific App’s, I prefer to build the Python code as an executable Stand-Alone-App and Run it (call it) from a Kicad Plugin.

So, first step is to build the code so it runs by using the Simple Plugin code setup (search for the Documentation). And, place your .py in the Scripting>Plugin folder (add an Icon to it or it gets a default icon). If it does the stuff on the PCB you want, continue…

It should run. If successful, you can build an Executable App by various means - I’ve used several but, prefer PyInstaller (note the Uppercase letters - there’s a bad look-alike pyinstaller (same name but Not the same code).

Once successful making it a Stand-Alone App, you can run it from a plugin by following my post(s) here. If you scroll through the full post, you’ll see link to my Colorizer App that contains a Plugin Folder (it contains the needed code but, you may need to Tweak it per the discussion so it runs on your OP system…)

you can modify this plugin a bit and make it work from cmd.

Create zonefiller.py:

import pcbnew

if __name__ == "__main__":
    board_path = "/home/aws/demo.kicad_pcb"
    board = pcbnew.LoadBoard(board_path)
    filler = pcbnew.ZONE_FILLER(board)
    zones = board.Zones()
    filler.Fill(zones)
    pcbnew.SaveBoard(board_path, board)

and run it with python zonerfiller.py

1 Like

It sadly doesn´t work.
Is Python 3.11.4 (64-Bit) the right Version?
Is there anything else that has to be installed? (besides KiCad obviously)
Do i have to change anything in the “main”?

When reporting that something doesn’t work you should specify as much detail as you can so others can help you. How exactly doesn’t it work? Was there any error output? How did you run it? Did you edit the path to the board? If you are on windows are you using python bundled with kicad?

I did exactly as adamws told me and changed the path.
When I typed python zonefiller.py in the cmd nothing happend. The cmd had no output and the zones weren´t filled.

And what do you mean with: “python bundled with kicad”? The Python scripting console?
Sorry I´m completly new to KiCad an Python.

On windows kicad installer contains it’s own python executable and libs. You have to use that if you want your scripts to be able to access pcbnew module, which the script provided by adamws does. So instead of just typing python in cmd you have to put full path to python.exe that is in kicad’s install directory.
For example:

"c:\Program Files\Kicad\7.0\bin\python.exe" zonefiller.py

Note the quotes around the path, they are necessary if you have spaces in the folders.

The command above also assumes that the working directory of your cmd window is where the zonefiller.py file is. If not then provide full path to the script too.

That command must have some output, it will not fail silently, if you still have an error post the output here.

Ok I did as you sad, executet cmd in the folder, where zonefiller.py is located and got this error:

C:\testpy>"C:\Program Files\KiCad\bin\python.exe" zonefiller.py
Traceback (most recent call last):
  File "zonefiller.py", line 5, in <module>
    board = pcbnew.LoadBoard(board_path)
  File "C:\Program Files\KiCad\lib\python2.7\site-packages/pcbnew.py", line 6876, in LoadBoard
    return _pcbnew.LoadBoard(*args)
IOError: Unable to open filename "C:    estpy\manual_1.kicad_pcb" for reading

I tried different folders but a part of the path in the last line is always missing.
The path should be: “C:\testpy\manual_1.kicad_pcb” and is correctly writen in the “zonefiller.py” file.
Could this be the Problem?

Yes, that is the problem. In python normal strings use backslash \ for escaping, it is interpreting \t as a tab char. To fix this either use double backslash C:\\testpy\\manual_1.kicad_pcb or raw string by prepending r before the string like this r"C:\testpy\manual_1.kicad_pcb" to disable escaping.

Ok that worked.

Now i got the next Error:

Full error text:
Expecting "'symbol'" in input/source
"C:\testpy\manual_1.kicad_pcb"
line 1, offset 48

Line 1 in that File is: “(kicad_pcb (version 20221018) (generator pcbnew)”
offset 48 would be right after the “w”

Do you have multiple kicad versions installed? That sounds like either the file got corrupted by earlier manipulations or you are using python from older kicad to try to open file created by newer kicad.

2 Likes

You found the problem!

I had KiCad 5 installed and used the path: “C:\Program Files\KiCad\bin\python.exe” zonefiller.py.
The right path is: “C:\Program Files\KiCad\7.0\bin\python.exe” zonefiller.py

I used the Path without the \7.0 an therefore the python version for KiCad 5.

Big thanks for helping me out!!

1 Like

I have another question:
Is ist possible to do the same for the tool:

Cleanup Graphics → Delete redundant graphics → Update PCB

and for:

Import → Graphics

I can´t find the python commands for that.

I found something in the sourcecode of KiCad: “C:\kicad-master\pcbnew\dialogs\dialog_cleanup_graphics_base.cpp” Line 113

cleaner.CleanupBoard( aDryRun, &m_items, m_createRectanglesOpt->GetValue(),
                                             m_deleteRedundantOpt->GetValue(),
                                             m_mergePadsOpt->GetValue() );

Do you think its possible to call that function with cmd and hand over the boardpath?
If it´s possible with a python scirpt, I would prefer not accessing the sourcecode.

I don’t think you can call those from python but at least the clean up doesn’t do much beyond removing 0 length segments and combining collinear and coincidental segments. It can be reimplemented in python.

Import graphics doesn’t have an easy solution though.

Ok. I want to use the Cleanup, because some .dxf Files that i´m importing have two outlines ontop off each other(collinear).
At the moment i´m deleting them with

Cleanup Graphics → Delete redundant graphics → Update PCB

now i want to automate that the same way we did with the zone_filler.

it is better to edit the contours in another program, for example qcad, then import it into the kicad, this also applies to 3d models …