Root/cad/test2/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        translate([0, -epsilon, -epsilon])
39            cube([x, r+epsilon, r+epsilon]);
40        translate([0, r, r])
41            rotate([0, 90, 0])
42            translate([0, 0, -epsilon])
43            cylinder(h = x+2*epsilon, r = r);
44    }
45}
46
47
48module fillet_circle(r, fillet_r)
49{
50    difference() {
51        cylinder(h = fillet_r, r = r+fillet_r);
52        translate([0, 0, fillet_r])
53            torus(r+fillet_r, fillet_r);
54    }
55}
56
57
58/* ----- Helper elements for chamfers -------------------------------------- */
59
60
61module chamfer_line(x, r)
62{
63    translate([-x/2, -r, -r])
64        difference() {
65        cube([x, r+epsilon, r+epsilon]);
66        rotate([0, 90, 0])
67            translate([0, 0, -epsilon])
68            cylinder(h = x+2*epsilon, r = r);
69    }
70}
71
72
73module chamfer_circle(r, fillet_r)
74{
75    difference() {
76        translate([-r-epsilon, -r-epsilon, -fillet_r])
77            cube([2*(r+epsilon), 2*(r+epsilon), fillet_r+epsilon]);
78        translate([0, 0, -fillet_r])
79            cylinder(h = fillet_r, r = r-fillet_r);
80        translate([0, 0, -fillet_r])
81            torus(r-fillet_r, fillet_r);
82    }
83}
84
85
86/* ----- Box with rounded corners ------------------------------------------ */
87
88
89module rbox_core(x, y, z, r)
90{
91    union() {
92        translate([0, 0, z/2])
93            cube([x-2*r, y, z], center = true);
94        translate([0, 0, z/2])
95            cube([x, y-2*r, z], center = true);
96    }
97}
98
99
100module rbox(x, y, z, r)
101{
102    union() {
103        rbox_core(x, y, z, r);
104        for (dx = [-1, 1]) {
105            for (dy = [-1, 1]) {
106                translate([dx*(x/2-r), dy*(y/2-r), 0])
107                        cylinder(h = z, r = r);
108            }
109        }
110    }
111}
112
113
114module rbox_fillet_bottom(x, y, z, r, fillet_r)
115{
116    union() {
117        for (a = [0, 180]) {
118            rotate([0, 0, a])
119                translate([0, y/2, 0])
120                fillet_line(x-2*r, fillet_r);
121            rotate([0, 0, a+90])
122                translate([0, x/2, 0])
123                fillet_line(y-2*r, fillet_r);
124        }
125        for (dx = [-1, 1]) {
126            for (dy = [-1, 1]) {
127                translate([dx*(x/2-r), dy*(y/2-r), 0])
128                    fillet_circle(r, fillet_r);
129            }
130        }
131    }
132}
133
134
135module rbox_chamfer_top_corners(x, y, z, r, chamfer_r)
136{
137    difference() {
138        union() {
139            for (dx = [-1, 1]) {
140                for (dy = [-1, 1]) {
141                    translate([dx*(x/2-r), dy*(y/2-r), z])
142                        chamfer_circle(r, chamfer_r);
143                }
144            }
145        }
146        rbox_core(x-epsilon, y-epsilon, z, r);
147    }
148}
149
150
151module rbox_chamfer_top(x, y, z, r, chamfer_r)
152{
153    union() {
154        for (a = [0, 180]) {
155            rotate([0, 0, a])
156                translate([0, y/2, z])
157                chamfer_line(x-2*r, chamfer_r);
158            rotate([0, 0, a+90])
159                translate([0, x/2, z])
160                chamfer_line(y-2*r, chamfer_r);
161        }
162        rbox_chamfer_top_corners(x, y, z, r, chamfer_r);
163    }
164}
165
166
167module rbox_chamfer_bottom(x, y, z, r, chamfer_r)
168{
169    rotate([180, 0, 0])
170        translate([0, 0, -z])
171        rbox_chamfer_top(x, y, z, r, chamfer_r);
172}
173
174
175/* ----- Button ------------------------------------------------------------ */
176
177
178module button_top()
179{
180    union() {
181        difference() {
182            rbox(but_top_x, but_top_y, but_top_z, but_corner_r);
183            rbox_chamfer_top(but_top_x, but_top_y, but_top_z, but_corner_r, but_chamfer_r);
184        }
185        rbox_fillet_bottom(but_top_x, but_top_y, but_top_z,
186             but_corner_r, but_fillet_r);
187    }
188}
189
190
191module button_base()
192{
193    translate([0, 0, -but_base_z])
194        difference() {
195        rbox(but_base_x, but_base_y, but_base_z+epsilon, but_corner_r);
196        rbox_chamfer_top(but_base_x, but_base_y, but_base_z+epsilon,
197            but_corner_r, but_chamfer_r);
198        rbox_chamfer_bottom(but_base_x, but_base_y, but_base_z+epsilon,
199            but_corner_r, but_chamfer_r);
200    }
201}
202
203
204module button()
205{
206    union() {
207        button_top();
208
209        /* Comment out this line to make CSG previews work */
210        button_base();
211    }
212}
213
214
215button();
216

Archive Download this file

Branches:
master



interactive