Root/cad/test1/button.scad

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

Archive Download this file

Branches:
master



interactive