Python script for setting initial size and visibility of all module references & values

Hi,

A task I’m finding I’m doing repeatedly is reformatting the text size for reference and value since I’m using a lot of libraries I’ve found online. I’ve made a simple script to get a uniform starting point. Hope it’s helpful for others.

#!/usr/bin/env python
 
from pcbnew import *
pcb = GetBoard()
moduleCount = 0
for aModule in pcb.GetModules():
    aModule.Reference().SetVisible(1)
    aModule.Reference().SetHeight(1000000)
    aModule.Reference().SetWidth(1000000)
    aModule.Value().SetVisible(1)
    aModule.Value().SetHeight(800000)
    aModule.Value().SetWidth(800000)
    moduleCount = moduleCount + 1

mc_str = str(moduleCount)
print('TextFormat script updated the text of '+mc_str+' Modules.')
print('[Board: '+pcb.GetFileName()+']')

TextFormat.py (505 Bytes)
It’s also attached here as a file. The version here sets the reference text to 1x1mm, the values to 0.8x0.8mm and both visible. It only changes text on modules, so any text elements you’ve added to the board won’t be changed.

To bend it to your preference, height and width are set as 1mm = 1,000,000 in the height or width function; to make either text item invisible, change the 1 to a 0.

Save your edited file in the KiCad script directory. On my Windows installation that’s …/kicad/bin/scripting/plugins.

To use it in KiCad, with your board open in pcbnew open the python scripting console (Tools/Scripting Console). After the >>> prompt appears, type import TextFormat. When you press enter, it will run on the currently open board design.

Once you’ve imported it, to invoke the script the next and subsequent times you need it, jump into the console and use the command reload(TextFormat)

Cheers! Geoff

1 Like

It seems the recent versions of PCBNEW’s python do not accept 1 and 0 for boolean arguments. You have to use the python’s True and False constants or else you will get an error.

#!/usr/bin/env python

from pcbnew import *
pcb = GetBoard()
moduleCount = 0
for aModule in pcb.GetModules():
    aModule.Reference().SetVisible(True)
    aModule.Reference().SetHeight(1000000)
    aModule.Reference().SetWidth(1000000)
    aModule.Value().SetVisible(True)
    aModule.Value().SetHeight(800000)
    aModule.Value().SetWidth(800000)
    moduleCount = moduleCount + 1

mc_str = str(moduleCount)
print('TextFormat script updated the text of '+mc_str+' Modules.')
print('[Board: '+pcb.GetFileName()+']')

I once had aspirations to use python for this task aswell, until I figured out that pcbnew had a menu option for this! I dont know the menu names off the top of my head, but you can easily change all refdes, value andere text sizes without scripting.

I looked but can’t see this. I can see you can set the preferences but it only impacts new additions to your board, not existing footprints. If you remember where you saw this could you post?

Thanks, Geoff

In my version of PCBNEW (BZR 6086), the menu option is Edit -> Reset Footprint Field Sizes.

The flexibility of the script approach is still much more valuable to my workflow but its good to know that the menu option exists!