2023-02-03 00:34:13 +01:00
#!/usr/bin/env ruby
# encoding: utf-8
# ruby: 3.0.0
= begin
2023-02-03 00:38:54 +01:00
KiCAD can have a KiCad database library
2023-02-03 00:34:13 +01:00
https : / / docs . kicad . org / master / en / eeschema / eeschema . html #database_libraries
2023-02-03 00:38:54 +01:00
this script creates views from the partdb table so to create a database for KiCAD
if wil also output the KiCad database library file
2023-02-03 00:34:13 +01:00
= end
2023-03-09 11:27:35 +01:00
require 'sqlite3'
2023-02-03 00:34:13 +01:00
require 'json'
2023-03-09 11:27:35 +01:00
# database file
DB_PATH = " partdb.db "
raise " DB file #{ DB_PATH } does not exist " unless File . file? DB_PATH
db = SQLite3 :: Database . new ( DB_PATH )
db . results_as_hash = true
2023-02-03 00:40:15 +01:00
KICAD_FILE = " partdb.kicad_dbl "
2023-02-03 01:41:53 +01:00
DEBUG = false # print debug information
2023-02-03 00:34:13 +01:00
2023-02-03 01:41:53 +01:00
puts " = create part to properties views = "
2023-02-03 00:34:13 +01:00
2023-02-03 02:01:53 +01:00
prop2view = [ { property : " kicad_symbol " } , { property : " kicad_footprint " } , { property : " qeda_part " } , { property : " qeda_variant " } , { property : " JLCPCB_CORRECTION " } ]
2023-02-03 01:41:53 +01:00
prop2view . each do | prop |
puts prop [ :property ]
prop [ :table ] = " part_ " + prop [ :property ] . downcase . gsub ( / [- \/ ] / , " _ " )
2023-03-09 11:27:35 +01:00
db . query ( " DROP VIEW IF EXISTS #{ prop [ :table ] } " )
view = " CREATE VIEW #{ prop [ :table ] } AS SELECT properties.part AS part, properties.value AS #{ prop [ :property ] } FROM properties JOIN property ON property.id = properties.property WHERE property.name = ' #{ prop [ :property ] } ' "
2023-02-03 01:41:53 +01:00
puts view if DEBUG
db . query ( view )
end
2023-02-03 00:34:13 +01:00
2023-02-03 02:01:53 +01:00
puts " = create part to distribution views = "
dist2view = [ { property : " LCSC " } , { property : " JLCPCB " } , { property : " DigiKey " } ]
dist2view . each do | prop |
puts prop [ :property ]
prop [ :table ] = " part_ " + prop [ :property ] . downcase . gsub ( / [- \/ ] / , " _ " )
2023-03-09 11:27:35 +01:00
db . query ( " DROP VIEW IF EXISTS #{ prop [ :table ] } " )
view = " CREATE VIEW #{ prop [ :table ] } AS SELECT distribution.part AS part, distribution.sku AS #{ prop [ :property ] } FROM distribution JOIN distributor ON distributor.id = distribution.distributor WHERE distributor.name = ' #{ prop [ :property ] } ' "
2023-02-03 02:01:53 +01:00
puts view if DEBUG
db . query ( view )
end
2023-02-03 01:41:53 +01:00
puts " = create part category tables = "
2023-02-03 00:34:13 +01:00
kicad_dbl = { } # kicad database library file
kicad_dbl [ " meta " ] = { version : 0 }
kicad_dbl [ " name " ] = " CuVoodoo Database Library "
kicad_dbl [ " description " ] = " database of all my parts "
# username/UID and password should be configured in DSN so we don't lead it when we share the resulting file
kicad_dbl [ " source " ] = { type : " odbc " , dsn : " partdb " , timeout_seconds : 3 }
kicad_dbl [ " libraries " ] = [ ]
categories = db . query ( " SELECT properties.value AS category FROM properties LEFT JOIN property ON property.id = properties.property WHERE property.name = 'category' " ) . to_a . collect { | row | row [ 'category' ] }
categories . uniq!
categories . each do | category |
puts category
2023-02-03 01:41:53 +01:00
table = " kicad_lib_ " + category . downcase . gsub ( / [- \/ ] / , " _ " )
2023-03-09 11:27:35 +01:00
db . query ( " DROP VIEW IF EXISTS #{ table } " )
view = " CREATE VIEW #{ table } AS "
2023-04-01 10:54:48 +02:00
view += " SELECT part.id, part.name, "
view += " COALESCE(part.description,parent.description) AS description, "
2023-02-26 12:10:01 +01:00
( prop2view + dist2view ) . each do | prop |
2023-02-03 01:41:53 +01:00
view += " COALESCE( #{ prop [ :table ] } 1. #{ prop [ :property ] } , #{ prop [ :table ] } 2. #{ prop [ :property ] } ) AS #{ prop [ :property ] } , "
end
2023-02-26 12:10:01 +01:00
view += " COALESCE(part.datasheet,parent.datasheet) AS datasheet FROM part "
2023-02-03 01:41:53 +01:00
view += " LEFT JOIN part AS parent ON parent.id = part.family "
2023-02-03 02:01:53 +01:00
( prop2view + dist2view ) . each do | prop |
2023-02-03 01:41:53 +01:00
view += " LEFT JOIN #{ prop [ :table ] } AS #{ prop [ :table ] } 1 ON #{ prop [ :table ] } 1.part = part.id "
view += " LEFT JOIN #{ prop [ :table ] } AS #{ prop [ :table ] } 2 ON #{ prop [ :table ] } 2.part = parent.id "
end
view += " WHERE part.id IN (SELECT part FROM properties LEFT JOIN property ON property.id = properties.property WHERE property.name = 'category' AND properties.value = ' #{ category } ') "
view += " OR parent.id IN (SELECT part FROM properties LEFT JOIN property ON property.id = properties.property WHERE property.name = 'category' AND properties.value = ' #{ category } ') "
puts view if DEBUG
2023-02-03 00:34:13 +01:00
db . query ( view )
2023-02-03 01:41:53 +01:00
2023-02-03 00:34:13 +01:00
library = { name : category , table : table , key : " name " , symbols : " kicad_symbol " , footprints : " kicad_footprint " }
library [ :properties ] = { description : " description " }
library [ :fields ] = [ ]
2023-02-26 12:14:00 +01:00
library [ :fields ] << { name : " Description " , column : " description " , visible_on_add : false , visible_in_chooser : true , show_name : false , inherit_properties : true }
library [ :fields ] << { name : " Datasheet " , column : " datasheet " , visible_on_add : false , visible_in_chooser : true , show_name : false , inherit_properties : true }
2023-02-03 02:01:53 +01:00
( prop2view + dist2view ) . each do | prop |
next if prop [ :property ] . start_with? " kicad_ "
library [ :fields ] << { name : prop [ :property ] , column : prop [ :property ] , visible_on_add : false , visible_in_chooser : true , show_name : false , inherit_properties : true }
end
2023-02-03 00:34:13 +01:00
kicad_dbl [ " libraries " ] << library
end
2023-02-03 00:40:15 +01:00
puts " writing KiCad database library file to #{ KICAD_FILE } "
File . open ( KICAD_FILE , " w " ) do | file |
2023-02-26 12:14:37 +01:00
file . write JSON . pretty_generate ( kicad_dbl )
2023-02-03 00:34:13 +01:00
end