This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kicad-fix-refs
executable file
·71 lines (59 loc) · 2.16 KB
/
kicad-fix-refs
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
#!/usr/bin/python
"""
When you copy-and-paste a hierarhical sheet, references of contents of the sheet is
erroneously duplicated too, which causes error when re-annotation is needed.
This tool replaces duplicate reference numbers with "?" string, thus allowing
annotation tool to re-annotate the copied sheets.
"""
import sys, re
filename = sys.argv[1]
re_annotate_all = False
if len(sys.argv) > 2 and "--all" in sys.argv:
print "*** Annotating all..."
re_annotate_all = True
write_to_file = False
if len(sys.argv) > 2 and "--write" in sys.argv:
print "*** Writing to file..."
write_to_file = True
if write_to_file:
ans = raw_input("Did you close the Schematic Editor? [y/n] ")
if ans != "y":
print "Schematic Editor should be closed before running this."
exit()
widlist = []
with open(filename, 'r+b') as f:
lines = []
pattern = re.compile('(.*Ref=")([A-Z]+[0-9]+)(".*)')
for line in f:
if re_annotate_all:
#F 0 "C?" H 3625 3100 50 0000 L CNN
patt = re.compile('(^F 0 ")([A-Z]+[0-9]+)(".*)')
r = patt.search(line)
if r:
id = r.group(2)
new_id = re.sub('[0-9]+', '?', id)
print "orig id {0} -> {1}".format(id, new_id)
new_line = patt.sub("\\1%s\\3" % new_id, line)
lines.append(new_line)
continue
ref = pattern.search(line)
if ref:
wid = ref.group(2)
if (wid in widlist) or re_annotate_all:
new_wid = re.sub('[0-9]+', '?', wid)
print "duplicate wid:", wid, "renamed to: ", new_wid, line
new_line = pattern.sub("\\1%s\\3" % new_wid, line)
lines.append(new_line)
else:
#print "found reference: ", wid, " (first occurrence)"
lines.append(line)
widlist.append(wid)
else:
lines.append(line)
new_content = ''.join(lines)
if write_to_file:
f.seek(0)
f.write(new_content)
f.truncate()
else:
print "**** This was a dry run, use with '--write' parameter to write to the file."