Running Python Plugin with cmd?

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 …

But i want to automate it, the same way we did with the ZONE_FILLER further up in this post.
To have a skript that can be run with cmd, so i don´t even have to open any Programm und just run this skript.

I don’t think there is API for it. If you have to automate it you will have to use mouse/keyboard emulation and automate launching the program and clicking menus/buttons with macro recording software. It’s not clean but it works.

You just made it up, there is no such command.

Maybe ChatGPT thinks there is :wink:

Or thinks there should be. :rofl: AI fibbing? Ha ha ha.

If you’d like to use it interactively in mintty (MSYS2), part of this could help:

PYTHONIOENCODING=UTF-8 winpty …/KiCad/7.0/bin/python.exe -im __init__

I found the following commad:

Collinear(SEG self, SEG aSeg) -> bool

https://docs.kicad.org/doxygen-python-7.0/classpcbnew_1_1SEG.html#a7e60f4067f5730e624d0ba891bc8ac1a

Can´t that be used simmilar to the zone_filler?

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)

I tried to change the skript to work with Collinear but I couldn´t make it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.