-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
helpers.py
202 lines (153 loc) · 5.87 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
"""Contains helper function used all over the plugin."""
import os
import re
import wx # pylint: disable=import-error
import wx.dataview # pylint: disable=import-error
PLUGIN_PATH = os.path.split(os.path.abspath(__file__))[0]
EXCLUDE_FROM_POS = 2
EXCLUDE_FROM_BOM = 3
def is_version8(version: str) -> bool:
"""Check if version is 8 or 8 Nightly build."""
return any(v in version for v in ("8.0", "8.99"))
def is_version7(version: str) -> bool:
"""Check if version is 7."""
return any(v in version for v in ("7.0"))
def is_version6(version: str) -> bool:
"""Check if version is 6."""
return any(v in version for v in ("6.0"))
def getWxWidgetsVersion():
"""Get wx widgets version."""
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"), encoding="utf-8") as f:
return f.read().strip()
def GetOS():
"""Get String with OS type."""
return wx.PlatformInformation.Get().GetOperatingSystemIdName()
def GetScaleFactor(window):
"""Workaround if wxWidgets Version does not support GetDPIScaleFactor, for Mac OS always return 1.0."""
if "Apple Mac OS" in GetOS():
return 1.0
if hasattr(window, "GetDPIScaleFactor"):
return window.GetDPIScaleFactor()
return 1.0
def HighResWxSize(window, size):
"""Workaround if wxWidgets Version does not support FromDIP."""
if hasattr(window, "FromDIP"):
return window.FromDIP(size)
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()
if hasattr(wx.SystemSettings, "GetAppearance") and hasattr(
wx.SystemSettings.GetAppearance, "IsUsingDarkBackground"
):
if wx.SystemSettings.GetAppearance().IsUsingDarkBackground():
img.Replace(0, 0, 0, 255, 255, 255)
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 dict_factory(cursor, row) -> dict:
"""Row factory that returns a dict."""
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def get_lcsc_value(fp):
"""Get the first lcsc number (C123456 for example) from the properties of the footprint."""
# KiCad 7.99
try:
for field in fp.GetFields():
if re.match(r"lcsc|jlc", field.GetName(), re.IGNORECASE) and re.match(
r"^C\d+$", field.GetText()
):
return field.GetText()
# KiCad <= V7
except AttributeError:
for key, value in fp.GetProperties().items():
if re.match(r"lcsc|jlc", key, re.IGNORECASE) and re.match(r"^C\d+$", value):
return value
return ""
def set_lcsc_value(fp, lcsc: str):
"""Set an lcsc number to the first matching propertie of the footprint, use LCSC as property name if not found."""
lcsc_field = None
for field in fp.GetFields():
if re.match(r"lcsc|jlc", field.GetName(), re.IGNORECASE) and re.match(
r"^C\d+$", field.GetText()
):
lcsc_field = field
if lcsc_field:
fp.SetField(lcsc_field.GetName(), lcsc)
else:
fp.SetField("LCSC", lcsc)
field = fp.GetFieldByName("LCSC")
field.SetVisible(False)
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_bit(value, bit):
"""Get 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_exclude_from_pos(footprint):
"""Get the 'exclude from POS' property of a footprint."""
if not footprint:
return None
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 None
val = footprint.GetAttributes()
return bool(get_bit(val, EXCLUDE_FROM_BOM))
def toggle_exclude_from_pos(footprint):
"""Toggle the 'exclude from POS' property of a footprint."""
if not footprint:
return None
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 None
val = footprint.GetAttributes()
val = toggle_bit(val, EXCLUDE_FROM_BOM)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_BOM))