I’ve given the plotting API some more thought, and I believe it’s better to use the KiCad CLI than the API for the actual plotting. The reason for this is that Board2Pdf has a CLI as well, and since the new API only works when KiCad is running, it won’t work as people expect.
I’ll have a closer look at the KiCad CLI and add a feature request for anything that’s needed for Board2Pdf to use it. I’m pretty sure not everything is in there as of now. For instance I would like to be able to set the worksheet and read and set Comment variables. I understand if these things goes against what the CLI should support, but I’ll add a feature request to at least start a dialog.
you can set the worksheet with kicad-cli today (--drawing-sheet).
You can also define project variables (--define-var) but I’m not sure whether or not that’s what you’re after - I think that will only fill in the comment fields in the drawing sheet title block if you’ve set up those fields ahead of time so that they’re filled with a text variable (${MY_COMMENT1}) which you’d then populate by defining that var in a kicad-cli arg.
I finally managed to build kicad-python (I think).
I wasn’t able to install protobuf-compiler using vcpkg. Seems like there are problems with a dependency called absail. But I was able to install protobuf using winget install protobuf. After doing that, pip install -e . worked.
But after installing the latest kicad-python, all examples gives me this error:
$ python hello.py
Traceback (most recent call last):
File “C:\git\other-repos\kicad-python\examples\hello.py”, line 20, in
from kipy import KiCad
File “C:\git\other-repos\kicad-python\kipy_init_.py”, line 18, in
from kipy.kicad import KiCad
File “C:\git\other-repos\kicad-python\kipy\kicad.py”, line 28, in
from kipy.board import Board
File “C:\git\other-repos\kicad-python\kipy\board.py”, line 22, in
from kipy.board_types import (
File “C:\git\other-repos\kicad-python\kipy\board_types.py”, line 22, in
from kipy.proto.common.types import KIID
File “C:\git\other-repos\kicad-python\kipy\proto\common_init_.py”, line 20, in
from .envelope_pb2 import *
ModuleNotFoundError: No module named ‘kipy.proto.common.envelope_pb2’
Any idea what might be wrong?
I tested changing the version in pyproject.toml to 0.1.99 to see that it works, and it seems to work fine to compile and install.
$ pip show kicad-python
Name: kicad-python
Version: 0.1.99
Some other binary needed I suppose. I’m not sure which…
$ poetry build
Preparing build environment with build-system requirements poetry-core, protoletariat == 3.3., setuptools, mypy-protobuf == 3.6.
Building kicad-python (0.1.1)
Building sdist
Built kicad_python-0.1.1.tar.gz
Building wheel
A setup.py file already exists. Using it.
Generating protobuf wrappers…
Generating Python classes from protobuf files…
C:\git\other-repos\kicad-python\kicad/api/proto: warning: directory does not exist.
Missing input file.
Post-processing with protoletariat…
Traceback (most recent call last):
File “C:\git\other-repos\kicad-python\setup.py”, line 39, in
build(setup_kwargs)
File “C:\git\other-repos\kicad-python\build.py”, line 43, in build
pre_build()
File “C:\git\other-repos\kicad-python\build.py”, line 28, in pre_build
generate_protos(proto_in, proto_out)
File “C:\git\other-repos\kicad-python\tools\generate_protos.py”, line 48, in generate_protos
subprocess.run([protol,
File “C:\Program Files\Python311\Lib\subprocess.py”, line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Program Files\Python311\Lib\subprocess.py”, line 1024, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File “C:\Program Files\Python311\Lib\subprocess.py”, line 1509, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] The system cannot find the file specified
Command ‘[‘C:/Users/denne/AppData/Local/Temp/tmpmhryzaft/.venv/Scripts/python.exe’, ‘C:\git\other-repos\kicad-python\setup.py’, ‘build’, ‘-b’, ‘C:\git\other-repos\kicad-python\build’, ‘–build-purelib’, ‘C:\git\other-repos\kicad-python\build\lib’, ‘–build-platlib’, ‘C:\git\other-repos\kicad-python\build\lib.win-amd64-cpython-311’]’ returned non-zero exit status 1.
Seems to work now! Had to install protoletariat and mypy-protobuf using pip install.
I didn’t have poetry virtualenv activated at first, but I then I followed the instructions in COMPILING.md and activated it.
I’m way in over my head here, but seems to work now! Thanks again @craftyjon
Now the create_zone.py example works as well.
The layer_indicator.py doesn’t work though. Looking at the Automatically-generated documentation for kicad-python I cannot find an attribute called ‘get_text_extents’.
EDIT: But I suppose the auto-generated docs is for the latest kipy release…
$ python layer_indicator.py
Traceback (most recent call last):
File “C:\git\other-repos\kicad-python\examples\layer_indicator\layer_indicator.py”, line 40, in
char_width = board.get_text_extents(sizing_text).size.x
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: ‘Board’ object has no attribute ‘get_text_extents’
The stuff albin and I are talking about is for developing the API (because they want to test using features that are not yet released to PyPI), not using the API, so it is more complex. There will eventually be more guides for users of the API, but they will assume that you are not a beginner to Python. If someone else wants to make a guide that is for complete beginners to Python that would be welcome, but it’s not something I’m going to do.
While your at it, I believe the round_tracks example has broken as well. It seemed to work fine with the last release of kicad-python (I’ve never tried it with a board where it actually does something), but with the kicad-python I compiled I get this error:
C:\git\other-repos\kicad-python\examples\round_tracks>python round_tracks_action.py
Traceback (most recent call last):
File “C:\git\other-repos\kicad-python\examples\round_tracks\round_tracks_action.py”, line 138, in run
self.allTracks = self.board.get_tracks()
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\git\other-repos\kicad-python\kipy\board.py”, line 353, in get_tracks
for item in self.get_items(
^^^^^^^^^^^^^^^
File “C:\git\other-repos\kicad-python\kipy\board.py”, line 347, in get_items
return [unwrap(item) for item in self._kicad.send(command, GetItemsResponse).items]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\git\other-repos\kicad-python\kipy\client.py”, line 87, in send
raise ApiError(f"KiCad returned error: {reply.status.error_message}",
kipy.errors.ApiError: KiCad returned error: no handler available for request of type kiapi.common.commands.GetItems
With the latest Nightly, the round tracks example initiates just fine when placing it in C:\Users\denne\Documents\KiCad\9.0\plugins. But every time I open the plugin I get an empty console opened as well. Just thought it was worth mentioning. No idea if it’s because how KiCad loads the plugin, or because of something in the plugin.
Did you run git submodule update --init in kicad-python folder as noted in COMPILING.md (before running poetry build)? That’s the only thing I can think of so far that would explain it. The Round Tracks example works for me.
Yes I did. That’s strange. I can follow the steps again tomorrow to see if I can get it to work. If it works for you then it’s probably some on my side. At least the other three examples work for me…
Tried again, but still cannot get the round tracks example to work when I build kicad-python myself. I get the same error message when I press Run.
(.env) C:\Users\denne\Documents\KiCad\kicad-python\examples\round_tracks>python round_tracks_action.py
Traceback (most recent call last):
File “C:\Users\denne\Documents\KiCad\kicad-python\examples\round_tracks\round_tracks_action.py”, line 138, in run
self.allTracks = self.board.get_tracks()
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\denne\Documents\KiCad\kicad-python\kipy\board.py”, line 353, in get_tracks
for item in self.get_items(
^^^^^^^^^^^^^^^
File “C:\Users\denne\Documents\KiCad\kicad-python\kipy\board.py”, line 347, in get_items
return [unwrap(item) for item in self._kicad.send(command, GetItemsResponse).items]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\denne\Documents\KiCad\kicad-python\kipy\client.py”, line 87, in send
raise ApiError(f"KiCad returned error: {reply.status.error_message}",
kipy.errors.ApiError: KiCad returned error: no handler available for request of type kiapi.common.commands.GetItems
The Layer Indicator example works now, and hello.py and create_zone.py still works.
The poetry shell command is no longer included in poetry, and when installing it using poetry self add poetry-plugin-shell I got some errors. So I followed Option 2 instead. https://python-poetry.org/docs/cli/#shell
Obviously it’s not a problem for me that the Round Tracks example doesn’t work for me. But it’s strange that it works for you and not me. If I do pip uninstall kicad-python followed by pip install kicad-python it works fine again. Only caveat that it doesn’t exit when done. I have to press Ctrl+C after it’s finished.
@craftyjon is there a way to get user defined layer names and “default” layer names over the new API?
KiCad CLI expects layer names such as “F.Cu” and “B.Cu”. That’s what I meant with “default” names.
I’ve managed to figure out that I can get ALL layer identifyers from BoardLayer.keys(), but they are in another format (BL_F_Cu, BL_B_Cu).
Sure, I could write a dict that translates these. Or possibly even do it by replacing characters. But it would be nice if I didn’t have to.
I’ve also figured out that I can get all layers that are enabled on the board by:
for layer in stackup.layers:
print(BoardLayer.Name(layer.layer))
But I can only figure out how to get id and name (such as BL_F_Cu). It would be helpful to also get the user defined name. Partly because it helps users in the Board2Pdf GUI, and partly because plotted pdfs are appended with the user defined name.