How to create a new netclasse by python? [scripting]

I want to automatic setup design rules when I create a new project via python,but I can not find a method to create a new netclass in python script.I found NETCLASS object not defined in pcbnew.py. does it not support right now?

@mmccoo might know that one…

looking at the code, it’s easy to modify existing netclasses. It doesn’t look like a constructor is exposed to Python.

Perhaps only as notes to myself:

import pcbnew
board = pcbnew.GetBoard()
ds = board.GetDesignSettings()
# ugly. exposes a public member not via an accessor method
nc = ds.m_NetClasses  

nc.Add(netclassptr)

But how do you get a net netclass ptr?

I’m guessing that pcbnew/swig/netclass.i can be modified to add a constructor method. Probably not difficult but it’s time for bed.

Sounds like a good topic for my blog.

if you add this line:

std::shared_ptr<NETCLASS>(std::string name)  { return new std::shared_ptr<NETCLASS>(new NETCLASS(name)); }

to pcbnew/swig/netclass.i and recompile/install you can do this:

import pcbnew
board = pcbnew.GetBoard()
ds = board.GetDesignSettings()
# ugly. exposes a public member not via an accessor method
nc = ds.m_NetClasses  
foo = pcbnew.NETCLASSPTR("foo")
nc.Add(foo)

and you’ll have a new netclass named foo in the design rules.

I’ll submit this change to the kicad folks. I haven’t decided yet whether to try to add an access function for m_NetClasses, so it may take some time. Probably not though. the real way to improve things is probably to develop a set of wrapper classes to make the python interface make more logical.

2 Likes

thx for your help. Kicad Python really need a big improve I think

the constructor is now in the kicad nightlies.

I agree that the python interface needs a lot of work. I encourage you to contribute to kicad. The developers are very welcoming.

2 Likes