Skip to content

Instantly share code, notes, and snippets.

@hanya
Last active September 18, 2017 13:21
Show Gist options
  • Save hanya/31272410f9a7399db2e338c37e1559b7 to your computer and use it in GitHub Desktop.
Save hanya/31272410f9a7399db2e338c37e1559b7 to your computer and use it in GitHub Desktop.
Customize text size and line width of KiCAD modules

Customize text size and line width of KiCAD modules

If you think official footprint modules have small text size or thin lines for drawings, you can change them with this script at once.

You need https://github.com/adamgreig/agg-kicad/blob/master/scripts/sexp.py file to execute this script. Obtain it and put it into the same directory with the customize_mods.py file.

Here is list of options can be specified.

python customize_mods.py -h
usage: customize_mods.py [-h] [--text_width TEXT_WIDTH]
                         [--text_height TEXT_HEIGHT]
                         [--text_thickness TEXT_THICKNESS]
                         [--ignore_text IGNORE_TEXT]
                         [--thin_min_width THIN_MIN_WIDTH]
                         [--thin_max_width THIN_MAX_WIDTH]
                         [--thin_width THIN_WIDTH]
                         [--midium_min_width MIDIUM_MIN_WIDTH]
                         [--midium_max_width MIDIUM_MAX_WIDTH]
                         [--midium_width MIDIUM_WIDTH]
                         [--thick_min_width THICK_MIN_WIDTH]
                         [--thick_max_width THICK_MAX_WIDTH]
                         [--thick_width THICK_WIDTH]
                         [--ignore_drawings IGNORE_DRAWINGS]
                         [--verbose VERBOSE]
                         [path]

Customize text size and line width of KiCAD modules

positional arguments:
  path                  default path is './*.pretty/*.kicad_mod'

optional arguments:
  -h, --help            show this help message and exit
  --text_width TEXT_WIDTH
                        text width
  --text_height TEXT_HEIGHT
                        text height
  --text_thickness TEXT_THICKNESS
                        text line thickness
  --ignore_text IGNORE_TEXT
                        Do not change text attributes.
  --thin_min_width THIN_MIN_WIDTH
                        width of drawings
  --thin_max_width THIN_MAX_WIDTH
                        width of drawings
  --thin_width THIN_WIDTH
                        width of drawings
  --midium_min_width MIDIUM_MIN_WIDTH
                        width of drawings
  --midium_max_width MIDIUM_MAX_WIDTH
                        width of drawings
  --midium_width MIDIUM_WIDTH
                        width of drawings
  --thick_min_width THICK_MIN_WIDTH
                        width of drawings
  --thick_max_width THICK_MAX_WIDTH
                        width of drawings
  --thick_width THICK_WIDTH
                        width of drawings
  --ignore_drawings IGNORE_DRAWINGS
                        Do not change drawing width.
  --verbose VERBOSE     verbose output

Here is an example to change text size to 2 mm widht, 2 mm height and 0.2 mm thickness.

python customize_mods.py --text_width=2 --text_height=2 --text_thickness=0.2

You can specify targets to convert as one of

  • path to .kicad_mod file
  • path to .pretty directory, which means all .kicad_mod files are parsed
  • path to a directory, which means all .pretty directories are parsed
  • wild card
  • default target is './.pretty/.kicad_mod', which means all .kicad_mod files contained in the .pretty directories which can be found in the current directory
# sexp from https://github.com/adamgreig/agg-kicad/blob/master/scripts/sexp.py
import sexp
import argparse
import glob
import os
import os.path
from os.path import join
def set_effects_font(s, width, height, thickness):
""" Changes font entries. """
changed = False
for v in s:
if v and v[0] == "effects":
for e in v:
if e and e[0] == "font":
for f in e:
if f:
if f[0] == "size" and len(f) == 3:
if f[1] != width:
f[1] = width
changed = True
if f[2] != height:
f[2] = height
changed = True
elif f[0] == "thickness" and len(f) == 2:
if f[1] != thickness:
f[1] = thickness
changed = True
return changed
def set_drawing_line_width(s, thin, midium, thick):
""" Changes line with of drawings. """
changed = False
for v in s:
if len(v) == 2 and v[0] == "width":
try:
w = float(v[1])
if thin[0] <= w <= thin[1]:
v[1] = thin[2]
changed = True
elif midium[0] <= w <= midium[1]:
v[1] = midium[2]
changed = True
elif thick[0] <= w <= thick[1]:
v[1] = thick[2]
changed = True
except:
pass
changed = True
DRAWINGS = {"fp_arc", "fp_circle", "fp_curve", "fp_line", "fp_poly"}
def customize(path, text_width, text_height, text_line_thickness,
thin, midium, thick,
verbose=False, ignore_text=False, ignore_drawings=False):
""" Changes font attributes and width of drawings. """
drawings = DRAWINGS
with open(path, "r") as f:
s = sexp.parse(f.read())
changed = False
for v in s:
if v:
name = v[0]
if name == "fp_text":
if not ignore_text:
if set_effects_font(v,
text_width, text_height, text_line_thickness):
changed = True
elif name in drawings:
if not ignore_drawings:
if set_drawing_line_width(v,
thin, midium, thick):
changed = True
if changed:
if verbose:
print(path)
with open(path, "w") as f:
f.write(sexp.generate(s))
def parse(args):
text_width = args.text_width
text_height = args.text_height
text_thickness = args.text_thickness
thin = args.thin_min_width, args.thin_max_width, args.thin_width
midium = args.midium_min_width, args.midium_max_width, args.midium_width
thick = args.thick_min_width, args.thick_max_width, args.thick_width
path = args.path
def parse_path(path):
if os.path.isfile(path) and path.endswith(".kicad_mod"):
customize(path, text_width, text_height, text_thickness,
thin, midium, thick, args.verbose)
elif os.path.isdir(path):
if path.endswith(".pretty"):
parse_expanded(join(path, "*.kicad_mod"))
else:
parse_expanded(join(path, "*.pretty", "*.kicad_mod"))
else:
return True
def parse_expanded(path):
for p in glob.glob(path):
parse_path(p)
if not parse_path(path):
pass
else:
parse_expanded(path)
def main():
parser = argparse.ArgumentParser(
description="Customize text size and line width of KiCAD modules")
parser.add_argument("--text_width", default="1",
help="text width")
parser.add_argument("--text_height", default="1",
help="text height")
parser.add_argument("--text_thickness", default="0.2",
help="text line thickness")
parser.add_argument("--ignore_text", default=False,
help="Do not change text attributes.")
parser.add_argument("--thin_min_width", type=float, default=0.01,
help="width of drawings")
parser.add_argument("--thin_max_width", type=float, default=0.099,
help="width of drawings")
parser.add_argument("--thin_width", type=float, default=0.1,
help="width of drawings")
parser.add_argument("--midium_min_width", type=float, default=0.1,
help="width of drawings")
parser.add_argument("--midium_max_width", type=float, default=0.1199,
help="width of drawings")
parser.add_argument("--midium_width", type=float, default=0.15,
help="width of drawings")
parser.add_argument("--thick_min_width", type=float, default=0.12,
help="width of drawings")
parser.add_argument("--thick_max_width", type=float, default=0.2,
help="width of drawings")
parser.add_argument("--thick_width", type=float, default=0.2,
help="width of drawings")
parser.add_argument("--ignore_drawings", default=False,
help="Do not change drawing width.")
parser.add_argument("--verbose", default=False, help="verbose output")
parser.add_argument("path", default=join(os.path.abspath("."), "*.pretty", "*.kicad_mod"),
nargs="?", help="default path is './*.pretty/*.kicad_mod'")
args = parser.parse_args()
parse(args)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment