forked from Bouni/kicad-jlcpcb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
198 lines (151 loc) · 4.52 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
import os
import re
THT = 0
SMD = 1
EXCLUDE_FROM_POS = 2
EXCLUDE_FROM_BOM = 3
NOT_IN_SCHEMATIC = 4
def get_version_info():
"""Get version info"""
path, filename = os.path.split(os.path.abspath(__file__))
fetch_head = os.path.join(path, ".git", "FETCH_HEAD")
if not os.path.isfile(fetch_head):
return "unknown"
with open(fetch_head) as f:
v = f.read()[:7]
return v
def get_valid_footprints(board):
"""Get all footprints that have a vaild 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:
refrerence = int(re.search("\d+", fp.GetReference())[0])
except:
refrerence = 0
return (package, refrerence)
def get_footprint_by_ref(board, ref):
"""get a footprint from the list of footprints by its Reference."""
for fp in get_valid_footprints(board):
if str(fp.GetReference()) == ref:
return fp
def get_bit(value, bit):
return value & (1 << bit)
def set_bit(value, bit):
return value | (1 << bit)
def clear_bit(value, bit):
return value & ~(1 << bit)
def toggle_bit(value, bit):
return value ^ (1 << bit)
def get_tht(footprint):
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, THT))
def get_smd(footprint):
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, SMD))
def get_exclude_from_pos(footprint):
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, EXCLUDE_FROM_POS))
def get_exclude_from_bom(footprint):
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, EXCLUDE_FROM_BOM))
def get_not_in_schematic(footprint):
if not footprint:
return
val = footprint.GetAttributes()
return bool(get_bit(val, NOT_IN_SCHEMATIC))
def set_tht(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):
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):
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):
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):
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):
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):
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):
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):
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):
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))