generated from zmkfirmware/unified-zmk-config-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
241 lines (184 loc) · 6.11 KB
/
helper.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
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
#!/usr/bin/env python3
# https://github.com/infused-kim/kb_ergogen_helper/blob/main/ergogen_helper.py
import argparse
import os
import pcbnew
import shutil
from pathlib import Path
class ErgogenHelperException(Exception):
pass
def get_traces(pcb):
try:
traces = pcb.GetTracks()
except Exception as e:
err = f'ERROR: Could not get traces: {e}'
raise ErgogenHelperException(err) from e
return traces
def check_traces_equal(trace_1, trace_2):
if (trace_1.GetStart() == trace_2.GetStart() and
trace_1.GetEnd() == trace_2.GetEnd()):
return True
else:
return False
def pcb_has_trace(pcb, lookup_trace):
traces = pcb.GetTracks()
for pcb_trace in traces:
if check_traces_equal(lookup_trace, pcb_trace) is True:
return True
return False
def get_trace_descr(trace):
start_x = trace.GetStart()[0] / 1000000
start_y = trace.GetStart()[1] / 1000000
end_x = trace.GetEnd()[0] / 1000000
end_y = trace.GetEnd()[1] / 1000000
length = trace.GetLength() / 1000000
return f'({start_x}, {start_y}) -> ({end_x}, {end_y}) ({length}mm)'
def filter_existing_traces(traces, pcb):
filtered_traces = []
for trace in traces:
if pcb_has_trace(pcb, trace) is False:
filtered_traces.append(trace)
removed_count = len(traces) - len(filtered_traces)
return (removed_count, filtered_traces)
def filter_locked_traces(traces):
filtered_traces = []
for trace in traces:
if trace.IsLocked() is False:
filtered_traces.append(trace)
removed_count = len(traces) - len(filtered_traces)
return (removed_count, filtered_traces)
def copy_traces(src_pcb, dst_pcb, unlocked_only=False):
traces = get_traces(src_pcb)
traces_total = len(traces)
if unlocked_only is True:
locked_num, traces = filter_locked_traces(traces)
if locked_num > 0:
print(f'WARN: Skipped {locked_num} locked traces')
existing_num, traces = filter_existing_traces(traces, dst_pcb)
if existing_num > 0:
print(f'WARN: Skipped {existing_num} existing traces')
for trace in traces:
try:
dst_pcb.Add(trace)
except Exception as e:
err = f'Could not copy trace: {e}'
raise ErgogenHelperException(err) from e
print(f'Copied {len(traces)} / {traces_total} traces.')
def lock_traces(pcb):
traces = get_traces(pcb)
for trace in traces:
trace.SetLocked(True)
print(f'Locked {len(traces)} traces...')
def save_pcb(pcb, should_backup, backup_name):
pcb_path = pcb.GetFileName()
pcb_path = Path(pcb_path)
if should_backup is True and os.path.exists(pcb_path):
backup_path = pcb_path.with_stem(f'{pcb_path.stem}_{backup_name}')
try:
shutil.copy(str(pcb_path), str(backup_path))
except Exception as e:
err = f'Could not backup pcb to {backup_path}: {e}'
raise ErgogenHelperException(err) from e
try:
pcb.Save(pcb_path)
except Exception as e:
err = f'Could not save pcb to {pcb_path}: {e}'
raise ErgogenHelperException(err) from e
def cmd_copy_traces(args):
try:
src_pcb = pcbnew.LoadBoard(args.src_pcb_path)
dst_pcb = pcbnew.LoadBoard(args.dst_pcb_path)
copy_traces(src_pcb, dst_pcb, args.unlocked_only)
save_pcb(dst_pcb, not args.no_backup, args.backup_name)
except ErgogenHelperException as e:
print(f'ERROR: {e}')
exit(-1)
def cmd_update_pcb(args):
try:
pcb = pcbnew.LoadBoard(args.pcb_path)
save_pcb(pcb, not args.no_backup, args.backup_name)
except ErgogenHelperException as e:
print(f'ERROR: {e}')
exit(-1)
def cmd_lock_traces(args):
try:
pcb = pcbnew.LoadBoard(args.pcb_path)
lock_traces(pcb)
save_pcb(pcb, not args.no_backup, args.backup_name)
except ErgogenHelperException as e:
print(f'ERROR: {e}')
exit(-1)
def main():
parser = argparse.ArgumentParser(
description='Utility to make ergogen keyboard development easier.'
)
subparsers = parser.add_subparsers(title='command', dest='cmd')
parser.add_argument(
'-n', '--backup-name',
default="orig",
help=(
'String to append to the filename of the backup '
'(default: %(default)s)'
)
)
parser.add_argument(
'-b', '--no-backup',
default=False,
action='store_true',
help='Skip backup of PCB (default: %(default)s)'
)
# Command: copy-traces
copy_traces_parser = subparsers.add_parser(
'copy-traces',
help='Copy traces from source PCB to destination PCB'
)
copy_traces_parser.add_argument(
'src_pcb_path',
help='The source PCB file path.'
)
copy_traces_parser.add_argument(
'dst_pcb_path',
help='The destination PCB file path.'
)
copy_traces_parser.add_argument(
'-u', '--unlocked-only',
default=False,
action='store_true',
help=(
'Skip locked traces and only copy unlocked ones '
'(default: %(default)s)'
)
)
copy_traces_parser.set_defaults(func=cmd_copy_traces)
# Subcommand: lock-traces
update_pcb = subparsers.add_parser(
'lock-traces',
help=(
'Sets all traces inside a KiCad PCB file to locked.'
)
)
update_pcb.add_argument(
'pcb_path',
help='The KiCad PCB file path.'
)
update_pcb.set_defaults(func=cmd_lock_traces)
# Subcommand: update-pcb
update_pcb = subparsers.add_parser(
'update-pcb',
help=(
'Update KiCad pcb from v5 to v7 by opening it and saving it again.'
)
)
update_pcb.add_argument(
'pcb_path',
help='The KiCad PCB file path.'
)
update_pcb.set_defaults(func=cmd_update_pcb)
args = parser.parse_args()
if args.cmd is None:
parser.print_help()
return
# Call the appropriate subcommand function based on the selected subcommand
args.func(args)
if __name__ == '__main__':
main()