Skip to content

Instantly share code, notes, and snippets.

@hanya
Last active October 6, 2016 13:16
Show Gist options
  • Save hanya/6eedbbbfc9c685068314abb37483db32 to your computer and use it in GitHub Desktop.
Save hanya/6eedbbbfc9c685068314abb37483db32 to your computer and use it in GitHub Desktop.
KICAD macro - set text properties on PCBnew

KICAD macro to set text properties on PCBnew

This macro provides way to set text properties on your PCBnew board. You can choose targets by regular expression of the module reference or text string.

Here is the function definition.

def set_text_properties(board, pattern, type="reference", width=None, height=None, thickness=None, visibility=None):
    """ Sets text properties which matches with the pattern. 
        @param board Pcbnew board
        @param pattern compiled regular expression or string
        @param type one of reference, value or text
        @param width character width in mm
        @param height character height in mm
        @param thickness line thickness in mm
        @param visibility visibility state
    """

If None is passed to property value, it would not touched.

Here is an example to set height, width and thickness of all reference text:

import pcbnew
set_text_properties(pcbnew.GetBoard(), ".*", "reference", 1.0, 1.0, 0.15, None)
def enumerate_text(board, exp):
""" Enumerate text drawings which match with the regular expression on silk layers.
@param board Pcbnew board
@param exp compiled regular expression or string
"""
import pcbnew
exp = re.compile(exp)
TEXTE_PCB = pcbnew.TEXTE_PCB
F_SilkS = pcbnew.F_SilkS
B_SilkS = pcbnew.B_SilkS
for d in board.GetDrawings():
if isinstance(d, TEXTE_PCB):
layer = d.GetLayer()
if layer == F_SilkS or layer == B_SilkS:
yield d
def apply_modules(board, exp, func, type="reference"):
""" Call func to modules which has reference match with the regular expression.
@param board Pcbnew board
@param exp compiled regular expression or string
@param func function which takes a module as its argument
@param type reference or value
"""
exp = re.compile(exp)
if type == "reference":
for module in board.GetModules():
if exp.match(module.GetReference()):
func(module)
elif type == "value":
for module in board.GetModules():
if exp.match(module.GetValue()):
func(module)
def set_text_properties(board, pattern, type="reference", width=None, height=None, thickness=None, visibility=None):
""" Sets text properties which matches with the pattern.
@param board Pcbnew board
@param pattern compiled regular expression or string
@param type one of reference, value or text
@param width character width in mm
@param height character height in mm
@param thickness line thickness in mm
@param visibility visibility state
"""
import pcbnew
set_width = not width is None
set_height = not height is None
set_thickness = not thickness is None
set_visibility = not visibility is None
if set_width:
_width = pcbnew.FromMM(width)
if set_height:
_height = pcbnew.FromMM(height)
if set_thickness:
_thickness = pcbnew.FromMM(thickness)
def _set(module):
ref = _getter(module)
if set_height:
ref.SetHeight(_height)
if set_width:
ref.SetWidth(_width)
if set_thickness:
ref.SetThickness(_thickness)
if set_visibility:
ref.SetVisible(visibility)
if type == "reference":
_getter = lambda module: module.Reference()
apply_modules(board, pattern, _set, type)
elif type == "value":
_getter = lambda module: module.Value()
apply_modules(board, pattern, _set, "reference")
elif type == "text":
import re
exp = re.compile(pattern)
_getter = lambda v: v
for draw in enumerate_text(board, pattern):
if exp.match(draw.GetText()):
_set(draw)
# import pcbnew
#set_text_properties(pcbnew.GetBoard(), ".*", "reference", 1.0, 1.0, 0.15, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment