Tutorials on python scripting in pcbnew

I’d be interested to see what you come up with. I’m not familiar with either python or the kicad API (just C++ for AVR - similar but different) and have so far been unable to identify the mechanism to place/import a footprint from a library into pcbnew.

The “basics” script crashes pcbnew with a segfault on latest nightly, linux version. Anyone else have the same issue?

Two possibilities come to mind, both of them have to do with the assumption of certain circuit elements.

First, the script assumes you have a net called clk. In the code, I do a find(’/clk’) without checking if anything is found. The subsequent call to .value()[0] then causes a seg fault for me if clk isn’t there.

Second, the script assume you have a module called U1. In this case, pcbnew doesn’t seg fault, but rather gives a cryptic message.

I’ve added a check for both in the code. Hopefully, that will fix your crash.

To get the full benefit of the demo, you’ll want to change clk and U1 to items you do have.

Sorry about that.

Here is my experiment. It has been sometime since I worked on this. I don’t know if it will work with latest build of pcbnew.

" the script assumes you have a net called clk."

Yes, that was it. Thanks

You did a great job. Explaning all that stuff.

I started scripting in KiCad 2 weeks ago and had to figure this stuff out all by myself. I also didn’t find the auto-generated API, until I stumbled across it in the manual.

Just one Question:
Did your pcbnew crash when you played around with Vias?
I’ve described that problem in this Post.

Edit: SOLVED it:
I forgot to set the Net.
Made some Quick Tests with Tracks and Vias:
-if Vias got the default NetCode 0, pcbnew crashes when zooming in
-tracks can have the default NetCode 0, without any problems
-if Vias got the default NetCode 0 and you move a component or change Canvas, a function is called, that sets the net to the net of the “connected” track

@dbrown2k I’ve posted a new entry on my blog on how to add additional modules to your design from pcbnew.
https://kicad.mmccoo.com/2017/02/22/how-to-add-mounting-holes/

The short answer is this:

  • Find the directory that contains your kicad_mod files or clone it from the kicad github. Let’s say a variable footprint_lib points there
  • create an instance of the PCB_IO plugin:
    io = pcbnew.PCB_IO()
  • ask it for a new module instance
    mod = io.FootprintLoad(footprint_lib, "1pin")

Also note, that I’ve created a table of contents page for the blog:
https://kicad.mmccoo.com/kicad-scripting-table-of-contents/

@hyOzd thank you for the tip. I haven’t had a chance to play it, but it’s high on my list.

DougE The 0 netcode via crash seems like a bug. Where in the manual did you find the documentation link? Perhaps its a portion of the manual I haven’t seen yet.

dBman Glad it’s working for you and thank you for the bug report. :wink:

3 Likes

Hi all,

Is there a way to manipulate kicad’s view with python scripts?
What I want to achieve is:

  • find component by reference - easy one
  • ensure it is visible so that I can actually see it in the window
  • change zoom to such level that I can locate the component on the board (among other component)
    I did some search in the internet and read the python API but unfortunately couldn’t find anything appropriate.
    Maybe I am to stupid to find it or maybe it doesn’t exist.
    I need it to improve kicad assistance in manual pick & place work.
    Now the only way is to:
  • hit Ctrl+F
  • enter components reference and hit enter
  • hit escape
  • and finally zoom out using Alt + F2 as the default zoom after search is way to big ant therefore I can’t locate the component on the board - need to see some other component for sense of direction.
    And the whole process must be repeated for each component :frowning:
    Help please :slight_smile:

First of all, your tutorial blog is awesome!

I would like to second the question of how to modify the pcbnew editors state.

Is there way to select a group of objects using python scripting so they can be moved/rotated together? As if I clicked or lasso’ed hundrets of them manually?

The theory its easy, find a common Property of the items you wont to manipulate and search for them. The difficult thing is to find that property. You could use a filter to get your wanted Items out of the modules list. (My first though, the pcbnew.EDA_ITEM.IsSelected Property wont work, as you can’t select multiple items. Maybe another property will work.)

The easiest way, is to pass a list of References of the components you want to modify. Not very smart, but functional.

Another idea (I couldn’t test and look up yet) is, to use the Block Operations and get its Item list.

@DougE Getting the module objects I’d like to group/modify is the part I already know how to do. I got a list of module objects but I cant find any documentation on how to interact with the pcbnew UI. I’d like my script to select a known group of modules, as if I selected those parts using the mouse for block moving/manipulation.

The “Selected” property you mentioned looks like the right approach, but again, nothing I do seems to have any effect on the UI. I tried the “SetSelected()” method which causes “IsSelected()” to return true on multiple objects, but nothing happens in the UI. I can’t move/rotate/manipulate the selected group :confused:.

So you want too select modules with the script and edit them with the UI Interface? Or am I on the wrong track.

Had a look in the source code (especially at block_module_editor.cpp). I’m not so familiar with C++, but I think they work with List, we don’t have access to from Python.
Why not use the script to modify the modules?

@pwuertz I like the idea of creating your own selection commands. I don’t like the popup listing the different filters. Like you, I also think the filters are not sufficient. I’ll poke around and see if I can find an appropriate python command(s)

@upanie I’ll see if I can find a command to rezoom programatically. I’ve already been looking for the command to redraw (simpler than zoom). Assuming I find it, I imagine zoom will not be far away.

Good ideas for future topics. Thank you.

@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
wx.App()

Enables you to use the wx inspection tool

import wx.lib.inspection
wx.lib.inspection.InspectionTool().Show()

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.)

Enjoy
Miles

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:

Miles

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