How to add a via through a script

I am trying to draw place a drill programatically on a pcb and I am quite stuck.

here is what I did so far:

import pcbnew
board = pcbnew.LoadBoard('sampleboard.kicad_pcb')
newvia = pcbnew.VIA(board)
newvia.TopLayer="F.Cu"
newvia.BottomLayer="B.Cu"


newvia.SetDrill(400000)
pos=pcbnew.wxPoint(10,20) # I want to place the via at x=10mm,y=20mm
newVia.SetPosition(pos)
newVia.SetNet( )# Now I am stuck. I want to set net to 'AGND'

1 Like

untested, but something like this should work:

newVia.SetNet(pcbnew.NETNAMES_MAP["AGND"]);

See KiCad Pcbnew Python Scripting: pcbnew.NETNAMES_MAP Class Reference

I also think for the position you need to give internal units (nanometers?) and not millimeters.

2 Likes

Thank you.

I was able to add the vias. Just in case someone else is reading this:

def create_via(board, net, pt):
    newvia = pcbnew.PCB_VIA(board)
    # need to add before SetNet will work, so just doing it first
    board.Add(newvia)
    newvia.SetNet(net)
    newvia.SetPosition(pcbnew.wxPoint(*pt))
    newvia.SetDrill(int(drillsize))
    newvia.SetWidth(int(viawidth))
    newvia.SetLayerPair(
        board.GetLayerID('F.Cu'),
        board.GetLayerID('F.Cu')
    )
    newvia.SetViaType(pcbnew.VIATYPE_THROUGH)


# Get the groundnet
nets = board.GetNetsByName()
AGND_net = nets.find("AGND")
groundnet = AGND_net.value()[1]



viapositions = np.genfromtxt('../viapositions.txt', delimiter=",")
for pos in viapositions:
    viaposition = pcbnew.wxPoint(int((pos[0]-xoffset)*SCALE), 1*int(pos[1]*SCALE))
    create_via(board, groundnet, viaposition)

This was mmccoo git repo was really useful reference:

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.