Root/cad/test1/button.scad

1epsilon = 0.01;
2
3but_top_x = 10;
4but_top_y = but_top_x+5;
5but_top_z = 1.5;
6
7but_corner_r = 2;
8
9but_base_border = 1;
10but_base_x = but_top_x+2*but_base_border;
11but_base_y = but_top_y+2*but_base_border;
12but_base_z = 0.5;
13
14but_fillet_r = 0.4;
15but_chamfer_r = 0.2;
16
17$fn = 40;
18
19
20/* ----- Basic solids ------------------------------------------------------ */
21
22
23module torus(r0, r1)
24{
25    rotate_extrude()
26        translate([r0, 0, 0])
27        circle(r = r1);
28}
29
30
31/* ----- Helper elements for fillets --------------------------------------- */
32
33
34module fillet_line(x, r)
35{
36    translate([-x/2, 0, 0])
37        difference() {
38        cube([x, r, r]);
39        translate([0, r, r])
40            rotate([0, 90, 0])
41            translate([0, 0, -epsilon])
42            cylinder(h = x+2*epsilon, r = r);
43    }
44}
45
46
47module fillet_circle(r, fillet_r)
48{
49    difference() {
50        cylinder(h = fillet_r, r = r+fillet_r);
51        translate([0, 0, fillet_r])
52            torus(r+fillet_r, fillet_r);
53    }
54}
55
56
57/* ----- Helper elements for chamfers -------------------------------------- */
58
59
60module chamfer_line(x, r)
61{
62    translate([-x/2, -r, -r])
63        difference() {
64        cube([x, r+epsilon, r+epsilon]);
65        rotate([0, 90, 0])
66            translate([0, 0, -epsilon])
67            cylinder(h = x+2*epsilon, r = r);
68    }
69}
70
71
72module chamfer_circle(r, fillet_r)
73{
74    difference() {
75        translate([-r-epsilon, -r-epsilon, -fillet_r])
76            cube([2*(r+epsilon), 2*(r+epsilon), fillet_r+epsilon]);
77        translate([0, 0, -fillet_r])
78            cylinder(h = fillet_r, r = r-fillet_r);
79        translate([0, 0, -fillet_r])
80            torus(r-fillet_r, fillet_r);
81    }
82}
83
84
85/* ----- Box with rounded corners ------------------------------------------ */
86
87
88module rbox_core(x, y, z, r)
89{
90    union() {
91        translate([0, 0, z/2])
92            cube([x-2*r, y, z], center = true);
93        translate([0, 0, z/2])
94            cube([x, y-2*r, z], center = true);
95    }
96}
97
98
99module rbox(x, y, z, r)
100{
101    union() {
102        rbox_core(x, y, z, r);
103        for (dx = [-1, 1]) {
104            for (dy = [-1, 1]) {
105                translate([dx*(x/2-r), dy*(y/2-r), 0])
106                        cylinder(h = z, r = r);
107            }
108        }
109    }
110}
111
112
113module rbox_fillet_bottom(x, y, z, r, fillet_r)
114{
115    union() {
116        for (a = [0, 180]) {
117            rotate([0, 0, a])
118                translate([0, y/2, 0])
119                fillet_line(x-2*r, fillet_r);
120            rotate([0, 0, a+90])
121                translate([0, x/2, 0])
122                fillet_line(y-2*r, fillet_r);
123        }
124        for (dx = [-1, 1]) {
125            for (dy = [-1, 1]) {
126                translate([dx*(x/2-r), dy*(y/2-r), 0])
127                    fillet_circle(r, fillet_r);
128            }
129        }
130    }
131}
132
133
134module rbox_chamfer_top_corners(x, y, z, r, chamfer_r)
135{
136    difference() {
137        union() {
138            for (dx = [-1, 1]) {
139                for (dy = [-1, 1]) {
140                    translate([dx*(x/2-r), dy*(y/2-r), z])
141                        chamfer_circle(r, chamfer_r);
142                }
143            }
144        }
145        rbox_core(x-epsilon, y-epsilon, z, r);
146    }
147}
148
149
150module rbox_chamfer_top(x, y, z, r, chamfer_r)
151{
152    union() {
153        for (a = [0, 180]) {
154            rotate([0, 0, a])
155                translate([0, y/2, z])
156                chamfer_line(x-2*r, chamfer_r);
157            rotate([0, 0, a+90])
158                translate([0, x/2, z])
159                chamfer_line(y-2*r, chamfer_r);
160        }
161        rbox_chamfer_top_corners(x, y, z, r, chamfer_r);
162    }
163}
164
165
166module rbox_chamfer_bottom(x, y, z, r, chamfer_r)
167{
168    rotate([180, 0, 0])
169        translate([0, 0, -z])
170        rbox_chamfer_top(x, y, z, r, chamfer_r);
171}
172
173
174/* ----- Button ------------------------------------------------------------ */
175
176
177module button_top()
178{
179    union() {
180        difference() {
181            rbox(but_top_x, but_top_y, but_top_z, but_corner_r);
182            rbox_chamfer_top(but_top_x, but_top_y, but_top_z, but_corner_r, but_chamfer_r);
183        }
184        rbox_fillet_bottom(but_top_x, but_top_y, but_top_z,
185             but_corner_r, but_fillet_r);
186    }
187}
188
189
190module button_base()
191{
192    translate([0, 0, -but_base_z])
193        difference() {
194        rbox(but_base_x, but_base_y, but_base_z, but_corner_r);
195        rbox_chamfer_top(but_base_x, but_base_y, but_base_z,
196            but_corner_r, but_chamfer_r);
197        rbox_chamfer_bottom(but_base_x, but_base_y, but_base_z,
198            but_corner_r, but_chamfer_r);
199    }
200}
201
202
203module button()
204{
205    union() {
206        button_top();
207        button_base();
208    }
209}
210
211
212button();
213

Archive Download this file

Branches:
master



interactive