Skip to content

Commit 91f920f

Browse files
committed
BoM merge
1 parent 66f817c commit 91f920f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

examples/jlcpcb_pcba_postprocess.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import argparse
22
import csv
33
import math
4-
from typing import Dict, Tuple
4+
import os
5+
from typing import Dict, Tuple, Optional
56

67
parser = argparse.ArgumentParser(description='Post-process KiCad BoM and position files to be compatible with JLC.')
78
parser.add_argument('file_path_prefix', type=str,
89
help='Path prefix to the part data, without the .csv or -top-post.csv postfix, ' +
910
'for example LedMatrix/gerbers/LedMatrix')
11+
parser.add_argument('--merge_boms', type=str, nargs='*',
12+
help="BoM CSVs to merge, for panelization. " +
13+
"If specified, replaces the BoM CSV in the file_path_prefix.")
1014
args = parser.parse_args()
1115

1216

@@ -177,6 +181,21 @@ def remap_by_dict(elt: str, remap_dict: Dict[str, str]) -> str:
177181

178182
refdes_lcsc_map: Dict[str, str] = {} # refdes -> LCSC part number
179183

184+
if args.merge_boms:
185+
if os.path.exists(f'{args.file_path_prefix}.csv'): # remove previous one to avoid confusion
186+
os.remove(f'{args.file_path_prefix}.csv')
187+
with open(f'{args.file_path_prefix}.csv', 'w', newline='') as bom_out:
188+
merged_csv_out: Optional[csv.DictWriter] = None
189+
for input_bom_file in args.merge_boms:
190+
with open(input_bom_file, 'r', newline='') as bom_in:
191+
csv_dict_in = csv.DictReader(bom_in)
192+
if merged_csv_out is None:
193+
assert csv_dict_in.fieldnames is not None
194+
merged_csv_out = csv.DictWriter(bom_out, fieldnames=csv_dict_in.fieldnames)
195+
merged_csv_out.writeheader()
196+
for row_dict in csv_dict_in:
197+
merged_csv_out.writerow(row_dict)
198+
180199
# while we don't need to modify this file, we do need the JLC P/N to refdes map
181200
# to apply the rotations, since that data isn't in the placement file
182201
with open(f'{args.file_path_prefix}.csv', 'r', newline='') as bom_in:

0 commit comments

Comments
 (0)