forked from Bouni/kicad-jlcpcb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schematicexport.py
276 lines (241 loc) · 10.7 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
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
"""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_version6, is_version7, is_version8
class SchematicExport:
"""A class to export Schematic files."""
# This only works with KiCad v6/v7/v8 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_version8(GetBuildVersion()):
self.logger.info("Kicad 8+...")
for path in paths:
self._update_schematic8(path)
elif is_version7(GetBuildVersion()):
self.logger.info("Kicad 7...")
for path in paths:
self._update_schematic7(path)
elif is_version6(GetBuildVersion()):
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["reference"]:
newLcsc = part["lcsc"]
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["reference"]:
newLcsc = part["lcsc"]
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)
def _update_schematic8(self, path):
"""Only works with KiCad V8 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"(.*)\\"')
atRx = re.compile("\\(at\\s(-?\\d+(?:.\\d+)?\\s-?\\d+(?:.\\d+)?)\\s\\d+\\)")
pinRx = re.compile('\\(pin\\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()
partSection = False
files_seen = set() # keeps sheet files already processed.
for i in range(0, len(lines) - 1):
inLine = lines[i].rstrip()
inLine2 = lines[i + 1].rstrip()
outLine = inLine
if "(symbol" in inLine and "(lib_id" in inLine2: # skip library section
partSection = True
# self.logger.info("line %d", i)
m = propRx.search(inLine)
m2 = atRx.search(inLine2)
if m and m2 and partSection:
key = m.group(1)
# self.logger.info("key %s", key)
# found a LCSC property, so update it if needed
if key in {"LCSC", "LCSC_PN", "JLC_PN"}:
value = m.group(2)
lastLcsc = value
if newLcsc not in (lastLcsc, ""):
self.logger.info("Updating %s on %s in %s", newLcsc, lastRef, path)
outLine = outLine.replace(
'"' + lastLcsc + '"', '"' + newLcsc + '"'
)
lastLcsc = newLcsc
if key == "Reference":
lastLoc = m2.group(1)
value = m.group(2)
# self.logger.info("value %s", value)
lastRef = value
for part in store_parts:
if value == part["reference"]:
newLcsc = part["lcsc"]
break
if key == "Sheetfile":
file_name = m.group(2)
if file_name not in files_seen:
files_seen.add(file_name)
dir_name = os.path.dirname(path)
self._update_schematic8(os.path.join(dir_name, file_name))
# if we hit the pin section without finding a LCSC property, add it
m3 = pinRx.search(inLine)
if m3 and partSection:
if lastLcsc == "" and newLcsc != "" and lastLoc != "":
self.logger.info("added %s to %s", newLcsc, lastRef)
newTxt = f'\t\t(property "LCSC" "{newLcsc}"\n\t\t\t(at {lastLoc} 0)'
newlines.append(newTxt)
newlines.append(
"\t\t\t(effects\n\t\t\t\t(font\n\t\t\t\t\t(size 1.27 1.27)\n\t\t\t\t)\n\t\t\t\t(hide yes)"
)
newlines.append("\t\t\t)")
newlines.append("\t\t)")
lastLoc = ""
lastLcsc = ""
newLcsc = ""
lastRef = ""
newlines.append(outLine)
newlines.append(lines[len(lines) - 1].rstrip())
if os.path.exists(path + "_old"):
os.remove(path + "_old")
os.rename(path, path + "_old")
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)