-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.py
285 lines (219 loc) · 7.36 KB
/
helpers.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
277
278
279
280
281
282
283
284
285
import os
import re
import wx
PLUGIN_PATH = os.path.split(os.path.abspath(__file__))[0]
THT = 0
SMD = 1
EXCLUDE_FROM_POS = 2
EXCLUDE_FROM_BOM = 3
NOT_IN_SCHEMATIC = 4
def is_nightly(version: str) -> bool:
"""Check if version is a Nightly build"""
return any(v in version for v in ("6.99", "7.0", "7.99"))
def getWxWidgetsVersion():
v = re.search(r"wxWidgets\s([\d\.]+)", wx.version())
v = int(v.group(1).replace(".", ""))
return v
def getVersion():
"""READ Version from file"""
if not os.path.isfile(os.path.join(PLUGIN_PATH, "VERSION")):
return "unknown"
with open(os.path.join(PLUGIN_PATH, "VERSION")) as f:
return f.read().strip()
def GetScaleFactor(window):
"""Workaround if wxWidgets Version does not support GetDPIScaleFactor"""
if hasattr(window, "GetDPIScaleFactor"):
return window.GetDPIScaleFactor()
else:
return 1.0
def HighResWxSize(window, size):
"""Workaround if wxWidgets Version does not support FromDIP"""
if hasattr(window, "FromDIP"):
return window.FromDIP(size)
else:
return size
def loadBitmapScaled(filename, scale=1.0, static=False):
"""Load a scaled bitmap, handle differences between Kicad versions"""
if filename:
path = os.path.join(PLUGIN_PATH, "icons", filename)
bmp = wx.Bitmap(path)
w, h = bmp.GetSize()
img = bmp.ConvertToImage()
bmp = wx.Bitmap(img.Scale(int(w * scale), int(h * scale)))
else:
bmp = wx.Bitmap()
if getWxWidgetsVersion() > 315 and not static:
return wx.BitmapBundle(bmp)
return bmp
def loadIconScaled(filename, scale=1.0):
"""Load a scaled icon, handle differences between Kicad versions"""
bmp = loadBitmapScaled(filename, scale=scale, static=False)
if getWxWidgetsVersion() > 315:
return bmp
return wx.Icon(bmp)
def natural_sort_collation(a, b):
"""Natural sort collation for use in sqlite."""
if a == b:
return 0
def convert(text):
return int(text) if text.isdigit() else text.lower()
def alphanum_key(key):
return [convert(c) for c in re.split("([0-9]+)", key)]
natorder = sorted([a, b], key=alphanum_key)
return -1 if natorder.index(a) == 0 else 1
def get_valid_footprints(board):
"""Get all footprints that have a valid reference (drop all REF**)"""
footprints = []
for fp in board.GetFootprints():
if re.match(r"\w+\d+", fp.GetReference()):
footprints.append(fp)
return footprints
def get_footprint_keys(fp):
"""get keys from footprint for sorting."""
try:
package = str(fp.GetFPID().GetLibItemName())
except:
package = ""
try:
reference = int(re.search("\d+", fp.GetReference())[0])
except:
reference = 0
return (package, reference)
def get_footprint_by_ref(board, ref):
"""get a footprint from the list of footprints by its Reference."""
fps = []
for fp in get_valid_footprints(board):
if str(fp.GetReference()) == ref:
fps.append(fp)
return fps
def get_bit(value, bit):
"""Get the nth bit of a byte."""
return value & (1 << bit)
def set_bit(value, bit):
"""Set the nth bit of a byte."""
return value | (1 << bit)
def clear_bit(value, bit):
"""Clear the nth bit of a byte."""
return value & ~(1 << bit)
def toggle_bit(value, bit):
"""Toggle the nth bit of a byte."""
return value ^ (1 << bit)
def get_tht(footprint):
"""Get the THT property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, THT))
def get_smd(footprint):
"""Get the SMD property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, SMD))
def get_exclude_from_pos(footprint):
"""Get the 'exclude from POS' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, EXCLUDE_FROM_POS))
def get_exclude_from_bom(footprint):
"""Get the 'exclude from BOM' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, EXCLUDE_FROM_BOM))
def get_not_in_schematic(footprint):
"""Get the 'not in schematic' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, NOT_IN_SCHEMATIC))
def set_tht(footprint):
"""Set the THT property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
val = set_bit(val, THT)
footprint.SetAttributes(val)
return bool(get_bit(val, THT))
def set_smd(footprint):
"""Set the SMD property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
val = set_bit(val, SMD)
footprint.SetAttributes(val)
return bool(get_bit(val, SMD))
def set_exclude_from_pos(footprint, v):
"""Set the 'exclude from POS' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
if v:
val = set_bit(val, EXCLUDE_FROM_POS)
else:
val = clear_bit(val, EXCLUDE_FROM_POS)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_POS))
def set_exclude_from_bom(footprint, v):
"""Set the 'exclude from BOM' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
if v:
val = set_bit(val, EXCLUDE_FROM_BOM)
else:
val = clear_bit(val, EXCLUDE_FROM_BOM)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_BOM))
def set_not_in_schematic(footprint, v):
"""Set the 'not in schematic' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
if v:
val = set_bit(val, NOT_IN_SCHEMATIC)
else:
val = clear_bit(val, NOT_IN_SCHEMATIC)
footprint.SetAttributes(val)
return bool(get_bit(val, NOT_IN_SCHEMATIC))
def toggle_tht(footprint):
"""Toggle the THT property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
val = toggle_bit(val, THT)
footprint.SetAttributes(val)
return bool(get_bit(val, THT))
def toggle_smd(footprint):
"""Toggle the SMD property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
val = toggle_bit(val, SMD)
footprint.SetAttributes(val)
return bool(get_bit(val, SMD))
def toggle_exclude_from_pos(footprint):
"""Toggle the 'exclude from POS' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
val = toggle_bit(val, EXCLUDE_FROM_POS)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_POS))
def toggle_exclude_from_bom(footprint):
"""Toggle the 'exclude from BOM' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
val = toggle_bit(val, EXCLUDE_FROM_BOM)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_BOM))
def toggle_not_in_schematic(footprint):
"""Toggle the 'not in schematic' property of a footprint."""
if not footprint:
return
val = footprint.GetAttributes()
val = toggle_bit(val, NOT_IN_SCHEMATIC)
footprint.SetAttributes(val)
return bool(get_bit(val, NOT_IN_SCHEMATIC))