Modifying Loaded Board in pcbnew with Python Script Plugin

I’m working on a plugin to make it easier to build a panelized PCB, since I don’t see anything else available for recent KiCad versions other than the “Append Board” feature. I did find an older “panelize.py” script out there, but it does not appear to be applicable to the current PCB file format. Besides being very manual, the “Append Board” feature has one issue that’s causing trouble for me: I want my panel to include rotated instances of the board I’m panelizing, but the orientations of reference designators change on the rotated boards because reference designators are automatically forced to appear in one of two orientations. In my plugin, I plan to automatically set reference designators invisible and replaced them with static silkscreen text, such that all panelized boards will be identical after rotation.

The model I’m currently working on involves importing a plugin and then using the plugin’s classes and functions to load one or more boards to be panelized, then instantiate one or more copies in the currently-loaded board as returned by pcbnew.GetBoard(). I’m making some early progress despite limited documentation, using a lot of trial and error. I’m presently able to read in a board from an external file and then add multiple partial copies of it at desired locations in the currently-loaded board. I have a ways to go yet, but I’d like to ask for some help with a couple issues that I haven’t been able to figure out myself yet, and I’m hoping that there’s a pcbnew Python plugin expert out there who can help me.

First, adding my board instances to the loaded board file doesn’t update the screen. I need to click the redraw button or perform some other action to redraw the screen, such as the zoom all function, to get the added board instances to show up on the screen. Is there a programmatic way to force a screen redraw from within a Python based pcbnew plugin?

Second, after I add my board instances, the “save” button doesn’t get un-grayed and enabled. I can set the modified flag with pcbnew.GetBoard().SetModified(), but doing so doesn’t enable the save button in the toolbar. Redrawing the screen after setting the modified flag doesn’t enable the save button, either. Can anybody help me figure out how to get pcbnew to accept that the loaded board has been changed?

If all else fails, I could probably change my model to save the panelized board to a new file rather than modifying the loaded board in pcbnew. But I’d rather not do that, because I think it would be clumsier and less flexible vs. allowing the user to interactively panelize in the loaded board (which may already contain stuff to be included in the panel) from the scripting console.

In case it matters, I’m currently developing this script under OSX with version 2014-16-16 BZR 5324. I’m also trying to build newer KiCad versions on my Mac, but I haven’t succeeded yet. I have some problems with the nightlies, and the Mac building instructions aren’t working for me; both probably because I’m running 10.10 (Yosemite) and the nightlies and build instructions appear to be for an older OSX version, prior to some compiler and library changes that broke a bunch of stuff. I’ll move my development and testing to a more recent version once I get one working well on my Mac.

This will be a great tool when you get it done! I know I’ll use it. Sorry I can’t answer your questions.

One thing: it would be great if you can break-out the tool that replicates the part references on the silkscreen. That would be useful even when you’re not doing an array. (Maybe this already exists.)

It should be easy to break out the part reference function once I write it. Come to think of it, I should also write a plugin that automates setting part values to invisible, since they’re visible in the stock library components and I never want them visible on my boards.

There’s already a checkbox in the pcbnew Visibles->Render list for turning off values. Isn’t that the same thing?

Does that checkbox affect the gerbers? I thought that only affects screen rendering.

You’re probably right about that. I thought that’s what you were talking about.

I just looked and the gerber file generator also has a checkbox for turning-off values.

Yeah, I guess I could also turn off the values during Gerber plotting. But you know me; I have to be difficult. :smile:

Since I’ve been developing this script in a pretty old KiCad build, I decided to try running in the latest OSX nightly build just to see if there was any change in behavior related to the issues I’m having trouble with here. Well, I discovered that when I open up the scripting console, “o” and “x” get interpreted as hotkeys instead of being sent to the console input stream. That makes it hard to imprt a mdule… :stuck_out_tongue:

After changing those two hotkey definitions to work around that roadblock, I see that the file dirty flag issue I’m having is still the same in the latest build. I hope there’s a way to work around it in the scripting environment.

The grandiose semiautomatic panelizer I envisioned turned out to be too difficult, so I just wrote the reference designator converter as a standalone function. I also wrote a function to unstack components newly added by reading a new netlist, and a couple others to make values all visible or invisible. It’s a first draft, but maybe it won’t set your computer on fire?

https://github.com/NF6X/KiCad-NF6X-Libs/blob/master/plugins/nf6xutil.py

hi @NF6X,
is there a way to invoke / export the pcb board to IDF from a python
command in pcbnew python kicad console?

I went through pcbnew.py file but it seems to me there is no command to
do it with the pcbnew python APIs…
thank you
Maurice

I don’t know of a way to do an IDF export from the python console. I do not recall seeing that function exposed in the Python API.

1 Like

I supposed there was no way to do it by python…
thank you,
Maurice

@NF6X
is there a way to starting a scripts when pcbnew is launched?
I mean I would like to have my script ‘my_script.py’ already executed or imported at pcbnew start,
so I could launch e.g. my_function() directly in pcbnew python console without having to type:

import script_xy

before

my_function()

thank you
Maurice

I do not know of a way to do that, but I haven’t gone looking for a way yet, either. Maybe there’s some python module in the distribution that gets loaded automatically, so you could splice in the stuff that you want to auto-run?

that would be useful, but I don’t know which modules are pre-loaded…
I think that the only module pre-loaded is pcbnew.py (under lib\python2.7\site-packages) …
In fact if I open the pcbnew console and e.g. I digit
pcbnew.GetBoard().GetFileName()
I get the right file name without errors

what could I do to let my script to be imported / loaded?
thanks again
Maurice

Yeah @maui , no idea, sorry… I’m just a user who has some python abilities, but not much more.

@Joan_Sparky
thank you anyway :slight_smile: