Source

boxer / boxer

  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
#!/usr/bin/perl
# Generate SVG files with a specified number of sides and tabs
# to make a self-locking acrylic or wooden laser cut box.
#
# (c) 2012 Trammell Hudson <hudson@osresearch.net>
#
use warnings;
use strict;
use Math::Trig;
use Getopt::Long;

my $units = "mm"; # mm = 1, inches = 1000 / 25.4
my $thickness = 3;
my $sides = 4;
my $length = 50;
my $width = 30;
my $height = 20;
my $tab_width = 6;
my $kerf = 0.1;

my $usage = <<"";
Usage: boxer [options] > box.svg
Options:
	-h | -? | --help	This help
	-t | --thickness N	Material thickness in mm
	-H | --height N		Height of the box, in mm
	-w | --width N		Outside edge length, in mm
	-l | --length N		Outside edge length, in mm for rectangles
	-k | --kerf N		Kerf in mm (typically 0.1)


GetOptions(
	'h|?|help'		=> sub { print $usage; exit 0 },
	't|thickness=f'		=> \$thickness,
	'H|height=f'		=> \$height,
	'w|width=f'		=> \$width,
	'l|length=f'		=> \$length,
	'k|kerf=f'		=> \$kerf,
) or die $usage;

die "tab should be wider than the material thickness\n"
	if $thickness > $tab_width;

my $num_x_tabs = int(($width - $thickness*2) / ($tab_width * 2));
my $num_y_tabs = int(($length - $thickness*2) / ($tab_width * 2));
my $num_z_tabs = int(($height - $thickness*2) / ($tab_width * 2));
die "tab width $tab_width too large for width $width\n"
	if $num_x_tabs == 0;
die "tab width $tab_width too large for length $length\n"
	if $num_y_tabs == 0;
die "tab width $tab_width too large for height $height\n"
	if $num_z_tabs == 0;

my $spacing = $kerf * 4;

# Compute the height of the interior triangle
#my $interior_angle = 360.0 / $sides;
#my $interior_len = $edge - $thickness * 2;
#my $interior_radius = ($interior_len/2) / tan(deg2rad($interior_angle/2));


