-
Notifications
You must be signed in to change notification settings - Fork 2
/
drawborder.py
316 lines (273 loc) · 10.5 KB
/
drawborder.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright (C) 2022 Girish Palya <girishji@gmail.com>
# License: https://opensource.org/licenses/MIT
#
# To run as script in python console,
# place or symplink this script to ~/Documents/KiCad/6.0/scripting/plugins
# Run from python console using 'import filename'
# To reapply:
# import importlib
# importlib.reload(filename)
# OR
# exec(open("path-to-script-file").read())
import pcbnew
from pcbnew import wxPoint
import math
DIM = 19.0 * pcbnew.IU_PER_MM
RADIUS = 3.0 * pcbnew.IU_PER_MM
BORDER = 4.0 * pcbnew.IU_PER_MM
HOLE_OFFSET = 1.0 * pcbnew.IU_PER_MM
WR = {
"offset": 64 * pcbnew.IU_PER_MM,
"depth": 87 * pcbnew.IU_PER_MM,
"standoff": 28 * pcbnew.IU_PER_MM,
}
SWITCHES = [pcbnew.GetBoard().FindFootprintByReference("S1")] # dummy
for i in range(1, 75):
SWITCHES.append(pcbnew.GetBoard().FindFootprintByReference("S" + str(i)))
def add_line(start, end, layer=pcbnew.Edge_Cuts):
board = pcbnew.GetBoard()
ls = pcbnew.PCB_SHAPE(board)
ls.SetShape(pcbnew.SHAPE_T_SEGMENT)
ls.SetStart(start)
ls.SetEnd(end)
ls.SetLayer(layer)
# ls.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
board.Add(ls)
def add_line_arc(start, center, reverse=False, angle=-90, layer=pcbnew.Edge_Cuts):
board = pcbnew.GetBoard()
arc = pcbnew.PCB_SHAPE(board)
arc.SetShape(pcbnew.SHAPE_T_ARC)
arc.SetStart(start)
arc.SetCenter(center)
arc.SetArcAngleAndEnd(-angle * 10, reverse)
arc.SetLayer(layer)
# arc.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
board.Add(arc)
def centerpt(start, quadrant, d=RADIUS):
ctr = {
1: wxPoint(start.x + d, start.y),
2: wxPoint(start.x, start.y + d),
3: wxPoint(start.x - d, start.y),
4: wxPoint(start.x, start.y - d),
}
return ctr[quadrant]
def endpt(start, quadrant, d=RADIUS):
end = {
1: wxPoint(start.x + d, start.y + d),
2: wxPoint(start.x - d, start.y + d),
3: wxPoint(start.x - d, start.y - d),
4: wxPoint(start.x + d, start.y - d),
}
return end[quadrant]
def transform(pt, around, theta):
matrix = [
[math.cos(math.radians(theta)), -math.sin(math.radians(theta))],
[math.sin(math.radians(theta)), math.cos(math.radians(theta))],
]
return wxPoint(
around.x + pt.x * matrix[0][0] + pt.y * matrix[0][1],
around.y + pt.x * matrix[1][0] + pt.y * matrix[1][1],
)
def remove_drawings():
board = pcbnew.GetBoard()
for t in board.GetDrawings():
board.Delete(t)
def draw_border_tilted_keys():
dim = DIM
brd = BORDER
rad = RADIUS
board = pcbnew.GetBoard()
switches = SWITCHES
sw = switches[64].GetPosition()
degl = -switches[64].GetOrientation() // 10
lsta = wxPoint(-dim / 2 - dim / 8, dim / 2 + brd)
lend = wxPoint(0, dim / 2 + brd)
sta = transform(lsta, sw, degl)
end = transform(lend, sw, degl)
add_line(sta, end)
sw = switches[65].GetPosition()
deg = 90 - switches[65].GetOrientation() // 10
lsta = wxPoint(-dim / 2 - brd + rad, dim / 2 + dim / 8 + brd)
lend = wxPoint(dim / 2 + brd - rad, dim / 2 + dim / 8 + brd)
sta = transform(lsta, sw, deg)
end = transform(lend, sw, deg)
add_line(sta, end)
ctr = transform(wxPoint(lsta.x, lsta.y - rad), sw, deg)
add_line_arc(sta, ctr)
end1 = transform(wxPoint(lsta.x - rad, lsta.y - rad), sw, deg)
lend2 = wxPoint(lsta.x - rad, lsta.y - dim / 2 + 5.5 * pcbnew.IU_PER_MM)
end2 = transform(lend2, sw, deg)
add_line(end1, end2)
ctr = transform(wxPoint(lend2.x - rad, lend2.y), sw, deg)
add_line_arc(end2, ctr, reverse=True, angle=90 + deg - degl)
ctr = transform(wxPoint(lend.x, lend.y - rad), sw, deg)
add_line_arc(end, ctr, reverse=True, angle=90)
lend1 = wxPoint(lend.x + rad, lend.y - rad)
end1 = transform(lend1, sw, deg)
lend2 = wxPoint(lend1.x, lend1.y - dim + 1 * pcbnew.IU_PER_MM)
end2 = transform(lend2, sw, deg)
add_line(end1, end2)
ctr = transform(wxPoint(lend2.x + rad, lend2.y), sw, deg)
add_line_arc(end2, ctr, reverse=False, angle=-90 + deg)
sw = switches[68].GetPosition()
degr = -switches[68].GetOrientation() // 10
lsta = wxPoint(0, dim / 2 + brd)
lend = wxPoint(dim / 2, dim / 2 + brd)
sta = transform(lsta, sw, degr)
end = transform(lend, sw, degr)
add_line(sta, end)
sw = switches[67].GetPosition()
deg = 90 - switches[67].GetOrientation() // 10
lend = wxPoint(-dim / 2 - brd + rad, dim / 2 + dim / 8 + brd)
lsta = wxPoint(dim / 2 + brd - rad, dim / 2 + dim / 8 + brd)
sta = transform(lsta, sw, deg)
end = transform(lend, sw, deg)
add_line(sta, end)
ctr = transform(wxPoint(lsta.x, lsta.y - rad), sw, deg)
add_line_arc(sta, ctr, reverse=True, angle=90)
end1 = transform(wxPoint(lsta.x + rad, lsta.y - rad), sw, deg)
lend2 = wxPoint(lsta.x + rad, lsta.y - dim / 2 + 5.0 * pcbnew.IU_PER_MM)
end2 = transform(lend2, sw, deg)
add_line(end1, end2)
ctr = transform(wxPoint(lend2.x + rad, lend2.y), sw, deg)
add_line_arc(end2, ctr, reverse=False, angle=-90 + deg - degr)
ctr = transform(wxPoint(lend.x, lend.y - rad), sw, deg)
add_line_arc(end, ctr, reverse=False, angle=-90)
lend1 = wxPoint(lend.x - rad, lend.y - rad)
end1 = transform(lend1, sw, deg)
lend2 = wxPoint(lend1.x, lend1.y - dim + 1 * pcbnew.IU_PER_MM)
end2 = transform(lend2, sw, deg)
add_line(end1, end2)
ctr = transform(wxPoint(lend2.x - rad, lend2.y), sw, deg)
add_line_arc(end2, ctr, reverse=True, angle=90 + deg)
def draw_support(start):
ho = HOLE_OFFSET
rad = RADIUS
switches = SWITCHES
def place_hole(center):
hole = pcbnew.GetBoard().FindFootprintByReference(
"HS" + str(draw_support.holenum)
)
draw_support.holenum += 1
if hole:
hole.SetPosition(center)
end = wxPoint(start.x, start.y + rad)
add_line(start, end)
sta = sleft = end
end = wxPoint(sta.x, sta.y + WR["depth"] - WR["standoff"] - 2 * rad)
add_line(sta, end)
place_hole(wxPoint(end.x + rad + ho, end.y - ho))
add_line_arc(end, centerpt(end, 1), reverse=True, angle=90)
sta = endpt(end, 1)
if not draw_support.width:
ctr = switches[66].GetPosition()
draw_support.width = ctr.x - WR["offset"] - sta.x + rad
print("width", draw_support.width)
# draw_support.width = 90500000.0
end = wxPoint(sta.x + draw_support.width - 2 * rad, sta.y)
add_line(sta, end)
add_line_arc(end, centerpt(end, 4), reverse=True, angle=90)
sta = endpt(end, 4)
place_hole(wxPoint(end.x - ho, end.y - rad - ho))
end = wxPoint(sta.x, sta.y - WR["depth"] + WR["standoff"] + 2 * rad)
add_line(sta, end)
sright = end
endrt = wxPoint(end.x, end.y - rad)
add_line(end, endrt)
# user.drawings lines
def draw_standoff(sta, end):
add_line(sta, end, layer=pcbnew.Dwgs_User)
add_line_arc(
sta, centerpt(sta, 2), reverse=True, angle=90, layer=pcbnew.Dwgs_User
)
add_line_arc(
end, centerpt(end, 2), reverse=False, angle=-90, layer=pcbnew.Dwgs_User
)
sta = wxPoint(sleft.x + rad, start.y)
place_hole(wxPoint(sta.x + ho, sta.y + rad + ho))
end = wxPoint(sright.x - rad, start.y)
place_hole(wxPoint(end.x - ho, end.y + rad + ho))
draw_standoff(sta, end)
return endrt
draw_support.width = 0
draw_support.holenum = 1
def draw_border():
dim = DIM
brd = BORDER
rad = RADIUS
switches = SWITCHES
blackpill_wid = (21.0 + 0.5) * pcbnew.IU_PER_MM
# wrist support
if not switches[1]:
draw_support(wxPoint(0, 0))
pcbnew.Refresh()
return
left = switches[30].GetPosition()
topr = switches[15].GetPosition()
tl = wxPoint(left.x - dim / 2 + rad, topr.y - dim / 2 - brd)
tr = wxPoint(topr.x + dim / 2 + brd - rad, topr.y - dim / 2 - brd)
add_line(tl, tr)
add_line_arc(tl, centerpt(tl, 2), reverse=True, angle=90)
end = wxPoint(tl.x - rad, tl.y + 2 * dim - rad + brd)
add_line(endpt(tl, 2), end)
blackpill = pcbnew.GetBoard().FindFootprintByReference("U1")
bl = switches[60].GetPosition()
ctr = switches[66].GetPosition()
sdeg = -switches[64].GetOrientation() // 10
if blackpill:
add_line_arc(end, centerpt(end, 3))
sta = endpt(end, 2)
end = wxPoint(sta.x + rad - blackpill_wid, sta.y)
add_line(sta, end)
bl = switches[60].GetPosition()
end1 = wxPoint(end.x, bl.y + dim / 2 + WR["standoff"])
add_line(end, end1)
sta = draw_support(end1)
end = wxPoint(sta.x, ctr.y + dim / 2 + brd + rad + 1 * 1e6)
add_line(sta, end)
add_line_arc(end, centerpt(end, 1), angle=-90 - sdeg)
else:
end1 = wxPoint(end.x, bl.y + dim / 2 + brd - rad)
add_line(end, end1)
add_line_arc(end1, centerpt(end1, 1), reverse=True, angle=90)
end2 = endpt(end1, 1)
end3 = wxPoint(end2.x - rad + dim * 3.5 + 2 * 1e6, end2.y)
add_line(end2, end3)
add_line_arc(end3, centerpt(end3, 2), reverse=False, angle=-sdeg)
sdeg = switches[68].GetOrientation() // 10
if blackpill:
sta = wxPoint(ctr.x + WR["offset"], ctr.y + dim / 2 + brd + rad + 1 * 1e6)
add_line_arc(sta, centerpt(sta, 3), reverse=True, angle=90 + sdeg)
end1 = wxPoint(sta.x, sta.y + WR["standoff"] - brd - rad - 1 * 1e6)
add_line(sta, end1)
sta = draw_support(end1)
end = wxPoint(sta.x, ctr.y + dim / 2 + brd + rad)
add_line(sta, end)
add_line_arc(end, centerpt(end, 1), reverse=False, angle=-90)
sta = endpt(end, 4)
else:
sta = wxPoint(ctr.x + WR["offset"], ctr.y + dim / 2 + brd)
add_line_arc(sta, centerpt(sta, 2), reverse=True, angle=sdeg)
draw_border_tilted_keys()
redge = switches[74].GetPosition()
end = wxPoint(redge.x + dim / 2 + brd - rad, sta.y)
add_line(sta, end)
add_line_arc(end, centerpt(end, 4), reverse=True, angle=+90)
sta = endpt(end, 4)
tsw = switches[59].GetPosition()
end = wxPoint(sta.x, tsw.y - dim / 2 - brd + rad)
add_line(sta, end)
add_line_arc(end, centerpt(end, 3), reverse=True, angle=+90)
end1 = endpt(end, 3)
tsw = switches[44].GetPosition()
end2 = wxPoint(tsw.x + dim / 2 + brd + rad, end1.y)
add_line(end1, end2)
add_line_arc(end2, centerpt(end2, 4), reverse=False, angle=-90)
end3 = endpt(end2, 3)
tsw = switches[15].GetPosition()
end4 = wxPoint(end3.x, tsw.y - dim / 2 - brd + rad)
add_line(end3, end4)
add_line_arc(end4, centerpt(end4, 3), reverse=True, angle=90)
pcbnew.Refresh()
remove_drawings()
draw_border()