Root/cad/test2/button.py

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

Archive Download this file

Branches:
master



interactive