-
Notifications
You must be signed in to change notification settings - Fork 0
/
bom.py
83 lines (68 loc) · 2.18 KB
/
bom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# run from parent folder:
# $ python scripts/bom.py pcb/*.xml _data/bill_of_materials.csv
import kicad_netlist_reader
import csv
import sys
net = kicad_netlist_reader.netlist(sys.argv[1])
try:
f = open(sys.argv[2], 'w')
except IOError:
e = "Can't open output file for writing: " + sys.argv[2]
print(__file__, ":", e, sys.stderr)
f = sys.stdout
out = csv.writer(f, lineterminator='\n', delimiter=',',
quotechar='\"', quoting=csv.QUOTE_ALL)
def get_datasheet(datasheet):
if datasheet == "~":
return None
return datasheet
def total_cost(unit_cost, quantity):
if not unit_cost:
return 0
return float("{0:.2f}".format(float(unit_cost) * int(quantity)))
# Write CSV
out.writerow([
'Designator', # E.g. U1, R1
'Value', # E.g. 10k, 0.1uF
'Qty', # Quantity of each part
'Package', # E.g. SMD, 0805, SOT-23-5
'Category', # E.g. Electronics, Connector, Mechanical, PCB
'Stock', # Internal stock location
'Manufacturer', # E.g. Vishay, Muticomp
'MPN', # Manufacturer part number
'Datasheet', # URL for Datasheet
'Vendor', # Name of vendor E.g. Element14, Digikey, RS Components, Mouser
'Link', # Vendor link to the exact part
'Unit', # Unit price
'Total', # Total price = Unit price * Quantity
'MOQ', # Minimum Order Quantity
'Description', # Auto-populated by KiCad
'DNP' # Do not populate comma, seperate designators
])
grouped = net.groupComponents()
for group in grouped:
refs = ""
dnp = ""
for component in group:
refs += component.getRef() + ", "
c = component
if c.getField("DNP") == "Yes":
dnp += c.getRef() + ", "
out.writerow([
refs,
c.getValue(),
len(group),
c.getField("Package"),
c.getField("Category"),
c.getField("Stock"),
c.getField("Manufacturer"),
c.getField("MPN"),
get_datasheet(c.getDatasheet()),
c.getField("Vendor"),
c.getField("Link"),
c.getField("Unit"),
total_cost(c.getField("Unit"), len(group)),
c.getField("MOQ"),
c.getPartName() + ": " + c.getDescription(),
dnp,
])