Creating scripts


#1

Hi
I wish to create a script that when I select a trace in pcbnew I can see it in a small window the length of the trace in mil and in mm
I am very new to scripting
can someone help from a to z how to create it in python and operate from pcbnew

TNX
Jacob


#2

Have a look here:


#3

I know I can but I wish to learn scripting
also if you select the trace you can see the length only in in. or mm.
I wish to see both of then in the same time


#4

This might be a good starting point.

Kicommand (lots of pyhton code that could give you ideas how to interact with kicad from python)

Action plugin (get your script into the tools menu of pcb_new)

For anything else, simply come back with detailed questions.


FAQ Index Thread
#5

It would be helpful if you let us know what are your programming skils.

Otherwise start with https://github.com/KiCad/kicad-source-mirror/blob/master/Documentation/development/pcbnew-plugins.md

And search for “kicad action plugins” on google for examples


#6

I am an electronic eng. know a little bit c did not written in python


#7

Firstly, I’ll try to write this as a FAQ entry as @hermit might use this.

Secondly, when talking about KiCad and scripting it usually means scripting pcbnew as eeschema does not have any support for scripting at the moment. And even with pcbnew, we can distinguish between scripting when pcbnew is running with opened pcb and running bare python scripts to modify pcbnew files directly.

Thirdly, always keep in mind that KiCad users are a heterogeneous group each with its own experiences and cultural background, so when you get advice keep that in mind. I have a 10+ years’ experience in C programing of MCUs mainly in the field of control systems in power electronics and 1+ years of python and I’ve written one KiCad Action plugin. So my python code is quite procedural with light use of object oriented features, list comprehensions and other python features.

So finally some advice on how to start:
The python interface to pcbnew is documented at: KiCAD pcbnew scripting

It documents all the classes and methods available. Keep in mind that this documentation is valid for current nightly release. I do not know where one can get the documentation for the latest stable release (4.0.7). Also the latest stable release (4.0.7) does not support Action plugins, just the basic scripting. I therefore highly recommend nightly releases for scripting development. All the usual warnings apply, when using nightlies.

While you don’t need an IDE for development of scripts I’ve used pyCharm and used python interpreter that comes with python (C:\Program Files\KiCad\bin\python.exe).

If I summarize what you want to do:

  1. Find the track selected and get its properties
  2. Show relevant data in a pop up window
    This is quite a complicated task for someone just starting, but some parts of it are quite easy. If I expand the tasks needed to be done and write the code for it, this should offer you a good start.

Firstly you have to import pcbnew module

import pcbnew

Then load the board (I am assuming we are running the script from pcbnew):

board = pcbnew.GetBoard()

Get all the tracks on the board:

tracks = board.GetTracks()

Iterate through all the tracks and find the one which is selected:

selected_track = none
    for track in tracks:
        if track.IsSelected():
            selected_track = track

Once we have the track we can print the width of the track:

print selected_track.GetWidth()

You can try running this in the pcbnew scripting console. Mind the indents in the for loop.

As for going towards your objective of showing the parameters in a windows, I’d firstly start with KiSelect and (action_menu_move_to_layer) action plugins. You will have to get familiar with wxpython.

My first step would be to write an action plugin, which when run would open a dialog window and show the track in for in the window. Once you have this working you are only one third of way there. You will have to figure how to create a window which is not a dialog so that you can have pcbnew window active (I’d look into Layer View Set for info how to do this) and finally how to pass UI events to action plugin (what you would probably want is to run the search on every click)

Hope this helps

EDIT: I am struggling with multiline code insert


FAQ Index Thread
#8

Appreciated. I’m just putting the index together and I’m far for the person to judge this stuff on the merits. Even though I had Rene’s post linked I added this also. (we now have three links to the topic) Still learning how to do the basics with the FAQ/WIKI thing. At some point I’m sure we will start going over this stuff and making some decisions. The one nice thing about doing it this way is that some people can read two authors saying the same thing and get more from one that the other.

Now that I’ve seen the alert, you might want to go ahead and edit the first line out and renumber. If we ever decide to pull this into one document it will make more sense. Thank you!


#9

Come to think of it, it would be easier if you started with an action plugin, which would delete selected tracks. As this functionality is missing in KiCad (there is only global delete option), it might come handy for somebody else.

Once you have it working for tacks, you can upgrade to delete selected tracks and/or zones and or modules and/or vias, …


#10

Thenk you
it is very helpful


#11

For code using triple backtick ```
i.e.

```
Some code
    indented
```

becomes

Some code
    indented

#12

Thank you. There is just too many versions of markdown.