Skip to main content

kigadgets

a.k.a. kicad-python: atait fork

A stable Python scripting API for KiCad based on Piers Titus van der Torren work and community input.

Documentation

KiCad and pcbnew expose a python interface that allows plugins and other procedural processing of PCB layouts. While it is a major differentiator of KiCad, there are limitations of using this python API directly. kigadgets attempts to simplify the software design to a level where hardware designers are able to write (and maintain) their own code for software-assisted layout.

kigadgets implements cross-version compatible and more intuitive patterns for objects, properties, units, string layers, and so on. It constructs an environment where pcbnew.py functionality can be used headless (outside the GUI), and the pcbnew GUI can import external python packages at runtime. Furthermore, it enables patterns for more advanced software engineering such as multiple entry points, layout hashes, and regression tests.

This package has been fully tested with KiCad 5.0 through KiCad 10.0 (Mac/Windows/Linux).

See tests to reproduce headless behavior and examples to reproduce GUI behavior. Note that v9 and v10 will give warnings about SWIG being deprecated, but they work just fine.

An excerpt

A simple pythonic script might look like this

print([track.layer for track in pcb.tracks])
print([track.width for track in pcb.tracks if track.is_selected])

which produces

[F.Cu, B.Cu, B.Cu]
[0.8, 0.6]

The python wrapper is handling things like calling the (sometimes hard to find) function names, sanitizing datatypes, looking up layers, and enabling the list comprehension.

track and board use properties to give an intuition of state, but they are dynamically interacting with the underlying C++ PCB_TRACK and BOARD that you see in the layout editor.

Installation via PyPI

pip install kigadgets
python -m kigadgets

The python -m kigadgets command automatically links paths needed for headless scripts to import the pcbnew module and for GUI plugins to find python packages external to KiCad, including kigadgets. For more detail on what the linker is doing, why, and advanced options, see here.

Mac users: There is an extra step. The above command will walk you through it. For more information about kipython on Mac, see the docs.

Try it out: Quit and reopen pcbnew application. Open its terminal, then run

pcb.add_circle((100, 100), 20, 'F.Silkscreen'); pcbnew.Refresh()

Snippet examples

These snippets are run in the GUI terminal. There is no preceding context; the linking step above provides pcb to the terminal. More examples in the docs.

Hide silkscreen labels of selected footprints

for fp in pcb.footprints:
    if fp.is_selected:
        fp.reference_label.visible = False
pcbnew.Refresh()

Change all drill diameters

Because planning ahead doesn't always work

for v in pcb.vias:
    if v.drill > 0.4 and v.drill < 0.6:
        v.drill = 0.5
pcbnew.Refresh()

Select everything schematically connected to this footprint

footprint = pcb.selected_items[0]
nets = {pad.net_name for pad in footprint.pads}
nets -= {'GND', '+5V'}  # because these are connected to everything
for mod in pcb.footprints:
    if any(pad.net_name in nets for pad in mod.pads):
        mod.select()

User input via layout

from kigadgets.drawing import Rectangle
my_rect = Rectangle((0,0), (60, 40))
pcb.add(my_rect)
pcbnew.Refresh()
print(my_rect.x, my_rect.contains((1,1)))  # 30 True
input('Go move that rectangle. When done, refocus in this terminal and press enter.')
# Go move the new rectangle in the editor
print(my_rect.x, my_rect.contains((1,1)))  # 15.2 False
pcb.remove(my_rect)
pcbnew.Refresh()

Action plugin examples

Mousebite in 200 lines

It is not feature-rich, but it works with very little code, basic user input, multiple entry points, and GUI Ctrl-Z behavior. Use it as a template for making and packaging your own action plugins based on kigadgets. Read about it here.

Mousebite demo

Onepush action

This demonstrates a GUI design workflow that is tightly integrated with external code: on-the-fly external module reloading, layout animation, and how to hotkey arbitrary code. The plugin is useful in its own right in addition to being a demonstration of some GUI-related functionality. Read about it here.

Onepush demo

Stodgier features

While helpful for small scripting (above), kigadgets also provides significant support for maintaining more complicated codebases that use pcbnew.py. It can give cross-version compatibility, code that is concise and modular (i.e. easier to maintain), and multiple CLI/API/GUI entry points.

All kigadgets.BoardItems are hashable based on their geometric contents. Together with lytest, these enable automated testing and things like diff --stat, ultimately giving more workflow options for developing action plugins and batch processing codebases.

See discussion on software engineering features in the docs.

KiCad has a rich landscape of user-developed tools, libraries, and plugins. It is worth understanding this landscape in order to use the right tool for the job, whether it turns out to be kigadgets, others, or multiple. See discussion of the landscape in the documentation.

Metadata

Release files for kigadgets 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kigadgets 0.5.2
File Size Uploaded
kigadgets-0.5.2.tar.gz 46.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kigadgets 0.5.2
File Interpreter ABI Platform
kigadgets-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 91.8 kB

Release files / kigadgets-0.5.2.tar.gz

Download URL kigadgets-0.5.2.tar.gz
Size 46.5 kB
Tags Source
SHA-256 checksum
How to use checksums
21b1d949d0daa108d650a2031c0e3dab4e34f999e3d83bdf3775e36ad567604b
BLAKE2b-256 checksum
How to use checksums
e809e7d7697b03777d99be7dfedb1d52bb619890279db6eacf4eae0e23090f17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.18

Release files / kigadgets-0.5.2-py3-none-any.whl

Download URL kigadgets-0.5.2-py3-none-any.whl
Size 45.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a635fe48ede9834152f47605bef71342d583036eadb744136210b6eaf38d0cc2
BLAKE2b-256 checksum
How to use checksums
bcc8bc5bda6684cd2229cfc26968fc58283e40d64815e38555d9962e575120a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page