forked from ai03-2725/another-keyboard-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plategen.py
657 lines (526 loc) · 31.4 KB
/
plategen.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# =================================#
# Plate Generator #
# =================================#
# By ai03
# Credits to
# Amtra5, Mxblue, Bakingpy,
# Senter, Pwner, Kevinplus, Deskthority Wiki,
# and any others I may have missed
# Automated production of high-end mechanical keyboard plate data
# No float rounding issues, pre-filleted corners, ready for production.
# EXTRA SUPPORTED JSON FIELDS:
# In addition to the KLE fields such as w: for width,
# _rs: Rotate the stabilizers
# _rc: Rotate switch cutout
# =================================#
# #
# =================================#
# Import necessities
import ezdxf
import sys
import json5
import argparse
from mpmath import mp, radians, matrix, cos, sin
from decimal import getcontext, Decimal
class PlateGenerator(object):
def __init__(self, arg_ct, arg_cr, arg_st, arg_sr, arg_at, arg_ar, arg_uw, arg_uh, arg_om, arg_of, arg_db):
# Set up decimal and mpmath
getcontext().prec = 50
mp.dps = 50
mp.pretty = True
# Create blank dxf workspace
self.plate = ezdxf.new(dxfversion='AC1024')
self.modelspace = self.plate.modelspace()
# Cutout type: mx, alps
self.cutout_type = arg_ct
# Cutout radius: The fillet radius ( 0 <= x <= 1/2 cutout width or height )
self.cutout_radius = Decimal(arg_cr)
# Stab type: mx-simple, large-cuts, alps-aek, alps-at101
self.stab_type = arg_st
# Stab radius: The fillet radius for stab cutouts ( 0 <= x <= 1 )
self.stab_radius = Decimal(arg_sr)
# Acoustic cuts: The cutouts typically found on high end plates beside the switches.
# This script only handles the thin short cuts vertically beside each switch cut, not the large ones, i.e. between fn row and alphas.
# none = disabled, typical = 1.5-1.75U only, extreme = On 1.5-2.75U
self.acoustics_type = arg_at
# Acoustic radius: Fillet radius for cuts mentioned above.
self.acoustics_radius = arg_ar
# Unit size (i.e. 1U = 19.05mm). ( 0 <= x <= inf, cap at 1000 for now )
self.unit_width = Decimal(arg_uw)
self.unit_height = Decimal(arg_uh)
# Output settings. Method can be file or stdout. self.filename is ignored if self.output_method is set to stdout
self.output_method = arg_om
self.filename = arg_of
# == Debug parameters ==#
# Tell user everything about what's going on and spam the console?
self.debug_log = arg_db
# Runtime vars that are often systematically changed or reset
# Current x/y coordinates
self.current_x = Decimal('0')
self.current_y = Decimal('0')
self.max_width = Decimal('0')
self.max_height = Decimal('0')
# Cutout sizes
self.cutout_width = Decimal('0')
self.cutout_height = Decimal('0')
# Used for parsing
self.reset_key_parameters()
self.current_rotx = Decimal('0')
self.current_roty = Decimal('0')
self.current_angle = Decimal('0')
# =================================#
# Classes #
# =================================#
class Switch:
def __init__(self, x_var, y_var):
# These fields correspond to the respective kle data
self.x = x_var
self.y = y_var
self.width = 1
self.height = 1
self.width_secondary = 1
self.height_secondary = 1
self.rotx = 0
self.roty = 0
self.angle = 0
self.cutout_angle = 0
self.stab_angle = 0
self.offset_x = 0
self.offset_y = 0
# Reset key default parameters
def reset_key_parameters(self):
self.current_width = Decimal('1')
self.current_height = Decimal('1')
self.current_width_secondary = Decimal('1')
self.current_height_secondary = Decimal('1')
self.current_stab_angle = Decimal('0')
self.current_cutout_angle = Decimal('0')
self.current_offset_x = Decimal('0')
self.current_offset_y = Decimal('0')
self.current_deco = False
# Modifies a point with rotation
def rotate_point_around_anchor(self, x, y, anchor_x, anchor_y, angle):
anglefrac = angle.as_integer_ratio()
radian_qty = radians(anglefrac[0] / anglefrac[1])
old_x = x - anchor_x
old_y = y - anchor_y
coord = matrix([float(old_x), float(old_y)])
transform = matrix([[cos(radian_qty), -sin(radian_qty)], [sin(radian_qty), cos(radian_qty)]])
result = transform * coord
new_x = Decimal(str(result[0]))
new_y = Decimal(str(result[1]))
new_x += anchor_x
new_y += anchor_y
return new_x, new_y
# Draw line segment rotated with respect to an anchor
def draw_rotated_line(self, x1, y1, x2, y2, anchor_x, anchor_y, angle):
coords_1 = self.rotate_point_around_anchor(x1, y1, anchor_x, anchor_y, angle)
coords_2 = self.rotate_point_around_anchor(x2, y2, anchor_x, anchor_y, angle)
self.modelspace.add_line((coords_1[0], coords_1[1]), (coords_2[0], coords_2[1]))
# Draw arc rotated with respect to an anchor
def draw_rotated_arc(self, x, y, anchor_x, anchor_y, radius, angle_start, angle_end, rotation):
coords = self.rotate_point_around_anchor(x, y, anchor_x, anchor_y, rotation)
self.modelspace.add_arc((coords[0], coords[1]), radius, float(angle_start + rotation),
float(angle_end + rotation))
# Stab cutout maker
# The x and y are center, like this:
#
# -------
# | |
# | X | - - - Center Y of switch
# | |
# |_ _|
# |_|
def make_stab_cutout(self, x, y, anchor_x, anchor_y, angle):
line_segments = []
corners = []
if self.stab_type == "mx-simple":
# Rectangular simplified mx cutout.
# A bit larger than stock to account for fillets.
line_segments.append(
(Decimal('-3.375') + self.stab_radius, Decimal('6'), Decimal('3.375') - self.stab_radius, Decimal('6')))
line_segments.append((Decimal('-3.375') + self.stab_radius, Decimal('-8'),
Decimal('3.375') - self.stab_radius, Decimal('-8')))
line_segments.append((Decimal('-3.375'), Decimal('6') - self.stab_radius, Decimal('-3.375'),
Decimal('-8') + self.stab_radius))
line_segments.append(
(Decimal('3.375'), Decimal('6') - self.stab_radius, Decimal('3.375'), Decimal('-8') + self.stab_radius))
corners.append((Decimal('-3.375') + self.stab_radius, Decimal('6') - self.stab_radius, 90, 180))
corners.append((Decimal('3.375') - self.stab_radius, Decimal('6') - self.stab_radius, 0, 90))
corners.append((Decimal('-3.375') + self.stab_radius, Decimal('-8') + self.stab_radius, 180, 270))
corners.append((Decimal('3.375') - self.stab_radius, Decimal('-8') + self.stab_radius, 270, 360))
elif self.stab_type == "large-cuts":
# Large, spacious 15x7 cutouts; 1mm from mx switch cutout top
line_segments.append(
(Decimal('-3.5') + self.stab_radius, Decimal('6'), Decimal('3.5') - self.stab_radius, Decimal('6')))
line_segments.append(
(Decimal('-3.5') + self.stab_radius, Decimal('-9'), Decimal('3.5') - self.stab_radius, Decimal('-9')))
line_segments.append(
(Decimal('-3.5'), Decimal('6') - self.stab_radius, Decimal('-3.5'), Decimal('-9') + self.stab_radius))
line_segments.append(
(Decimal('3.5'), Decimal('6') - self.stab_radius, Decimal('3.5'), Decimal('-9') + self.stab_radius))
corners.append((Decimal('-3.5') + self.stab_radius, Decimal('6') - self.stab_radius, 90, 180))
corners.append((Decimal('3.5') - self.stab_radius, Decimal('6') - self.stab_radius, 0, 90))
corners.append((Decimal('-3.5') + self.stab_radius, Decimal('-9') + self.stab_radius, 180, 270))
corners.append((Decimal('3.5') - self.stab_radius, Decimal('-9') + self.stab_radius, 270, 360))
elif self.stab_type == "alps-aek" or self.stab_type == "alps-at101":
# Rectangles 2.67 wide, 5.21 high.
line_segments.append((Decimal('-1.335') + self.stab_radius, Decimal('-3.875'),
Decimal('1.335') - self.stab_radius, Decimal('-3.875')))
line_segments.append((Decimal('-1.335') + self.stab_radius, Decimal('-9.085'),
Decimal('1.335') - self.stab_radius, Decimal('-9.085')))
line_segments.append((Decimal('-1.335'), Decimal('-3.875') - self.stab_radius, Decimal('-1.335'),
Decimal('-9.085') + self.stab_radius))
line_segments.append((Decimal('1.335'), Decimal('-3.875') - self.stab_radius, Decimal('1.335'),
Decimal('-9.085') + self.stab_radius))
corners.append((Decimal('-1.335') + self.stab_radius, Decimal('-3.875') - self.stab_radius, 90, 180))
corners.append((Decimal('1.335') - self.stab_radius, Decimal('-3.875') - self.stab_radius, 0, 90))
corners.append((Decimal('-1.335') + self.stab_radius, Decimal('-9.085') + self.stab_radius, 180, 270))
corners.append((Decimal('1.335') - self.stab_radius, Decimal('-9.085') + self.stab_radius, 270, 360))
else:
print("Unsupported stab type.", file=sys.stderr)
print("Stab types: mx-simple, large-cuts, alps-aek, alps-at101", file=sys.stderr)
exit(1)
for line in line_segments:
self.draw_rotated_line(x + Decimal(str(line[0])), y + Decimal(str(line[1])), x + Decimal(str(line[2])),
y + Decimal(str(line[3])), anchor_x, anchor_y, angle)
for arc in corners:
self.draw_rotated_arc(x + Decimal(str(arc[0])), y + Decimal(str(arc[1])), anchor_x, anchor_y,
self.stab_radius, arc[2], arc[3], angle)
# Calls make stab cutout based on unit width and style
def generate_stabs(self, center_x, center_y, angle, unitwidth):
if self.stab_type == "mx-simple" or self.stab_type == "large-cuts":
# Switch based on unit width
# These spacings are based on official mx datasheets and deskthority measurements
if unitwidth >= 8:
# self.make_stab_cutout(x, y, anchor_x, anchor_y, angle)
self.make_stab_cutout(center_x + Decimal('66.675'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('66.675'), center_y, center_x, center_y, angle)
elif unitwidth >= 7:
self.make_stab_cutout(center_x + Decimal('57.15'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('57.15'), center_y, center_x, center_y, angle)
elif unitwidth == 6.25:
self.make_stab_cutout(center_x + Decimal('50'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('50'), center_y, center_x, center_y, angle)
elif unitwidth == 6:
self.make_stab_cutout(center_x + Decimal('38.1'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('57.15'), center_y, center_x, center_y, angle)
elif unitwidth >= 3:
self.make_stab_cutout(center_x + Decimal('19.05'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('19.05'), center_y, center_x, center_y, angle)
elif unitwidth >= 2:
self.make_stab_cutout(center_x + Decimal('11.938'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('11.938'), center_y, center_x, center_y, angle)
elif self.stab_type == "alps-aek":
# These are mostly based on measurements.
# If someone has datasheets, please let me know
if unitwidth >= 6.5:
self.make_stab_cutout(center_x + Decimal('45.3'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('45.3'), center_y, center_x, center_y, angle)
elif unitwidth >= 6.25:
self.make_stab_cutout(center_x + Decimal('41.86'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('41.86'), center_y, center_x, center_y, angle)
elif unitwidth >= 2:
self.make_stab_cutout(center_x + Decimal('14'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('14'), center_y, center_x, center_y, angle)
elif unitwidth >= 1.75:
self.make_stab_cutout(center_x + Decimal('12'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('12'), center_y, center_x, center_y, angle)
elif self.stab_type == "alps-at101":
# These are mostly based on measurements.
# If someone has datasheets, please let me know
if unitwidth >= 6.5:
self.make_stab_cutout(center_x + Decimal('45.3'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('45.3'), center_y, center_x, center_y, angle)
elif unitwidth >= 6.25:
self.make_stab_cutout(center_x + Decimal('41.86'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('41.86'), center_y, center_x, center_y, angle)
elif unitwidth >= 2.75:
self.make_stab_cutout(center_x + Decimal('20.5'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('20.5'), center_y, center_x, center_y, angle)
elif unitwidth >= 2:
self.make_stab_cutout(center_x + Decimal('14'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('14'), center_y, center_x, center_y, angle)
elif unitwidth >= 1.75:
self.make_stab_cutout(center_x + Decimal('12'), center_y, center_x, center_y, angle)
self.make_stab_cutout(center_x - Decimal('12'), center_y, center_x, center_y, angle)
# Draw switch cutout
def draw_switch_cutout(self, mm_center_x, mm_center_y, angle):
# Make some variables for the sake of legibility
mm_y_top = mm_center_y + (self.cutout_height / Decimal('2'))
mm_y_bottom = mm_center_y - (self.cutout_height / Decimal('2'))
mm_x_left = mm_center_x - (self.cutout_width / Decimal('2'))
mm_x_right = mm_center_x + (self.cutout_width / Decimal('2'))
# First draw the line segments: top, bottom, left, right
self.draw_rotated_line(mm_x_left + self.cutout_radius, mm_y_top, mm_x_right - self.cutout_radius, mm_y_top,
mm_center_x, mm_center_y, angle)
self.draw_rotated_line(mm_x_left + self.cutout_radius, mm_y_bottom, mm_x_right - self.cutout_radius,
mm_y_bottom,
mm_center_x, mm_center_y, angle)
self.draw_rotated_line(mm_x_left, mm_y_top - self.cutout_radius, mm_x_left, mm_y_bottom + self.cutout_radius,
mm_center_x, mm_center_y, angle)
self.draw_rotated_line(mm_x_right, mm_y_top - self.cutout_radius, mm_x_right, mm_y_bottom + self.cutout_radius,
mm_center_x, mm_center_y, angle)
# Now render corner arcs: top left, top right, bottom left, bottom right
self.draw_rotated_arc(mm_x_left + self.cutout_radius, mm_y_top - self.cutout_radius, mm_center_x, mm_center_y,
self.cutout_radius, Decimal('90'), Decimal('180'), angle)
self.draw_rotated_arc(mm_x_right - self.cutout_radius, mm_y_top - self.cutout_radius, mm_center_x, mm_center_y,
self.cutout_radius, Decimal('0'), Decimal('90'), angle)
self.draw_rotated_arc(mm_x_left + self.cutout_radius, mm_y_bottom + self.cutout_radius, mm_center_x,
mm_center_y, self.cutout_radius, Decimal('180'), Decimal('270'), angle)
self.draw_rotated_arc(mm_x_right - self.cutout_radius, mm_y_bottom + self.cutout_radius, mm_center_x,
mm_center_y, self.cutout_radius, Decimal('270'), Decimal('360'), angle)
# Use the functions above to render an entire switch - Cutout, stabs, and all
def render_switch(self, switch):
# Coord differs for regular vs rotated
if switch.rotx != 0 or switch.roty != 0 or switch.angle != 0:
# rotx and roty are the raw base coords for anchor
# Then, upper left is offset from there
mm_x = (switch.rotx + switch.offset_x) * self.unit_width
mm_y = (-switch.roty - switch.offset_y) * self.unit_height
# Confirmed coords are correct at this point
# Something going haywire after this
else:
# Otherwise, derive mm based on x and y in units
mm_x = switch.x * self.unit_width
mm_y = switch.y * self.unit_height
# Then, derive the center of the switch based on width and height
mm_center_x = mm_x + ((switch.width / Decimal('2')) * self.unit_width)
mm_center_y = mm_y - ((switch.height / Decimal('2')) * self.unit_height)
# Then, rotate the points if angle != 0
if switch.angle != Decimal('0'):
rotated_central_coords = self.rotate_point_around_anchor(mm_center_x, mm_center_y,
(switch.rotx * self.unit_width),
-(switch.roty * self.unit_height), switch.angle)
mm_center_x = rotated_central_coords[0]
mm_center_y = rotated_central_coords[1]
# Do some calculations to see if a rotated switch exceeds current max boundaries
unrotated_x = (switch.rotx + switch.offset_x) * self.unit_width
unrotated_y = (-switch.roty - switch.offset_y) * self.unit_height
corners = [(unrotated_x, unrotated_y),
(unrotated_x + (switch.width * self.unit_width), unrotated_y),
(unrotated_x, unrotated_y - (switch.height * self.unit_height)),
(unrotated_x + (switch.width * self.unit_width), unrotated_y - (switch.height * self.unit_height))
]
for corner in corners:
rotated_corner = self.rotate_point_around_anchor(corner[0], corner[1], mm_center_x, mm_center_y,
switch.angle)
if rotated_corner[0] > self.max_width:
self.max_width = rotated_corner[0]
if rotated_corner[1] < self.max_height:
self.max_height = rotated_corner[1]
# Draw main switch cutout
self.draw_switch_cutout(mm_center_x, mm_center_y, switch.angle + switch.cutout_angle)
# Adjust width for vertically tall keys, and generate stabs
apparent_width = switch.width
if switch.width < switch.height:
apparent_width = switch.height
self.generate_stabs(mm_center_x, mm_center_y, switch.angle + switch.stab_angle, apparent_width)
# Generate switch cutout sizes
def initialize_variables(self):
if self.cutout_type == "mx":
self.cutout_width = Decimal('14')
self.cutout_height = Decimal('14')
elif self.cutout_type == "alps":
self.cutout_width = Decimal('15.50')
self.cutout_height = Decimal('12.80')
else:
print("Unsupported cutout type.", file=sys.stderr)
print("Supported: mx, alps", file=sys.stderr)
exit(1)
# Check if values legal
# Cutout radius: The fillet radius ( 0 <= x <= 1/2 width or height)
if (self.cutout_radius < 0 or self.cutout_radius > (self.cutout_width / 2) or self.cutout_radius > (
self.cutout_height / 2)):
print("Radius must be between 0 and half the cutout width/height.", file=sys.stderr)
exit(1)
# Unit size ( 0 <= x <= inf, cap at 1000 for now )
if self.unit_width < 0 or self.unit_width > 1000:
print("Unit size must be between 0 and 1000", file=sys.stderr)
exit(1)
if self.unit_height < 0 or self.unit_height > 1000:
print("Unit size must be between 0 and 1000", file=sys.stderr)
exit(1)
# Check if output method is legal
if self.output_method != "stdout" and self.output_method != "file":
print("Unsupported output method specified", file=sys.stderr)
exit(1)
def generate_plate(self, input_data=None):
# Init vars
self.initialize_variables()
# If debug matrix is on, make sth generic
if not input_data:
input_data = self.debug_matrix_data
# Sanitize by removing \" (KLE's literal " for a label)
# input_data = input_data.replace('\n', '')
# input_data = input_data.replace(r'\"', '')
# TODO: Filter out improper quotes from " being in a label!
if self.debug_log:
print("Filtered input data:")
print(input_data)
print("")
# Parse KLE data
all_switches = []
json_data = json5.loads('[' + input_data + ']')
for row in json_data:
if self.debug_log:
print(">>> ROW BEGIN")
print(str(row))
# KLE standard supports first row being metadata.
# If it is, ignore.
if isinstance(row, dict):
if self.debug_log:
print("!!! Row is metadata. Skip.")
continue
for key in row:
# The "key" can either be a legend (actual key) or dictionary of data (for succeeding key).
# If it's just a string, it's just a key. Create one and add to list
if isinstance(key, str):
if self.current_deco:
self.reset_key_parameters()
continue
# First, we simply make the switch
current_switch = self.Switch(self.current_x, self.current_y)
# For x and y offset, check if any rotation spec is set.
if self.current_rotx != 0 or self.current_roty != 0 or self.current_angle != 0:
# This means we RETAIN rx or ry from previous. How awful of a syntax. Seriously KLE?
# If set, store the value
current_switch.offset_x = self.current_offset_x
current_switch.offset_y = self.current_offset_y
# Check and see if it's a y record
if self.max_height > -self.current_roty - self.current_offset_y:
self.max_height = -self.current_roty - self.current_offset_y
else:
# Otherwise, append
self.current_x += self.current_offset_x
self.current_y -= self.current_offset_y
current_switch.x += self.current_offset_x
current_switch.y -= self.current_offset_y
self.current_offset_x = Decimal('0')
self.current_offset_y = Decimal('0')
# Check and see if it's a y record
if self.max_height > self.current_y - self.current_height:
self.max_height = self.current_y - self.current_height
# Then, adjust the x coord for next switch
self.current_x += self.current_width
# If this is a x record, update properly
if self.max_width < self.current_x:
self.max_width = self.current_x
# And we adjust the fields as necessary.
# These default to 1 unless edited by a data field preceding
current_switch.width = self.current_width
current_switch.height = self.current_height
current_switch.width_secondary = self.current_width_secondary
current_switch.height_secondary = self.current_height_secondary
current_switch.rotx = self.current_rotx
current_switch.roty = self.current_roty
current_switch.angle = self.current_angle
current_switch.stab_angle = self.current_stab_angle
current_switch.cutout_angle = self.current_cutout_angle
# Deal with some certain cases
# For example, vertical keys created by stretching height to be larger than width
# The key's cutout angle and stab angle should be offset by 90 degrees to compensate.
# This effectively transforms the key to a vertical
# This also handles ISO
if self.current_width < self.current_height and self.current_height >= 1.75:
current_switch.cutout_angle -= Decimal('90')
current_switch.stab_angle -= Decimal('90')
all_switches.append(current_switch)
# Reset the fields to their defaults
self.reset_key_parameters()
# Otherwise, it's a data dictionary. We must parse it properly
else:
for i in key:
# i = The dictionary key. Not the keyboard kind of key
# j = The corresponding value.
j = key[i]
# Large if-else chain to set params
if str(i) == "w":
# w = Width
self.current_width = Decimal(str(j))
elif str(i) == "h":
# h = Height
self.current_height = Decimal(str(j))
elif str(i) == "w2":
# w2 = Secondary width
self.current_width_secondary = Decimal(str(j))
elif str(i) == "h2":
# h2 = Secondary height
self.current_height_secondary = Decimal(str(j))
elif str(i) == "rx":
# rx = Rotation anchor x
self.current_rotx = Decimal(str(j))
elif str(i) == "ry":
# ry = Rotation anchor y
self.current_roty = Decimal(str(j))
elif str(i) == "r":
# r = Rotation angle OPPOSITE OF typical counterclockwise-from-xpositive
self.current_angle = -Decimal(str(j))
elif str(i) == "_rs":
# rs = Rotation angle offset for stabilizer OPPOSITE OF typical counterclockwise-from-xpositive
self.current_stab_angle = -Decimal(str(j))
elif str(i) == "_rc":
# rs = Switch cutout angle offset for stabilizer OPPOSITE OF typical counterclockwise-from-xpositive
self.current_cutout_angle = -Decimal(str(j))
elif str(i) == "x":
# x = X offset for next keys OR offset from rotation anchor (seriously kle?)
self.current_offset_x = Decimal(str(j))
elif str(i) == "y":
# y = Y offset for next keys OR offset from rotation anchor (seriously kle?)
self.current_offset_y = Decimal(str(j))
elif str(i) == "d":
# Key is decoration.
self.current_deco = True
# Finished row
self.current_y -= Decimal('1')
self.current_x = Decimal('0')
# At this point, the keys are built.
# Adjust max width/height from units to mm
self.max_width = self.max_width * self.unit_width
self.max_height = self.max_height * self.unit_height
# Render each one by one.
for switch in all_switches:
self.render_switch(switch)
# Draw outer bounds - top, bottom, left, right
self.modelspace.add_line((0, 0), (self.max_width, 0))
self.modelspace.add_line((0, self.max_height), (self.max_width, self.max_height))
self.modelspace.add_line((0, 0), (0, self.max_height))
self.modelspace.add_line((self.max_width, 0), (self.max_width, self.max_height))
if self.debug_log:
print("Complete! Saving plate to specified output")
if self.output_method == "file":
self.plate.saveas(self.filename + '.dxf')
else:
self.plate.write(sys.stdout)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Create a plate DXF based on KLE raw data.')
# Note: The args will be fed into Decimal(), which takes strings
parser.add_argument("-ct", "--cutout-type", help="Switch cutout type. Supported: mx, alps; Default: mx", type=str,
default='mx')
parser.add_argument("-cr", "--cutout-radius", help="Switch cutout fillet radius. Default: 0.5", type=str,
default='0.5')
parser.add_argument("-st", "--stab-type",
help="Stabilizer type. Supported: mx-simple, large-cuts, alps-aek, alps-at101; Default: mx-simple",
type=str, default='mx-simple')
parser.add_argument("-sr", "--stab-radius", help="Stabilizer cutout fillet radius. Default: 0.5", type=str,
default='0.5')
parser.add_argument("-at", "--acoustics-type",
help="Acoustic cutouts type. Supported: none, typical, extreme; Default: none", type=str,
default='none')
parser.add_argument("-ar", "--acoustics-radius", help="Acoustic cutouts fillet radius. Default: 0.5", type=str,
default='0.5')
parser.add_argument("-uw", "--unit-width", help="Key unit width. Default: 19.05", type=str, default='19.05')
parser.add_argument("-uh", "--unit-height", help="Key unit height. Default: 19.05", type=str, default='19.05')
parser.add_argument("-om", "--output-method",
help="The save method for data. Supported: stdout, file; Default: stdout", type=str,
default='stdout')
parser.add_argument("-of", "--output-filename",
help="Output file name if using file output-method. Default: plate.dxf", type=str,
default='plate.dxf')
parser.add_argument("--debug-log", help="Spam output with useless info.", action="store_true", default=False)
args = parser.parse_args()
gen = PlateGenerator(args.cutout_type, args.cutout_radius, args.stab_type, args.stab_radius, args.acoustics_type,
args.acoustics_radius, args.unit_width, args.unit_height,
args.output_method, args.output_filename, args.debug_log)
input_data = sys.stdin.read()
gen.generate_plate(input_data)