forked from larsch/lasercut-box-openscad
-
Notifications
You must be signed in to change notification settings - Fork 2
/
box.scad
447 lines (406 loc) · 11.1 KB
/
box.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
w_divider_color = "CadetBlue";
h_divider_color = "CornflowerBlue";
front_color = "RoyalBlue";
back_color = "RoyalBlue";
bottom_color = "SlateBlue";
top_color = "SlateBlue";
left_color = "MediumAquamarine";
right_color = "MediumAquamarine";
e = 0.01;
module box(width, height, depth, thickness,
finger_width, // (default = 2 * thickness)
finger_margin, // (default = 2 * thickness)
inner = false,
open = false,
open_bottom = false,
inset = 0,
dividers = [ 0, 0 ],
holes = [],
hole_dia = 0,
ears = 0,
assemble = false,
hole_width = false,
kerf = 0.07,
labels = false,
finger_cutout_radius = 0,
finger_cutout_offset = 0,
airgap_radius = 0,
airgap_width = 0,
explode = 0,
spacing = 0)
{
w = inner ? width + 2 * thickness : width;
h = inner ? height + 2 * thickness : height;
d = inner ? depth + 2 * thickness : depth;
t = thickness;
hm = h - inset;
fm = (finger_margin == undef) ? thickness * 2 : finger_margin;
fw = (finger_width == undef) ? thickness * 2 : finger_width;
keep_top = !open;
keep_bottom = !open_bottom;
kc = kerf / 2;
ears_radius = ears;
ears_width = 3;
// Kerf compensation modifier
module compkerf() { offset(delta = kc) children(); }
// 2D panels with finger cuts
module left() { cut_left() panel2d(d, h); }
module right() { cut_right() panel2d(d, h); }
module top() {
if (ears_radius > 0) {
difference() {
panel2d(w, d);
translate([t, d-t+e]) panel2d(2*t, t);
translate([t, -e]) panel2d(2*t, t);
}
} else {
cut_top() panel2d(w, d);
}
}
module bottom() { cut_bottom() panel2d(w, d); }
module ears_outer(is_front) {
translate([is_front ? 0 : w, h])
circle(ears_radius, [0, 0]);
}
module ears_inner(is_front) {
translate([is_front ? 0 : w, h])
difference() {
circle(ears_radius-ears_width, [0, 0]);
square([t, t]);
}
}
module back() {
cut_back() difference() {
union() {
panel2d(w, h);
if (ears_radius > 0)
ears_outer(false);
}
if (len(holes) > 0)
for (i = [ 0 : len(holes)-1 ])
hole([w-holes[i][0], holes[i][1]]);
if (ears_radius > 0)
ears_inner(false);
if (airgap_radius) {
union() {
translate([w/2 - airgap_width/2, h]) circle(r = airgap_radius);
translate([w/2 + airgap_width/2, h]) circle(r = airgap_radius);
translate([w/2 - airgap_width/2, h - airgap_radius]) square(airgap_width, airgap_radius);
}
}
}
}
module hole(center) {
translate(center) circle(d = hole_dia);
}
module sector(radius, angles, fn = 24) {
r = radius / cos(180 / fn);
step = -360 / fn;
points = concat([[0, 0]],
[for(a = [angles[0] : step : angles[1] - 360])
[r * cos(a), r * sin(a)]
],
[[r * cos(angles[1]), r * sin(angles[1])]]
);
difference() {
circle(radius, $fn = fn);
polygon(points);
}
}
module front() {
cut_front() difference() {
union()
{
panel2d(w, h);
if (ears_radius > 0)
ears_outer(true);
}
if (len(holes) > 0)
for (i = [ 0 : len(holes)-1 ])
hole(holes[i]);
if (ears_radius > 0)
ears_inner(true);
if (finger_cutout_radius > 0)
translate([w/2, h - finger_cutout_offset])
sector(finger_cutout_radius, [180, 360], 64);
}
}
module w_divider() {
difference() {
cut_w_divider() translate([0, t, 0]) panel2d(w, h-t);
if (airgap_radius) {
union() {
translate([w/2 - airgap_width/2, h]) circle(r = airgap_radius);
translate([w/2 + airgap_width/2, h]) circle(r = airgap_radius);
translate([w/2 - airgap_width/2, h - airgap_radius])
square(airgap_width, airgap_radius);
}
}
}
}
module h_divider() { cut_h_divider() translate([0, t, 0]) panel2d(d, h-t); }
// Panels positioned in 3D
module front3d() {
translate([0,t-explode,0])
rotate(90, [1,0,0])
panelize(w,h, "Front", front_color)
front();
}
module back3d() {
translate([w,d-t+explode,0])
rotate(-90, [1,0,0])
rotate(180,[0,0,1])
panelize(w, h, "Back", back_color)
back();
}
module bottom3d() {
translate([w,0,t-explode])
rotate(180,[0,1,0])
panelize(w, d, "Bottom", bottom_color)
bottom();
}
module top3d() {
translate([0, 0, h-t+explode+(ears_radius > 0 ? t : 0)])
panelize(w, d, "Top", top_color)
top();
}
module left3d() {
translate([t-explode,d,0])
rotate(-90,[0,1,0])
rotate(-90,[0,0,1])
panelize(d, h, "Left", left_color)
left();
}
module right3d() {
translate([w-t+explode,0,0])
rotate(90,[0,1,0])
rotate(90,[0,0,1])
panelize(d, h, "Right", right_color)
right();
}
module w_divider3d() {
if (dividers[0] > 0) {
ndivs = dividers[0];
for (i = [ 1 : 1 : ndivs ])
translate([0, d/(ndivs+1)*i+t/2,0])
rotate(90, [1,0,0])
panelize(w,h, "Divider", w_divider_color)
w_divider();
}
}
module h_divider3d() {
translate([0,0,explode > e && dividers[0] > 0 ? h + explode : explode])
if (dividers[1] > 0) {
ndivs = dividers[1];
for (i = [1 : 1 : ndivs])
translate([w/(ndivs+1)*i-t/2,0,0])
rotate(90, [1,0,0])
rotate(90, [0,1,0])
panelize(d,h, "Divider", h_divider_color)
h_divider();
}
}
module w_dividers() {
if (dividers[0] > 0) {
ndivs = dividers[0];
for (i = [0 : 1 : ndivs-1])
translate([i*(w+e)+spacing*(i+1),0,0])
w_divider();
}
}
module h_dividers() {
if (dividers[1] > 0) {
ndivs = dividers[1];
for (i = [0 : 1 : ndivs-1])
translate([i*(d+e)+spacing*(i+1),0,0])
h_divider();
}
}
// Panelized 2D rendering for cutting
module box2d() {
compkerf() front();
x1 = w + kc * 2 + e + spacing;
translate([x1,0]) compkerf() back();
x2 = x1 + w + 2 * kc + e + ears_radius + spacing;
translate([x2,0]) compkerf() left();
x3 = x2 + d + 2 * kc + e + spacing;
translate([x3,0]) compkerf() right();
y1 = h + kc * 2 + e + ears_radius + spacing;
if (keep_bottom) {
x4 = 0;
translate([x4,y1]) compkerf() bottom();
}
if (keep_top) {
x5 = w + 2 * kc + e + spacing;
translate([x5,y1]) compkerf() top();
}
x6 = w + 2 * kc + (keep_top ? w+e : 0) + e + spacing;
translate([x6,y1]) compkerf() w_dividers();
translate([x6+kerf,y1 + (dividers[0] > 0 ? y1 : 0)]) compkerf() h_dividers();
}
// Assembled box in 3D
module box3d() {
front3d();
back3d();
if (keep_bottom)
translate([0,0,inset]) bottom3d();
if (keep_top)
top3d();
left3d();
right3d();
w_divider3d();
h_divider3d();
}
// Finger cutting operators
module cut_front() {
difference() {
children();
if (keep_bottom) translate([0,inset]) cuts(w);
if (keep_top && (ears_radius == 0)) movecutstop(w, h) cuts(w);
movecutsleft(w, h) cuts(h);
movecutsright(w, h) cuts(h);
if (dividers[1] > 0) {
ndivs = dividers[1];
for (i = [1 : 1 : ndivs])
movecuts(w/(ndivs+1)*i-t/2, 0) cuts(h, li = thickness*2);
}
holecuts();
}
}
module cut_w_divider() {
difference() {
children();
movecutsleft(w, h) invcuts(h, ri = thickness*2);
movecutsright(w, h) invcuts(h, li = thickness*2);
if (dividers[1] > 0) {
ndivs = dividers[1];
for (i = [1 : 1 : ndivs])
movecuts(w/(ndivs+1)*i-t/2, h/2) square([h / 2, thickness]);
}
holecuts();
}
}
module cut_h_divider() {
difference() {
children();
movecutsleft(d, h) invcuts(h, ri = thickness*2);
movecutsright(d, h) invcuts(h, li = thickness*2);
if (dividers[0] > 0) {
ndivs = dividers[0];
for (i = [1 : 1 : ndivs])
movecuts(d/(ndivs+1)*i-t/2, 0) square([h / 2, thickness]);
}
holecuts();
}
}
module cut_top() {
difference() {
children();
invcuts(w);
movecutstop(w, d) invcuts(w);
movecutsleft(w, d) translate([t,0]) invcuts(d-2*t);
movecutsright(w, d) translate([t,0]) invcuts(d-2*t);
}
}
module cut_left() {
difference() {
children();
if (keep_bottom) translate([t,inset]) cuts(d-2*t);
if (keep_top && (ears_radius == 0)) movecutstop(d, h) translate([t,0]) cuts(d-2*t);
movecutsleft(d, h) invcuts(h);
movecutsright(d, h) invcuts(h);
if (dividers[0] > 0) {
ndivs = dividers[0];
for (i = [1 : 1 : ndivs])
movecuts(d/(ndivs+1)*i-t/2, 0) cuts(h, li = thickness*2);
}
}
}
module cut_bottom() { cut_top() children(); }
module cut_right() { cut_left() children(); }
module cut_back() { cut_front() children(); }
// Handle hole
module holecuts() {
if (hole_width) {
r = hole_height / 2;
hull() {
translate([w/2 - hole_width/2 + r, h - hole_margin - r])
circle(r = r);
translate([w/2 + hole_width/2 - r, h - hole_margin - r])
circle(r = r);
}
}
}
// Finger cuts (along x axis)
module cuts(tw, li = 0, ri = 0, full = false) {
w = tw - li - ri;
innerw = w - t*2 - 2 * fm;
tc1 = floor((innerw / fw - 1) / 2);
tc = (tc1 < 0) ? 0 : tc1;
steps = tc * 2 + 1;
fw_fitting = innerw / steps;
// Use a default finger if we cant fit one within margins
fw1 = (innerw < fw) ? fw : fw_fitting;
// Divide length by 3 if we can fit less that 3 fingers
fw2 = (tw < fw * 3) ? (tw / 3) : fw1;
stepsize = fw2;
x = (w - steps * stepsize) / 2;
fw_minimum = t;
if (tw >= 3 * fw_minimum) {
translate([li,0])
for (i = [0:tc]) {
translate([x+i*stepsize*2,-e])
square([stepsize, t+e]);
}
}
}
// Inverse finger cuts (along x axis)
module invcuts(w, li = 0, ri = 0, full = true) {
difference() {
translate([-2*e,-e]) square([w+4*e,t+e]);
cuts(w, li, ri, full);
}
}
// Finger cut positioning operators
module movecutstop(w, h) {
translate([w,h,0])
rotate(180,[0,0,1])
children();
}
module movecutsleft(w, h) {
translate([0, h/2])
rotate(-90, [0,0,1])
translate([-h/2,0])
children();
}
module movecutsright(w, h) {
translate([w-t,0])
rotate(90,[0,0,1])
translate([0,-t])
children();
}
module movecuts(w, h) {
translate([w, h])
rotate(90,[0,0,1])
translate([0,-t])
children();
}
// Turn 2D Panel into 3D
module panelize(x, y, name, cl) {
color(cl)
linear_extrude(height = t)
children();
if (labels) {
color("Yellow")
translate([x/2,y/2,t+1])
text(text = name, halign = "center", valign="center");
}
}
module panel2d(x, y) {
square([x,y]);
}
if (assemble)
box3d();
else
box2d();
}