-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabeling.scad
349 lines (304 loc) · 11.6 KB
/
labeling.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
// Labeling library
include <common.scad>
use <math_funcs.scad>
$fs = 0.1;
default_marker_radius = 1.5;
function size_label_height(marker_radius=default_marker_radius) =
marker_radius * 2;
module size_label(distance,
over=false,
marker_radius=default_marker_radius,
line_radius=0.1,
color="grey",
sigfig=2,
rotation=0) {
module size_end() {
cylinder(0.1, marker_radius, marker_radius * 0.75);
}
rounded = is_num(distance)
? round(pow(10, sigfig) * distance) / pow(10, sigfig)
: distance;
rotate([0, rotation, 0])
translate([0, 0, (over ? 1 : -1) * marker_radius + 0.01])
color(color) {
rotate([0, 90, 0]) {
size_end();
// translated in Z, because this is unrotated, and
// cylinder height is in Z
translate([0, 0, distance]) mirror([0, 0, 1]) size_end();
cylinder(distance, r=line_radius);
}
assert(abs(rotation) >= 0 && abs(rotation) <= 360,
str("size_label rotation must be between 0 and 360 (found ",
rotation, ")"));
// center text left-to-right at low angles, but move it to the
// side above 30º
halign = (abs(rotation) <= 30 ||
(abs(rotation) >= 150 && abs(rotation) <= 210) ||
abs(rotation) >= 330)
? "center"
: (((rotation < -30 && rotation > -150) ||
(rotation > 210 && rotation < 330))
? (over ? "right" : "left")
: (over ? "left" : "right"));
// move the text below or above at low angles, but put it
// right in the middle above 45º
valign = (abs(rotation) <= 45 || abs(rotation) >= 315)
? (over ? "bottom" : "top")
: ((abs(rotation) >= 135 && abs(rotation) <= 225)
? (over ? "top" : "bottom")
: "center");
translate([distance / 2, 0, (over ? 1 : -1) * marker_radius / 2])
rotate([90, -rotation, 0])
text(str(rounded),
halign=halign,
valign=valign,
size=marker_radius);
}
}
//TODO: currently only works for 0 <= |angle| <= 180
module angle_label(angle, reference_angle, size, color="grey") {
assert((-180 <= angle) && (angle <= 180));
color(color) {
rotate([0, -reference_angle, 0]) {
cube([size, 0.1, 0.1]);
difference() {
rotate([90, 0, 0])
cylinder(h=0.1, r=size * 2 / 3, center=true);
rotate([90, 0, 0])
cylinder(h=0.12, r=size * 2 / 3 - 0.1, center=true);
translate([-size, -0.06, (angle >= 0) ? -size * 2 : 0])
cube([size * 2, 0.12, size * 2]);
rotate([0, -angle, 0])
translate([-size, -0.06, (angle >= 0) ? 0 : -size])
cube([size * 2, 0.12, size]);
}
}
translate([cos(reference_angle + angle / 2) * size * 7 / 8,
0,
sin(reference_angle + angle / 2) * size * 7 / 8])
rotate([90, 0, 0])
text(str(abs(angle), "º"),
halign="center",
valign="center",
size=sin(abs(angle)) * size * 0.45);
}
}
// Each element in children_info should be a vector of three elements:
// [
// name, // String to show next to the component
// count, // Number to show in parentheses next to name
// size // vec3 of the size of the component
// ]
module key(children_info=[],
text_color="black",
spacing=size_label_height() / 2) {
function name(i) = children_info[i][0];
function count(i) = children_info[i][1];
function size(i) = children_info[i][2];
assert(len(children_info) == $children,
str("Length of key children (", $children,
") and info (", len(children_info), ") do not match."));
// midpoints for each child
heights = [ for(i = 0, h = 0;
i < $children;
h = h + size(i).z + spacing, i = i + 1) h];
text_size = 0.5 * min([for (i = [0 : $children - 1]) size(i).z]);
for (i = [0 : len(children_info) - 1]) {
translate([0, 0, heights[i]]) {
translate([-text_size / 2, 0, size(i).z / 2])
rotate([90, 0, 0])
color(text_color)
text(count(i) == undef
? name(i)
: str(name(i), " (", count(i), ")"),
halign="right",
valign="center",
size=text_size);
children(i);
}
}
}
// Create a third-angle projection of the piece, when viewed from the
// front (looking in the positive-Y direction). The object to be
// viewed should be entirely in the +X+Y+Z octant.
//
// The first arguments is the rough size of the object. The next three
// are vec3 denoting [N, E, S] how many size_labels are stacked on that
// side. These are used to determine how far to move each projected
// view.
//
// The module expects either 2, 3 or 4 children. They are:
// 1: The object to be projected
// 2: Additional elements (e.g. sizes) to be shown in the front
// projection
// 3: Additional elements for the right-side projection. If
// omitted, no right projection or top projection will be
// created (use an empty node, and set right_labels=undef if you
// want a top view but not a right view).
// 4: Additional elements for the top projection. If omitted, no
// top projection will be created.
//
// All additional elements should be created around the object,
// oriented such that they would display correctly if the view were
// simply rotated. This module will rotate them to face forward as it
// creates each projection view. The ta_right_side and ta_top_side
// modules do the correct translation, to allow you to specify
// positioning along X and Z axes, as they will be seen.
//
// The resulting third-angle projection should be viewed in Orthogonal
// mode.
ta_default_front_labels = [0,0,1];
ta_default_right_labels = [0,1,1];
ta_default_top_labels = [0,1,0];
ta_default_spacing = 1;
module third_angle(size,
front_labels=ta_default_front_labels,
right_labels=ta_default_right_labels,
top_labels=ta_default_top_labels,
spacing=ta_default_spacing) {
assert($children >= 2 && $children <=4,
str("third_angle requires 2 - 4 children ",
"- object, front, [right, top] - but passed ", $children))
// make the bottom of the bottom front label sit at zero
translate([0,
0,
size_label_height()
* (front_labels == undef ? 0 : front_labels[2])]) {
// Front view
children([0:1]);
// Right view
front_right_label_space = size_label_height()
* (front_labels == undef ? 0 : front_labels[1]);
if ($children > 2 && right_labels != undef)
translate([spacing + size.x + front_right_label_space, 0, 0])
rotate([0, 0, -90])
translate([-size.x, 0, 0])
children([0,2]);
// Top view
front_top_label_space = size_label_height() *
((front_labels == undef ? 0 : front_labels[0]) +
(top_labels == undef ? 0 : top_labels[2]));
if ($children > 3 && top_labels != undef)
translate([0, 0, spacing + size.z + front_top_label_space])
rotate([90, 0, 0])
translate([0, 0, -size.z])
children([0,3]);
}
}
module ta_right_side(x_size) {
translate([x_size, 0, 0]) rotate([0, 0, 90]) children();
}
module ta_top_side(z_size) {
translate([0, 0, z_size]) rotate([-90, 0, 0]) children();
}
function third_angle_size(size,
front_labels=ta_default_front_labels,
right_labels=ta_default_right_labels,
top_labels=ta_default_top_labels,
spacing=ta_default_spacing) =
[size.x
+ spacing
+ (right_labels == undef ? 0 : size.y)
+ (size_label_height()
* (front_labels[1] + (right_labels == undef ? 0 : right_labels[1]))),
max(size.x, size.y, size.z),
size.z
+ spacing
+ (top_labels == undef ? 0 : size.y)
+ (size_label_height()
* (front_labels[0] + front_labels[2]
+ (top_labels == undef ? 0 : (top_labels[0] + top_labels[2]))))];
module elision(size=[1,1,1]) {
difference() {
cube(size+[2,2,2]*$err, center=true);
translate(scale(size, [-0.5, 0, -0.1]))
rotate([90, 0, 0])
scale([2,1,1])
cylinder(size.y+4*$err, d=size.z * 0.2, center=true);
translate(scale(size, [0.5, 0, 0.1]))
rotate([90, 0, 0])
scale([2,1,1])
cylinder(size.y+4*$err, d=size.z * 0.2, center=true);
}
translate(scale(size, [-0.5, 0, 0.1]))
rotate([90, 0, 0])
scale([2,1,1])
cylinder(size.y+4*$err, d=size.z * 0.2, center=true);
translate(scale(size, [0.5, 0, -0.1]))
rotate([90, 0, 0])
scale([2,1,1])
cylinder(size.y+4*$err, d=size.z * 0.2, center=true);
}
// A little bar to indicate alignment.
module guide(length, r=0.05, color=[0.5, 0.5, 0.5, 0.25]) {
color(color)
cylinder(length, r, r);
}
module arrow(shaft_length=1,
head_length=0.75,
head_angle=30,
r=0.05,
color="grey") {
color(color) {
cylinder(shaft_length, r, r);
rotate([0, -head_angle, 0]) cylinder(head_length, r, r);
rotate([0, head_angle, 0]) cylinder(head_length, r, r);
}
}
// test
size_label(50);
translate([50, 0, 0]) arrow();
key([["foo", 1, [1,1,1]],
["bar", 2, [2,2,2]]]) {
cube([1,1,1]);
cube([2,2,2]);
}
translate([0, 0, 50]) third_angle([5, 2, 3]) {
cube([5, 2, 3]);
size_label(5);
ta_right_side(5) {
size_label(2);
translate([2, 0]) rotate([0, -90, 0]) size_label(3);
}
}
translate([0, 0, 70]) third_angle([5, 2, 3], top_labels=[1, 0, 1]) {
difference() {
cube([5, 2, 3]);
translate([2.5, 2, -0.01]) cylinder(3.02, r=1);
}
size_label(5);
ta_right_side(5) {
translate([0, 0, 3]) size_label(2, over=true);
translate([2, 0]) rotate([0, -90, 0]) size_label(3);
}
ta_top_side(3) {
translate([1.5, 0, 2]) size_label(2, over=true);
size_label(5);
}
}
translate([10, 0]) angle_label(30, 0, 20);
translate([10, 0]) angle_label(30, 30, 20);
translate([10, 0]) angle_label(30, -30, 20);
translate([10, 0]) angle_label(15, -90, 20);
translate([50, 0, 40]) {
rotate([90, 0, 0]) text("under, neg", size=2, halign="right");
translate([0, 0, 15])
rotate([90, 0, 0])
text("under, pos", size=2, halign="right");
translate([0, 0, 30])
rotate([90, 0, 0])
text("over, neg", size=2, halign="right");
translate([0, 0, 45])
rotate([90, 0, 0])
text("over, pos", size=2, halign="right");
for (a = [0 : 15 : 360]) {
translate([a+5, 0, -10])
rotate([90, 0, 0])
text(str(a), size=2, halign="center", valign="top");
translate([a, 0, 0]) size_label(10, over=false, rotation=-a);
translate([a, 0, 15]) size_label(10, over=false, rotation=a);
translate([a, 0, 30]) size_label(10, over=true, rotation=-a);
translate([a, 0, 45]) size_label(10, over=true, rotation=a);
}
}