-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcheck_drc.py
More file actions
1206 lines (1023 loc) · 51.8 KB
/
check_drc.py
File metadata and controls
1206 lines (1023 loc) · 51.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
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
"""
DRC Checker - Find overlapping tracks and vias between different nets.
"""
import sys
import argparse
import math
import fnmatch
from collections import defaultdict
from typing import List, Tuple, Set, Optional, Dict, Any
from kicad_parser import parse_kicad_pcb, Segment, Via, Pad
from geometry_utils import (
point_to_segment_distance,
closest_point_on_segment,
segment_to_segment_closest_points,
)
from net_queries import expand_pad_layers
class SpatialIndex:
"""Grid-based spatial index for fast proximity queries."""
def __init__(self, cell_size: float = 2.0):
"""Initialize with given cell size in mm."""
self.cell_size = cell_size
self.inv_cell_size = 1.0 / cell_size
# Dict[layer][cell_key] -> list of (object, net_id)
self.cells_by_layer: Dict[str, Dict[Tuple[int, int], List[Tuple[Any, int]]]] = defaultdict(lambda: defaultdict(list))
# For objects that span all layers (vias)
self.all_layer_cells: Dict[Tuple[int, int], List[Tuple[Any, int]]] = defaultdict(list)
def _get_cell(self, x: float, y: float) -> Tuple[int, int]:
"""Get cell coordinates for a point."""
return (int(x * self.inv_cell_size), int(y * self.inv_cell_size))
def _get_segment_cells(self, seg: Segment) -> Set[Tuple[int, int]]:
"""Get all cells a segment passes through."""
cells = set()
x1, y1 = seg.start_x, seg.start_y
x2, y2 = seg.end_x, seg.end_y
# Add endpoint cells
cells.add(self._get_cell(x1, y1))
cells.add(self._get_cell(x2, y2))
# Walk along segment and add intermediate cells
dx = x2 - x1
dy = y2 - y1
length = math.sqrt(dx*dx + dy*dy)
if length > 0:
# Sample every half cell size
steps = max(1, int(length * self.inv_cell_size * 2))
for i in range(1, steps):
t = i / steps
x = x1 + t * dx
y = y1 + t * dy
cells.add(self._get_cell(x, y))
return cells
def add_segment(self, seg: Segment, net_id: int):
"""Add a segment to the index."""
cells = self._get_segment_cells(seg)
layer_cells = self.cells_by_layer[seg.layer]
for cell in cells:
layer_cells[cell].append((seg, net_id))
def add_via(self, via: Via, net_id: int):
"""Add a via to the index (spans all layers)."""
cell = self._get_cell(via.x, via.y)
self.all_layer_cells[cell].append((via, net_id))
def add_pad(self, pad: Pad, net_id: int, expanded_layers: List[str]):
"""Add a pad to the index."""
# Pad covers a rectangular area
half_x = pad.size_x / 2
half_y = pad.size_y / 2
min_cell = self._get_cell(pad.global_x - half_x, pad.global_y - half_y)
max_cell = self._get_cell(pad.global_x + half_x, pad.global_y + half_y)
for layer in expanded_layers:
if not layer.endswith('.Cu'):
continue
layer_cells = self.cells_by_layer[layer]
for cx in range(min_cell[0], max_cell[0] + 1):
for cy in range(min_cell[1], max_cell[1] + 1):
layer_cells[(cx, cy)].append((pad, net_id))
def get_nearby_segments(self, seg: Segment) -> List[Tuple[Segment, int]]:
"""Get segments that might be near the given segment (same layer, nearby cells)."""
cells = self._get_segment_cells(seg)
layer_cells = self.cells_by_layer[seg.layer]
seen = set()
result = []
for cell in cells:
for obj, net_id in layer_cells.get(cell, []):
if isinstance(obj, Segment) and id(obj) not in seen:
seen.add(id(obj))
result.append((obj, net_id))
return result
def get_nearby_for_via(self, via: Via, layer: str) -> List[Tuple[Any, int]]:
"""Get objects near a via on a specific layer."""
cell = self._get_cell(via.x, via.y)
# Check neighboring cells too (via has size)
result = []
seen = set()
layer_cells = self.cells_by_layer[layer]
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
neighbor = (cell[0] + dx, cell[1] + dy)
for obj, net_id in layer_cells.get(neighbor, []):
if id(obj) not in seen:
seen.add(id(obj))
result.append((obj, net_id))
return result
def get_nearby_vias(self, via: Via) -> List[Tuple[Via, int]]:
"""Get vias near the given via."""
cell = self._get_cell(via.x, via.y)
result = []
seen = set()
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
neighbor = (cell[0] + dx, cell[1] + dy)
for obj, net_id in self.all_layer_cells.get(neighbor, []):
if isinstance(obj, Via) and id(obj) not in seen:
seen.add(id(obj))
result.append((obj, net_id))
return result
def get_nearby_pads(self, x: float, y: float, layer: str) -> List[Tuple[Pad, int]]:
"""Get pads near a point on a specific layer."""
cell = self._get_cell(x, y)
result = []
seen = set()
layer_cells = self.cells_by_layer[layer]
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
neighbor = (cell[0] + dx, cell[1] + dy)
for obj, net_id in layer_cells.get(neighbor, []):
if isinstance(obj, Pad) and id(obj) not in seen:
seen.add(id(obj))
result.append((obj, net_id))
return result
def matches_any_pattern(name: str, patterns: List[str]) -> bool:
"""Check if a net name matches any of the given patterns (fnmatch style)."""
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
return True
return False
def segment_to_segment_distance(seg1: Segment, seg2: Segment) -> float:
"""Calculate minimum distance between two segments."""
dist, _, _ = segment_to_segment_closest_points(seg1, seg2)
return dist
def segments_cross(seg1: Segment, seg2: Segment, tolerance: float = 0.001) -> Tuple[bool, Optional[Tuple[float, float]]]:
"""Check if two segments on the same layer cross each other.
Returns (True, intersection_point) if they cross, (False, None) otherwise.
Segments that share an endpoint are not considered crossing.
"""
if seg1.layer != seg2.layer:
return False, None
x1, y1 = seg1.start_x, seg1.start_y
x2, y2 = seg1.end_x, seg1.end_y
x3, y3 = seg2.start_x, seg2.start_y
x4, y4 = seg2.end_x, seg2.end_y
# Check if segments share an endpoint (not a crossing)
def points_equal(ax, ay, bx, by):
return abs(ax - bx) < tolerance and abs(ay - by) < tolerance
if (points_equal(x1, y1, x3, y3) or points_equal(x1, y1, x4, y4) or
points_equal(x2, y2, x3, y3) or points_equal(x2, y2, x4, y4)):
return False, None
# Direction vectors
dx1, dy1 = x2 - x1, y2 - y1
dx2, dy2 = x4 - x3, y4 - y3
# Cross product of direction vectors
cross = dx1 * dy2 - dy1 * dx2
if abs(cross) < 1e-10:
# Parallel segments - no crossing
return False, None
# Solve for parameters t and u where:
# (x1, y1) + t * (dx1, dy1) = (x3, y3) + u * (dx2, dy2)
dx3, dy3 = x3 - x1, y3 - y1
t = (dx3 * dy2 - dy3 * dx2) / cross
u = (dx3 * dy1 - dy3 * dx1) / cross
# Check if intersection is within both segments (exclusive of endpoints)
eps = 0.001 # Small margin to exclude near-endpoint intersections
if eps < t < 1 - eps and eps < u < 1 - eps:
# Calculate intersection point
ix = x1 + t * dx1
iy = y1 + t * dy1
return True, (ix, iy)
return False, None
def check_segment_overlap(seg1: Segment, seg2: Segment, clearance: float, clearance_margin: float = 0.05):
"""Check if two segments on the same layer violate clearance.
Args:
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
Violations smaller than clearance * clearance_margin are ignored.
Returns:
(has_violation, overlap, closest_pt1, closest_pt2)
"""
if seg1.layer != seg2.layer:
return False, 0.0, None, None
# Required distance is half-widths plus clearance
required_dist = seg1.width / 2 + seg2.width / 2 + clearance
actual_dist, pt1, pt2 = segment_to_segment_closest_points(seg1, seg2)
overlap = required_dist - actual_dist
# Use clearance-based tolerance (10% of clearance by default)
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, pt1, pt2
return False, 0.0, None, None
def check_via_segment_overlap(via: Via, seg: Segment, clearance: float, clearance_margin: float = 0.05) -> Tuple[bool, float]:
"""Check if a via overlaps with a segment on any common layer.
Args:
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
"""
# Standard through-hole vias go through ALL copper layers, not just the ones listed
# Only skip non-copper layers
if not seg.layer.endswith('.Cu'):
return False, 0.0
required_dist = via.size / 2 + seg.width / 2 + clearance
actual_dist = point_to_segment_distance(via.x, via.y,
seg.start_x, seg.start_y,
seg.end_x, seg.end_y)
overlap = required_dist - actual_dist
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap
return False, 0.0
def check_via_via_overlap(via1: Via, via2: Via, clearance: float, clearance_margin: float = 0.05) -> Tuple[bool, float]:
"""Check if two vias overlap.
Args:
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
"""
# All vias are through-hole, so they always potentially conflict
required_dist = via1.size / 2 + via2.size / 2 + clearance
actual_dist = math.sqrt((via1.x - via2.x)**2 + (via1.y - via2.y)**2)
overlap = required_dist - actual_dist
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap
return False, 0.0
def point_to_rect_distance(px: float, py: float, cx: float, cy: float,
half_x: float, half_y: float,
corner_radius: float = 0.0) -> float:
"""Calculate distance from a point to an axis-aligned rectangle with optional rounded corners.
Args:
px, py: Point coordinates
cx, cy: Rectangle center coordinates
half_x, half_y: Rectangle half-widths
corner_radius: Radius of rounded corners (0 for sharp corners)
Returns:
Distance from point to rectangle edge (0 if point is inside)
"""
# Position relative to rectangle center
rel_x = abs(px - cx)
rel_y = abs(py - cy)
if corner_radius > 0:
# Inner rectangle bounds (where corners start)
inner_half_x = half_x - corner_radius
inner_half_y = half_y - corner_radius
# Check if point is in a corner region
if rel_x > inner_half_x and rel_y > inner_half_y:
# Distance to corner arc center
dx = rel_x - inner_half_x
dy = rel_y - inner_half_y
dist_to_corner_center = math.sqrt(dx * dx + dy * dy)
# Distance to arc edge (negative if inside)
return max(0, dist_to_corner_center - corner_radius)
# Point is along a flat edge - rectangular distance
dx = max(0, rel_x - half_x)
dy = max(0, rel_y - half_y)
return math.sqrt(dx * dx + dy * dy)
def segment_to_rect_distance(x1: float, y1: float, x2: float, y2: float,
cx: float, cy: float, half_x: float, half_y: float,
corner_radius: float = 0.0) -> Tuple[float, Tuple[float, float]]:
"""Calculate minimum distance from a segment to an axis-aligned rectangle.
Args:
x1, y1, x2, y2: Segment endpoints
cx, cy: Rectangle center coordinates
half_x, half_y: Rectangle half-widths
corner_radius: Radius of rounded corners (0 for sharp corners)
Returns:
(distance, closest_point_on_segment)
"""
# Sample points along the segment and find minimum distance to rectangle
# This is a simplified approach - for production code would use proper geometry
min_dist = float('inf')
closest_pt = (x1, y1)
# Check endpoints and intermediate points
num_samples = max(10, int(math.sqrt((x2-x1)**2 + (y2-y1)**2) / 0.05)) # Sample every ~0.05mm
for i in range(num_samples + 1):
t = i / num_samples
px = x1 + t * (x2 - x1)
py = y1 + t * (y2 - y1)
dist = point_to_rect_distance(px, py, cx, cy, half_x, half_y, corner_radius)
if dist < min_dist:
min_dist = dist
closest_pt = (px, py)
return min_dist, closest_pt
def check_pad_segment_overlap(pad: Pad, seg: Segment, clearance: float,
routing_layers: List[str],
clearance_margin: float = 0.05) -> Tuple[bool, float, Optional[Tuple[float, float]]]:
"""Check if a segment is too close to a pad on the same layer.
Args:
pad: Pad object with global_x, global_y, size_x, size_y, layers
seg: Segment to check against
clearance: Minimum clearance in mm
routing_layers: List of routing layer names (for expanding *.Cu wildcards)
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
Returns:
(has_violation, overlap_mm, closest_point_on_segment)
"""
# Expand pad layers (handles *.Cu wildcards)
expanded_layers = expand_pad_layers(pad.layers, routing_layers)
# Check if segment is on a layer the pad is on
if seg.layer not in expanded_layers:
return False, 0.0, None
# Corner radius based on pad shape (circle/oval use min dimension, roundrect uses rratio)
if pad.shape in ('circle', 'oval'):
corner_radius = min(pad.size_x, pad.size_y) / 2
elif pad.shape == 'roundrect':
corner_radius = pad.roundrect_rratio * min(pad.size_x, pad.size_y)
else:
corner_radius = 0.0
# Calculate distance from segment to rectangular pad (with optional rounded corners)
dist_to_pad, closest_pt = segment_to_rect_distance(
seg.start_x, seg.start_y, seg.end_x, seg.end_y,
pad.global_x, pad.global_y, pad.size_x / 2, pad.size_y / 2,
corner_radius
)
# Required clearance: segment half-width + clearance
# (dist_to_pad is already edge-to-edge from pad)
required_dist = seg.width / 2 + clearance
overlap = required_dist - dist_to_pad
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, closest_pt
return False, 0.0, None
def check_pad_via_overlap(pad: Pad, via: Via, clearance: float,
routing_layers: List[str],
clearance_margin: float = 0.05) -> Tuple[bool, float]:
"""Check if a via is too close to a pad.
Args:
pad: Pad object
via: Via to check against
clearance: Minimum clearance in mm
routing_layers: List of routing layer names (for expanding *.Cu wildcards)
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
Returns:
(has_violation, overlap_mm)
"""
# Expand pad layers (handles *.Cu wildcards)
expanded_layers = expand_pad_layers(pad.layers, routing_layers)
# Vias are through-hole, so they conflict with pads on any copper layer
if not any(layer.endswith('.Cu') for layer in expanded_layers):
return False, 0.0
# Corner radius based on pad shape (circle/oval use min dimension, roundrect uses rratio)
if pad.shape in ('circle', 'oval'):
corner_radius = min(pad.size_x, pad.size_y) / 2
elif pad.shape == 'roundrect':
corner_radius = pad.roundrect_rratio * min(pad.size_x, pad.size_y)
else:
corner_radius = 0.0
# Distance from via center to pad edge (accounts for rounded corners)
dist_to_pad = point_to_rect_distance(
via.x, via.y,
pad.global_x, pad.global_y,
pad.size_x / 2, pad.size_y / 2,
corner_radius
)
# Required clearance: via half-size + clearance
# (dist_to_pad is already edge-to-edge from pad)
required_dist = via.size / 2 + clearance
overlap = required_dist - dist_to_pad
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap
return False, 0.0
def check_via_drill_overlap(via1: Via, via2: Via, hole_to_hole_clearance: float,
clearance_margin: float = 0.05) -> Tuple[bool, float]:
"""Check if two via drill holes violate hole-to-hole clearance.
Args:
via1, via2: Via objects with drill attribute
hole_to_hole_clearance: Minimum clearance between drill hole edges in mm
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
Returns:
(has_violation, overlap_mm)
"""
# Required distance between drill hole centers
required_dist = via1.drill / 2 + via2.drill / 2 + hole_to_hole_clearance
actual_dist = math.sqrt((via1.x - via2.x)**2 + (via1.y - via2.y)**2)
overlap = required_dist - actual_dist
tolerance = hole_to_hole_clearance * clearance_margin
if overlap > tolerance:
return True, overlap
return False, 0.0
def check_pad_drill_via_overlap(pad: Pad, via: Via, hole_to_hole_clearance: float,
clearance_margin: float = 0.05) -> Tuple[bool, float]:
"""Check if a via drill hole is too close to a pad's drill hole.
Args:
pad: Pad object with drill attribute (through-hole pad)
via: Via to check against
hole_to_hole_clearance: Minimum clearance between drill hole edges in mm
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
Returns:
(has_violation, overlap_mm)
"""
if pad.drill <= 0:
return False, 0.0 # SMD pad, no drill
# Required distance between drill hole centers
required_dist = pad.drill / 2 + via.drill / 2 + hole_to_hole_clearance
actual_dist = math.sqrt((pad.global_x - via.x)**2 + (pad.global_y - via.y)**2)
overlap = required_dist - actual_dist
tolerance = hole_to_hole_clearance * clearance_margin
if overlap > tolerance:
return True, overlap
return False, 0.0
def check_segment_board_edge(seg: Segment, board_bounds: Tuple[float, float, float, float],
clearance: float, clearance_margin: float = 0.05) -> Tuple[bool, float, str]:
"""Check if a segment is too close to the board edge.
Args:
seg: Segment to check
board_bounds: (min_x, min_y, max_x, max_y) of the board
clearance: Minimum clearance from board edge in mm
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
Returns:
(has_violation, overlap_mm, edge_name)
"""
min_x, min_y, max_x, max_y = board_bounds
half_width = seg.width / 2
required_clearance = clearance + half_width
# Check all segment points against all edges
for x, y in [(seg.start_x, seg.start_y), (seg.end_x, seg.end_y)]:
# Left edge
dist_left = x - min_x
if dist_left < required_clearance:
overlap = required_clearance - dist_left
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "left"
# Right edge
dist_right = max_x - x
if dist_right < required_clearance:
overlap = required_clearance - dist_right
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "right"
# Bottom edge
dist_bottom = y - min_y
if dist_bottom < required_clearance:
overlap = required_clearance - dist_bottom
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "bottom"
# Top edge
dist_top = max_y - y
if dist_top < required_clearance:
overlap = required_clearance - dist_top
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "top"
return False, 0.0, ""
def check_via_board_edge(via: Via, board_bounds: Tuple[float, float, float, float],
clearance: float, clearance_margin: float = 0.05) -> Tuple[bool, float, str]:
"""Check if a via is too close to the board edge.
Args:
via: Via to check
board_bounds: (min_x, min_y, max_x, max_y) of the board
clearance: Minimum clearance from board edge in mm
clearance_margin: Fraction of clearance to use as tolerance (default 0.10 = 10%).
Returns:
(has_violation, overlap_mm, edge_name)
"""
min_x, min_y, max_x, max_y = board_bounds
half_size = via.size / 2
required_clearance = clearance + half_size
x, y = via.x, via.y
# Left edge
dist_left = x - min_x
if dist_left < required_clearance:
overlap = required_clearance - dist_left
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "left"
# Right edge
dist_right = max_x - x
if dist_right < required_clearance:
overlap = required_clearance - dist_right
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "right"
# Bottom edge
dist_bottom = y - min_y
if dist_bottom < required_clearance:
overlap = required_clearance - dist_bottom
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "bottom"
# Top edge
dist_top = max_y - y
if dist_top < required_clearance:
overlap = required_clearance - dist_top
tolerance = clearance * clearance_margin
if overlap > tolerance:
return True, overlap, "top"
return False, 0.0, ""
def write_debug_lines(pcb_file: str, violations: List[dict], clearance: float, layer: str = "User.7"):
"""Write debug lines to PCB file showing violation locations.
Adds gr_line elements connecting closest points of violating segments.
"""
import uuid
# Read the PCB file
with open(pcb_file, 'r', encoding='utf-8') as f:
content = f.read()
# Generate gr_line elements for segment-segment violations
debug_lines = []
print(f"\nDebug lines (center-to-center distance, required clearance = {clearance}mm):")
for v in violations:
if v['type'] == 'segment-segment' and 'closest_pt1' in v and v['closest_pt1']:
pt1 = v['closest_pt1']
pt2 = v['closest_pt2']
dist = math.sqrt((pt2[0] - pt1[0])**2 + (pt2[1] - pt1[1])**2)
# Track width is typically 0.1mm, so required center-to-center = 0.1 + clearance = 0.2mm
required = 0.1 + clearance # half-width + half-width + clearance = track_width + clearance
violation_amt = required - dist
print(f" {v['net1']} <-> {v['net2']}: dist={dist:.4f}mm, required={required:.3f}mm, violation={violation_amt:.4f}mm")
print(f" from ({pt1[0]:.4f}, {pt1[1]:.4f}) to ({pt2[0]:.4f}, {pt2[1]:.4f})")
line = f'''\t(gr_line
\t\t(start {pt1[0]:.6f} {pt1[1]:.6f})
\t\t(end {pt2[0]:.6f} {pt2[1]:.6f})
\t\t(stroke
\t\t\t(width 0.05)
\t\t\t(type solid)
\t\t)
\t\t(layer "{layer}")
\t\t(uuid "{uuid.uuid4()}")
\t)'''
debug_lines.append(line)
if not debug_lines:
print(f"No debug lines to write")
return
# Insert before the final closing paren
debug_text = '\n'.join(debug_lines)
last_paren = content.rfind(')')
new_content = content[:last_paren] + '\n' + debug_text + '\n' + content[last_paren:]
with open(pcb_file, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"\nWrote {len(debug_lines)} debug line(s) to layer {layer}")
def run_drc(pcb_file: str, clearance: float = 0.1, net_patterns: Optional[List[str]] = None,
debug_output: bool = False, quiet: bool = False,
hole_to_hole_clearance: float = 0.2, board_edge_clearance: float = 0.0,
clearance_margin: float = 0.05):
"""Run DRC checks on the PCB file.
Args:
pcb_file: Path to the KiCad PCB file
clearance: Minimum clearance in mm
net_patterns: Optional list of net name patterns (fnmatch style) to focus on.
If provided, only checks involving at least one matching net are reported.
debug_output: If True, write debug lines to User.7 layer showing violation locations
quiet: If True, only print a summary line unless there are violations
hole_to_hole_clearance: Minimum clearance between drill hole edges in mm (default: 0.2)
board_edge_clearance: Minimum clearance from board edge in mm (0 = use clearance)
"""
# Use track clearance for board edge if not specified
effective_board_edge_clearance = board_edge_clearance if board_edge_clearance > 0 else clearance
if quiet and net_patterns:
# Print a brief summary line in quiet mode
print(f"Checking {', '.join(net_patterns)} for DRC...", end=" ", flush=True)
elif not quiet:
print(f"Loading {pcb_file}...")
pcb_data = parse_kicad_pcb(pcb_file)
if not quiet:
print(f"Found {len(pcb_data.segments)} segments and {len(pcb_data.vias)} vias")
# Helper to check if a net_id matches the filter patterns
def net_matches_filter(net_id: int) -> bool:
if net_patterns is None:
return True # No filter, include all
net_info = pcb_data.nets.get(net_id, None)
if net_info is None:
return False
return matches_any_pattern(net_info.name, net_patterns)
# Helper to check if a violation involves at least one matching net
def violation_matches_filter(net1_str: str, net2_str: str) -> bool:
if net_patterns is None:
return True
return matches_any_pattern(net1_str, net_patterns) or matches_any_pattern(net2_str, net_patterns)
if net_patterns and not quiet:
print(f"Filtering to nets matching: {net_patterns}")
# Get routing layers for pad layer expansion
routing_layers = list(set(seg.layer for seg in pcb_data.segments if seg.layer.endswith('.Cu')))
if not routing_layers:
routing_layers = ['F.Cu', 'B.Cu'] # Fallback
# Build spatial index for fast proximity queries
if not quiet:
print("Building spatial index...")
spatial_idx = SpatialIndex(cell_size=2.0) # 2mm cells
# Add all segments to spatial index
for seg in pcb_data.segments:
spatial_idx.add_segment(seg, seg.net_id)
# Add all vias to spatial index
for via in pcb_data.vias:
spatial_idx.add_via(via, via.net_id)
# Add all pads to spatial index
pads_by_net = pcb_data.pads_by_net
for net_id, pads in pads_by_net.items():
for pad in pads:
expanded_layers = expand_pad_layers(pad.layers, routing_layers)
spatial_idx.add_pad(pad, net_id, expanded_layers)
# Group vias by net (still needed for some checks)
vias_by_net = {}
for via in pcb_data.vias:
if via.net_id not in vias_by_net:
vias_by_net[via.net_id] = []
vias_by_net[via.net_id].append(via)
violations = []
# Pre-compute matching nets for filtering
if net_patterns:
matching_net_ids = set(net_id for net_id in pcb_data.nets.keys() if net_matches_filter(net_id))
if not quiet:
print(f"Filtering to {len(matching_net_ids)} matching nets")
else:
matching_net_ids = None
# Check segment-to-segment violations using spatial index
if not quiet:
print("\nChecking segment-to-segment clearances...")
checked_pairs = set() # Track checked segment pairs to avoid duplicates
for seg1 in pcb_data.segments:
net1 = seg1.net_id
net1_matches = matching_net_ids is None or net1 in matching_net_ids
# Get nearby segments from spatial index (same layer only)
for seg2, net2 in spatial_idx.get_nearby_segments(seg1):
if net1 == net2:
continue # Same net
if seg1 is seg2:
continue # Same segment
# Skip if neither net matches filter
net2_matches = matching_net_ids is None or net2 in matching_net_ids
if not net1_matches and not net2_matches:
continue
# Avoid checking same pair twice
pair_key = (min(id(seg1), id(seg2)), max(id(seg1), id(seg2)))
if pair_key in checked_pairs:
continue
checked_pairs.add(pair_key)
has_violation, overlap, pt1, pt2 = check_segment_overlap(seg1, seg2, clearance, clearance_margin)
if has_violation:
net1_name = pcb_data.nets.get(net1, None)
net2_name = pcb_data.nets.get(net2, None)
net1_str = net1_name.name if net1_name else f"net_{net1}"
net2_str = net2_name.name if net2_name else f"net_{net2}"
violations.append({
'type': 'segment-segment',
'net1': net1_str,
'net2': net2_str,
'layer': seg1.layer,
'overlap_mm': overlap,
'loc1': (seg1.start_x, seg1.start_y, seg1.end_x, seg1.end_y),
'loc2': (seg2.start_x, seg2.start_y, seg2.end_x, seg2.end_y),
'closest_pt1': pt1,
'closest_pt2': pt2,
})
# Also check for segment crossings (different nets)
crosses, cross_point = segments_cross(seg1, seg2)
if crosses:
net1_name = pcb_data.nets.get(net1, None)
net2_name = pcb_data.nets.get(net2, None)
net1_str = net1_name.name if net1_name else f"net_{net1}"
net2_str = net2_name.name if net2_name else f"net_{net2}"
violations.append({
'type': 'segment-crossing',
'net1': net1_str,
'net2': net2_str,
'layer': seg1.layer,
'cross_point': cross_point,
'loc1': (seg1.start_x, seg1.start_y, seg1.end_x, seg1.end_y),
'loc2': (seg2.start_x, seg2.start_y, seg2.end_x, seg2.end_y),
})
# Check for same-net segment crossings using spatial index
if not quiet:
print("Checking for same-net segment crossings...")
same_net_checked = set()
for seg1 in pcb_data.segments:
net_id = seg1.net_id
if matching_net_ids is not None and net_id not in matching_net_ids:
continue
for seg2, net2 in spatial_idx.get_nearby_segments(seg1):
if net2 != net_id:
continue # Different net
if seg1 is seg2:
continue
pair_key = (min(id(seg1), id(seg2)), max(id(seg1), id(seg2)))
if pair_key in same_net_checked:
continue
same_net_checked.add(pair_key)
crosses, cross_point = segments_cross(seg1, seg2)
if crosses:
net_name = pcb_data.nets.get(net_id, None)
net_str = net_name.name if net_name else f"net_{net_id}"
violations.append({
'type': 'segment-crossing-same-net',
'net1': net_str,
'net2': net_str,
'layer': seg1.layer,
'cross_point': cross_point,
'loc1': (seg1.start_x, seg1.start_y, seg1.end_x, seg1.end_y),
'loc2': (seg2.start_x, seg2.start_y, seg2.end_x, seg2.end_y),
})
# Check via-to-segment violations using spatial index
if not quiet:
print("Checking via-to-segment clearances...")
for via in pcb_data.vias:
via_net = via.net_id
via_net_matches = matching_net_ids is None or via_net in matching_net_ids
# Check against segments on each copper layer (vias go through all layers)
for layer in routing_layers:
for obj, seg_net in spatial_idx.get_nearby_for_via(via, layer):
if not isinstance(obj, Segment):
continue
seg = obj
if via_net == seg_net:
continue # Same net
seg_net_matches = matching_net_ids is None or seg_net in matching_net_ids
if not via_net_matches and not seg_net_matches:
continue
has_violation, overlap = check_via_segment_overlap(via, seg, clearance, clearance_margin)
if has_violation:
via_net_name = pcb_data.nets.get(via_net, None)
seg_net_name = pcb_data.nets.get(seg_net, None)
via_net_str = via_net_name.name if via_net_name else f"net_{via_net}"
seg_net_str = seg_net_name.name if seg_net_name else f"net_{seg_net}"
violations.append({
'type': 'via-segment',
'net1': via_net_str,
'net2': seg_net_str,
'layer': seg.layer,
'overlap_mm': overlap,
'via_loc': (via.x, via.y),
'seg_loc': (seg.start_x, seg.start_y, seg.end_x, seg.end_y),
})
# Check via-to-via violations using spatial index
if not quiet:
print("Checking via-to-via clearances...")
via_via_checked = set()
for via1 in pcb_data.vias:
net1 = via1.net_id
net1_matches = matching_net_ids is None or net1 in matching_net_ids
for via2, net2 in spatial_idx.get_nearby_vias(via1):
if via1 is via2:
continue
net2_matches = matching_net_ids is None or net2 in matching_net_ids
if not net1_matches and not net2_matches:
continue
pair_key = (min(id(via1), id(via2)), max(id(via1), id(via2)))
if pair_key in via_via_checked:
continue
via_via_checked.add(pair_key)
has_violation, overlap = check_via_via_overlap(via1, via2, clearance, clearance_margin)
if has_violation:
net1_name = pcb_data.nets.get(net1, None)
net2_name = pcb_data.nets.get(net2, None)
net1_str = net1_name.name if net1_name else f"net_{net1}"
net2_str = net2_name.name if net2_name else f"net_{net2}"
violations.append({
'type': 'via-via' if net1 != net2 else 'via-via-same-net',
'net1': net1_str,
'net2': net2_str,
'overlap_mm': overlap,
'loc1': (via1.x, via1.y),
'loc2': (via2.x, via2.y),
})
# Check pad-to-segment violations using spatial index
if not quiet:
print("Checking pad-to-segment clearances...")
pad_net_ids = list(pads_by_net.keys())
for seg in pcb_data.segments:
seg_net = seg.net_id
seg_net_matches = matching_net_ids is None or seg_net in matching_net_ids
# Get pads near the segment endpoints
for x, y in [(seg.start_x, seg.start_y), (seg.end_x, seg.end_y)]:
for pad, pad_net in spatial_idx.get_nearby_pads(x, y, seg.layer):
if pad_net == seg_net:
continue # Same net
pad_net_matches = matching_net_ids is None or pad_net in matching_net_ids
if not seg_net_matches and not pad_net_matches:
continue
has_violation, overlap, closest_pt = check_pad_segment_overlap(
pad, seg, clearance, routing_layers, clearance_margin
)
if has_violation:
pad_net_name = pcb_data.nets.get(pad_net, None)
seg_net_name = pcb_data.nets.get(seg_net, None)
pad_net_str = pad_net_name.name if pad_net_name else f"net_{pad_net}"
seg_net_str = seg_net_name.name if seg_net_name else f"net_{seg_net}"
violations.append({
'type': 'pad-segment',
'net1': pad_net_str,
'net2': seg_net_str,
'layer': seg.layer,
'overlap_mm': overlap,
'pad_loc': (pad.global_x, pad.global_y),
'pad_ref': f"{pad.component_ref}.{pad.pad_number}",
'seg_loc': (seg.start_x, seg.start_y, seg.end_x, seg.end_y),
'closest_pt': closest_pt,
})
# Check pad-to-via violations using spatial index
if not quiet:
print("Checking pad-to-via clearances...")
for via in pcb_data.vias:
via_net = via.net_id
via_net_matches = matching_net_ids is None or via_net in matching_net_ids
for layer in routing_layers:
for pad, pad_net in spatial_idx.get_nearby_pads(via.x, via.y, layer):
if pad_net == via_net:
continue # Same net
pad_net_matches = matching_net_ids is None or pad_net in matching_net_ids
if not via_net_matches and not pad_net_matches:
continue
has_violation, overlap = check_pad_via_overlap(
pad, via, clearance, routing_layers, clearance_margin
)
if has_violation:
pad_net_name = pcb_data.nets.get(pad_net, None)
via_net_name = pcb_data.nets.get(via_net, None)
pad_net_str = pad_net_name.name if pad_net_name else f"net_{pad_net}"
via_net_str = via_net_name.name if via_net_name else f"net_{via_net}"
violations.append({
'type': 'pad-via',
'net1': pad_net_str,
'net2': via_net_str,
'overlap_mm': overlap,
'pad_loc': (pad.global_x, pad.global_y),
'pad_ref': f"{pad.component_ref}.{pad.pad_number}",
'via_loc': (via.x, via.y),
})
# Dummy variables for compatibility with remaining code
via_net_ids = list(vias_by_net.keys())
matching_via_nets = matching_net_ids
matching_seg_net_set = matching_net_ids
matching_pad_nets = matching_net_ids
# Check hole-to-hole clearance (via drill to via drill)
if hole_to_hole_clearance > 0:
if not quiet:
print("Checking via drill hole-to-hole clearances...")
all_vias = list(pcb_data.vias)
for i in range(len(all_vias)):
via1 = all_vias[i]
via1_matches = matching_via_nets is None or via1.net_id in matching_via_nets
for j in range(i + 1, len(all_vias)):