Tutorials on python scripting in pcbnew

@DougE Yes, I would like to select modules by script and then move/manipulate the block using the UI (i.e. I still want to use the UI for working on the layout).

Often I find myself in a situation where I’d like to move around a group of objects coherently, but the need to select every object single handedly whenever I loose the selection really bothers me. PcbNew does not have a “group” feature, so I figured defining groups via script and recall them would be the easiest approach (much like CTRL+Number lets you define/recall groups of units in mainstream strategy games).

The point is, nothing I do from the scripting interface seems to have a direct effect on the UI. If you change the position of an object for example you need to click around a bit until the UI actually picks up the change and draws the new position.

@mmccoo You mentioned a redraw or update command for the UI? Maybe a module.SetSelected() followed by ui.redraw/update() will do the trick?

I have added another post to my blog. This time it’s about how to add your own command on the top, left, or right toolbars.

kicad.mmccoo.com (I’m not posting a direct link for fear that I’ll get flagged again for spamming. If you feel this thread is spam, please give a comment on why)

To summarize the important bits:

import wx
import wx.aui # this is the key one

Enables you to use the wx inspection tool

import wx.lib.inspection

These exports also cause pcbnew to give you the correct wxApp and wxAuiControl pointers (otherwise, you’d get a parent class pointer missing the interesting APIs.)

I appreciate the topic ideas I’ve gotten on this thread:

  • zoom
  • redraw (though I guess that’s my own)
  • autoload python
  • special selections (this one could be hugely useful, I think. I’m thinking I could use this with sheet based schematics.)


1 Like

Thanks again for taking the time to share your knowledge. :+1:

I really hope the spam flag was some kind of mistake. Please mods confirm this.

I checked, @davidsrsb took care of that and as far as I can see he didn’t agree with the @system flagging him for spamming.
The system is just a little bit more careful with new users as @mmccoo joined on 17.2.2017, so he fell into the raster as basic user it seems… don’t know when he’ll get member status, there are a couple of tiny hurdles to jump over for:

I oked @mmccoo posts and unblocked them. To get promoted @mmccoo needs to read some other threads and post in a few. Reading the forum rules gets you a badge too

1 Like

Thank you @dBman, @Joan_Sparky and @davidsrsb for the support and info on how the system works. I’d assumed it was just generic infrastructure + overzealous flag police. Guess I shoulda rtm :slight_smile:


Unfortunately not that easy… this info is pretty well hidden, gotta go to your Preferences and there to Badges and then hopefully there is a link somewhere… :smirk:

1 Like

I have added a new post to the list of tutorials about python scripting in pcbnew.

In a nutshell, there are two new python APIs (you’ll need to use a nightly build or recompile yourself)



x = pcbnew.FromMM(10) y = pcbnew.FromMM(10) width = pcbnew.FromMM(20) height = pcbnew.FromMM(20) pcbnew.WindowZoom(x, y, width, height)

Folks that have expressed interest in such a capability: @upanie @pwuertz


@mmccoo Unfortunately the Refresh command doesn’t do the trick for me. There is probably another mechanism involved in the selection handling of objects in pcbnew.

I did check out the Zoom function just for fun. It only works for the classic view though, not with the OpenGL canvas enabled. I liked your post on adding such functionality yourself and found your git commit. It’s a nice entry point for someone without knowledge of the code! What I get from this is that PCB_EDIT_FRAME seems to handle two different canvas objects (GetCanvas, GetGalCanvas, IsGalCanvasActive). This is probably why there is kind of a break in functionality between the different views in pcbview.

Thanks for digging into this!

Hi @mmccoo @pwuertz
is there a way to know from a python script, if GalCanvas is active?

Thanks so much for this information!
It took me about 4 hours to work out how to add filled zones (like mask) and text.
Very rough demo here in case someone else needs it: https://github.com/mattvenn/kicad/blob/master/python_api/test.py#L74

What did you use to draw that UML diagram? I’ve been looking for something that would draw UML diagrams that look as neat as that but have been disappointed by everything. I would love to know.

@royalt Sorry for the really late reply. I used http://plantuml.com/ to generate the UML.

@pwuertz On refreshing the GAL canvas, I have submitted a patch so the pcbnew.Refresh(); will get GAL canvas to update. I hope to have it merged in the next days.

It’s been a while since I’ve posted to this thread. I’ve added a new blog post, this time about applying the placement from the schematic to the layout. This helps get an initial placement that isn’t random.


I also have a show youtube video demo:

One question for everyone: when posting about new blog posts, should I continue this thread or start a new one?

Personally, even though I go for long periods without working on this stuff, I like coming back to one place to see if anyone has asked a question. Better late than never? There aren’t a lot of people out there that are comfortable with the kicad code base.


Continue this old one.

1 Like

I’ve added this thread to the FAQ. Please consider editing the original post from time to time if you feel that something could be added that would keep someone from having to read the entire thread.

Agree, continue


1 Like

thank you for the feedback.

changing the original post… Duh. should have thought of that myself. In fact, i think have have done just that in the past.

Youth is walking into a room and remember why. :slight_smile:

Does the autoplacement script work with hiearchical schematics?

@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:


Here’s the post:

Here’s a related post on another blog of mine talking about the challenges of parsing svgs:

Here’s a video mostly talking about the steps needed in inkscape:

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.

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