forked from Bouni/kicad-jlcpcb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschematicexport.py
181 lines (156 loc) · 6.68 KB
/
schematicexport.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
"""Module for exporting LCSC data to schematic."""
import logging
import os
import os.path
import re
from pcbnew import GetBuildVersion # pylint: disable=import-error
from .helpers import is_nightly
class SchematicExport:
"""A class to export Schematic files."""
# This only works with KiCad V6/V7 files, if the format changes, this will probably break
def __init__(self, parent):
self.logger = logging.getLogger(__name__)
self.parent = parent
def load_schematic(self, paths):
"""Load schematic file."""
if is_nightly(GetBuildVersion()):
self.logger.info("Kicad 7+...")
for path in paths:
self._update_schematic7(path)
else:
self.logger.info("Kicad 6...")
for path in paths:
self._update_schematic(path)
def _update_schematic(self, path):
"""Only works with KiCad V6 files."""
self.logger.info("Reading %s...", path)
# Regex to look through schematic property, if we hit the pin section without finding a LCSC property, add it
# keep track of property ids and Reference property location to use with new LCSC property
propRx = re.compile(
'\\(property\\s\\"(.*)\\"\\s\\"(.*)\\"\\s\\(id\\s(\\d+)\\)\\s\\(at\\s(-?\\d+(?:.\\d+)?\\s-?\\d+(?:.\\d+)?)\\s\\d+\\)'
)
pinRx = re.compile('\\(pin\\s\\"(.*)\\"\\s\\(')
store_parts = self.parent.store.read_all()
lastID = -1
lastLoc = ""
lastLcsc = ""
newLcsc = ""
lastRef = ""
lines = []
newlines = []
with open(path, encoding="utf-8") as f:
lines = f.readlines()
if os.path.exists(path + "_old"):
os.remove(path + "_old")
os.rename(path, path + "_old")
partSection = False
for line in lines:
inLine = line.rstrip()
outLine = inLine
if "(symbol (lib_id" in inLine: # skip library section
partSection = True
m = propRx.search(inLine)
if m and partSection:
key = m.group(1)
value = m.group(2)
lastID = int(m.group(3))
# found a LCSC property, so update it if needed
if key == "LCSC":
lastLcsc = value
if newLcsc not in (lastLcsc, ""):
self.logger.info("Updating %s on %s", newLcsc, lastRef)
outLine = outLine.replace(
'"' + lastLcsc + '"', '"' + newLcsc + '"'
)
lastLcsc = newLcsc
if key == "Reference":
lastLoc = m.group(4)
lastRef = value
for part in store_parts:
if value == part[0]:
newLcsc = part[3]
break
# if we hit the pin section without finding a LCSC property, add it
m = pinRx.search(inLine)
if m:
if lastLcsc == "" and newLcsc != "" and lastLoc != "" and lastID != -1:
self.logger.info("added %s to %s", newLcsc, lastRef)
newTxt = f' (property "LCSC" "{newLcsc}" (id {lastID + 1}) (at {lastLoc} 0)'
newlines.append(newTxt)
newlines.append(" (effects (font (size 1.27 1.27)) hide)")
newlines.append(" )")
lastID = -1
lastLoc = ""
lastLcsc = ""
newLcsc = ""
lastRef = ""
newlines.append(outLine)
with open(path, "w", encoding="utf-8") as f:
for line in newlines:
f.write(line + "\n")
self.logger.info("Added LCSC's to %s(maybe?)", path)
def _update_schematic7(self, path):
"""Only works with KiCad V7 files."""
self.logger.info("Reading %s...", path)
# Regex to look through schematic property, if we hit the pin section without finding a LCSC property, add it
# keep track of property ids and Reference property location to use with new LCSC property
propRx = re.compile(
'\\(property\\s\\"(.*)\\"\\s\\"(.*)\\"\\s\\(at\\s(-?\\d+(?:.\\d+)?\\s-?\\d+(?:.\\d+)?)\\s\\d+\\)'
)
pinRx = re.compile('\\(pin\\s\\"(.*)\\"\\s\\(')
store_parts = self.parent.store.read_all()
lastLoc = ""
lastLcsc = ""
newLcsc = ""
lastRef = ""
lines = []
newlines = []
with open(path, encoding="utf-8") as f:
lines = f.readlines()
if os.path.exists(path + "_old"):
os.remove(path + "_old")
os.rename(path, path + "_old")
partSection = False
for line in lines:
inLine = line.rstrip()
outLine = inLine
if "(symbol (lib_id" in inLine: # skip library section
partSection = True
m = propRx.search(inLine)
if m and partSection:
key = m.group(1)
value = m.group(2)
# found a LCSC property, so update it if needed
if key == "LCSC":
lastLcsc = value
if newLcsc not in (lastLcsc, ""):
self.logger.info("Updating %s on %s", newLcsc, lastRef)
outLine = outLine.replace(
'"' + lastLcsc + '"', '"' + newLcsc + '"'
)
lastLcsc = newLcsc
if key == "Reference":
lastLoc = m.group(3)
lastRef = value
for part in store_parts:
if value == part[0]:
newLcsc = part[3]
break
# if we hit the pin section without finding a LCSC property, add it
m = pinRx.search(inLine)
if m:
if lastLcsc == "" and newLcsc != "" and lastLoc != "":
self.logger.info("added %s to %s", newLcsc, lastRef)
newTxt = f' (property "LCSC" "{newLcsc}" (at {lastLoc} 0)'
newlines.append(newTxt)
newlines.append(" (effects (font (size 1.27 1.27)) hide)")
newlines.append(" )")
lastLoc = ""
lastLcsc = ""
newLcsc = ""
lastRef = ""
newlines.append(outLine)
with open(path, "w", encoding="utf-8") as f:
for line in newlines:
f.write(line + "\n")
self.logger.info("Added LCSC's to %s (maybe?)", path)