Root/cad/test1/button.py

Source at commit 2db4c1a3a80a5d22de58ed81b9932afe7ea15626 created 8 years 3 months ago.
By Werner Almesberger, cad/test1/: experiment with Free scripted CAD systems (OpenSCAD and Cadmium)
1#!/usr/bin/python
2
3# all dimensions are mm
4
5from cadmium import *
6
7
8epsilon = 0.01
9noise = epsilon/10
10
11but_top_x = 10.0
12but_top_y = but_top_x+5.0
13but_top_z = 1.5
14
15but_corner_r = 2.0
16
17but_base_border = 1.0
18but_base_x = but_top_x+2*but_base_border
19but_base_y = but_top_y+2*but_base_border
20but_base_z = 0.5
21
22but_fillet_r = 0.4
23but_chamfer_r = 0.2
24
25
26# ----- Helper elements for fillets -------------------------------------------
27
28
29def fillet_line(x, r):
30    s = Box(x, r, r)
31    s -= Cylinder(r, h = x+2*epsilon). \
32        translate(0, 0, -epsilon). \
33        rotate(Y_axis, 90). \
34        translate(0, r, r)
35    return s.translate(-x/2, 0, 0)
36
37def fillet_circle(r, fillet_r):
38    return Cylinder(r+fillet_r, h = fillet_r)- \
39        Torus(r+fillet_r, fillet_r, center = True). \
40        translate(0, 0, fillet_r)
41
42
43# ----- Helper elements for chamfers ------------------------------------------
44
45
46def chamfer_line (x, r):
47    s = Box(x, r+epsilon, r+epsilon)
48    s -= Cylinder(r, h = x+2*epsilon). \
49        translate(0, 0, -epsilon). \
50        rotate(Y_axis, 90)
51    return s.translate(-x/2, -r, -r)
52
53def chamfer_circle(r, fillet_r):
54    return Box(2*(r+epsilon), 2*(r+epsilon), fillet_r+epsilon). \
55        translate(-r-epsilon, -r-epsilon, -fillet_r)- \
56        Cylinder(r-fillet_r, h = fillet_r). \
57        translate(0, 0, -fillet_r)- \
58        Torus(r-fillet_r, fillet_r, center = True). \
59        translate(0, 0, -fillet_r)
60
61
62# ----- Box with rounded corners ----------------------------------------------
63
64
65def rbox_core(x, y, z, r):
66    return Box(x-2*r, y, z, center = True).translate(0, 0, z/2)+ \
67        Box(x, y-2*r, z, center = True).translate(0, 0, z/2)
68
69def rbox(x, y, z, r):
70    s = rbox_core(x, y, z, r)
71    for dx in [-1, 1]:
72        for dy in [-1, 1]:
73            s += Cylinder(r, h = z). \
74                translate(dx*(x/2-r), dy*(y/2-r), 0)
75    return s
76
77def rbox_fillet_bottom(x, y, z, r, fillet_r):
78    s = None
79    for a in [0, 180]:
80        t = fillet_line(x-2*r, fillet_r). \
81            translate(0, y/2, 0). \
82            rotate(Z_axis, a)
83        if s is None:
84            s = t
85        else:
86            s += t
87        s += fillet_line(y-2*r, fillet_r). \
88            translate(0, x/2, 0). \
89            rotate(Z_axis, a+90)
90    for dx in [-1, 1]:
91        for dy in [-1, 1]:
92            s += fillet_circle(r, fillet_r). \
93                translate(dx*(x/2-r), dy*(y/2-r), 0)
94    return s
95
96def rbox_chamfer_top_corners(x, y, z, r, chamfer_r):
97    s = None
98    for dx in [-1, 1]:
99        for dy in [-1, 1]:
100            t = chamfer_circle(r, chamfer_r). \
101                translate(dx*(x/2-r), dy*(y/2-r), z)
102            if s is None:
103                s = t
104            else:
105                s += t
106    return s-rbox_core(x-epsilon, y-epsilon, z, r)
107
108def rbox_chamfer_top(x, y, z, r, chamfer_r):
109    s = rbox_chamfer_top_corners(x, y, z, r, chamfer_r)
110    for a in [0, 180]:
111        s += chamfer_line(x-2*r, chamfer_r). \
112            translate(0, y/2, z+noise). \
113            rotate(Z_axis, a)
114        s += chamfer_line(y-2*r, chamfer_r). \
115            translate(0, x/2, z+noise). \
116            rotate(Z_axis, a+90)
117    return s
118
119def rbox_chamfer_bottom(x, y, z, r, chamfer_r):
120    return rbox_chamfer_top(x, y, z, r, chamfer_r). \
121        translate(0, 0, -z). \
122        rotate(X_axis, 180)
123
124# ----- Button ----------------------------------------------------------------
125
126
127def button_top():
128    return rbox(but_top_x, but_top_y, but_top_z, but_corner_r)- \
129        rbox_chamfer_top(but_top_x, but_top_y, but_top_z, \
130        but_corner_r, but_chamfer_r)+ \
131        rbox_fillet_bottom(but_top_x, but_top_y, but_top_z, but_corner_r,
132        but_fillet_r)
133
134def button_base():
135    s = rbox(but_base_x, but_base_y, but_base_z, but_corner_r)- \
136        rbox_chamfer_top(but_base_x, but_base_y, but_base_z, \
137        but_corner_r, but_chamfer_r)- \
138        rbox_chamfer_bottom(but_base_x, but_base_y, but_base_z, \
139        but_corner_r, but_chamfer_r)
140    return s.translate(0, 0, -but_base_z)
141
142def button():
143    return button_top()+button_base()
144
145b = button()
146b.toSTL("button.stl")
147

Archive Download this file

Branches:
master



interactive