-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlayer_swap_optimization.py
More file actions
1359 lines (1197 loc) · 70.9 KB
/
layer_swap_optimization.py
File metadata and controls
1359 lines (1197 loc) · 70.9 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Layer swap optimization for PCB routing.
This module handles the upfront layer swap optimization that happens before routing:
- Diff pair source and target layer swaps
- Single-ended source and target layer swaps
- Solo switches and retry loops
The goal is to minimize the number of vias needed by swapping stubs to compatible layers.
"""
from typing import List, Dict, Set, Tuple, Optional
from dataclasses import dataclass
from kicad_parser import PCBData
from routing_config import GridRouteConfig, DiffPairNet
from connectivity import is_edge_stub
from stub_layer_switching import (
get_stub_info, apply_stub_layer_switch, collect_stubs_by_layer,
collect_stub_endpoints_by_layer, validate_swap, validate_single_swap,
collect_single_ended_stubs_by_layer, revert_stub_layer_switch,
STUB_OVERLAP_Y_TOLERANCE
)
from diff_pair_routing import get_diff_pair_endpoints
from connectivity import get_net_endpoints, get_multipoint_net_pads
def _find_blocking_single_ended_nets(
stub_p, stub_n, dest_layer: str, pcb_data: PCBData, diff_pair_net_ids: Set[int]
) -> List[int]:
"""
Find single-ended net IDs that block the given stubs from moving to dest_layer.
Returns list of net IDs that:
1. Have segments on dest_layer that overlap with stub bounding box
2. Are NOT part of any diff pair
"""
our_segments = stub_p.segments + stub_n.segments
our_net_ids = {stub_p.net_id, stub_n.net_id}
blocking_nets = set()
# Use same tolerance as validate_stub_no_overlap
y_tolerance = STUB_OVERLAP_Y_TOLERANCE
for seg in pcb_data.segments:
if seg.layer != dest_layer:
continue
if seg.net_id in our_net_ids:
continue
if seg.net_id in diff_pair_net_ids:
continue
# Check if segment bounding box overlaps with any of our segments
for our_seg in our_segments:
# Segment objects have start_x, start_y, end_x, end_y attributes
our_x_min = min(our_seg.start_x, our_seg.end_x)
our_x_max = max(our_seg.start_x, our_seg.end_x)
our_y_min = min(our_seg.start_y, our_seg.end_y)
our_y_max = max(our_seg.start_y, our_seg.end_y)
seg_x_min = min(seg.start_x, seg.end_x)
seg_x_max = max(seg.start_x, seg.end_x)
seg_y_min = min(seg.start_y, seg.end_y)
seg_y_max = max(seg.start_y, seg.end_y)
# Check if bounding boxes overlap (with y tolerance)
x_overlap = our_x_min <= seg_x_max and seg_x_min <= our_x_max
y_overlap = (our_y_min - y_tolerance) <= seg_y_max and seg_y_min <= (our_y_max + y_tolerance)
if x_overlap and y_overlap:
blocking_nets.add(seg.net_id)
break
return list(blocking_nets)
def _get_single_ended_stub_on_layer(
pcb_data: PCBData, net_id: int, layer: str, config: GridRouteConfig
):
"""Get stub info for a single-ended net on the given layer."""
sources, targets, error = get_net_endpoints(pcb_data, net_id, config)
if error or not sources or not targets:
return None
src_layer = config.layers[sources[0][2]]
tgt_layer = config.layers[targets[0][2]]
# Check if this net has a stub on the requested layer
if src_layer == layer:
return get_stub_info(pcb_data, net_id, sources[0][3], sources[0][4], layer)
elif tgt_layer == layer:
return get_stub_info(pcb_data, net_id, targets[0][3], targets[0][4], layer)
return None
def _validate_single_ended_swap(
stub, dest_layer: str, pcb_data: PCBData, config: GridRouteConfig,
exclude_net_ids: Set[int] = None
) -> bool:
"""Validate that a single-ended stub can move to dest_layer without conflicts."""
from stub_layer_switching import segments_intersect_2d
if exclude_net_ids is None:
exclude_net_ids = set()
# Check for segment intersections on destination layer
our_segments = stub.segments
our_net_ids = {stub.net_id} | exclude_net_ids
for our_seg in our_segments:
for other in pcb_data.segments:
if other.layer != dest_layer:
continue
if other.net_id in our_net_ids:
continue
# Check if segments actually intersect
if segments_intersect_2d(
(our_seg.start_x, our_seg.start_y), (our_seg.end_x, our_seg.end_y),
(other.start_x, other.start_y), (other.end_x, other.end_y)
):
return False
return True
def apply_diff_pair_layer_swaps(
pcb_data: PCBData,
config: GridRouteConfig,
diff_pair_ids_to_route_set: List[Tuple[str, DiffPairNet]],
diff_pairs: Dict[str, DiffPairNet],
can_swap_to_top_layer: bool,
all_segment_modifications: List,
all_swap_vias: List,
verbose: bool = False
) -> Tuple[int, Dict, Dict]:
"""
Apply upfront layer swap optimization for diff pairs.
Args:
pcb_data: PCB data structure (modified in place)
config: Routing configuration
diff_pair_ids_to_route_set: List of (pair_name, pair) tuples to route
diff_pairs: Dict of all diff pairs
can_swap_to_top_layer: Whether stubs can be swapped to F.Cu
all_segment_modifications: List to append layer modifications (modified in place)
all_swap_vias: List to append vias from swapping (modified in place)
verbose: Whether to print verbose output
Returns:
(total_layer_swaps, all_stubs_by_layer, stub_endpoints_by_layer)
"""
print(f"\nAnalyzing layer swaps for {len(diff_pair_ids_to_route_set)} diff pair(s)...")
# Collect layer info for pairs we're routing
pair_layer_info = {} # pair_name -> (src_layer, tgt_layer, sources, targets, pair)
for pair_name, pair in diff_pair_ids_to_route_set:
sources, targets, error = get_diff_pair_endpoints(pcb_data, pair.p_net_id, pair.n_net_id, config)
if error or not sources or not targets:
continue
src_layer = config.layers[sources[0][4]]
tgt_layer = config.layers[targets[0][4]]
pair_layer_info[pair_name] = (src_layer, tgt_layer, sources, targets, pair)
# Build layer info for ALL diff pairs (for finding swap partners)
all_pair_layer_info = {} # pair_name -> (src_layer, tgt_layer, sources, targets, pair)
for pair_name, pair in diff_pairs.items():
sources, targets, error = get_diff_pair_endpoints(pcb_data, pair.p_net_id, pair.n_net_id, config)
if error or not sources or not targets:
continue
src_layer = config.layers[sources[0][4]]
tgt_layer = config.layers[targets[0][4]]
all_pair_layer_info[pair_name] = (src_layer, tgt_layer, sources, targets, pair)
# Pre-collect all stub segments by layer for validation
all_stubs_by_layer = collect_stubs_by_layer(pcb_data, all_pair_layer_info, config)
# Pre-collect all stub endpoints by layer for proximity checking
stub_endpoints_by_layer = collect_stub_endpoints_by_layer(pcb_data, all_pair_layer_info, config)
# Find pairs that need layer switches (src != tgt layer)
pairs_needing_via = [(name, info) for name, info in pair_layer_info.items()
if info[0] != info[1]]
# Try to find swap partners for pairs needing via
applied_swaps = set()
swap_count = 0
total_layer_swaps = 0
# Phase 1: Source segment overlap swaps
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue
# Get our source stub info
src_p_stub = get_stub_info(pcb_data, pair.p_net_id,
sources[0][5], sources[0][6], src_layer)
src_n_stub = get_stub_info(pcb_data, pair.n_net_id,
sources[0][7], sources[0][8], src_layer)
if not src_p_stub or not src_n_stub:
continue
swap_partner = None
swap_partner_stubs = None
# Find which nets on target layer actually overlap with our stub segments
overlapping_nets = set()
our_stubs = src_p_stub.segments + src_n_stub.segments
for stub_seg in our_stubs:
stub_y_min = min(stub_seg.start_y, stub_seg.end_y) - STUB_OVERLAP_Y_TOLERANCE
stub_y_max = max(stub_seg.start_y, stub_seg.end_y) + STUB_OVERLAP_Y_TOLERANCE
stub_x_min = min(stub_seg.start_x, stub_seg.end_x)
stub_x_max = max(stub_seg.start_x, stub_seg.end_x)
for seg in pcb_data.segments:
if seg.layer != tgt_layer:
continue
seg_y_min = min(seg.start_y, seg.end_y)
seg_y_max = max(seg.start_y, seg.end_y)
seg_x_min = min(seg.start_x, seg.end_x)
seg_x_max = max(seg.start_x, seg.end_x)
# Check Y and X overlap
if seg_y_max >= stub_y_min and seg_y_min <= stub_y_max:
if seg_x_max >= stub_x_min and seg_x_min <= stub_x_max:
overlapping_nets.add(seg.net_id)
# Find which diff pair the overlapping nets belong to
for other_name, other_info in all_pair_layer_info.items():
if other_name == pair_name:
continue
other_src_layer, other_tgt_layer, other_sources, other_targets, other_pair = other_info
# Check if their source is on our target layer and overlaps
if other_src_layer != tgt_layer:
continue
if other_pair.p_net_id not in overlapping_nets and other_pair.n_net_id not in overlapping_nets:
continue
# IMPORTANT: Don't break a pair that was already OK!
# After swap, partner's source will be on our src_layer.
# Partner is OK if: their new source (src_layer) == their target (other_tgt_layer)
# OR if they already needed a via (can't make it worse)
partner_already_needs_via = (other_src_layer != other_tgt_layer)
partner_would_be_ok_after = (src_layer == other_tgt_layer)
if not partner_already_needs_via and not partner_would_be_ok_after:
# Partner was OK but swap would break them - skip
continue
# Get their source stub info
other_src_p_stub = get_stub_info(pcb_data, other_pair.p_net_id,
other_sources[0][5], other_sources[0][6], other_src_layer)
other_src_n_stub = get_stub_info(pcb_data, other_pair.n_net_id,
other_sources[0][7], other_sources[0][8], other_src_layer)
if other_src_p_stub and other_src_n_stub:
swap_partner = other_name
swap_partner_stubs = (other_src_p_stub, other_src_n_stub, other_src_layer)
break
if swap_partner and swap_partner_stubs:
# Found a swap partner! Swap source layers
other_src_p_stub, other_src_n_stub, other_src_layer = swap_partner_stubs
_, _, _, _, other_pair = all_pair_layer_info[swap_partner]
# Validate swap before applying
our_valid, our_reason = validate_swap(
src_p_stub, src_n_stub, tgt_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=swap_partner,
swap_partner_net_ids={other_pair.p_net_id, other_pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
partner_valid, partner_reason = validate_swap(
other_src_p_stub, other_src_n_stub, src_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=pair_name,
swap_partner_net_ids={pair.p_net_id, pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if not our_valid or not partner_valid:
reason = our_reason if not our_valid else partner_reason
print(f" Source swap validation failed for {pair_name}: {reason}")
continue # Try target swap later
# Check if swap would move stubs to F.Cu (top layer)
# Skip if can_swap_to_top_layer is False and either destination is F.Cu
# Exception: allow edge stubs (on BGA boundary) to swap to F.Cu
if not can_swap_to_top_layer and (tgt_layer == 'F.Cu' or src_layer == 'F.Cu'):
# Check if stubs moving to F.Cu are edge stubs
allow_swap = True
if tgt_layer == 'F.Cu':
# Our stubs would move to F.Cu - check if they're edge stubs
if not (is_edge_stub(src_p_stub.pad_x, src_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(src_n_stub.pad_x, src_n_stub.pad_y, config.bga_exclusion_zones)):
allow_swap = False
if src_layer == 'F.Cu' and allow_swap:
# Their stubs would move to F.Cu - check if they're edge stubs
if not (is_edge_stub(other_src_p_stub.pad_x, other_src_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(other_src_n_stub.pad_x, other_src_n_stub.pad_y, config.bga_exclusion_zones)):
allow_swap = False
if not allow_swap:
continue
# Our source: src_layer -> tgt_layer
# Their source: other_src_layer (=tgt_layer) -> src_layer
vias1, mods1 = apply_stub_layer_switch(pcb_data, src_p_stub, tgt_layer, config, debug=False)
vias2, mods2 = apply_stub_layer_switch(pcb_data, src_n_stub, tgt_layer, config, debug=False)
vias3, mods3 = apply_stub_layer_switch(pcb_data, other_src_p_stub, src_layer, config, debug=False)
vias4, mods4 = apply_stub_layer_switch(pcb_data, other_src_n_stub, src_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2 + mods3 + mods4)
all_vias = vias1 + vias2 + vias3 + vias4
all_swap_vias.extend(all_vias)
# Update all_stubs_by_layer to reflect the layer changes
# pair_name: src_layer -> tgt_layer
if src_layer in all_stubs_by_layer:
all_stubs_by_layer[src_layer] = [
s for s in all_stubs_by_layer[src_layer] if s[0] != pair_name
]
if tgt_layer not in all_stubs_by_layer:
all_stubs_by_layer[tgt_layer] = []
all_stubs_by_layer[tgt_layer].append(
(pair_name, src_p_stub.segments + src_n_stub.segments)
)
# swap_partner: other_src_layer -> src_layer
if other_src_layer in all_stubs_by_layer:
all_stubs_by_layer[other_src_layer] = [
s for s in all_stubs_by_layer[other_src_layer] if s[0] != swap_partner
]
if src_layer not in all_stubs_by_layer:
all_stubs_by_layer[src_layer] = []
all_stubs_by_layer[src_layer].append(
(swap_partner, other_src_p_stub.segments + other_src_n_stub.segments)
)
# Update stub_endpoints_by_layer to reflect the layer changes
if src_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = [
e for e in stub_endpoints_by_layer[src_layer] if e[0] != pair_name
]
if tgt_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = []
stub_endpoints_by_layer[tgt_layer].append(
(pair_name, [(src_p_stub.x, src_p_stub.y), (src_n_stub.x, src_n_stub.y)])
)
if other_src_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[other_src_layer] = [
e for e in stub_endpoints_by_layer[other_src_layer] if e[0] != swap_partner
]
if src_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = []
stub_endpoints_by_layer[src_layer].append(
(swap_partner, [(other_src_p_stub.x, other_src_p_stub.y), (other_src_n_stub.x, other_src_n_stub.y)])
)
applied_swaps.add(pair_name)
applied_swaps.add(swap_partner)
swap_count += 1
total_layer_swaps += 1
via_msg = f", added {len(all_vias)} pad via(s)" if all_vias else ""
print(f" Source swap: {pair_name} ({src_layer}->{tgt_layer}) <-> {swap_partner} ({other_src_layer}->{src_layer}){via_msg}")
if swap_count > 0:
print(f"Applied {swap_count} source layer swap(s)")
# Phase 2: Solo source layer switches (no partner needed)
solo_src_count = 0
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue
# Check if we can move source stubs to target layer without a partner
src_p_stub = get_stub_info(pcb_data, pair.p_net_id,
sources[0][5], sources[0][6], src_layer)
src_n_stub = get_stub_info(pcb_data, pair.n_net_id,
sources[0][7], sources[0][8], src_layer)
if not src_p_stub or not src_n_stub:
continue
# Check if swap would move stubs to F.Cu (top layer)
# Exception: allow edge stubs to swap to F.Cu
if not can_swap_to_top_layer and tgt_layer == 'F.Cu':
if not (is_edge_stub(src_p_stub.pad_x, src_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(src_n_stub.pad_x, src_n_stub.pad_y, config.bga_exclusion_zones)):
continue
# Validate solo switch: source stubs move to target layer
valid, reason = validate_swap(
src_p_stub, src_n_stub, tgt_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=None,
swap_partner_net_ids=set(),
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if valid:
# Apply solo source switch
vias1, mods1 = apply_stub_layer_switch(pcb_data, src_p_stub, tgt_layer, config, debug=False)
vias2, mods2 = apply_stub_layer_switch(pcb_data, src_n_stub, tgt_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2)
all_vias = vias1 + vias2
all_swap_vias.extend(all_vias)
# Update all_stubs_by_layer to reflect the layer change
# Structure is (pair_name, segments) tuples
# Remove from old layer and add to new layer
if src_layer in all_stubs_by_layer:
all_stubs_by_layer[src_layer] = [
s for s in all_stubs_by_layer[src_layer]
if s[0] != pair_name # s[0] is pair_name
]
if tgt_layer not in all_stubs_by_layer:
all_stubs_by_layer[tgt_layer] = []
# Add combined segments for this pair on new layer
combined_segments = src_p_stub.segments + src_n_stub.segments
all_stubs_by_layer[tgt_layer].append((pair_name, combined_segments))
# Update stub_endpoints_by_layer
if src_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = [
e for e in stub_endpoints_by_layer[src_layer] if e[0] != pair_name
]
if tgt_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = []
stub_endpoints_by_layer[tgt_layer].append(
(pair_name, [(src_p_stub.x, src_p_stub.y), (src_n_stub.x, src_n_stub.y)])
)
applied_swaps.add(pair_name)
solo_src_count += 1
total_layer_swaps += 1
via_msg = f", added {len(all_vias)} pad via(s)" if all_vias else ""
print(f" Solo source switch: {pair_name} ({src_layer}->{tgt_layer}){via_msg}")
else:
print(f" Solo source switch validation failed for {pair_name}: {reason}")
if solo_src_count > 0:
print(f"Applied {solo_src_count} solo source layer switch(es)")
# Phase 3: Target-side segment overlap swaps
target_swap_count = 0
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue
# Get our target stub info
tgt_p_stub = get_stub_info(pcb_data, pair.p_net_id,
targets[0][5], targets[0][6], tgt_layer)
tgt_n_stub = get_stub_info(pcb_data, pair.n_net_id,
targets[0][7], targets[0][8], tgt_layer)
if not tgt_p_stub or not tgt_n_stub:
continue
swap_partner = None
swap_partner_stubs = None
# Find which nets on source layer actually overlap with our target stub segments
overlapping_nets = set()
our_stubs = tgt_p_stub.segments + tgt_n_stub.segments
for stub_seg in our_stubs:
stub_y_min = min(stub_seg.start_y, stub_seg.end_y) - STUB_OVERLAP_Y_TOLERANCE
stub_y_max = max(stub_seg.start_y, stub_seg.end_y) + STUB_OVERLAP_Y_TOLERANCE
stub_x_min = min(stub_seg.start_x, stub_seg.end_x)
stub_x_max = max(stub_seg.start_x, stub_seg.end_x)
for seg in pcb_data.segments:
if seg.layer != src_layer:
continue
seg_y_min = min(seg.start_y, seg.end_y)
seg_y_max = max(seg.start_y, seg.end_y)
seg_x_min = min(seg.start_x, seg.end_x)
seg_x_max = max(seg.start_x, seg.end_x)
# Check Y and X overlap
if seg_y_max >= stub_y_min and seg_y_min <= stub_y_max:
if seg_x_max >= stub_x_min and seg_x_min <= stub_x_max:
overlapping_nets.add(seg.net_id)
# Find which diff pair the overlapping nets belong to
for other_name, other_info in all_pair_layer_info.items():
if other_name == pair_name:
continue
other_src_layer, other_tgt_layer, other_sources, other_targets, other_pair = other_info
# Check if their target is on our source layer and overlaps
if other_tgt_layer != src_layer:
continue
if other_pair.p_net_id not in overlapping_nets and other_pair.n_net_id not in overlapping_nets:
continue
# IMPORTANT: Don't break a pair that was already OK!
# After swap, partner's target will be on our tgt_layer.
# Partner is OK if: their source (other_src_layer) == their new target (tgt_layer)
# OR if they already needed a via (can't make it worse)
partner_already_needs_via = (other_src_layer != other_tgt_layer)
partner_would_be_ok_after = (other_src_layer == tgt_layer)
if not partner_already_needs_via and not partner_would_be_ok_after:
# Partner was OK but swap would break them - skip
continue
# Get their target stub info
other_tgt_p_stub = get_stub_info(pcb_data, other_pair.p_net_id,
other_targets[0][5], other_targets[0][6], other_tgt_layer)
other_tgt_n_stub = get_stub_info(pcb_data, other_pair.n_net_id,
other_targets[0][7], other_targets[0][8], other_tgt_layer)
if other_tgt_p_stub and other_tgt_n_stub:
swap_partner = other_name
swap_partner_stubs = (other_tgt_p_stub, other_tgt_n_stub, other_tgt_layer)
break
if swap_partner and swap_partner_stubs:
# Found a swap partner! Swap target layers
other_tgt_p_stub, other_tgt_n_stub, other_tgt_layer = swap_partner_stubs
_, _, _, _, other_pair = all_pair_layer_info[swap_partner]
# Validate swap before applying
our_valid, our_reason = validate_swap(
tgt_p_stub, tgt_n_stub, src_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=swap_partner,
swap_partner_net_ids={other_pair.p_net_id, other_pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
partner_valid, partner_reason = validate_swap(
other_tgt_p_stub, other_tgt_n_stub, tgt_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=pair_name,
swap_partner_net_ids={pair.p_net_id, pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if not our_valid or not partner_valid:
reason = our_reason if not our_valid else partner_reason
print(f" Target swap validation failed for {pair_name}: {reason}")
continue
# Check if swap would move stubs to F.Cu (top layer)
# Skip if can_swap_to_top_layer is False and either destination is F.Cu
# Exception: allow edge stubs to swap to F.Cu
if not can_swap_to_top_layer and (src_layer == 'F.Cu' or tgt_layer == 'F.Cu'):
# Check if stubs moving to F.Cu are edge stubs
allow_swap = True
if src_layer == 'F.Cu':
# Our stubs would move to F.Cu - check if they're edge stubs
if not (is_edge_stub(tgt_p_stub.pad_x, tgt_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(tgt_n_stub.pad_x, tgt_n_stub.pad_y, config.bga_exclusion_zones)):
allow_swap = False
if tgt_layer == 'F.Cu' and allow_swap:
# Their stubs would move to F.Cu - check if they're edge stubs
if not (is_edge_stub(other_tgt_p_stub.pad_x, other_tgt_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(other_tgt_n_stub.pad_x, other_tgt_n_stub.pad_y, config.bga_exclusion_zones)):
allow_swap = False
if not allow_swap:
continue
# Our target: tgt_layer -> src_layer
# Their target: other_tgt_layer (=src_layer) -> tgt_layer
vias1, mods1 = apply_stub_layer_switch(pcb_data, tgt_p_stub, src_layer, config, debug=False)
vias2, mods2 = apply_stub_layer_switch(pcb_data, tgt_n_stub, src_layer, config, debug=False)
vias3, mods3 = apply_stub_layer_switch(pcb_data, other_tgt_p_stub, tgt_layer, config, debug=False)
vias4, mods4 = apply_stub_layer_switch(pcb_data, other_tgt_n_stub, tgt_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2 + mods3 + mods4)
all_vias = vias1 + vias2 + vias3 + vias4
all_swap_vias.extend(all_vias)
# Update stub_endpoints_by_layer for both pairs
# Our targets move from tgt_layer to src_layer
if tgt_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = [
e for e in stub_endpoints_by_layer[tgt_layer] if e[0] != pair_name
]
if src_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = []
stub_endpoints_by_layer[src_layer].append(
(pair_name, [(tgt_p_stub.x, tgt_p_stub.y), (tgt_n_stub.x, tgt_n_stub.y)])
)
# Their targets move from other_tgt_layer to tgt_layer
if other_tgt_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[other_tgt_layer] = [
e for e in stub_endpoints_by_layer[other_tgt_layer] if e[0] != swap_partner
]
if tgt_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = []
stub_endpoints_by_layer[tgt_layer].append(
(swap_partner, [(other_tgt_p_stub.x, other_tgt_p_stub.y), (other_tgt_n_stub.x, other_tgt_n_stub.y)])
)
applied_swaps.add(pair_name)
applied_swaps.add(swap_partner)
target_swap_count += 1
total_layer_swaps += 1
via_msg = f", added {len(all_vias)} pad via(s)" if all_vias else ""
print(f" Target swap: {pair_name} ({tgt_layer}->{src_layer}) <-> {swap_partner} ({other_tgt_layer}->{tgt_layer}){via_msg}")
if target_swap_count > 0:
print(f"Applied {target_swap_count} target layer swap(s)")
# Phase 4: Solo target layer switches (no partner needed)
solo_switch_count = 0
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue
# Check if we can move target stubs to source layer without a partner
tgt_p_stub = get_stub_info(pcb_data, pair.p_net_id,
targets[0][5], targets[0][6], tgt_layer)
tgt_n_stub = get_stub_info(pcb_data, pair.n_net_id,
targets[0][7], targets[0][8], tgt_layer)
if not tgt_p_stub or not tgt_n_stub:
missing = []
if not tgt_p_stub:
missing.append("P")
if not tgt_n_stub:
missing.append("N")
print(f" Solo target switch skipped for {pair_name}: can't find {'+'.join(missing)} stub at target ({targets[0][5]:.2f}, {targets[0][6]:.2f}) on {tgt_layer}")
continue
# Check if swap would move stubs to F.Cu (top layer)
# Exception: allow edge stubs to swap to F.Cu
if not can_swap_to_top_layer and src_layer == 'F.Cu':
if not (is_edge_stub(tgt_p_stub.pad_x, tgt_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(tgt_n_stub.pad_x, tgt_n_stub.pad_y, config.bga_exclusion_zones)):
print(f" Solo target switch skipped for {pair_name}: would move to F.Cu (top layer)")
continue
# Validate solo switch: target stubs move to source layer
valid, reason = validate_swap(
tgt_p_stub, tgt_n_stub, src_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=None,
swap_partner_net_ids=set(),
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if valid:
# Apply solo target switch
vias1, mods1 = apply_stub_layer_switch(pcb_data, tgt_p_stub, src_layer, config, debug=False)
vias2, mods2 = apply_stub_layer_switch(pcb_data, tgt_n_stub, src_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2)
all_vias = vias1 + vias2
all_swap_vias.extend(all_vias)
# Update all_stubs_by_layer to reflect the layer change
# Structure is (pair_name, segments) tuples
if tgt_layer in all_stubs_by_layer:
all_stubs_by_layer[tgt_layer] = [
s for s in all_stubs_by_layer[tgt_layer]
if s[0] != pair_name
]
if src_layer not in all_stubs_by_layer:
all_stubs_by_layer[src_layer] = []
combined_segments = tgt_p_stub.segments + tgt_n_stub.segments
all_stubs_by_layer[src_layer].append((pair_name, combined_segments))
# Update stub_endpoints_by_layer
if tgt_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = [
e for e in stub_endpoints_by_layer[tgt_layer] if e[0] != pair_name
]
if src_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = []
stub_endpoints_by_layer[src_layer].append(
(pair_name, [(tgt_p_stub.x, tgt_p_stub.y), (tgt_n_stub.x, tgt_n_stub.y)])
)
applied_swaps.add(pair_name)
solo_switch_count += 1
total_layer_swaps += 1
via_msg = f", added {len(all_vias)} pad via(s)" if all_vias else ""
print(f" Solo target switch: {pair_name} ({tgt_layer}->{src_layer}){via_msg}")
else:
print(f" Solo target switch validation failed for {pair_name}: {reason}")
if solo_switch_count > 0:
print(f"Applied {solo_switch_count} solo target layer switch(es)")
# Phase 5: Retry solo switches if any progress was made (newly freed layers may allow more switches)
if solo_src_count > 0 or solo_switch_count > 0:
retry_round = 1
while True:
retry_round += 1
retry_src_count = 0
retry_tgt_count = 0
# Retry solo source switches
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue
src_p_stub = get_stub_info(pcb_data, pair.p_net_id,
sources[0][5], sources[0][6], src_layer)
src_n_stub = get_stub_info(pcb_data, pair.n_net_id,
sources[0][7], sources[0][8], src_layer)
if not src_p_stub or not src_n_stub:
continue
if not can_swap_to_top_layer and tgt_layer == 'F.Cu':
if not (is_edge_stub(src_p_stub.pad_x, src_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(src_n_stub.pad_x, src_n_stub.pad_y, config.bga_exclusion_zones)):
continue
valid, reason = validate_swap(
src_p_stub, src_n_stub, tgt_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=None,
swap_partner_net_ids=set(),
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if valid:
vias1, mods1 = apply_stub_layer_switch(pcb_data, src_p_stub, tgt_layer, config, debug=False)
vias2, mods2 = apply_stub_layer_switch(pcb_data, src_n_stub, tgt_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2)
all_vias = vias1 + vias2
all_swap_vias.extend(all_vias)
if src_layer in all_stubs_by_layer:
all_stubs_by_layer[src_layer] = [s for s in all_stubs_by_layer[src_layer] if s[0] != pair_name]
if tgt_layer not in all_stubs_by_layer:
all_stubs_by_layer[tgt_layer] = []
all_stubs_by_layer[tgt_layer].append((pair_name, src_p_stub.segments + src_n_stub.segments))
if src_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = [e for e in stub_endpoints_by_layer[src_layer] if e[0] != pair_name]
if tgt_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = []
stub_endpoints_by_layer[tgt_layer].append((pair_name, [(src_p_stub.x, src_p_stub.y), (src_n_stub.x, src_n_stub.y)]))
applied_swaps.add(pair_name)
retry_src_count += 1
total_layer_swaps += 1
via_msg = f", added {len(all_vias)} pad via(s)" if all_vias else ""
print(f" Solo source switch (round {retry_round}): {pair_name} ({src_layer}->{tgt_layer}){via_msg}")
# Retry solo target switches
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue
tgt_p_stub = get_stub_info(pcb_data, pair.p_net_id,
targets[0][5], targets[0][6], tgt_layer)
tgt_n_stub = get_stub_info(pcb_data, pair.n_net_id,
targets[0][7], targets[0][8], tgt_layer)
if not tgt_p_stub or not tgt_n_stub:
continue
if not can_swap_to_top_layer and src_layer == 'F.Cu':
if not (is_edge_stub(tgt_p_stub.pad_x, tgt_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(tgt_n_stub.pad_x, tgt_n_stub.pad_y, config.bga_exclusion_zones)):
continue
valid, reason = validate_swap(
tgt_p_stub, tgt_n_stub, src_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=None,
swap_partner_net_ids=set(),
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if valid:
vias1, mods1 = apply_stub_layer_switch(pcb_data, tgt_p_stub, src_layer, config, debug=False)
vias2, mods2 = apply_stub_layer_switch(pcb_data, tgt_n_stub, src_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2)
all_vias = vias1 + vias2
all_swap_vias.extend(all_vias)
if tgt_layer in all_stubs_by_layer:
all_stubs_by_layer[tgt_layer] = [s for s in all_stubs_by_layer[tgt_layer] if s[0] != pair_name]
if src_layer not in all_stubs_by_layer:
all_stubs_by_layer[src_layer] = []
all_stubs_by_layer[src_layer].append((pair_name, tgt_p_stub.segments + tgt_n_stub.segments))
if tgt_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = [e for e in stub_endpoints_by_layer[tgt_layer] if e[0] != pair_name]
if src_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = []
stub_endpoints_by_layer[src_layer].append((pair_name, [(tgt_p_stub.x, tgt_p_stub.y), (tgt_n_stub.x, tgt_n_stub.y)]))
applied_swaps.add(pair_name)
retry_tgt_count += 1
total_layer_swaps += 1
via_msg = f", added {len(all_vias)} pad via(s)" if all_vias else ""
print(f" Solo target switch (round {retry_round}): {pair_name} ({tgt_layer}->{src_layer}){via_msg}")
if retry_src_count == 0 and retry_tgt_count == 0:
break # No more progress
print(f"Applied {retry_src_count + retry_tgt_count} additional solo switch(es) in round {retry_round}")
# Phase 6: Two-pair swaps for remaining pairs that weren't handled
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue
# Look for another pair that also needs a via to swap with
# Option 1: Swap sources - we want our source to become tgt_layer
# Option 2: Swap targets - we want our target to become src_layer
for other_name, other_info in pairs_needing_via:
if other_name in applied_swaps or other_name == pair_name:
continue
other_src, other_tgt, other_sources, other_targets, other_pair = other_info
swap_type = None
our_stubs = None
their_stubs = None
our_new_layer = None
their_new_layer = None
# Option 1: Swap sources
# Their source is on our target layer, our source is on their target layer
if other_src == tgt_layer and src_layer == other_tgt:
swap_type = "source"
our_new_layer = tgt_layer
their_new_layer = src_layer
# Our source stubs
p_stub = get_stub_info(pcb_data, pair.p_net_id,
sources[0][5], sources[0][6], src_layer)
n_stub = get_stub_info(pcb_data, pair.n_net_id,
sources[0][7], sources[0][8], src_layer)
# Their source stubs
other_p_stub = get_stub_info(pcb_data, other_pair.p_net_id,
other_sources[0][5], other_sources[0][6], other_src)
other_n_stub = get_stub_info(pcb_data, other_pair.n_net_id,
other_sources[0][7], other_sources[0][8], other_src)
our_stubs = (p_stub, n_stub)
their_stubs = (other_p_stub, other_n_stub)
# Option 2: Swap targets
# Their target is on our source layer, our target is on their source layer
elif other_tgt == src_layer and tgt_layer == other_src:
swap_type = "target"
our_new_layer = src_layer
their_new_layer = tgt_layer
# Our target stubs
p_stub = get_stub_info(pcb_data, pair.p_net_id,
targets[0][5], targets[0][6], tgt_layer)
n_stub = get_stub_info(pcb_data, pair.n_net_id,
targets[0][7], targets[0][8], tgt_layer)
# Their target stubs
other_p_stub = get_stub_info(pcb_data, other_pair.p_net_id,
other_targets[0][5], other_targets[0][6], other_tgt)
other_n_stub = get_stub_info(pcb_data, other_pair.n_net_id,
other_targets[0][7], other_targets[0][8], other_tgt)
our_stubs = (p_stub, n_stub)
their_stubs = (other_p_stub, other_n_stub)
# Try swap if we have valid stubs, otherwise try the other side
if swap_type and our_stubs[0] and our_stubs[1] and their_stubs[0] and their_stubs[1]:
# Check if swap would move stubs to F.Cu (top layer)
# Exception: allow edge stubs to swap to F.Cu
allow_swap = True
if not can_swap_to_top_layer and (our_new_layer == 'F.Cu' or their_new_layer == 'F.Cu'):
if our_new_layer == 'F.Cu':
if not (is_edge_stub(our_stubs[0].pad_x, our_stubs[0].pad_y, config.bga_exclusion_zones) or
is_edge_stub(our_stubs[1].pad_x, our_stubs[1].pad_y, config.bga_exclusion_zones)):
allow_swap = False
if their_new_layer == 'F.Cu' and allow_swap:
if not (is_edge_stub(their_stubs[0].pad_x, their_stubs[0].pad_y, config.bga_exclusion_zones) or
is_edge_stub(their_stubs[1].pad_x, their_stubs[1].pad_y, config.bga_exclusion_zones)):
allow_swap = False
if not allow_swap:
pass # Skip this swap - would move non-edge stubs to top layer
else:
# Validate swap before applying
our_valid, our_reason = validate_swap(
our_stubs[0], our_stubs[1], our_new_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=other_name,
swap_partner_net_ids={other_pair.p_net_id, other_pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
their_valid, their_reason = validate_swap(
their_stubs[0], their_stubs[1], their_new_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=pair_name,
swap_partner_net_ids={pair.p_net_id, pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if not our_valid or not their_valid:
reason = our_reason if not our_valid else their_reason
print(f" Two-pair {swap_type} swap validation failed for {pair_name}: {reason}")
# Don't break - continue to try fallback target swap
else:
# Apply swaps
_, mods1 = apply_stub_layer_switch(pcb_data, our_stubs[0], our_new_layer, config, debug=False)
_, mods2 = apply_stub_layer_switch(pcb_data, our_stubs[1], our_new_layer, config, debug=False)
_, mods3 = apply_stub_layer_switch(pcb_data, their_stubs[0], their_new_layer, config, debug=False)
_, mods4 = apply_stub_layer_switch(pcb_data, their_stubs[1], their_new_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2 + mods3 + mods4)
# Update stub_endpoints_by_layer for both pairs
# Our pair moves to our_new_layer
our_orig_layer = src_layer if swap_type == "source" else tgt_layer
if our_orig_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[our_orig_layer] = [
e for e in stub_endpoints_by_layer[our_orig_layer] if e[0] != pair_name
]
if our_new_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[our_new_layer] = []
stub_endpoints_by_layer[our_new_layer].append(
(pair_name, [(our_stubs[0].x, our_stubs[0].y), (our_stubs[1].x, our_stubs[1].y)])
)
# Their pair moves to their_new_layer
their_orig_layer = other_src if swap_type == "source" else other_tgt
if their_orig_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[their_orig_layer] = [
e for e in stub_endpoints_by_layer[their_orig_layer] if e[0] != other_name
]
if their_new_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[their_new_layer] = []
stub_endpoints_by_layer[their_new_layer].append(
(other_name, [(their_stubs[0].x, their_stubs[0].y), (their_stubs[1].x, their_stubs[1].y)])
)
applied_swaps.add(pair_name)
applied_swaps.add(other_name)
swap_count += 1
total_layer_swaps += 1
print(f" Swap {swap_type}s: {pair_name} <-> {other_name}")
break
if swap_type == "source":
# Source swap failed, try target swap with same pair
if other_tgt == src_layer and tgt_layer == other_src:
# Our target stubs
p_stub = get_stub_info(pcb_data, pair.p_net_id,
targets[0][5], targets[0][6], tgt_layer)
n_stub = get_stub_info(pcb_data, pair.n_net_id,
targets[0][7], targets[0][8], tgt_layer)
# Their target stubs
other_p_stub = get_stub_info(pcb_data, other_pair.p_net_id,
other_targets[0][5], other_targets[0][6], other_tgt)
other_n_stub = get_stub_info(pcb_data, other_pair.n_net_id,
other_targets[0][7], other_targets[0][8], other_tgt)
if p_stub and n_stub and other_p_stub and other_n_stub:
# Check if swap would move stubs to F.Cu (top layer)
# Exception: allow edge stubs to swap to F.Cu
allow_swap = True
if not can_swap_to_top_layer and (src_layer == 'F.Cu' or tgt_layer == 'F.Cu'):
if src_layer == 'F.Cu':
if not (is_edge_stub(p_stub.pad_x, p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(n_stub.pad_x, n_stub.pad_y, config.bga_exclusion_zones)):
allow_swap = False
if tgt_layer == 'F.Cu' and allow_swap:
if not (is_edge_stub(other_p_stub.pad_x, other_p_stub.pad_y, config.bga_exclusion_zones) or
is_edge_stub(other_n_stub.pad_x, other_n_stub.pad_y, config.bga_exclusion_zones)):
allow_swap = False
if not allow_swap:
pass # Skip this swap - would move non-edge stubs to top layer
else:
# Validate fallback target swap before applying
our_valid, our_reason = validate_swap(
p_stub, n_stub, src_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=other_name,
swap_partner_net_ids={other_pair.p_net_id, other_pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
their_valid, their_reason = validate_swap(
other_p_stub, other_n_stub, tgt_layer, all_stubs_by_layer,
pcb_data, config, swap_partner_name=pair_name,
swap_partner_net_ids={pair.p_net_id, pair.n_net_id},
stub_endpoints_by_layer=stub_endpoints_by_layer
)
if not our_valid or not their_valid:
reason = our_reason if not our_valid else their_reason
print(f" Fallback target swap validation failed for {pair_name}: {reason}")
else:
_, mods1 = apply_stub_layer_switch(pcb_data, p_stub, src_layer, config, debug=False)
_, mods2 = apply_stub_layer_switch(pcb_data, n_stub, src_layer, config, debug=False)
_, mods3 = apply_stub_layer_switch(pcb_data, other_p_stub, tgt_layer, config, debug=False)
_, mods4 = apply_stub_layer_switch(pcb_data, other_n_stub, tgt_layer, config, debug=False)
all_segment_modifications.extend(mods1 + mods2 + mods3 + mods4)
# Update stub_endpoints_by_layer for both pairs
# Our targets move from tgt_layer to src_layer
if tgt_layer in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = [
e for e in stub_endpoints_by_layer[tgt_layer] if e[0] != pair_name
]
if src_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[src_layer] = []
stub_endpoints_by_layer[src_layer].append(
(pair_name, [(p_stub.x, p_stub.y), (n_stub.x, n_stub.y)])
)
# Their targets move from other_tgt to tgt_layer
if other_tgt in stub_endpoints_by_layer:
stub_endpoints_by_layer[other_tgt] = [
e for e in stub_endpoints_by_layer[other_tgt] if e[0] != other_name
]
if tgt_layer not in stub_endpoints_by_layer:
stub_endpoints_by_layer[tgt_layer] = []
stub_endpoints_by_layer[tgt_layer].append(
(other_name, [(other_p_stub.x, other_p_stub.y), (other_n_stub.x, other_n_stub.y)])
)
applied_swaps.add(pair_name)
applied_swaps.add(other_name)
swap_count += 1
total_layer_swaps += 1
print(f" Swap targets: {pair_name} <-> {other_name}")
break
if swap_count > 0:
print(f"Applied {swap_count} layer swap(s)")
# Phase 7: Try single-ended swaps to make room for diff pairs that still need vias
# Build set of all diff pair net IDs for exclusion
diff_pair_net_ids = set()
for pair_name, pair in diff_pairs.items():
diff_pair_net_ids.add(pair.p_net_id)
diff_pair_net_ids.add(pair.n_net_id)
single_ended_swap_count = 0
for pair_name, (src_layer, tgt_layer, sources, targets, pair) in pairs_needing_via:
if pair_name in applied_swaps:
continue