Global setting of drill sizes with Python in PCBNew-How?

Is a python script available that can set all the drill sizes on a board to 1.00mm? I’d like to be able to do this to save time and avoid the occasional mistake. I am a beginner with python but I’ve been using Kicad for around 7 years or so. I have not used any scripting in Kicad so I’d like to start by trying this if possible. Can anyone help? Thanks.

Don’t know of a script, but you can always open the kicad_pcb file in a text editor like notepad++ and search for those drills

There are via drill entries for example:

(via (at 161.5 92.25) (size 0.6) (drill 0.4) (layers F.Cu B.Cu) (net 17))

Or the ones from pads of footprints:

(pad “” np_thru_hole circle (at 82.5 -47.5) (size 2.1 2.1) (drill 2.1) (layers *.Cu *.Paste *.Mask) (solder_paste_margin 0.001))

So be careful what and how you change stuff in there, no safety net… backup advised.

I’m wondering if pad.SetDrillSize in this python script ( could be used somehow?

`#!/usr/bin/env python2.7
from pcbnew import *

size_0_6mm = wxSizeMM(0.6,0.6)
size_1_0mm = wxSizeMM(1.0,1.0)

create a blank board

pcb = BOARD()


create a new module, it’s parent is our previously created pcb

module = MODULE(pcb)
module.SetReference(“M1”) # give it a reference name
pcb.Add(module) # add it to our pcb
m_pos = wxPointMM(50,50)

create a pad array and add it to the module

n = 1
for y in range (0,10):
for x in range (0,10):
pad = D_PAD(module)
pt = wxPointMM(1.27x,1.27y)

save the PCB to disk


pcb = LoadBoard(“my2.kicad_pcb”)

print map( lambda x: x.GetReference() , list(pcb.GetModules()))

for m in pcb.GetModules():
for p in m.Pads():
print p.GetPadName(), p.GetPosition(), p.GetOffset()



I’ve tried looking in a few header files in the source code for clues but I don’t know what I’m looking for. Any clues?Also how do I put code in the correct tags in my reply?

Let’s start from #1
What drill sizes do you want to adjust?
TH pads?
Mounting holes?

Then, with above method (kicad_pcb file, text editor) you’re done in 20 minutes.

Python scripting won’t help you or make the problem easier to handle, to the contrary you will need way more knowledge and time to get it done…

I just want to create a script that will allow the university students I work with to set all the drill holes to 1.00mm. Students cant edit the kicad_pcb file directly.
As we produce our PCB’s on an LPKF router we have to keep manually setting the drill holes sizes to the ones that the machine uses. We can not do what you suggest for every single board that we get off every student. If it was my own PCB the issue would not arrise in the first place as I always make sure my drill holes are the correct size.
I’m sorry if you misunderstood what was required. Also Kicad has python scripting so why not use it?

Well, there you go, this is the reference for the D_PAD class that you found in that script up there:

Are you sure just adjusting the drill sizes will do what you need?
What about:

  • clearances (between pads or tracks)
  • pad size vs drill hole size (just increasing the drill size will increase failure rate for hit&miss)
  • layout changes needed due to above points

Wouldn’t it be better to create a project with the correct settings (template) and distribute that to your students BEFORE they start?
Naturally won’t save them from using footprints with “wrong” sized pads/clearances, unless you provide these as well.

This is an older thread, & OP may have solved this, but I was doing a different script, that can expand to do this:
DrillDot sets the required over-rule drill size, here the same for via and non-smd pads.

# - downsizes holes to all be small, for manual drill centre assist.
#  Load PCB Design, then Run this, and DO NOT SAVE design!! (rename before load to be safest)
# Usage:  in PcbNew Python Console
# >>> execfile("C:\KiCad_Python\DrillDot\")
# Output:
# Start
# Done with 13 Vias , 34 Pads, Press F11 to refresh display 
# Refs:  C:\Program Files\KiCad\lib\python2.7\site-packages\
import pcbnew

DrillDot = 0.456    # << Sets Marker hole for Drill Size in mm

print '# Start'
pcb = pcbnew.GetBoard()

#    def SetDrill(self, aDrill):      """SetDrill(VIA self, int aDrill)"""             return _pcbnew.VIA_SetDrill(self, aDrill)
#    def GetDrill(self):              """GetDrill(VIA self) -> int"""                  return _pcbnew.VIA_GetDrill(self)
#    def GetDrillValue(self):         """GetDrillValue(VIA self) -> int"""             return _pcbnew.VIA_GetDrillValue(self)
#    def SetDrillSize(self, aSize):   """SetDrillSize(PAD_List self, wxSize aSize)"""  return _pcbnew.PAD_List_SetDrillSize(self, aSize)
#    def GetDrillSize(self):          """GetDrillSize(PAD_List self) -> wxSize"""      return _pcbnew.PAD_List_GetDrillSize(self)
#    GetAttribute()  pcbnew.PAD_STANDARD,pcbnew.PAD_SMD,pcbnew.PAD_CONN,pcbnew.PAD_HOLE_NOT_PLATED
#    drill: drill size in mm, single value for round hole, or tuple for oblong hole.

vCount = 0
pCount = 0
for item in pcb.GetTracks():                      # Can be VIA or TRACK 
    if type(item) is pcbnew.VIA:
        item.SetDrill(pcbnew.FromMM(DrillDot))    # << This works to re-set VIA Drill Sizes 
        vCount = vCount+1 
for pad in pcb.GetPads():    
    if pad.GetAttribute() <> pcbnew.PAD_SMD:         # Cleans count by skip SMD Pads (not drilled)
       pad.SetDrillSize(wxSizeMM(DrillDot,DrillDot)) # Note: Also makes ovals round, 
       pCount = pCount+1                             
print "# Done with %s Vias , %s Pads, Press F11 to refresh display "%(vCount,pCount)
1 Like

Thanks, I’ll give this a try with some test PCB’s.