From 77353aa822312469a8b2a36a30bf784316980aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Tue, 17 Dec 2019 15:54:02 +0100 Subject: [PATCH] Rakefile: add bom target to export bill of material --- hardware/README.md | 12 ++++++++++-- hardware/Rakefile | 26 +++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/hardware/README.md b/hardware/README.md index 6e47f00..7996e4a 100644 --- a/hardware/README.md +++ b/hardware/README.md @@ -150,13 +150,21 @@ to export the netlist (in tEDAx format): rake netlist ~~~ -to export archive (with symbols included in schematic) and pdf: +to export as pdf: ~~~ rake print ~~~ +BOM +--- + +to export the bill of material (as CSV): +~~~ +rake bom +~~~ + board ------ +===== the `usb_cable_test.lht` file is the board layout source file. it has been drawn using [coralEDA pcb-rnd](http://repo.hu/projects/pcb-rnd/). diff --git a/hardware/Rakefile b/hardware/Rakefile index 4322b87..23c7a59 100644 --- a/hardware/Rakefile +++ b/hardware/Rakefile @@ -108,6 +108,11 @@ notes = [ "#{name}.notes.txt" ] task :notes => notes CLOBBER.include(notes) +desc "export BOMs from schematic" +boms = [ "#{name}.bom.csv" ] +task :bom => boms +CLOBBER.include(boms) + # =============== # file generation # =============== @@ -175,6 +180,19 @@ rule ".notes.txt" => ".sch" do |t| end end +desc "generate BOM file from schematic" +rule ".bom.csv" => ".sch" do |t| + attributes = ["category", "device", "value", "description", "manufacturer", "manufacturer-id", "datasheet", "lcsc-id", "aliexpress-id", "alternatives"] + bom_data = bom2(t.prerequisites[0],attributes) + CSV.open(t.name, "wb") do |csv| + all_attributes = ["refdes","qty"]+attributes + csv << all_attributes + bom_data.each do |line| + csv << all_attributes.collect{|attribute| line[attribute]} + end + end +end + # ================ # helper functions # ================ @@ -195,10 +213,12 @@ def bom2(schematic, attributes) end # generate bom2 list = `lepton-netlist --backend bom2 --backend-option attribs=#{attributes*','} --quiet --output - #{schematic} 2> /dev/null` - list.gsub!(/(\d[Mkmµ]?)\?/, '\1Ω') # UTF-8 characters like Ω are replaced with ? by gnetlist - list.gsub!(/(https?:\/\/[^:]*):/, '"\1":') # ':' (like in links) are not protected + list = list.each_line {|l| '"' + l + '"' + '\n' } + list.gsub!(/^(.+)/, '"\1') + list.gsub!(/(.+)$/, '\1"') + list.gsub!(/(?!http):(?!\/\/)/, '\1":"\2') # protect the values between ':' (such as URLs) # parse bom2 - csv = CSV.parse(list,{:col_sep => ":"}) + csv = CSV.parse(list, {:col_sep => ":", :quote_char => '"'}) csv[1..-1].each do |row| line = {} row.each_index do |col|