Functions and scripts that act on a KiCad project

update_templates[source]

update_templates(v:"verbose", overwrite:"overwrite existing templates", root:"project root directory"='.')

Install templates from the kicad_helpers/templates directory (ignoring anything in the project's .gitignore list).

The following templates are stored in the kicad_helpers/templates folder and are included with the python package by adding graft kicad_helpers/templates to the MANIFEST.in file:

├── .github
│   └── workflows
│       └── build.yml
├── .kicad_helpers_config
│   ├── drc.yaml
│   ├── erc.yaml
│   ├── manufacturers
│   │   ├── PCBWay.yaml
│   │   └── default.yaml
│   ├── pcb_pdf.yaml
│   ├── pcb_svg.yaml
│   ├── sch_pdf.yaml
│   └── sch_svg.yaml
├── kitspace.yaml
├── settings.ini
└── tests
    └── Tests.ipynb

add_badges[source]

add_badges(root:"project root directory"='.', v:"verbose"=False, github:"github"=False, kitspace:"kitspace"=False)

Add badges to the README.md file.

This function is available as a command line script:

> kh_add_badges --help
usage: kh_add_badges [-h] [--root ROOT] [--v] [--github] [--kitspace]

Add badges to the README.md file.

optional arguments:
  -h, --help   show this help message and exit
  --root ROOT  project root directory (default: .)
  --v          verbose (default: False)
  --github     github (default: False)
  --kitspace   kitspace (default: False)

install_git_filters[source]

install_git_filters(root='.', v=False)

Install git filters to prevent insignificant changes to the kicad *.pro and *.sch files from being tracked by git.

See: https://jnavila.github.io/plotkicadsch/

update_gitignore[source]

update_gitignore(root='.', v=False)

Add the following entries to the .gitignore file:

    _autosave*
    *bak
    *.xml
    .ipynb_checkpoints
    *-erc.txt
    *-drc.txt
    kibot_errors.filter

update_project[source]

update_project(v:"verbose", overwrite:"overwrite existing templates", root:"project root directory"='.')

Setup a new project (or update an existing project) with templates from the kicad_helpers/templates directory. Also installs git filters to prevent insignificant changes to the kicad *.pro and *.sch files from being tracked by git (see https://jnavila.github.io/plotkicadsch/ for more details).

This function is available as a command line script:

> kh_update --help
usage: kh_update [-h] [--v] [--overwrite] [--root ROOT]

Setup a new project (or update an existing project) with templates from the `kicad_helpers/templates` directory. Also
installs git filters to prevent insignificant changes to the kicad `*.pro` and `*.sch` files from being tracked by git
(see https://jnavila.github.io/plotkicadsch/ for more details).

optional arguments:
  -h, --help   show this help message and exit
  --v          verbose (default: False)
  --overwrite  overwrite existing templates (default: False)
  --root ROOT  project root directory (default: .)

> kh_update --v --overwrite
Render kitspace.yaml template.
Render settings.ini template.
Render tests/Tests.ipynb template.
Render .github/workflows/build.yml template.
Render .kicad_helpers_config/sch_pdf.yaml template.
Render .kicad_helpers_config/pcb_svg.yaml template.
Render .kicad_helpers_config/erc.yaml template.
Render .kicad_helpers_config/sch_svg.yaml template.
Render .kicad_helpers_config/pcb_pdf.yaml template.
Render .kicad_helpers_config/drc.yaml template.
Render .kicad_helpers_config/manufacturers/default.yaml template.
Render .kicad_helpers_config/manufacturers/PCBWay.yaml template.
"*.pro filter=kicad_project" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitattributes
"*.sch filter=kicad_sch" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitattributes
Add filters to git config.
"_autosave*" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitignore
"*bak" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitignore
"*.xml" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitignore
".ipynb_checkpoints" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitignore
"*-erc.txt" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitignore
"*-drc.txt" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitignore
"kibot_errors.filter" already exists in /home/ryan/dev/python/kicad-helpers/_temp/.gitignore

sch_to_bom[source]

sch_to_bom(root:"project root directory"='.', v:"verbose"=False, overwrite:"update existing schematic"=False)

Update/create BOM from KiCad schematic.

This function can also be called via a command line script:

> kh_sch_to_bom --help
usage: kh_sch_to_bom [-h] [--root ROOT] [--v] [--overwrite]

Update/create BOM from KiCad schematic.

optional arguments:
  -h, --help   show this help message and exit
  --root ROOT  project root directory (default: .)
  --v          verbose (default: False)
  --overwrite  update existing schematic (default: False)

Running kh_sch_to_bom within your project dictory will generate a BOM file (e.g., manufacturing/default/project-name-BOM.csv) by extracting fields from the KiCad schematic file (e.g., project-name.sch).

> kh_sch_to_bom --v
/home/ryan/miniconda3/envs/kh/bin/python3.9 -m kifield -r --nobackup --overwrite --group -x /home/ryan/dev/python/kicad-helpers/_temp/40-channel-hv-switching-board.sch -i /home/ryan/dev/python/kicad-helpers/_temp/manufacturing/default/40-channel-hv-switching-board-BOM.csv


Here's an example of a BOM extracted from a KiCad schematic.

Refs Quantity MPN Manufacturer datasheet footprint value
0 C1, C3, C5, C6, C8-C13, C16 11 CL21B104KBCNNNC Samsung NaN Capacitors_SMD:C_0805 0.1uF
1 C14, C15 2 CL21C151JBANNNC Samsung NaN Capacitors_SMD:C_0805 150pF
2 C2, C4 2 T491D336K020AT KEMET NaN Sci-Bots:SM2917 33uF
3 C7 1 CL21B103KCANNNC Samsung NaN Capacitors_SMD:C_0805 0.01uF
4 CH0-CH39 40 AQW214EAZ Panasonic NaN SMD_Packages:DIP-8_SMD AQW214
5 DS1 1 150080BS75000 Würth Elektronik NaN LEDs:LED_0805 +3.3V_PWR
6 FB1-FB5 5 742792040 Würth Elektronik NaN Resistors_SMD:R_0805 FERRITE
7 JP1 1 DNP DNP NaN Resistors_SMD:R_0805 JUMPER
8 P1 1 DNP DNP NaN Pin_Headers:Pin_Header_Angled_1x06 CONN_01X06
9 P2 1 DNP DNP NaN Pin_Headers:Pin_Header_Straight_2x03 CONN_01X06
10 P3-P6 4 DNP DNP NaN Connect:1pin CONN_01X01
11 P7 1 TFM-125-02-L-DH Samtec NaN Sci-Bots:TFM-125-02-X-DH TFM-125-02-X-DH
12 R1, R6, R7, R12, R13, R18, R19, R24, R25, R30,... 40 RC0805FR-074K75L Yageo NaN Resistors_SMD:R_0805 4.75k
13 R125, R126 2 RC0805FR-07150RL Yageo NaN Resistors_SMD:R_0805 150
14 R2-R5, R8-R11, R14-R17, R20-R23, R26-R29, R32-... 80 RC0805FR-07220RL Yageo NaN Resistors_SMD:R_0805 220
15 R43 1 RC0805FR-0710KL Yageo NaN Resistors_SMD:R_0805 10k
16 R45 1 RC0805FR-071KL Yageo NaN Resistors_SMD:R_0805 1k
17 R99, R100 2 RC0805FR-0751RL Yageo NaN Resistors_SMD:R_0805 51
18 U1-U5 5 MM74HC595MX onsemi ~ Sci-Bots:SO16E 74HC595A
19 U6 1 W25Q64FVSSIG TR Winbond NaN SMD_Packages:SOIC-8-N W25Q64FV
20 U7 1 ATMEGA328P-AU Microchip NaN Housings_QFP:TQFP-32_7x7mm_Pitch0.8mm ATMEGA328P-AU
21 Y1 1 CSTCE8M00G55-R0 Murata http://www.murata.com/~/media/webrenewal/suppo... Sci-Bots:CSTCE_G15C RESONATOR

bom_to_sch[source]

bom_to_sch(root:"project root directory"='.', v:"verbose"=False, overwrite:"update existing schematic"=False)

Update KiCad schematic from BOM file.

This function can also be called via a command line script:

> kh_bom_to_sch --help
usage: kh_bom_to_sch [-h] [--root ROOT] [--v] [--overwrite]

Update KiCad schematic from BOM file.

optional arguments:
  -h, --help   show this help message and exit
  --root ROOT  project root directory (default: .)
  --v          verbose (default: False)
  --overwrite  update existing schematic (default: False)

Running kh_bom_to_sch within your project dictory will read the BOM file (e.g., manufacturing/default/project-name-BOM.csv), and import the fields back into the KiCad schematic file (e.g., project-name.sch).

> kh_sch_to_bom --v
/home/ryan/miniconda3/envs/kh/bin/python3.9 -m kifield -r --nobackup --overwrite --fields ~Quantity -x /home/ryan/dev/python/kicad-helpers/_temp/manufacturing/default/40-channel-hv-switching-board-BOM.csv -i /home/ryan/dev/python/kicad-helpers/_temp/40-channel-hv-switching-board.sch


export_manufacturing[source]

export_manufacturing(root:"project root directory"='.', manufacturer:""default" or manufacturer name"='default', v:"verbose"=False, output:"output path relative to ROOT"='.')

Export manufacturing files (gerber, drill, and position) by running KiBot in a local docker container.

This function can also be called via a command line script:

> kh_export_man --help
usage: kh_export_man [-h] [--root ROOT] [--manufacturer MANUFACTURER] [--v] [--output OUTPUT]

Export manufacturing files (gerber, drill, and position) by running KiBot in a local docker container.

optional arguments:
  -h, --help                   show this help message and exit
  --root ROOT                  project root directory (default: .)
  --manufacturer MANUFACTURER  "default" or manufacturer name (default: default)
  --v                          verbose (default: False)
  --output OUTPUT              output path relative to ROOT (default: .)

Running kh_export_man within your project directory will export manufacturing outputs (e.g., gerber, drill, and position files).

> kh_export_man --output outputs
> tree outputs
.
├── gerbers
│   ├── 40-channel-hv-switching-board-NPTH.drl
│   ├── 40-channel-hv-switching-board.drl
│   ├── 40-channel-hv-switching-board.gbl
│   ├── 40-channel-hv-switching-board.gbo
│   ├── 40-channel-hv-switching-board.gbp
│   ├── 40-channel-hv-switching-board.gbs
│   ├── 40-channel-hv-switching-board.gl2
│   ├── 40-channel-hv-switching-board.gl3
│   ├── 40-channel-hv-switching-board.gm1
│   ├── 40-channel-hv-switching-board.gtl
│   ├── 40-channel-hv-switching-board.gto
│   ├── 40-channel-hv-switching-board.gtp
│   └── 40-channel-hv-switching-board.gts
└── position
    ├── bottom_pos.pos
    └── top_pos.pos

2 directories, 15 files

You can generate manufacturer-specific outputs using the --manufacturer flag, e.g.:

> kh_export_man --manufacturer PCBWay --output outputs

Support for additional manufacturers can be added by creating a *.yaml file in the .kicad_helpers_config/manufacturers directory.

export_sch[source]

export_sch(root:"project root directory"='.', ext:"svg or pdf"='pdf', v:"verbose"=False, output:"output path relative to ROOT"='.')

Export the schematic by running KiBot in a local docker container.

This function can also be called via a command line script:

> kh_export_sch --help
usage: kh_export_sch [-h] [--root ROOT] [--ext EXT] [--v] [--output OUTPUT]

Export the schematic by running KiBot in a local docker container.

optional arguments:
  -h, --help       show this help message and exit
  --root ROOT      project root directory (default: .)
  --ext EXT        svg or pdf (default: pdf)
  --v              verbose (default: False)
  --output OUTPUT  output path relative to ROOT (default: .)

Export the schematic(s) as a pdfs:

> kh_export_sch --ext pdf --output outputs
> tree outputs
.
└── 40-channel-hv-switching-board-schematic.pdf

0 directories, 1 file

Export the schematic(s) as svgs:

> kh_export_sch --ext svg --output outputs
> tree outputs
.
├── 40-channel-hv-switching-board-schematic.svg
├── switches_0-19-switches_0-19.svg
└── switches_20-39-switches_20-39.svg

0 directories, 3 files

export_pcb[source]

export_pcb(root:"project root directory"='.', ext:"svg or pdf"='pdf', v:"verbose"=False, output:"output path relative to ROOT"='.')

Export the pcb layout by running KiBot in a local docker container.

This function can also be called via a command line script:

> kh_export_pcb --help
usage: kh_export_pcb [-h] [--root ROOT] [--ext EXT] [--v] [--output OUTPUT]

Export the pcb layout by running KiBot in a local docker container.

optional arguments:
  -h, --help       show this help message and exit
  --root ROOT      project root directory (default: .)
  --ext EXT        svg or pdf (default: pdf)
  --v              verbose (default: False)
  --output OUTPUT  output path relative to ROOT (default: .)

Export the board layout as pdfs:

> kh_export_pcb --ext pdf --output outputs
> tree outputs
.
├── 40-channel-hv-switching-board-3_3V.pdf
├── 40-channel-hv-switching-board-B_Mask.pdf
├── 40-channel-hv-switching-board-B_Paste.pdf
├── 40-channel-hv-switching-board-B_SilkS.pdf
├── 40-channel-hv-switching-board-Back.pdf
├── 40-channel-hv-switching-board-Edge_Cuts.pdf
├── 40-channel-hv-switching-board-F_Mask.pdf
├── 40-channel-hv-switching-board-F_Paste.pdf
├── 40-channel-hv-switching-board-F_SilkS.pdf
├── 40-channel-hv-switching-board-Front.pdf
└── 40-channel-hv-switching-board-GND.pdf

0 directories, 11 files

Export the board layout as svgs:

> kh_export_pcb --ext svg --output outputs
> tree outputs
.
├── 40-channel-hv-switching-board-3_3V.svg
├── 40-channel-hv-switching-board-B_Mask.svg
├── 40-channel-hv-switching-board-B_Paste.svg
├── 40-channel-hv-switching-board-B_SilkS.svg
├── 40-channel-hv-switching-board-Back.svg
├── 40-channel-hv-switching-board-Edge_Cuts.svg
├── 40-channel-hv-switching-board-F_Mask.svg
├── 40-channel-hv-switching-board-F_Paste.svg
├── 40-channel-hv-switching-board-F_SilkS.svg
├── 40-channel-hv-switching-board-Front.svg
└── 40-channel-hv-switching-board-GND.svg

0 directories, 11 files

run_erc[source]

run_erc(root:"project root directory"='.', v:"verbose"=False)

Run electrical rules check (ERC) to verify schematic connections. It checks for output pin conflicts, missing drivers and unconnected pins. Print the report to stdout.

This function can also be called via a command line script:

> kh_run_erc --help
usage: kh_run_erc [-h] [--root ROOT] [--v]

Run electrical rules check (ERC) to verify schematic connections. It checks for output pin conflicts, missing drivers
and unconnected pins. Print the report to `stdout`.

optional arguments:
  -h, --help   show this help message and exit
  --root ROOT  project root directory (default: .)
  --v          verbose (default: False)

The ERC report is printed to stdout and can be redirected to a file:

> kh_run_erc > erc_report.txt
> cat erc_report.txt
ERC report (Thu Nov 25 20:22:11 2021, Encoding UTF8 )

***** Sheet /

***** Sheet /switches_0-19/

***** Sheet /switches_20-39/

 ** ERC messages: 0  Errors 0  Warnings 0


run_drc[source]

run_drc(root:"project root directory"='.', v:"verbose"=False)

Run design rules check (DRC) and print the report to stdout.

This function can also be called via a command line script:

> kh_run_drc --help
usage: kh_run_drc [-h] [--root ROOT] [--v]

Run design rules check (DRC) and print the report to `stdout`.

optional arguments:
  -h, --help   show this help message and exit
  --root ROOT  project root directory (default: .)
  --v          verbose (default: False)

The DRC report is printed to stdout and can be redirected to a file:

> kh_run_drc > drc_report.txt
> cat drc_report.txt
** Drc report for /workdir/40-channel-hv-switching-board.kicad_pcb **
** Created on 2021-11-25 20:22:30 **

** Found 4 DRC errors **
ErrType(45): Courtyards overlap
    @(90.400 mm, 105.600 mm): Footprint C6 on Front
    @(88.500 mm, 105.600 mm): Footprint C7 on Front
ErrType(45): Courtyards overlap
    @(37.725 mm, 111.425 mm): Footprint R99 on Front
    @(34.725 mm, 111.425 mm): Footprint R125 on Front
ErrType(45): Courtyards overlap
    @(37.750 mm, 113.475 mm): Footprint R100 on Front
    @(34.725 mm, 113.475 mm): Footprint R126 on Front
ErrType(45): Courtyards overlap
    @(44.450 mm, 110.200 mm): Footprint FB1 on Back
    @(44.450 mm, 111.900 mm): Footprint FB3 on Back

** Found 0 unconnected pads **

** End of Report **


set_date[source]

set_date(date:"date (defaults to today's date)"=None, root:"project root directory"='.', v:"verbose"=False)

Set the date in all schematic and board files.

The following command sets the date in all schematic and board files to today's date:

> kh_set_date
schematic_metadata = {'Title': '"40-channel HV switching board"', 'Date': '"2021-11-25"', 'Rev': '"v1.0"', 'Comp': '"Sci-Bots Inc."'}
board_metadata = {'title': '"40-channel HV switching board"', 'date': '2021-11-25', 'rev': 'v1.0', 'company': '"Sci-Bots Inc."'}

set_revision[source]

set_revision(revision:"revision", root:"project root directory"='.', v:"verbose"=False)

Set the revision in all schematic and board files.

The following command sets the revision in all schematic and board files:

> kh_set_revision v1.0
schematic_metadata = {'Title': '"40-channel HV switching board"', 'Date': '"2021-11-25"', 'Rev': '"v1.0"', 'Comp': '"Sci-Bots Inc."'}
board_metadata = {'title': '"40-channel HV switching board"', 'date': '2021-11-25', 'rev': 'v1.0', 'company': '"Sci-Bots Inc."'}