Tutorials on python scripting in pcbnew


#41

@MitjaN yes,the replicate script does work with hierarchical schematics. The trick is whether to look for replication on the lower levels of hierarchy or the upper ones. I wish pcbnew supported properties. After I did my placement by sch script, I think I might be able to fake the properties thing by just parsing the schematics. Among other things, that would be a good place to store the desired spacings from one copy to the next. It would further make things clearer because it would also say which level is replicated. Send me a link to a testcase and I can take a look if it’s still relevant.

@hermit It appears I can’t edit my earlier posts. I don’t see the pencil icon on posts older than 40 days.

New post on zones with holes from svg

In other news, I have a new blog post. It’s about a script (and the hows behind it) that takes an svg file (only inkscape tested) and converts it to zones like this:

https://i0.wp.com/kicad.mmccoo.com/wp-content/uploads/2018/01/kicad_svg_zone.png

Here’s the post:
https://kicad.mmccoo.com/2018/01/11/zones-with-holes-from-svg/

Here’s a related post on another blog of mine talking about the challenges of parsing svgs:
https://nerd.mmccoo.com/2018/01/11/understanding-and-parsing-svg-paths/

Here’s a video mostly talking about the steps needed in inkscape:
https://youtu.be/5vWBWuKbFg8

Sorry it sometimes takes a while for me to reply to this thread. The announcements tend to get lost with other stuff in my gmail.

Any feedback, criticism, ideas,… are welcome. Replies to this thread, references in other threads, or direct mail (address is on my blog) are fine.


Editing older posts
#42

5 posts were split to a new topic: Editing older posts


#43

This is really a great post, thank you so much for compiling this information.

Does anyone know of a similar post that focuses on the schematic side of things???


#44

eeschema does not yet have a Python API, but there are some standalone Python wrappers for schematic and symbol libraries, e.g. https://github.com/KiCad/kicad-library-utils/blob/master/sch/sch.py


#45

I tried to using the replicatelayout.py on the latest KiCad version on ubuntu (4.0.7) but it fails due to missing functions in the pcbnew.py. is there any chance to get it running on a stable release or does I have to wait for 5.0.0?


#46

@Tojan Could you post the error message you’re getting?

I usually run on a Dev build but I should probably try using the stable PPA version (I’m guessing that’s what you have).

There is a nightly PPA as well

The kicad folks are pretty good about keeping the nightlies functional.

If you do try with a nightly and it still doesn’t work, then definitely let me know. Also, if you have test design I can try, I would love that too.


#47

Due to work regulations it is not possible for me to work with the nightly build I’m afraid.

the error code is:

execfile("/home/tojan/workspace/github/kicad_mmccoo/replicatelayout/replicatelayout.py"
… )
Traceback (most recent call last):
File “”, line 1, in
File “/home/tojan/workspace/github/kicad_mmccoo/replicatelayout/replicatelayout.py”, line 151, in
SheetInstance.RegisterModulesAndNets(board)
File “/home/tojan/workspace/github/kicad_mmccoo/replicatelayout/replicatelayout.py”, line 97, in RegisterModulesAndNets
for net in board.GetNetsByNetcode().values():
File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 4806, in
getattr = lambda self, name: _swig_getattr(self, BOARD, name)
File “/usr/lib/python2.7/dist-packages/pcbnew.py”, line 83, in _swig_getattr
raise AttributeError("’%s’ object has no attribute ‘%s’" % (class_type.name, name))
AttributeError: ‘BOARD’ object has no attribute ‘GetNetsByNetcode’

As said I took a look into the pcbnew.py of the stable build and it seems like some functions and attributes you are using are missing there.


#48

I have a new post on my kicad scripting blog. This one showcases some of the things you can do in scripting (If I were god, most of the editing functionality of pcbnew… most tools actually, would be writting in scripting languages. It would be much easier for folks to customize)

https://kicad.mmccoo.com/2018/01/30/scripting-fusion-360-designs-into-kicad/

I’ve made a youtube showing all of the steps I used to generate this board:

which I drew mostly in fusion 360:

Here’s the video:
https://youtu.be/av5HwAFl2VI

The post and the video don’t explain much about how the scripts work, but I do want to recommend a couple existing python libraries out there:

When playing around with the libraries below, remember that while they may return some cryptic data structure, you can often wrap that structure into a list(blah) call. Those structures are often/usually just a list and passing it to the list function gives you the kinds of lists you’ll fine more familiar.

The SciPy library is fantastic for graph algorithms like minimum spanning tree and delanay triangulation

The shapely library is very nice for polygon manipulation. Merging polygons, bloating and shrinking them… Designing layout is mostly geometric, so this is a nice tool to have in your pocket.

The dxf_grabber library is how I parse dxf files. Together with shapely, you can do a lot of import stuff.


#49

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/