Python script for placing micro vias

Does anyone know how to place micro-vias with a python script?
I tried with the code below, but it fails in KiCAD 8.0.3

def placeVia(x,y,lu,ll,dv,dh):
    v = pcbnew.PCB_VIA(pcb)
    x = int(x)
    y = int(y)
    v.SetPosition(pcbnew.VECTOR2I(x,y))
    v.SetDrill(int(dh*mm))
    v.SetWidth(int(dv*mm))
    v.SetLayerPair(pcbnew.LSET.LayerToInt(pcbnew.LSET.GetLayerName(lu)), pcbnew.LSET.LayerToInt(pcbnew.LSET.GetLayerName(ll)))
    pcb.Add(v)

It works without the v.SetLayerPair (suggestion from chatGPT, probably old code)

Any suggestions highly appreciated!

Best Regards
Bent

I never done >2 layer PCB’s, nor python scripting in KiCad, but I do now there is a difference between micro via’s and blind / buried via’s. Buried via’s are drilled through the half fabricate, and thus can connect between all layers in that half fabricate, while micro via’s only connect from the active layer to one layer below it. This may explain why:

To paulvdh,

You are right, but in KiCAD you can set the micro via to span from e.g. F.Cu to In3.Cu, and in the kicad_pcb file they are shown as

(via micro
		(at 82.0925 71.9575)
		(size 0.3)
		(drill 0.1)
		(layers "F.Cu" "In3.Cu")
		(net 1)
		(uuid "04cf38de-bafa-48fb-ae26-7dd281ebff64")
	)

Therefore I am quite sure that you can do it in a script as well. I have 2000+ via’s to place, so I an quite motivated to find a way to automate the process!

So, my idea at the moment, is to make a simple text substitution in the .kicad_pcb file using a Python script outside KiCAD.

Thanks for your input!

Best Regards
Bent

You can indeed set these to arbitrary layers, but from F.Cu to In3.Cu would be nearly impossible to manufacture. But also, as far as I know KiCad does have: PCB Editor / File / Board Setup / Board Stackup / Physical stackup but this does not define layer lamination order, and thus KiCad simply does not know what is doable for the PCB.

And then there is LTCC (Low-temperature Cofired Ceramics) This is a quite special PCB manufacturing technique, can have over 50 layers, and vias can start and stop at arbitrary layers. Enforcing a layer lamination order in KiCad would be senseless for LTCC.

In the end, I agree with you, If you can do it in KiCad’s gui, then you should also be able to script it, but I can’t help with scripting.

It should be obvious that you can’t form a pair with one item.

But LayerPair doesn’t seem to work for vias. You can try

v.SetTopLayer(pcbnew.In1_Cu)
v.SetBottomLayer(pcbnew.B_Cu)

Hi paulvdh

Actually I think I have to change it to a blind via F.Cu to In3.Cu, easier to manufacure. Anyway I have to discuss it with the Mfg. Regarding the script it was easy (well quick and dirty, but it worked!). Se below:

fin = open('pxxxx.kicad_pcb', mode='rt', encoding='utf-8')

lines = fin.readlines()
fin.close()

fout = open('pxxxx_mv.kicad_pcb', mode='wt', encoding='utf-8')

for line in lines:
    s1 = line.replace('\n','')
    if s1.lstrip() =='(via':
        fout.write(f'{s1} micro\n')
    elif s1.lstrip() == '(layers "F.Cu" "B.Cu")':
        fout.write(f"{s1.replace('B.Cu','In3.Cu')}\n")
    else:
        fout.write(f'{s1}\n')
        
fout.close()

Thank you again for your help and time!

Best Regards
Bent

The type can be set to micro with

v. SetViaType(1)

Hi eelik

OK, I have seen the functions, but not yet tried them. Another thing is that I find it difficult to find the arguments to functions e.g. “pcbnew.In1_Cu” etc., were are these documented?

I will try your solution and let you know if it works!

It works!
Also from your example:

v.SetTopLayer(pcbnew.In1_Cu)
v.SetBottomLayer(pcbnew.B_Cu)
v.SetViaType(1)

I guessed that: 1 = pcbnew.VIATYPE_MICROVIA as in

v.SetViaType(pcbnew.VIATYPE_MICROVIA)

And it was!!

Thank for your help!

PS.: One last thing: How do you make these grayed out code sections???

Best Regards
Bent

Short answer: nowhere where you could find them. :smiley:

The “API” isn’t really an API, it’s python bindings for some of the internals of KiCad. Some parts of it are documented in the python classes, some parts are very difficult to decipher. You can refer to the KiCad C++ documentation, but it’s difficult, too. I happen to know that many enums are found directly under the pcbnew. You can find more information about the layers in Plugin howtos · Wiki · eelik-kicad / KiCad Documentation · GitLab. I also now found the via types by just experimenting in the KiKad python shell, using the tabulator code completion:

>>> pcbnew.VIA → press Tab

(You found the via types while I was writing, congratulations :slight_smile: )

Use “preformatted text” ( </> ) from the forum editor toolbar on a selection. You can see how it adds tickmarks around text.

EDIT: the pcbnew module also includes several useful helper functions. For example, instead of int(dh*mm) you can type the value in mm, for example 0.3 for 0.3 mm drill and use pcbnew.fromMM(): pcbnew.fromMM(0.3). That makes the code easy to write and understand.

Hi Eeli

Ah, I saw it, but I did not think of it as “code style” !! Now i know! Thank you.
Also I’l have a look at your Gitlab site!
Also the “code completion” was new to me! Nice info, thanks.

I think you are right everything in KiCAD Python is “leaning by doing”.

Thank you so much, have a nice day!

Best Regards
Bent

Nah, actually the Tools → Scripting Console should give completion hints automatically, like in most semi-intelligent coding IDEs, while you’re typing. Tab just completes then.

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