-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathschematic_updater.py
More file actions
376 lines (310 loc) · 12.8 KB
/
schematic_updater.py
File metadata and controls
376 lines (310 loc) · 12.8 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
Schematic Updater - Updates KiCad schematic files with pad swaps from routing.
When routing applies pad swaps (target swaps or polarity swaps), this module
updates the corresponding .kicad_sch files to keep schematics in sync with PCB.
"""
import os
import re
from typing import List, Dict, Optional, Tuple
def find_schematic_files(schematic_dir: str) -> List[str]:
"""Find all .kicad_sch files in a directory (non-recursive)."""
if not os.path.isdir(schematic_dir):
return []
sch_files = []
for filename in os.listdir(schematic_dir):
if filename.endswith('.kicad_sch'):
sch_files.append(os.path.join(schematic_dir, filename))
return sch_files
def find_all_schematics_for_component(schematic_dir: str, component_ref: str) -> List[str]:
"""
Find all .kicad_sch files containing a component reference.
Multi-unit symbols (like U2A, U2B) may have different units in different
schematic files, but the lib_symbol definition is embedded in each file.
This returns ALL files with a matching Reference so the caller can try each.
Args:
schematic_dir: Directory containing .kicad_sch files
component_ref: Component reference designator (e.g., "U3", "IC1")
Returns:
List of paths to schematic files (may be empty)
"""
sch_files = find_schematic_files(schematic_dir)
matching_files = []
# Pattern to find symbol instances with matching Reference property
# Look for: (property "Reference" "U3" ...) within a (symbol ...) block
ref_pattern = re.compile(
r'\(property\s+"Reference"\s+"' + re.escape(component_ref) + r'"',
re.IGNORECASE
)
for sch_path in sch_files:
try:
with open(sch_path, 'r', encoding='utf-8') as f:
content = f.read()
if ref_pattern.search(content):
matching_files.append(sch_path)
except (IOError, UnicodeDecodeError):
continue
return matching_files
def swap_pins_in_schematic(schematic_path: str, component_ref: str,
pad1: str, pad2: str, verbose: bool = False) -> bool:
"""
Swap two pin numbers for a component in a schematic file.
This swaps the (number "...") values in the lib_symbols section,
which changes which physical pin each signal is assigned to.
Args:
schematic_path: Path to .kicad_sch file
component_ref: Component reference (e.g., "U3")
pad1: First pin number to swap (e.g., "C10")
pad2: Second pin number to swap (e.g., "E10")
verbose: Print debug info
Returns:
True if swap was successful, False otherwise
"""
try:
with open(schematic_path, 'r', encoding='utf-8') as f:
content = f.read()
except (IOError, UnicodeDecodeError) as e:
if verbose:
print(f" Error reading {schematic_path}: {e}")
return False
# First, find which lib_symbol this component uses by finding a symbol instance
# Symbol instances look like: (symbol (lib_id "Library:SymbolName") ... (property "Reference" "U3") ...)
# Find symbol instance with matching Reference
symbol_instance_pattern = re.compile(
r'\(symbol\s*\n?\s*\(lib_id\s+"([^"]+)"\)',
re.DOTALL
)
lib_id = None
for match in symbol_instance_pattern.finditer(content):
# Find the end of this symbol block
start_pos = match.start()
depth = 0
end_pos = start_pos
for i, c in enumerate(content[start_pos:]):
if c == '(':
depth += 1
elif c == ')':
depth -= 1
if depth == 0:
end_pos = start_pos + i + 1
break
symbol_block = content[start_pos:end_pos]
# Check if this symbol has our Reference
ref_match = re.search(
r'\(property\s+"Reference"\s+"' + re.escape(component_ref) + r'"',
symbol_block
)
if ref_match:
lib_id = match.group(1)
break
if not lib_id:
if verbose:
print(f" Component {component_ref} not found in {schematic_path}")
return False
# Now find the lib_symbols section and the symbol definition for this lib_id
# lib_symbols format: (lib_symbols (symbol "Library:SymbolName" ...pin definitions...))
# The symbol name in lib_symbols matches the lib_id
lib_symbol_pattern = re.compile(
r'\(symbol\s+"' + re.escape(lib_id) + r'"',
re.DOTALL
)
lib_symbol_match = lib_symbol_pattern.search(content)
if not lib_symbol_match:
if verbose:
print(f" lib_symbol for {lib_id} not found")
return False
# Find the full lib_symbol block
start_pos = lib_symbol_match.start()
depth = 0
end_pos = start_pos
for i, c in enumerate(content[start_pos:]):
if c == '(':
depth += 1
elif c == ')':
depth -= 1
if depth == 0:
end_pos = start_pos + i + 1
break
lib_symbol_block = content[start_pos:end_pos]
# Find the pin definitions with (number "pad1") and (number "pad2")
# Pin number format (spans multiple lines):
# (number "E21"
# (effects ...)
# )
num1_pattern = re.compile(r'\(number\s+"' + re.escape(pad1) + r'"', re.DOTALL)
num2_pattern = re.compile(r'\(number\s+"' + re.escape(pad2) + r'"', re.DOTALL)
num1_matches = list(num1_pattern.finditer(lib_symbol_block))
num2_matches = list(num2_pattern.finditer(lib_symbol_block))
if not num1_matches or not num2_matches:
if verbose:
if not num1_matches:
print(f" Pin number {pad1} not found in lib_symbol")
if not num2_matches:
print(f" Pin number {pad2} not found in lib_symbol")
return False
# Swap all occurrences of the pin numbers
# Use placeholders to avoid double-replacement
placeholder1 = f"__PIN_NUM_PLACEHOLDER_1_{pad1}__"
placeholder2 = f"__PIN_NUM_PLACEHOLDER_2_{pad2}__"
new_lib_symbol_block = lib_symbol_block
# Replace pad1 with placeholder
new_lib_symbol_block = re.sub(
r'\(number\s+"' + re.escape(pad1) + r'"',
f'(number "{placeholder1}"',
new_lib_symbol_block
)
# Replace pad2 with placeholder
new_lib_symbol_block = re.sub(
r'\(number\s+"' + re.escape(pad2) + r'"',
f'(number "{placeholder2}"',
new_lib_symbol_block
)
# Now replace placeholders with swapped values
new_lib_symbol_block = new_lib_symbol_block.replace(
f'(number "{placeholder1}"',
f'(number "{pad2}"'
)
new_lib_symbol_block = new_lib_symbol_block.replace(
f'(number "{placeholder2}"',
f'(number "{pad1}"'
)
# Replace the lib_symbol block in the content
new_content = content[:start_pos] + new_lib_symbol_block + content[end_pos:]
# Write the modified content back
try:
with open(schematic_path, 'w', encoding='utf-8') as f:
f.write(new_content)
return True
except IOError as e:
if verbose:
print(f" Error writing {schematic_path}: {e}")
return False
def apply_swaps_to_schematics(schematic_dir: str, swap_list: List[Dict],
verbose: bool = False) -> Tuple[int, int]:
"""
Apply all swaps to schematic files.
Args:
schematic_dir: Directory containing .kicad_sch files
swap_list: List of swap dicts with keys:
- component_ref: str (e.g., "U3")
- pad1: str (e.g., "C10")
- pad2: str (e.g., "E10")
verbose: Print progress info
Returns:
(swaps_applied, swaps_failed) tuple
"""
if not swap_list:
return (0, 0)
if not os.path.isdir(schematic_dir):
print(f"Warning: Schematic directory not found: {schematic_dir}")
return (0, len(swap_list))
print(f"\nUpdating schematics in {schematic_dir}...")
swaps_applied = 0
swaps_failed = 0
# Group swaps by component to find schematics efficiently
component_swaps: Dict[str, List[Dict]] = {}
for swap in swap_list:
comp = swap['component_ref']
if comp not in component_swaps:
component_swaps[comp] = []
component_swaps[comp].append(swap)
# Cache of component -> list of candidate schematic files
component_to_schematics: Dict[str, List[str]] = {}
for component_ref, swaps in component_swaps.items():
# Find all schematic files that contain this component
if component_ref not in component_to_schematics:
component_to_schematics[component_ref] = find_all_schematics_for_component(
schematic_dir, component_ref
)
candidate_files = component_to_schematics[component_ref]
if not candidate_files:
print(f" Warning: Component {component_ref} not found in any schematic")
swaps_failed += len(swaps)
continue
for swap in swaps:
pad1 = swap['pad1']
pad2 = swap['pad2']
# Update ALL candidate files that have the lib_symbol with these pins
# (the same lib_symbol definition is embedded in every schematic file
# that uses any unit of this component)
files_updated = []
for sch_path in candidate_files:
if swap_pins_in_schematic(sch_path, component_ref, pad1, pad2, verbose=verbose):
files_updated.append(os.path.basename(sch_path))
if files_updated:
print(f" {component_ref}: {pad1} <-> {pad2} in {', '.join(files_updated)}")
swaps_applied += 1
else:
print(f" Warning: Failed to swap {component_ref}:{pad1} <-> {pad2}")
swaps_failed += 1
return (swaps_applied, swaps_failed)
def collect_swaps_from_state(state) -> List[Dict]:
"""
Collect all swaps from a RoutingState into a unified format.
Args:
state: RoutingState object from routing
Returns:
List of swap dicts with {component_ref, pad1, pad2}
"""
swaps = []
# Single-ended target swaps
if hasattr(state, 'single_ended_target_swap_info'):
for info in state.single_ended_target_swap_info:
if 'n1_pad' in info and 'n2_pad' in info:
pad1 = info['n1_pad']
pad2 = info['n2_pad']
# Only add if same component (typical case)
if pad1.component_ref == pad2.component_ref:
swaps.append({
'component_ref': pad1.component_ref,
'pad1': pad1.pad_number,
'pad2': pad2.pad_number
})
else:
# Different components - add both
swaps.append({
'component_ref': pad1.component_ref,
'pad1': pad1.pad_number,
'pad2': pad2.pad_number
})
swaps.append({
'component_ref': pad2.component_ref,
'pad1': pad2.pad_number,
'pad2': pad1.pad_number
})
# Diff pair target swaps
if hasattr(state, 'target_swap_info'):
for info in state.target_swap_info:
# Each diff pair swap involves 4 pads: p1_p, p1_n, p2_p, p2_n
# The P pads swap with each other, and N pads swap with each other
if all(k in info for k in ['p1_p_pad', 'p2_p_pad', 'p1_n_pad', 'p2_n_pad']):
p1_p = info['p1_p_pad']
p2_p = info['p2_p_pad']
p1_n = info['p1_n_pad']
p2_n = info['p2_n_pad']
# P pads swap
if p1_p.component_ref == p2_p.component_ref:
swaps.append({
'component_ref': p1_p.component_ref,
'pad1': p1_p.pad_number,
'pad2': p2_p.pad_number
})
# N pads swap
if p1_n.component_ref == p2_n.component_ref:
swaps.append({
'component_ref': p1_n.component_ref,
'pad1': p1_n.pad_number,
'pad2': p2_n.pad_number
})
# Polarity swaps (uses pad_p/pad_n keys)
if hasattr(state, 'pad_swaps'):
for swap_info in state.pad_swaps:
if 'pad_p' in swap_info and 'pad_n' in swap_info:
pad_p = swap_info['pad_p']
pad_n = swap_info['pad_n']
if pad_p.component_ref == pad_n.component_ref:
swaps.append({
'component_ref': pad_p.component_ref,
'pad1': pad_p.pad_number,
'pad2': pad_n.pad_number
})
return swaps