Author Topic: Shell script to generate BOM from KiCad netlist  (Read 5174 times)

0 Members and 1 Guest are viewing this topic.

Offline salfter

  • Contributor
  • Posts: 22
  • Country: us
    • My Blog
Shell script to generate BOM from KiCad netlist
« on: September 12, 2014, 12:54:27 pm »
I've done my last few projects with Upverter.  While it makes things ridiculously easy (shortest learning curve of any EDA system I've run across so far), there's still a nagging concern in the back of my mind WRT locking my work into a proprietary system.  They're supposed to be working on ways to import/export designs, but they're very much a work in progress right now.  I couldn't even get it to reimport a design I had exported in its own format, for instance.

With the above in mind, I've been spending the past few days wrapping my head around KiCad.  I had used gEDA on and off in the past (most recently for this adapter that puts a Raspberry Pi in one of your Apple II's expansion slots, but KiCad seems to have a bit more development and other hubbub going around it.  It was a little bit painful to get it running, but I think I have most of the basics figured out.  I took my reflow toaster controller design and have so far redone the schematic in KiCad. I've not yet regenerated the PCB design, but I have footprints for all components (I think).  At this point, I'm making sure all components in the schematic are tagged with manufacturer and distributor part numbers, datasheet URLs, etc. so that things like automatic BOM generation will be possible.  In particular, I have the following fields defined for most components and should have all of them done before too long:

  • datasheet URL
  • manufacturer name ("Manufacturer")
  • manufacturer part number ("Part Number")
  • DigiKey part number ("DigiKey P/N")
  • Mouser part number("Mouser P/N")

I already have all of this info in the Upverter project, so it's a simple matter of editing it into the schematic.  I'm doing that with a text editor; the file format is fairly simple.  Doing the same for future projects would be a bit more work, but I'm thinking I should build up a collection of pretagged components that can be brought into a design.  For things like capacitors and resistors, a script that generates them might be feasible, as the part numbers follow a pattern.

It looks like KiCad doesn't really do BOM generation on its own.  You're expected to take the files it creates and mangle one of them into a BOM.  With all of the components tagged as described above, though, it wasn't too much work to knock together a script that would convert the netlist into a BOM:

Code: [Select]
echo -e "Quantity\tVendor P/N\tReference\tValue\tManufacturer\tManufacturer P/N"
sed "s/[()]/ /g" $1 | \
tr -d "\r\"" | \
awk \
'$1 == "comp" {ref=$3; next}
 $1 == "value" {sub(".*" $1 FS, ""); val=$0; next}
 $1 == "field" && $3 == "Manufacturer" {sub(".*" $3 FS, ""); mfr=$0}
 $1 == "field" && $3 == "Part" {sub(".*" $4 FS, ""); pn=$0}
 $1 == "field" && $3 == "'$2'" {sub(".*" $4 FS, ""); vpn=$0}
 $1 == "tstamp" && ref != "" {print vpn"\t"ref"\t"val"\t"mfr"\t"pn; ref=val=mfr=pn=vpn=""}
' | \
sed "s/\t /\t/g" | \
sort | \
sed "s/ \t/\t/g;s/\t /\t/g;s/^ //g" | tee /tmp/tmp$$ | \
awk 'BEGIN {FS="\t"} $1 != "" {print}' | \
awk \
'BEGIN {FS="\t"; qty=1}
 $1 != last {if (refs!="") print qty"\t"vpn"\t"refs"\t"val"\t"mfr"\t"pn; refs=""; qty=0}
 {vpn=$1; refs=refs""$2" "; val=$3; mfr=$4; pn=$5}
 {qty++; last=$1}
 END {print qty"\t"vpn"\t"refs"\t"val"\t"mfr"\t"pn}
awk 'BEGIN {FS="\t"} $1 == "" {print "\t"$0}' /tmp/tmp$$
rm /tmp/tmp$$

The first parameter is the netlist file to read.  The second parameter is the vendor...should be a single word with no spaces, so if your favorite distributor has a name with two or more words, strip out the spaces or abbreviate it somehow.  The script only depends on awk, sed, tr, and tee, which come with every system out there (except Windows, but Cygwin will correct that deficiency for you).  Invoke it like this:

Code: [Select]
./ DigiKey >ReflowToaster.csv
The file is actually tab-delimited, not comma-delimited, but most spreadsheet software will load it in just the same.
« Last Edit: September 12, 2014, 12:57:16 pm by salfter »

Offline HackedFridgeMagnet

  • Super Contributor
  • ***
  • Posts: 1931
  • Country: au
Re: Shell script to generate BOM from KiCad netlist
« Reply #1 on: September 12, 2014, 01:20:18 pm »
It does do BOM generation both from the schematic package and the PCB package.

But I think recently there is also some plugin capability, if you want to change the script.

I have just been using the vanilla one from pcbnew, export as csv, then load it into excel, flip it around manually and upload to digikey.
I could should improve this process but I am not prepared to put the work in at the moment.
So I do end up ordering components that I already have, just because I can't be arsed figuring out small quantities of stock.


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo