-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-bom-and-pos.py
executable file
·93 lines (78 loc) · 2.6 KB
/
fix-bom-and-pos.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
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
import csv
import sys
# Packages we know need rotation.
ROTATE_PACKAGES = {
'LQFP-48_7x7mm_P0.5mm': -90,
'SO-16_3.9x9.9mm_P1.27mm': -90,
'SSOP-24_5.3x8.2mm_P0.65mm': -90,
'TO-252-3_TabPin2': 180,
'TSSOP-14_4.4x5mm_P0.65mm': -90,
'TSSOP-16_4.4x3.6mm_P0.4mm': -90,
'TSSOP-16_4.4x5mm_P0.65mm': -90,
'TSSOP-20_4.4x6.5mm_P0.65mm': -90,
'TSSOP-24_4.4x7.8mm_P0.65mm': -90,
'TSOP-II-44_10.16x18.41mm_P0.8mm': -90,
'VSSOP-8_2.3x2mm_P0.5mm': -90,
}
# Packages we know don't need rotation.
OKAY_PACKAGES = [
'C_0603_1608Metric',
'C_1206_3216Metric',
'Crystal_SMD_0603-2Pin_6.0x3.5mm',
'L_0603_1608Metric',
'R_0603_1608Metric',
'TSOP-I-48_18.4x12mm_P0.5mm',
]
# Packages JLCPCB doesn't have at all AFAICT.
DONT_HAVE_PACKAGES = [
'CP_Radial_D5.0mm_P2.00mm',
'PinHeader_2x26_P2.54mm_Vertical',
'TO-220-3_Horizontal_TabDown',
]
bom_in, bom_out, pos_in, pos_out = sys.argv[1:]
with open(bom_in, 'r', newline='') as f:
reader = csv.DictReader(f)
bom_fields = reader.fieldnames
bom_rows = [row for row in reader]
with open(pos_in, 'r', newline='') as f:
reader = csv.DictReader(f)
pos_fields = reader.fieldnames
pos_rows = [row for row in reader]
skipped_names = []
with open(bom_out, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=bom_fields)
writer.writeheader()
for row in bom_rows:
part_num = row['JLCPCB Part#']
name = row['Designator']
# Only write rows we have part numbers for. The others, we don't expect
# JLCPCB to populate. They will sometimes guess a part number for us, but
# it's never right, so it's best to skip these lines.
if part_num:
writer.writerow(row)
else:
print('Skipping {}, no part number'.format(name))
skipped_names.extend(name.split(','))
with open(pos_out, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=pos_fields)
writer.writeheader()
for row in pos_rows:
package = row['Package']
name = row['Designator']
rotation = float(row['Rotation'])
# Skip anything we omitted from the BOM.
if name in skipped_names:
continue
# Correct the rotation of certain items.
if package in ROTATE_PACKAGES:
print('Fixing rotation of {}, package {}'.format(name, package))
rotation += ROTATE_PACKAGES[package]
while rotation < 0: rotation += 360
while rotation >= 360: rotation -= 360
row['Rotation'] = '{:.6f}'.format(rotation)
elif package in OKAY_PACKAGES or package in DONT_HAVE_PACKAGES:
pass
else:
print('Unsure about rotation of {}, package {}'.format(name, package))
writer.writerow(row)