-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.scad
358 lines (323 loc) · 10.6 KB
/
common.scad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
E = 2.7182818284590452;
// extra space, so difference() operations in 3D are rendered without
// z-fighting in the OpenSCAD preview
dif = 1;
dif_factor = 1.01;
// sometimes a large value is needed to cut certain regions
max_value = 100000;
// sometimes a small value is needed for 3D hulling between nearly 2D shapes
min_value = 0.00001;
// Acute angle between horizon and part. The smallest angle the printer
// can handle without supports.
printer_max_overhang_degrees = 55;
// TODO relate clearance to printer nozzle diameter
clearance_fit = 0.1;
clearance_medium = 0.2;
clearance_loose = 0.4;
function clamp(v, min_, max_) = min(max(v, min_), max_);
module cylinder_slice(r, h, a){
rotate_extrude(angle=a) {
square([r, h]);
}
}
// Linear extrude with the top tapered to a specifiable angle. One use case is
// extruding shapes that should be printable without supports (when rotated
// upside down).
// h is total height (including tapered section), angle_x are the degrees for
// tapering on the x axis, width_y is the extent of the children on the y axis.
module tapered_linear_extrude(h, angle_x, width_y) {
// z-height of tapered section
taper_z = tan(angle_x) * width_y;
// rotate back, put tapered section on top, translate to touch xy plane
translate([0, 0, h - taper_z]) {
mirror([0, 0, 1]) {
rotate([-angle_x, 0, 0]) {
// extrude, rotate on x, cut with plane-filling cube
render() difference() {
rotate([angle_x, 0, 0]) {
translate([0, 0, -taper_z]) {
linear_extrude(h) {
children();
}
}
}
translate([-max_value/2, -max_value/2, -max_value]) {
cube(max_value);
}
}
}
}
}
}
module pill(d, l, center=false) {
translation = center? [0, -(l-d)/2] : [d/2, d/2];
translate(translation) {
hull() {
circle(d=d);
translate([0, l - d]) {
circle(d=d);
}
}
}
}
// Two-dimensional U shape. The U shape opens towards +y.
// With u_angle, the U can be opened (<180) or closed (>180) further.
// If center=true, U bend's origin will be positioned at (0, 0, 0).
// If center_rotation=true, the shape is rotated so that the U shape opens
// towards +y even if u_angle != 180.
// Two children objects can be passed, which will be positioned at the center
// of one of the two U's tips. If children_only=true, the U shape is not drawn,
// and only the child positioning is run.
module u_shape(d_inner, t, arms_l, u_angle=180, center=true,
center_rotation=true, children_only=false) {
translation = center? [0, 0] : (d_inner/2 + t) * [1, 1];
rotation = center_rotation? [0, 0, -(u_angle - 180) / 2] : [0, 0, 0];
translate(translation) {
rotate(rotation) {
if (!children_only) {
rotate([0, 0, 180]) {
difference() {
circle_section(d_inner/2 + t, u_angle);
circle_section(d_inner/2, u_angle);
}
}
}
rotate([0, 0, u_angle - 180]) {
translate([d_inner/2, 0]) {
if (!children_only) {
square([t, arms_l]);
}
if ($children == 2) {
translate([t/2, arms_l]) {
children(0);
}
}
}
}
translate([-d_inner/2 - t, 0]) {
if (!children_only) {
square([t, arms_l]);
}
if ($children == 2) {
translate([t/2, arms_l]) {
children(1);
}
}
}
}
}
}
module closing(r) {
offset(-r) {
offset(r) {
children();
}
}
}
module opening(r) {
offset(r) {
offset(-r) {
children();
}
}
}
module circle_section(r, deg) {
// triangle section covering up to 90°
module 90_deg_segment(d) {
points = [[0, 0], [1, 0], [cos(d), sin(d)]];
polygon(points);
}
// combination of triangle sections covering up to 360°
module 360_deg_segment() {
for (i = [0:90:360]) {
if (deg > i) {
rotate([0, 0, i]) {
90_deg_segment(min(deg - i, 90));
}
}
}
}
intersection() {
circle(r);
scale(2*r) {
360_deg_segment();
}
}
}
// For 2D round chamfers, see https://forum.openscad.org/how-to-make-round-chamfer-at-2D-object-tp19714p19799.html
module round_chamfer(r=0, delta=0, chamfer=false, keep_size = false) {
if (keep_size) {
offset(r)
offset(-r)
offset(-r)
offset(r)
children();
} else {
offset(r = r, chamfer = chamfer)
children();
}
}
// Given child objects and a box-shaped approximation of their hull, apply
// rounding to a certain edge. `radius` is the rounding radius, `which_edge` is
// the edge identifier in [0,11], `object_dims` are the dimensions of the
// box surrounding the to-be-rounded object as [x,y,z].
module round_out_3d(radius, which_edge, object_dims) {
axis_index = floor(which_edge / 4);
// the length of the rounding template depends on the axis in which the edge lies
edge_len = object_dims[axis_index];
// rotate to place rounding template in the axis in which the edge lies
rot_to_axis = [[90,0,90],[-90,-90,0],[0,0,0]][axis_index];
// translations necessary for whichever axis is chosen
// example: axis 11 lies in the z-axis furthest away from the origin - therefore, the rounding template needs to be translated along the objects x and y axes
do_tr_ax1 = (which_edge % 4) >= 2? 1 : 0;
do_tr_ax2 = (which_edge % 4) % 2 == 1? 1 : 0;
// translations along an axis as far as the object is long in that axis
tr_x = [object_dims[0],0,0];
tr_y = [0,object_dims[1],0];
tr_z = [0,0,object_dims[2]];
tr_ax1 = [tr_y, tr_x, tr_x][axis_index];
tr_ax2 = [tr_z, tr_z, tr_y][axis_index];
// rotation according to which edge is chosen
rot_to_edge_angle = [0,-90,90,180][do_tr_ax1*2 + do_tr_ax2];
rot_to_edge = rot_to_edge_angle * [[1,0,0],[0,-1,0],[0,0,1]][axis_index];
difference() {
children();
// move rounding template according to edge selection
translate(do_tr_ax2 * tr_ax2)
translate(do_tr_ax1 * tr_ax1)
rotate(rot_to_edge)
rotate(rot_to_axis)
// rounding template
translate([-dif,-dif,0]) {
difference() {
translate([0,0,-dif])
cube([radius+dif, radius+dif, edge_len+2*dif]);
translate([radius+dif,radius+dif,-2*dif])
cylinder(r=radius, h=edge_len+4*dif);
}
}
}
}
// Honeycomb structure.
// Parameters: hex inner diameter, wall thickness, how many in x, how many in y
module honeycomb_2d(s, t, x, y, center=false) {
module hex() {
difference() {
circle($fn=6, r=s+t);
circle($fn=6, r=s);
}
}
module hex_column(y) {
for (yi=[0:1:y-1]) {
dx = 0;
dy = 2*cos(30)*(s+t/2);
translate(yi*[dx,dy]) {
hex();
}
}
}
module hex_grid() {
for (xi=[0:1:x-1]) {
dx = xi*3*sin(30)*(s+t/2);
dy = (xi%2 == 0? 0 : 1)*sin(60)*(s+t/2);
translate([dx, dy]) {
hex_column(y);
}
}
}
if (center) {
dx = -(x-1)/2*3*sin(30)*(s+t/2);
dy = -y*cos(30)*(s+t/2);
translate([dx, dy]) {
hex_grid();
}
} else {
hex_grid();
}
}
// Diamond structure.
// t: strut thickness
// s: horizontal inner diameter (measured at the center of the struts, i.e. assuming t=0)
// x: how many in x
// y: how many in y
// opening angle: an opening angle of 45° will produce squares standing on a corner
// invert: do not draw the struts, draw the diamonds inside the struts
module diamond_2d(s, t, x, y, opening_angle=printer_max_overhang_degrees, center=false, invert=false) {
module piece() {
hyp = 0.5 * s / cos(opening_angle);
translate([-s/2, 0]) {
rotate([0, 0, opening_angle]) {
translate([0, -t/2]) {
square([hyp, t]);
}
}
}
}
module diamond() {
mirror_copy([0, 1, 0]) {
mirror_copy([1, 0, 0]) {
piece();
}
}
}
module diamond_grid() {
// make a grid of diamonds
for (xi=[0:1:x-1]) {
for (yi=[0:1:y-1]) {
translate([xi * dx, yi * dy]) {
diamond();
}
}
}
}
dx = s;
dy = s * tan(opening_angle);
module struts() {
if (center) {
translate([(1-x)/2 * dx, (1-y)/2 * dy]) {
diamond_grid();
}
} else {
translate([dx/2, dy/2]) {
diamond_grid();
}
}
}
if (invert) {
difference() {
square([x*dx, y*dy], center=center);
struts();
}
} else {
struts();
}
}
// mirror and keep the original
module mirror_copy(v) {
mirror(v) {
children();
}
children();
}
// Cylinder with dimples in it. Can be differenced with other geometry to
// signify where a model can be grabbed or pinched.
module finger_negative_with_dimples(h, d=25, depth=0.25, dimple_r = 1.5, dimples_per_360=19) {
// vertical distance of rings depends on dimple angular distance
ring_z_dist = 0.5 * PI * d / dimples_per_360;
// number of rings depends on cylinder target height
num_rings = max(ceil(h / ring_z_dist), 1) + 1;
difference() {
cylinder(d=d, h=h);
for (ring = [0:num_rings-1]) {
for (xi = [0:dimples_per_360-1]) {
// two rows with interleaved children
z_rot_degrees = 360 * (xi + 1) + (ring % 2 == 0? 0 : 180);
rotate([0, 0, z_rot_degrees / dimples_per_360]) {
translate([d/2 + (1 - depth) * dimple_r, 0, ring * ring_z_dist]) {
sphere(dimple_r, $fn=30);
}
}
}
}
}
}