-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_vias.py
executable file
·109 lines (80 loc) · 2.65 KB
/
fix_vias.py
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
#!/usr/bin/env python
import sys
import pcbnew
def fmt(value, suffix=''):
# format KiCad value (integer, 10e-6mm)
v = float(value) * (10**-6)
return '{:.2f}{}'.format(v, suffix)
def fmtu(value):
return fmt(value, 'mm')
# convert mm value to KiCad internal size value
def mm2kicad(value):
return int(value / 10**-6)
def get_via_module(board, drill, diameter, net):
n = 'VIA-{}-{}'.format(fmt(drill), fmt(diameter))
# instantiate new module
m = pcbnew.MODULE(board)
m.SetReference('REF**')
m.SetValue(n)
m.SetLastEditTime(pcbnew.GetNewTimeStamp())
# adjust reference and value display settings
r = m.Reference()
r.SetVisible(False)
r.SetTextSize(pcbnew.wxSize(mm2kicad(0.3), mm2kicad(0.3)))
r.SetThickness(mm2kicad(0.075))
v = m.Value()
v.SetVisible(False)
v.SetTextSize(pcbnew.wxSize(mm2kicad(0.3), mm2kicad(0.3)))
v.SetThickness(mm2kicad(0.075))
#v.SetLayer() # TODO: Move value to F.Fab layer (F.SilkS is default)
lib_id = pcbnew.LIB_ID(n)
m.SetFPID(lib_id)
# create through-hole pad
pad = pcbnew.D_PAD(m)
pad.SetPadName('1')
pad.SetNet(net)
pad.SetPosition(pcbnew.wxPoint(0, 0))
pad.SetShape(pcbnew.PAD_SHAPE_CIRCLE)
pad.SetSize(pcbnew.wxSize(diameter, diameter))
pad.SetDrillShape(pcbnew.PAD_DRILL_SHAPE_CIRCLE)
pad.SetDrillSize(pcbnew.wxSize(drill, drill))
pad.SetLayerSet(pcbnew.LSET.AllCuMask())
pad.SetZoneConnection(pcbnew.PAD_ZONE_CONN_FULL)
# add pad to module
m.Add(pad)
return m
if __name__ == '__main__':
fn = sys.argv[1]
# load board
board = pcbnew.LoadBoard(fn)
if not board:
sys.exit(1)
nets = board.GetNetsByNetcode()
#print('Nets:')
#for c, net in nets.items():
# print(" * {:3d} {}".format(c, net.GetNetname()))
#print
search_net = nets[0]
replace_net = board.FindNet('GND')
print('Finding vias in net 0:')
tracks = board.TracksInNet(search_net.GetNet())
for t in tracks:
# check if track is via
is_via = pcbnew.VIA.ClassOf(t)
if not is_via:
continue
# cast track to via
via = pcbnew.Cast_to_VIA(t)
print('* ({}, {}) drill {} diameter {}'.format(
fmt(via.GetStart().x), fmt(via.GetStart().y),
fmtu(via.GetDrill()),
fmtu(via.GetWidth())
))
# create replacement module
via_m = get_via_module(board, via.GetDrill(), via.GetWidth(), replace_net)
via_m.SetPosition(via.GetStart())
via_m.SetTimeStamp(via.GetTimeStamp())
# replace via with module
board.Remove(via)
board.Add(via_m)
board.Save(fn)