|
1 | 1 | import argparse |
2 | 2 | import csv |
3 | 3 | import math |
4 | | -from typing import Dict, Tuple |
| 4 | +import os |
| 5 | +from typing import Dict, Tuple, Optional |
5 | 6 |
|
6 | 7 | parser = argparse.ArgumentParser(description='Post-process KiCad BoM and position files to be compatible with JLC.') |
7 | 8 | parser.add_argument('file_path_prefix', type=str, |
8 | 9 | help='Path prefix to the part data, without the .csv or -top-post.csv postfix, ' + |
9 | 10 | '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.") |
10 | 14 | args = parser.parse_args() |
11 | 15 |
|
12 | 16 |
|
@@ -177,6 +181,21 @@ def remap_by_dict(elt: str, remap_dict: Dict[str, str]) -> str: |
177 | 181 |
|
178 | 182 | refdes_lcsc_map: Dict[str, str] = {} # refdes -> LCSC part number |
179 | 183 |
|
| 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 | + |
180 | 199 | # while we don't need to modify this file, we do need the JLC P/N to refdes map |
181 | 200 | # to apply the rotations, since that data isn't in the placement file |
182 | 201 | with open(f'{args.file_path_prefix}.csv', 'r', newline='') as bom_in: |
|
0 commit comments