print <<"";
<!-- Created with boxer (http://trmm.net/) -->
<svg xmlns="http://www.w3.org/2000/svg">
<g transform="scale(3.543307)"><!-- scale to mm -->


sub make_translate
{
	my ($x,$y) = @_;
	return "translate($x,$y)";
}


sub make_path
{
	my $rc = <<"";
	<path
		stroke		= "#ff0000"
		fill		= "none"
		stroke-width	= "0.1px"
		d		= "M

	for my $pt (@_)
	{
		my ($x,$y) = @$pt;
		$rc .= "$x,$y\n";
	}

	$rc .= <<"";
	"/>

	return $rc;
}


#
# Applies a transform to a list of SVG objects (paths or other groups)
# Returns a SVG group
#
sub make_group
{
	my $transform = shift;

	my $rc = <<"";
	<g
		transform	= "$transform"
	>

	$rc .= join '\n', @_;
	$rc .= <<"";
	</g>

	return $rc;
}


#
# Generate the flat top and bottom pieces.
# Returns a SVG path
#
sub make_top
{
	my $rc;

	my @points;

	# Bottom edge
	push @points, [$thickness, $thickness];
	my $x = $tab_width;
	for my $n (1..$num_x_tabs)
	{
		push @points,
			[$x - $kerf, $thickness],
			[$x - $kerf, 0],
			[$x + $tab_width + $kerf, 0],
			[$x + $tab_width + $kerf, $thickness],
			;
		$x += $tab_width * 2;
	}

	# Right edge
	push @points, [$width - $thickness, $thickness];
	my $y = $tab_width;
	for my $n (1..$num_y_tabs)
	{
		push @points,
			[$width - $thickness, $y - $kerf],
			[$width, $y - $kerf],
			[$width, $y + $tab_width + $kerf],
			[$width - $thickness, $y + $tab_width + $kerf],
			;
		$y += $tab_width * 2;
	}

	# Top edge
	push @points, [$width - $thickness, $length - $thickness];
	$x = $width - $tab_width;
	for my $n (1..$num_x_tabs)
	{
		push @points,
			[$x + $kerf, $length - $thickness],
			[$x + $kerf, $length],
			[$x - $tab_width - $kerf, $length],
			[$x - $tab_width - $kerf, $length - $thickness],
			;
		$x -= $tab_width * 2;
	}

	# Left edge
	push @points, [$thickness, $length - $thickness];
	$y = $length - $tab_width;
	for my $n (1..$num_y_tabs)
	{
		push @points,
			[$thickness, $y + $kerf],
			[0, $y + $kerf],
			[0, $y - $tab_width - $kerf],
			[$thickness, $y - $tab_width - $kerf],
			;
		$y -= $tab_width * 2;
	}

	# Close the path
	push @points, [$thickness, $thickness];

	return make_path(@points);
}


sub make_side
{
	my ($x_dim, $num_x_tabs) = @_;
	my @points;

	# Bottom edge
	push @points, [0,0];
	my $x = $tab_width;
	for my $n (1..$num_x_tabs)
	{
		push @points,
			[$x - $kerf, 0],
			[$x + $kerf, $thickness],
			[$x - $kerf + $tab_width, $thickness],
			[$x + $kerf + $tab_width, 0],
			;
		$x += $tab_width * 2;
	}

	# Height edge
	push @points, [$x_dim - $thickness, 0];
	my $z = $tab_width;
	for my $n (1..$num_z_tabs)
	{
		push @points,
			[$x_dim - $thickness, $z - $kerf],
			[$x_dim, $z - $kerf],
			[$x_dim, $z + $tab_width + $kerf],
			[$x_dim - $thickness, $z + $tab_width + $kerf],
			;
		$z += $tab_width * 2;
	}

	# Top eddge
	push @points, [$x_dim - $thickness, $height];
	$x = $num_x_tabs * $tab_width * 2;
	for my $n (1..$num_x_tabs)
	{
		push @points,
			[$x + $kerf, $height],
			[$x - $kerf, $height - $thickness],
			[$x + $kerf - $tab_width, $height - $thickness],
			[$x - $kerf - $tab_width, $height],
			;
		$x -= $tab_width * 2;
	}

	# Height edge
	push @points, [0, $height];
	$z = $num_z_tabs * $tab_width * 2;
	for my $n (1..$num_z_tabs)
	{
		push @points,
			[0, $z + $kerf],
			[$thickness, $z - $kerf],
			[$thickness, $z + $kerf - $tab_width],
			[0, $z - $kerf - $tab_width],
			;
		$z -= $tab_width * 2;
	}

	# Close it back to the bottom
	push @points, [0,0];

	return make_path(@points);
}

print make_group("translate(0,0)", make_top());
print make_group("translate(" . ($width+$kerf*2) .",0)", make_top());
print make_group(
	make_translate(0, $length + $spacing),
	make_side($width, $num_x_tabs)
);
print make_group(
	make_translate($width + $spacing, $length + $spacing),
	make_side($width, $num_x_tabs)
);

print make_group(
	make_translate(0, $length + $height + $spacing * 2),
	make_side($length, $num_y_tabs)
);
print make_group(
	make_translate($length + $spacing, $length + $height + $spacing * 2),
	make_side($length, $num_y_tabs)
);

print <<"";
</g>
</svg>

__END__


#
# Draw the top (and bottom piece)
#
print <<"";
<g transform="translate($edge $edge)">

for my $side (0..$sides-1)
{
	my $angle = (360 / $sides) * $side;
	my $hx = $interior_len / 2;

	# Put one tab in the middle
	my $tab_x1 = $edge/2 - $tab_width/2 - $kerf - $thickness/2;
	my $tab_x2 = $edge/2 + $tab_width/2 + $kerf - $thickness/2;

	print <<"";
	<g
		transform="rotate(-$angle) translate(-$hx $interior_radius)"
	><path
		stroke="#ff0000"
		fill="none"
		stroke-width="0.1px"
		d="M

	print <<"";
		0 0
		$tab_x1 0
		$tab_x1 $thickness
		$tab_x2 $thickness
		$tab_x2 0
		$interior_len 0

	print <<"";
	"/></g>

}

print <<"";
</g>

#
# Draw the edge pieces
#
print <<"";
<g transform="translate(0 @{[$edge*2]})">

for my $side (0..$sides-1)
{
	my $origin_x = $side * ($edge + $thickness + 2);

	# Put one tab in the middle of the top and bottom edges
	# Make it smaller than the kerf for a tighter fit with the
	# prodruting tab on the top/bottom pieces
	my $top_tab_x1 = $edge/2 - $tab_width/2 + $kerf - $thickness/2;
	my $top_tab_x2 = $edge/2 + $tab_width/2 - $kerf - $thickness/2;

	my $bottom_tab_x1 = $edge/2 - $tab_width/2 + $kerf + $thickness/2;
	my $bottom_tab_x2 = $edge/2 + $tab_width/2 - $kerf + $thickness/2;

	# Put one tab in the middle of the left edge.  It goes in,
	# so make it smaller than than the kerf.
	my $left_tab_x1 = $height/2 - $tab_width/2 + $kerf;
	my $left_tab_x2 = $height/2 + $tab_width/2 - $kerf;

	# Put one tab in the middle of the right edge.  it goes out,
	# so make it larger than the kerf.
	my $right_tab_x1 = $height/2 - $tab_width/2 - $kerf;
	my $right_tab_x2 = $height/2 + $tab_width/2 + $kerf;

	print <<"";
	<g transform="translate($origin_x 0)">

	print <<"";
	<!-- bottom edge -->
	<path
		transform="translate(0 0) rotate(0 0 0)"
		stroke="#ff0000"
		fill="none"
		stroke-width="0.1px"
		d="M
			$thickness 0
			@{[$bottom_tab_x1-$kerf]} 0
			$bottom_tab_x1 -$thickness
			$bottom_tab_x2 -$thickness
			@{[$bottom_tab_x2+$kerf]} 0
			$edge 0
		"
	/>

	print <<"";
	<!-- right side edge; tab goes out-->
	<path
		transform="translate($edge 0) rotate(-90 0 0)"
		stroke="#ff0000"
		stroke-width="0.1px"
		fill="none"
		d="M
			0 0
			$right_tab_x1 0
			$right_tab_x1 $thickness
			$right_tab_x2 $thickness
			$right_tab_x2 0
			$height 0
		"
	/>

	print <<"";
	<!-- top edge -->
	<path
		transform="translate($edge -$height) rotate(-180 0 0)"
		stroke="#ff0000"
		stroke-width="0.1px"
		fill="none"
		d="M
			0 0
			@{[$top_tab_x1-$kerf]} 0
			$top_tab_x1 -$thickness
			$top_tab_x2 -$thickness
			@{[$top_tab_x2+$kerf]} 0
			@{[$edge - $thickness]} 0
		"
	/>

	print <<"";
	<!-- left side edge; tab goes in-->
	<path
		transform="translate($thickness -$height) rotate(-270 0 0)"
		stroke="#ff0000"
		stroke-width="0.1px"
		fill="none"
		d="M
			0 0
			@{[$left_tab_x1-$kerf]} 0
			$left_tab_x1 -$thickness
			$left_tab_x2 -$thickness
			@{[$left_tab_x2+$kerf]} 0
			$height 0
		"
	/>

	print <<"";
	</g>

}

print <<"";
</g>


print <<"";
</g>
</svg>

__END__
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.