-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodules.scad
190 lines (155 loc) · 7.43 KB
/
modules.scad
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
// Key dimensions
top_base_sagitta = APPLY_CYLINDRICAL_DISH ? KEY_DIMENSIONS[0] : 0;
top_base_width = KEY_DIMENSIONS[1] * KEY_SIZE;
bottom_base_length = KEY_DIMENSIONS[2];
bottom_base_width = KEY_DIMENSIONS[3] - KEY_DIMENSIONS[1] + KEY_DIMENSIONS[1] * KEY_SIZE;
bottom_base_angle_back = KEY_DIMENSIONS[4];
// Functions used to calculate dimensions of the cylindrical dish
function sagitta(radius, chord) = radius - pow(pow(radius, 2) - pow(chord/2, 2), 0.5);
function central_chord(chord, sagitta) = pow(chord/2, 2)/sagitta;
function cylinder_radius(chord, sagitta) = (central_chord(chord, sagitta) + sagitta)/2;
// Generates the bases of the key using the minkowski function
module base(width, length, extrusion) {
minkowski() {
cube([width - 2 * BASE_RADIUS, length - 2 * BASE_RADIUS, extrusion/2]);
translate([BASE_RADIUS, BASE_RADIUS, 0])
cylinder(h=extrusion/2, r=BASE_RADIUS);
}
}
// Generates the cylindrical dish
module dish_cylinder(top_base_rotated_length, rotated_cylinder_translate, cylinder_dish_radius, top_base_sagitta, back_cylinder_translate) {
translate([0, top_base_rotated_length + rotated_cylinder_translate, cylinder_dish_radius - top_base_sagitta])
rotate([90,0,0])
cylinder(h=top_base_rotated_length + rotated_cylinder_translate + back_cylinder_translate, r=cylinder_dish_radius);
}
// Basic key shape
module key_shape(top_base_translate, top_base_height_back, top_base_angle, top_base_rotated_length, back_cylinder_translate, rotated_cylinder_translate, cylinder_dish_radius) {
difference() {
hull() {
base(bottom_base_width, bottom_base_length, BOTTOM_BASE_EXTRUSION_HEIGHT);
translate([(bottom_base_width-top_base_width)/2, top_base_translate, top_base_height_back - TOP_BASE_EXTRUSION_HEIGHT])
rotate([-top_base_angle, 0, 0])
base(top_base_width, top_base_rotated_length, TOP_BASE_EXTRUSION_HEIGHT);
}
if (APPLY_CYLINDRICAL_DISH) {
translate([bottom_base_width/2, top_base_translate, top_base_height_back])
rotate([-top_base_angle, 0, 0])
dish_cylinder(top_base_rotated_length, rotated_cylinder_translate, cylinder_dish_radius, top_base_sagitta, back_cylinder_translate);
}
else {
translate([(bottom_base_width - top_base_width)/2, top_base_translate , top_base_height_back])
rotate([-top_base_angle, 0, 0])
cube([top_base_width, top_base_rotated_length, top_base_height_back]);
}
}
}
// Basic function that generates the key
module key(row, symbol_number) {
// Row dimensions
KEY_ROW_DIMENSIONS = ROW_DIMENSIONS[row];
top_base_height_back = KEY_ROW_DIMENSIONS[0];
top_base_angle = APPLY_KEY_ANGLE ? KEY_ROW_DIMENSIONS[2] : 0;
top_base_translate = top_base_height_back/tan(bottom_base_angle_back);
bottom_base_angle_front = KEY_ROW_DIMENSIONS[1];
// Calculations for the top base
top_base_length = (bottom_base_length * tan(bottom_base_angle_front) - top_base_height_back)/(tan(bottom_base_angle_front) - tan(top_base_angle));
top_base_height_front = top_base_height_back - top_base_length * tan(top_base_angle);
top_base_rotated_length = top_base_length/cos(top_base_angle);
cylinder_dish_radius = cylinder_radius(top_base_width, top_base_sagitta);
rotated_cylinder_translate = top_base_sagitta/tan(bottom_base_angle_front-top_base_angle);
back_cylinder_translate = top_base_sagitta/tan(bottom_base_angle_back+top_base_angle);
// Scale to generate the internal part of the key
key_scale = (bottom_base_width - 2 * KEY_THICKNESS) / bottom_base_width;
// Side angle used for the support
bottom_base_angle_side = atan(top_base_height_back/((bottom_base_width-top_base_width)/2));
difference() {
union() {
difference() {
key_shape(top_base_translate, top_base_height_back, top_base_angle, top_base_rotated_length, back_cylinder_translate, rotated_cylinder_translate, cylinder_dish_radius);
translate([KEY_THICKNESS, KEY_THICKNESS, 0])
scale(key_scale)
key_shape(top_base_translate, top_base_height_back, top_base_angle, top_base_rotated_length, back_cylinder_translate, rotated_cylinder_translate, cylinder_dish_radius);
}
difference() {
translate([bottom_base_width/2, bottom_base_length/2, CONNECTOR_HEIGHT])
connector(max(top_base_height_front, top_base_height_back));
if (APPLY_CYLINDRICAL_DISH) {
translate([bottom_base_width/2, top_base_translate, top_base_height_back])
rotate([-top_base_angle, 0, 0])
dish_cylinder(top_base_rotated_length, rotated_cylinder_translate, cylinder_dish_radius, top_base_sagitta, back_cylinder_translate);
}
else {
translate([(bottom_base_width - top_base_width)/2, top_base_translate , top_base_height_back])
rotate([-top_base_angle, 0, 0])
cube([top_base_width, top_base_rotated_length, top_base_height_back]);
}
}
}
}
if (APPLY_SYMBOL) {
translate([0, 0, top_base_height_back])
rotate([-top_base_angle, 0, 0])
translate([bottom_base_width/2, top_base_rotated_length/2 + top_base_translate, 0])
symbol(top_base_rotated_length, symbol_number);
}
if (APPLY_SUPPORT) {
translate([bottom_base_width, bottom_base_length/2, 0])
support(bottom_base_angle_side);
translate([0, bottom_base_length/2, 0])
rotate([0, 0, 180])
support(bottom_base_angle_side);
translate([bottom_base_width/2, bottom_base_length, 0])
rotate([0, 0, 90])
support(bottom_base_angle_front);
translate([bottom_base_width/2, 0, 0])
rotate([0, 0, -90])
support(bottom_base_angle_back);
}
}
// Generates the support used for CNC machining the key
module support(bottom_base_angle_side) {
support_base_translate = SUPPORT_HEIGHT/tan(bottom_base_angle_side);
translate([-support_base_translate, SUPPORT_WIDTH/2, 0])
rotate([90, 0, 0])
linear_extrude(height=SUPPORT_WIDTH)
polygon([[0, SUPPORT_HEIGHT], [support_base_translate, 0], [support_base_translate + SUPPORT_LENGTH, 0], [support_base_translate + SUPPORT_LENGTH, SUPPORT_HEIGHT]]);
}
// Generates the connector for the key
module connector(top_base_height_back) {
sagitta_difference = sagitta(CONNECTOR_RADIUS, CONNECTOR_THICKNESS);
union() {
difference() {
cylinder(h=top_base_height_back - CONNECTOR_HEIGHT, r = CONNECTOR_RADIUS);
cylinder(h=top_base_height_back - CONNECTOR_HEIGHT, r = CONNECTOR_RADIUS - CONNECTOR_THICKNESS);
translate([-CONNECTOR_MIDDLE_SPACE/2, -CONNECTOR_RADIUS, 0])
cube([CONNECTOR_MIDDLE_SPACE, 2 * CONNECTOR_RADIUS, top_base_height_back - CONNECTOR_HEIGHT - CONNECTOR_SUPPORT_HEIGHT]);
}
}
}
module connector_test() {
translate([0, 0, top_base_height_back - CONNECTOR_HEIGHT])
base(top_base_width, top_base_rotated_length, KEY_THICKNESS);
translate([top_base_width/2, top_base_length/2, 0])
connector();
}
module symbol(top_base_rotated_length, symbol_number) {
symbol_path = SYMBOL_FILES[symbol_number];
symbol_initial_width = dxf_dim(file=symbol_path, name="total_width");
symbol_initial_length = dxf_dim(file=symbol_path, name="total_height");
symbol_width_scale = (top_base_width - SYMBOL_SPACING)/symbol_initial_width;
symbol_length_scale = (top_base_rotated_length - SYMBOL_SPACING)/symbol_initial_length;
symbol_scale = min(symbol_width_scale, symbol_length_scale);
symbol_width = symbol_initial_width * symbol_scale;
symbol_length = symbol_initial_length * symbol_scale;
if (DEBUG_SYMBOL) {
color("blue")
translate([-symbol_width/2, -symbol_length/2, 0])
cube([symbol_width, symbol_length, 0.1]);
}
color("red")
translate([symbol_width/2, symbol_length/2, 0])
scale([symbol_scale, symbol_scale, 1])
rotate([0, 0, 180])
linear_extrude(height=SYMBOL_THICKNESS)
import(file=symbol_path);
}