Creating a net in Python

I’m wondering if anyone’s got any idea how to programmatically
create a net from python.

I can see I can do:
track.SetNetCode(integer)

But I’m really struggling to work out how to create a net, to pass to SetNetCode.

You can see @ https://github.com/anfractuosity/pcbdisplay/blob/master/pcbdisplay.py
How I’m currently creating tracks/pads, which I need to assign to my own net programmatically.

Thanks!

Do you need to net name changed in just the board file? or in the schematic as well?If you need to change it in the Schematic as well, you can’t do that using the current scripting interface. You probably should be creating nets in the schematic (this is just a warning).

Or is it a new net-class that you would like to create (trace, space, via sizes and so on)?

Hi,

Thanks for your reply.

I’d like to change the net code for my traces and pads, in the board file, via Python.

I’m very stuck as to how I’d go about creating a new net code though.

(The reason I need to do this, is because when I create the tracks programmatically via Python at the moment, they seem to get assigned to the ‘default’ net, which means that when I add components from the schematic to the PCB, they seem to try to use the tracks I created via Python, which I don’t want to happen)

cheers

First create a new NETINFO_ITEM instance.

net = NETINFO_ITEM(board, "net_name")

Add this net to ‘board’, this will assign a ‘net code’ to your net.

board.AppendNet(net)

Learn the net code:

print(net.GetNet())

Assign net to a certain track:

track.SetNetCode(net.GetNet())
1 Like

Cheers!

I can’t seem to find where NETINFO_ITEM exists though?

I do, “import pcbnew”, then looked at the functions in there, but can’t seem to see it?

It’s a class from the pcbnew module. You can use it after importing.

from pcbnew import NETINFO_ITEM

This is very weird, that doesn’t exist for me:

from pcbnew import NETINFO_ITEM
Traceback (most recent call last):
File “”, line 1, in
ImportError: cannot import name NETINFO_ITEM

Also I just loaded pcbnew.py into a text editor there doesn’t appear to be any classes with that name for me.

I’m using the latest version of kicad from the github mirror - https://github.com/KiCad/kicad-source-mirror

Hmm strange. I’m not using exactly latest version but it’s recent. This is my debian package version:

0.201512080931+6353~38~ubuntu14.04.1

I doubt that they remove it. Are you building kicad yourself? Maybe your build routine isn’t updating the “pcbnew.py” file? AFAIK this file is completely auto generated.

I’ve just downloaded the latest debian package from the ppa. Yep, indeed it’s removed. Let me investigate a little further. I will let you know if I can find anything useful.

Cheers! It’s much appreciated

No luck. I’ve created a bug report.

I think i’ve possibly worked it out. https://github.com/KiCad/kicad-source-mirror/commit/ee3418e90bd7fb2a5578fb18d3cc90821a32c9ba

The %ignore for NETINFO_ITEM

I removed that, and it seems to have built that class, just gonna see if it works

Nice find on the bug. Having the same problem last night giving this one a try.

Ah Im wondering if other stuff has changed too:

net = NETINFO_ITEM(pcb, "bob")

File “/usr/local/lib/python2.7/dist-packages/pcbnew.py”, line 7125, in init
def init(self, *args, **kwargs): raise AttributeError(“No constructor defined - class is abstract”)
AttributeError: No constructor defined - class is abstract

Here is the C++ constructor for the NETINFO_ITEM class

NETINFO_ITEM( BOARD* aParent, const wxString& aNetName, int aNetCode )

There is a aNetCode. Some more reading and it does not seem to be the way to construct a net, it is only the container for some information about the net i.e. name, parent, and the number id of the net.

BTW why is that you would like to create nets in the layout not in the schematic anyways. This seems a little backwards to what I would think the normal method would be.

Did anyone get a working version of this ?
Seems some comments back in Feb around bugs, as best I can tell NETINFO_ITEM exists in new builds, but it seems the syntax above is not quite right here ( #14 & #15) ?

Change of a Reference seems as simple as part.SetReference(“U2n”),
but a change of a Netname seems more complex, but surely is possible ?

What is the simplest way to
a) globally rename a net ?
b) add a new net name ?

I did some investigations today. Couldn’t find a way. NETINFO_ITEM is reported as abstract from the scripting side, but when I look at the c++ code there are certainly a couple occurances of new NETINFO_ITEM(...). That means it’s not actually abstract.

I’ve already updated the bug report, lets see if anyone responds.

1 Like

Thanks, I guess it was nothing simple I missed.
You could ask in the bug report about create or existence of a SetNetName(“NewName”), which is a more direct means to at least rename a net.

Does anyone know what the strangely named
AddNative(NETCLASS self, wxString aNetname)
does, or have examples of its use ?

I don’t have any experience with swig but from source code:

        %rename(AddNative) *::Add;

it looks like, any C++ member function named Add is renamed to AddNative for python, so that it can be wrapped by Python code like this for ex:

    def Add(self,item):

        itemC = item.Cast()

        if type(itemC) is D_PAD:
            item.thisown=0
            self.Pads().PushBack(itemC)
        elif type(itemC) in [ TEXTE_PCB, DIMENSION, TEXTE_MODULE, DRAWSEGMENT,EDGE_MODULE]:
            item.thisown = 0
            self.GraphicalItems().PushBack(item)

IMO AddNative is not supposed to be used directly from python.