Tutorials on python scripting in pcbnew

I have a new post/youtube:
Kicad C++ walkthrough (adding python footprint APIs)

First, two important notes:

  • It came to my attention that kicad 4.0.7 is missing a bunch of python interface related stuff. From my experience, the nightlies available from the kicad download page are quite good and stable.
  • The first post of this thread has been converted to a wiki and I will use it as a sort of table of contents for this thread.

This time, I talk a bit about the c++ code of pcbnew, focusing on the parts that relate to the python interface.

I also have a related blog post, but it doesn’t really add anything.
https://kicad.mmccoo.com/2018/04/03/kicad-c-walkthrough-footprint-apis/

3 Likes

I have a new video demonstrating some plugins I wrote for importing fusion360 DXF files. This is basically the same as my previous post, except “productized” as plugins.

The code for the plugins can be found here:
https://github.com/mmccoo/kicad_mmccoo

See the README on how to enable them in your env.

Hope you find it helpful.

4 Likes

3 posts were split to a new topic: Getting the area of the board

This post isn’t directly about scripting in pcbnew but is likely of interest to those running linux. In recent months, it’s been difficult/impossible for some to run pcbnew scripting at all due to seg faults.

I recommend running ubuntu 16.04 with the kicad nightly build

Ubuntu 18 and Linux Mint (based on ubuntu) expose some incompatibilities between Kicad and the libraries it depends on. Notably wxPython, but also wxWidgets and gtk. it’s a version mismatch thing.

As far as I understand it, the version mismatches have always been there, but we’ve gotten away with it. Recent distros have cleaned up which versions are installed to be more correct and it’s exposed our sins.

In my experience, the kicad team does a fantastic job in keeping the nightlies functional. In fact, because the official releases as so far between, it’s quite possible that the python scripts you find for pcbnew will not work on any official release. This is often the case with my own plugins. As I develop them, I discover that something is missing from the pcbnew API. So I implement it and submit a patch.

1 Like

I have a new video and some new code. This time it’s about a way to record the actions of a pcbnew plugin. It generates a file listing all of the pcbnew calls made and sourcing that file should produce the same output.

There are two use cases I had in mind.

  1. creating regression tests. The kicad team doesn’t want your plugin code (I assume they don’t) but plugins are a great way to test whether something in pcbnew’s python has changed. So Instead of running the plugin, run a journal of a run.

  2. Learning how someone did something. Maybe I want to do something similar to someone else’s plugin. Maybe that plugin has some sections that I just don’t understand. Perhaps the math is not relevant to what I want, maybe I’m a new programmer, a zillion reasons. I just want to know how they got pcbnew to do a thing.

The code can be found here on github:

Here’s a video demonstrating the code and how it works.

1 Like

I’m not 100% sure this post will be of interest to everyone, but…

I’ve been doing a fair bit with Boost’s c++ polygon and geometry libraries. These are libraries that enable one to manipulate polygons. merge, inflate, subtract. This can be useful for things like generating copper pours, design rule checking and more.

Since they’re c++ libraries (though the shapely is a wrapper around boost::geometry), they’re not directly python scripting. But since this thread is a clearing house of my writings, I’m adding it here.

I have other nerdly posting on my non-kicad blog: Using Boost polygon/geometry. Tutorial/tips/workarounds – Miles's Nerdy Thoughts

Here’s the video:

2 Likes

Hi @mmccoo, I’m getting an ‘internal server error’ when clicking on any of your links… Could you look into this? Thanks!!!

A post was split to a new topic: Manipulating Planes

iam creating a small plugin for pcbnew…i just want to get the path of the file currently opened in pcb new.since iam using os module of python to exectute some command, i need to change the directory to that particular path of file which is opened now in kicad.

@moderators

Usually old threads get closed quickly after they’ve been dragged up.

This one seems worthy to be moved into the FAQ section after some sanitation.

I don’t know much about Python, and have never used it in KiCad, so I have not read much of this thread and can not help here.

Take a look at the already published action plugins. Most of them are listed here.

This post actually predates the FAQ. It is a WIKI. I think @ mmccoo did this post as a request from me in another thread. So, yeah. :wink:

iam using kifield library to export and import the schematic data to and fro from excel sheet.Iam having a problem where export command when triggered from python program is working, but import doesn’t.when i run these commands from command prompt both commands are working fine as expected.Iam using os module of python to trigger the commands.Any help would be highly appreciated.

  1. use os.path.dirname() to determine directory of the file
  2. you need spaces after -i and before -w arguments
  3. use subprocess module to run
1 Like

Thanks…That worked…but now iam creating one more plugin to add a data validation to the exported excel sheet. iam using pandas.The problem is when i import pandas in pcbnew console iam getting
ImportError: Missing required dependencies [‘numpy’, ‘pytz’, ‘dateutil’].

i downloaded a numpy zip and extracted to kicad/lib/python2.7/ sitepackages. then tried to import numpy.Iam getting this error message>
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.

KiCad has moved on and is also using Python V3 now
Python V2 has finally been declared obsolete (that took 10 years too long).

To verify what Python version your KiCad uses, have a look at the version info, for example from Eeschema / Help / About KiCad / [ Show Version Info]. It should have a line like: “KICAD_SCRIPTING_PYTHON3=ON”

Installing Python libraries should not be done by downloading zip files.
Have you tried:

pip install numpy

There are still remnants of Python V2 lurking around. Sometimes you have to use “pip3” to install stuff for Python V3.x

1 Like

If you are not on windows then use pip to install needed packages. If you are on windows and use nightly kicad then use pip from kicad’s install directory. If you are on windows and use 5.1 kicad then it’s more trouble than it’s worth to install anything depending on numpy because that build is using a msys2/mingw toolchain and requires numpy compiled on that toolchain. I advise to switch to nightly.

Ok…will try in that way. One more query here…Can we add a dropdown for value field in kicad eeschema symbol fields table. i can see a browse button in footprint library. similar to that on value field i need a dropdown of predefined values. how to acheive this?

Short of modifying and rebuilding kicad code, you can’t.

I need to read values from Comment fields in a script. Something like GetTitle() but for Comment. I don’t know how to do it. Will you help me? Thanks.