-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.scad
174 lines (150 loc) · 5.34 KB
/
utils.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
// Copyright (c) 2017 Clément Bœsch <u pkh.me>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
include <globals.scad>
function unknown_info() = [
["category", "unknown"],
["watch", "nowhere"],
];
function unknown_drill_info() = [
["category", "unknown"],
["watch", "horizon"],
];
function in2mm(v) = 2.54 * v / 100;
function pmul(v,m) = [for (i=[0:len(v)-1]) v[i] * m[i]];
function map_get(data, key) =
let(s = search([key], data)[0])
s == [] ? s : data[s][1];
function default_top_vents(board_dim, s=.3) = [
["dim", board_dim * s],
["pos", [0, 0]],
];
function default_bottom_vents(board_dim, s=.7) = default_top_vents(board_dim, s);
function default_vents(board_dim, bottom_s=.7, top_s=.3) = [
["top", default_top_vents(board_dim, top_s)],
["bottom", default_bottom_vents(board_dim, bottom_s)],
];
module set_component(board_dim, comp_info, debug=false) {
dim = comp_info[1];
corner_comp = comp_info[2];
rot = comp_info[3];
corner_board = comp_info[4];
position = comp_info[5];
corner_pos = pmul(corner_comp, dim) * -.5;
board_corner_pos = pmul(corner_board, board_dim) * .5;
tr = position + board_corner_pos;
if (debug) {
translate(tr) {
color("blue") cylinder(h=20, d=.25, center=true);
color("green") rotate([90, 0, 0]) cylinder(h=20, d=.25, center=true);
color("red") rotate([0, 90, 0]) cylinder(h=20, d=.25, center=true);
color("pink") sphere(.5);
}
}
translate(tr)
rotate(rot * 90)
translate(corner_pos)
children(0);
}
module set_components(board_dim, comp_info_list, debug=false) {
for (i = [0:len(comp_info_list)-1])
set_component(board_dim, comp_info_list[i], debug)
children(i);
}
module bounding_box(board_dim, comp_info_list, padding, extruded_dim=50) {
for (comp_info = comp_info_list) {
info = comp_info[0];
dim = comp_info[1];
watch = map_get(info, "watch");
category = map_get(info, "category");
/* extrude mask and its inverse */
ex_mask = [watch == "horizon" ? 1 : 0, 0, watch == "sky" ? 1 : 0];
ex_mask_inv = [for (i=[0:len(ex_mask)-1]) 1-ex_mask[i]];
set_component(board_dim, comp_info) {
box_dim = pmul(ex_mask_inv, dim) + ex_mask*extruded_dim;
all_tr = [extruded_dim-dim[0], 0, extruded_dim-dim[2]] * .5;
box_tr = pmul(all_tr, ex_mask);
translate(box_tr) {
minkowski() {
cube(box_dim, center=true);
sphere(d=2*padding, center=true);
}
}
}
}
}
module demo_board(board_dim) {
rotate([0, 0, $t*360])
children();
}
module plate_2d(l, w, corner_radius=0) {
if (!corner_radius) {
square([l, w], center=true);
} else {
translate([corner_radius - l/2, corner_radius - w/2]) {
minkowski() {
square([l - corner_radius*2, w - corner_radius*2]);
circle(r=corner_radius);
}
}
}
}
module _hole_positions(holes_pos) {
for (pos = holes_pos)
translate(pos)
children();
}
module extrude_plate(height, holes_pos=[], hole_d=1, ring_off=0, clr=c_darkgray, clr_ring=c_gold) {
translate([0, 0, -height/2]) {
color(clr) {
linear_extrude(height) {
difference() {
children(0);
_hole_positions(holes_pos)
circle(d=hole_d + ring_off);
}
}
}
if (ring_off) {
color(clr_ring) {
linear_extrude(height) {
_hole_positions(holes_pos) {
difference() {
circle(d=hole_d + ring_off);
circle(d=hole_d);
}
}
}
}
}
}
}
module stack_animate(time, pad=40, axis=[0,0,1]) {
function slope_a(x1, y1, x2, y2) = (y1 - y2) / (x1 - x2);
function slope_b(x1, y1, x2, y2) = (x1*y2 - x2*y1) / (x1 - x2);
function clip_time(t, mint, maxt) = min(max(t, mint), maxt);
for (i = [0:$children-1]) {
start_pos = (i + 1) * pad;
end_pos = 0;
start_time = i / $children;
end_time = (i + 1) / $children;
a = slope_a(start_time, start_pos, end_time, end_pos);
b = slope_b(start_time, start_pos, end_time, end_pos);
t = clip_time(time, start_time, end_time);
translate(axis * (a*t + b))
children(i);
}
}
module filter(id) {
children(id);
}