-
Notifications
You must be signed in to change notification settings - Fork 46
/
boardgame_insert_toolkit_lib.2.scad
2712 lines (2195 loc) · 105 KB
/
boardgame_insert_toolkit_lib.2.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 MysteryDough https://www.thingiverse.com/MysteryDough/
//
// Released under the Creative Commons - Attribution - Non-Commercial - Share Alike License.
// https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
VERSION = "2.45";
COPYRIGHT_INFO = "\tThe Boardgame Insert Toolkit\n\thttps://github.com/IdoMagal/The-Boardgame-Insert-Toolkit\n\n\tCopyright 2020 Ido Magal\n\tCreative Commons - Attribution - Non-Commercial - Share Alike.\n\thttps://creativecommons.org/licenses/by-nc-sa/4.0/legalcode";
fn = $preview ? 10 : 100;
$fn = fn;
// constants
k_key = 0;
k_value = 1;
epsilon = $preview ? 0.02 : 0; // extend cuts by a bit to fight z-fighting during preview
k_x = 0;
k_y = 1;
k_z = 2;
k_front = 0;
k_back = 1;
k_left = 2;
k_right = 3;
k_front_left = 0;
k_back_right = 1;
k_back_left = 2;
k_front_right = 3;
t = true;
f = false;
///////////////////////
// PARAMETER KEYWORDS
TYPE = "type";
BOX = "box";
DIVIDERS = "dividers";
SPACER = "spacer";
BOX_LID = "box_lid";
DIV_THICKNESS = "div_thickness";
DIV_TAB_SIZE_XY = "div_tab_size";
DIV_TAB_RADIUS = "div_tab_radius";
DIV_TAB_CYCLE = "div_tab_cycle";
DIV_TAB_CYCLE_START = "div_tab_cycle_start";
DIV_TAB_TEXT = "div_tab_text";
DIV_TAB_TEXT_SIZE = "DIV_TAB_TEXT_size";
DIV_TAB_TEXT_FONT = "DIV_TAB_TEXT_font";
DIV_TAB_TEXT_SPACING = "DIV_TAB_TEXT_spacing";
DIV_TAB_TEXT_CHAR_THRESHOLD = "DIV_TAB_TEXT_char_threshold";
DIV_FRAME_SIZE_XY = "div_frame_size";
DIV_FRAME_TOP = "div_frame_top";
DIV_FRAME_BOTTOM = "div_frame_bottom";
DIV_FRAME_COLUMN = "div_frame_column";
DIV_FRAME_RADIUS = "div_frame_radius";
DIV_FRAME_NUM_COLUMNS = "div_frame_num_columns";
// BOX PARAMETERS
BOX_SIZE_XYZ = "box_size";
BOX_COMPONENT = "component";
BOX_VISUALIZATION = "visualization";
BOX_NO_LID_B = "no_lid";
BOX_STACKABLE_B = "stackable";
LID_FIT_UNDER_B = "fit_lid_under";
LID_SOLID_B = "box_lid_solid";
LID_HEIGHT = "lid_height";
LID_CUTOUT_SIDES_4B = "lid_cutout_sides";
LID_LABELS_INVERT_B = "lid_label_inverted";
LID_SOLID_LABELS_DEPTH = "lid_label_depth";
LID_LABELS_BG_THICKNESS = "lid_label_bg_thickness";
LID_LABELS_BORDER_THICKNESS = "lid_label_border_thickness";
LID_STRIPE_WIDTH = "lid_stripe_width";
LID_STRIPE_SPACE = "lid_stripe_space";
LID_INSET_B = "lid_inset";
LID_TABS_4B = "lid_tabs";
LID_PATTERN_RADIUS = "lid_hex_radius";
LID_PATTERN_N1 = "lid_pattern_n1";
LID_PATTERN_N2 = "lid_pattern_n2";
LID_PATTERN_ANGLE = "lid_pattern_angle";
LID_PATTERN_ROW_OFFSET = "lid_pattern_row_offset";
LID_PATTERN_COL_OFFSET = "lid_pattern_col_offset";
LID_PATTERN_THICKNESS = "lid_pattern_thickness";
// COMPARTMENT PARAMETERS
CMP_NUM_COMPARTMENTS_XY = "num_compartments";
CMP_COMPARTMENT_SIZE_XYZ = "compartment_size";
CMP_SHAPE = "shape";
CMP_SHAPE_ROTATED_B = "shape_rotated_90";
CMP_SHAPE_VERTICAL_B = "shape_vertical";
CMP_PADDING_XY = "padding";
CMP_PADDING_HEIGHT_ADJUST_XY = "padding_height_adjust";
CMP_MARGIN_FBLR = "margin_dim";
CMP_CUTOUT_SIDES_4B = "cutout_sides";
CMP_CUTOUT_CORNERS_4B = "cutout_corners";
CMP_CUTOUT_HEIGHT_PCT = "cutout_height_percent";
CMP_CUTOUT_DEPTH_PCT = "cutout_depth_percent";
CMP_CUTOUT_WIDTH_PCT = "cutout_width_percent";
CMP_CUTOUT_BOTTOM_B = "cutout_bottom";
CMP_CUTOUT_BOTTOM_PCT = "cutout_bottom_percent";
CMP_CUTOUT_TYPE = "cutout_type";
CMP_SHEAR = "shear";
CMP_FILLET_RADIUS = "fillet_radius";
CMP_PEDESTAL_BASE_B = "push_base";
// LABEL PARAMETERS
LBL_TEXT = "text";
LBL_IMAGE = "image";
LBL_SIZE = "size";
LBL_PLACEMENT = "placement";
LBL_FONT = "font";
LBL_DEPTH = "depth";
LBL_SPACING = "spacing";
//LBL_AUTO_CHAR_COUNT = "char_auto";
LABEL = "label";
// LABEL PLACEMENT VALUES
FRONT = "front";
BACK = "back";
LEFT = "left";
RIGHT = "right";
FRONT_WALL = "front-wall";
BACK_WALL = "back-wall";
LEFT_WALL = "left-wall";
RIGHT_WALL = "right-wall";
CENTER = "center";
BOTTOM = "bottom";
///
AUTO = "auto";
MAX = "max";
ENABLED_B = "enabled";
ROTATION = "rotation";
POSITION_XY = "position";
// SHAPES
SQUARE = "square";
HEX = "hex";
HEX2 = "hex2";
OCT = "oct";
OCT2 = "oct2";
ROUND = "round";
FILLET = "fillet";
INTERIOR = "interior";
EXTERIOR = "exterior";
BOTH = "both";
DISTANCE_BETWEEN_PARTS = 2;
////////////////////
// key-values helpers
function __index_of_key( table, key ) = search( [ key ], table )[ k_key ];
function __value( table, key, default = false ) = __index_of_key( table, key ) == [] ? default : table[ __index_of_key( table, key ) ][ k_value ];
///////////////////////
// determines whether lids are output.
g_b_print_lid = t;
// determines whether boxes are output.
g_b_print_box = t;
// determines whether to output everything as placeholders.
g_b_fit_test = f;
// Focus on one box
g_isolated_print_box = "";
// Used to visualize how all of the boxes fit together.
g_b_visualization = f;
g_b_vis_actual = g_b_visualization && $preview;
// Turn off labels during preview.
g_b_preview_no_labels = f;
g_b_no_labels_actual = g_b_preview_no_labels && $preview;
// Makes solid simple lids instead of the honeycomb ones.
// Might be faster to print. Definitely faster to render.
g_b_simple_lids = f;
// default = 1.5
g_wall_thickness = 1.5;
// thickness of detent. For a looser snap fit, reduce this. For a tighter snap fit, increase it. ( recommended 0.05 increments )
g_detent_thickness = 0.25;
g_detent_spacing = 2;
g_detent_dist_from_corner = 1.5;
// default = g_wall_thickness
g_lid_thickness = g_wall_thickness;
// give each compartment a different color. Useful for development
g_b_colorize = true;
// tolerance for fittings. This is the gap between fitting pieces,
// such as lids and boxes. Increase to loosen the fit and decrease to
// tighten it.
g_tolerance = 0.1;
// this adjusts the position of the lid detents downwards.
// The larger the value, the bigger the gap between the lid and the box.
g_tolerance_detent_pos = 0.1;
// This determines whether the default single material version is output, or, if printing in multiple materials,
// which layer to output.
//
g_print_mmu_layer = "default"; // [ "default" | "mmu_box_layer" | "mmu_label_layer" ]
m_tab_corner_gap = 4;
m_wall_underside_lid_storage_depth = 7;
m_corner_width = 6;
m_lid_notch_height = 2.0;
m_lid_notches = true;
module debug( w = 0.2, l = 100 )
{
#translate( [ -w/2, -w/2, -l/2])
cube( [ w , w, l ] );
#translate( [ -w/2, -l/2, -w/2])
cube( [ w , l, w ] );
#translate( [ -l/2, -w/2, -w/2])
cube( [ l , w, w ] );
}
module RotateAndMoveBackToOrigin(a, extents )
{
pos =
a == 90 ? [ extents[1], 0, 0] :
a == -90 ? [ 0, extents[0], 0 ] :
a == -180 ? [ extents[1], extents[0], 0 ] :
[0,0,0];
translate( pos )
rotate( a=a, v=[0,0,1])
children();
}
module RotateAboutPoint(a, v, pt)
{
translate(pt)
rotate(a,v)
translate(-pt)
children();
}
module MirrorAboutPoint( v, pt)
{
translate(pt)
mirror( v )
translate(-pt)
children();
}
function __element( i ) = data[ i ][1];
function __num_elements() = len( data );
function __type( lmnt ) = __value( lmnt, TYPE, default = BOX);
function __is_element_isolated_for_print() = g_isolated_print_box != "" && ( __index_of_key( data, g_isolated_print_box ) != [] ) ;
function __is_element_enabled( lmnt ) = __value( lmnt, ENABLED_B, default = true);
function __element_dimensions( lmnt ) = __type( lmnt ) == DIVIDERS ?
[ __div_frame_size( lmnt )[k_x], __div_total_height( lmnt ) ] :
__value( lmnt, BOX_SIZE_XYZ, default = [ 100, 100] );
function __element_position_x( i ) = __element( i - 1 ) == undef ? 0 : __is_element_enabled( __element( i - 1 ) ) ? __element_dimensions( __element( i - 1 ) )[ k_x ] + __element_position_x( i - 1 ) + DISTANCE_BETWEEN_PARTS : __element_position_x( i - 2 );
//vis
function __box_vis_data( box ) = __value( box, BOX_VISUALIZATION, default = "");
function __box_vis_position( box ) = __value( __box_vis_data( box ), POSITION_XY );
function __box_vis_rotation( box ) = __value( __box_vis_data( box ), ROTATION );
function __div_thickness( div ) = __value( div, DIV_THICKNESS, default = 0.5 );
function __div_tab_size( div ) = __value( div, DIV_TAB_SIZE_XY, default = [32, 14] );
function __div_tab_radius( div ) = __value( div, DIV_TAB_RADIUS, default = 4 );
function __div_tab_cycle( div ) = __value( div, DIV_TAB_CYCLE, default = 3 );
function __div_tab_cycle_start( div ) = __value( div, DIV_TAB_CYCLE_START, default = 1 );
function __div_total_height( div ) = __div_tab_size( div )[k_y] + __div_frame_size( div )[k_y];
function __div_tab_text ( div ) = __value( div, DIV_TAB_TEXT, default = ["001","002", "003" ] );
function __div_tab_text_size ( div ) = __value( div, DIV_TAB_TEXT_SIZE, default = 7 );
function __div_tab_text_font ( div ) = __value( div, DIV_TAB_TEXT_FONT, default = "Stencil Std:style=Bold" );
function __div_tab_text_spacing ( div ) = __value( div, DIV_TAB_TEXT_SPACING, default = 1.1 );
function __div_tab_text_char_threshold ( div ) = __value( div, DIV_TAB_TEXT_CHAR_THRESHOLD, default = 4 );
function __div_tab_size( div ) = __value( div, DIV_TAB_SIZE_XY, default = [32, 14] );
function __div_frame_size( div ) = __value( div, DIV_FRAME_SIZE_XY, default = [80, 80] );
function __div_frame_top( div ) = __value( div, DIV_FRAME_TOP, default = 10 );
function __div_frame_bottom( div ) = __value( div, DIV_FRAME_BOTTOM, default = 10 );
function __div_frame_column( div ) = __value( div, DIV_FRAME_COLUMN, default = 7 );
function __div_frame_radius( div ) = __value( div, DIV_FRAME_RADIUS, default = 15 );
function __div_frame_num_columns( div ) = __value( div, DIV_FRAME_NUM_COLUMNS, default = -1 );
// is the text a string or a list of strings?
function __is_text( label ) = is_string( __value( label, LBL_TEXT ) ) || is_list(__value( label, LBL_TEXT ) );
function __is_multitext( label ) = is_list(__value( label, LBL_TEXT ));
function __is_multiimage( label ) = is_list(__value( label, LBL_IMAGE ));
function __label_text( label, r = 0, c = 0 ) = __is_text( label ) ?
( __is_multitext( label ) ? __value( label, LBL_TEXT, default = "" )[c][r] : __value( label, LBL_TEXT, default = "" ) ) :
"";
function __label_image( label, r = 0, c = 0 ) = !__is_text( label ) ?
( __is_multiimage( label ) ? __value( label, LBL_IMAGE, default = "" )[c][r] : __value( label, LBL_IMAGE, default = "" ) ) :
"";
function __label_size_raw( label ) = __value( label , LBL_SIZE, default = AUTO );
function __label_size_is_auto( label ) = __label_size_raw( label ) == AUTO;
function __label_size( label ) = __label_size_is_auto( label ) ? 10 : __label_size_raw( label);
function __label_rotation_raw( label ) = __value( label, ROTATION, default = 0 ) % 360;
function __label_rotation( label ) = __label_rotation_raw( label ) +
( __label_placement_is_left( label ) ? 90 : __label_placement_is_right( label ) ? -90 : __label_placement_is_front( label ) && __label_placement_is_wall( label ) ? 0 : 0 );
function __label_depth( label ) = __value( label, LBL_DEPTH, default = 0.2 );
function __label_placement_raw( label ) = __value( label, LBL_PLACEMENT, default = CENTER );
function __label_placement_is_center( label ) = __label_placement_raw( label ) == CENTER;
function __label_placement_is_back( label ) = __label_placement_raw( label ) == BACK || __label_placement_raw( label ) == BACK_WALL;
function __label_placement_is_front( label ) = __label_placement_raw( label ) == FRONT || __label_placement_raw( label ) == FRONT_WALL;
function __label_placement_is_left( label ) = __label_placement_raw( label ) == LEFT || __label_placement_raw( label ) == LEFT_WALL;
function __label_placement_is_right( label ) = __label_placement_raw( label ) == RIGHT || __label_placement_raw( label ) == RIGHT_WALL;
function __label_placement_is_bottom( label ) = __label_placement_raw( label ) == BOTTOM;
function __label_placement_is_wall( label ) =
__label_placement_raw( label ) == BACK_WALL ||
__label_placement_raw( label ) == FRONT_WALL ||
__label_placement_raw( label ) == LEFT_WALL ||
__label_placement_raw( label ) == RIGHT_WALL ;
function __label_offset( label ) = __value( label, POSITION_XY, default = [0,0] );
function __label_font( label ) = __value( label, LBL_FONT, default = "Stencil Std:style=Bold" );
function __label_spacing( label ) = __value( label, LBL_SPACING, default = 1 );
function __label_scale_magic_factor( label ) = 1.2 + (1 * abs(tan( __label_rotation( label ) % 90 )) );
function __label_auto_width( label, x, y) = __label_size_is_auto( label ) ?
( cos( __label_rotation( label ) ) * ( x/__label_scale_magic_factor( label ) )) +
( abs( sin( __label_rotation( label ) ) ) * ( y/__label_scale_magic_factor( label ) )) :
__is_text( label ) ? 0 : __label_size( label );
module Colorize()
{
if ( g_b_vis_actual )
{
color( rands(0,1,3), 0.5 )
children();
}
else
{
children();
}
}
module Shear( x, y, height )
{
translate( [0,0,height/2])
multmatrix(m = [
[ 1, 0, sin(x), 0],
[ 0, 1, sin(y), 0],
[ 0, 0, 1, 0]
])
translate( [0,0,-height/2])
children();
}
module MakeAll()
{
echo( str( "\n\n\n", COPYRIGHT_INFO, "\n\n\tVersion ", VERSION, "\n\n" ));
if ( __is_element_isolated_for_print() )
{
element = __value( data, g_isolated_print_box );
if ( __type( element ) == DIVIDERS )
{
MakeDividers( element );
}
else
{
MakeBox( element );
}
}
else
{
for( i = [ 0: __num_elements() - 1 ] )
{
element = __element( i );
element_position = ( g_b_vis_actual && __box_vis_position( element ) != [] ) ? __box_vis_position( element ) : [ __element_position_x( i ), 0, 0 ];
element_rotation = ( g_b_vis_actual && __box_vis_rotation( element ) != undef ) ? __box_vis_rotation( element ) : 0;
translate( element_position )
RotateAndMoveBackToOrigin( element_rotation, __element_dimensions( i ) )
{
if ( __is_element_enabled( element ) )
{
Colorize()
{
if ( __type( element ) == DIVIDERS )
{
MakeDividers( element );
}
else
{
MakeBox( element );
}
}
}
}
}
}
}
module MakeDividers( div )
{
height = __div_frame_size( div )[k_y];
width = __div_frame_size( div )[k_x];
depth = __div_thickness( div );
tab_width = __div_tab_size( div )[k_x];
tab_height = __div_tab_size( div )[k_y];
tab_radius = __div_tab_radius( div );
tab_text = __div_tab_text( div );
font_size = __div_tab_text_size( div );
font = __div_tab_text_font( div );
font_spacing = __div_tab_text_spacing( div );
number_of_letters_before_scale_to_fit = __div_tab_text_char_threshold( div );;
divider_bottom = __div_frame_bottom( div );
divider_top = __div_frame_bottom( div );
divider_column = __div_frame_column( div );
divider_corner_radius = __div_frame_radius( div );
num_columns = __div_frame_num_columns( div );
number_of_tabs_per_row = __div_tab_cycle( div );
tab_starting_position = __div_tab_cycle_start( div );
space_between_tabs = (width - tab_width ) / ( number_of_tabs_per_row - 1 );
for (idx = [ 0 : len( tab_text ) - 1 ] )
{
tab_idx = (idx + tab_starting_position - 1) % number_of_tabs_per_row;
tab_offset = space_between_tabs * tab_idx;
y_offset = idx * ( height + tab_height + DISTANCE_BETWEEN_PARTS );
translate( [ 0, y_offset, 0])
MakeDivider(title = tab_text[idx], tab_offset = tab_offset );
}
module MakeDivider( title, tab_offset )
{
column_height = height - tab_height/2;
gap_size = ( width - ( ( 2 + num_columns ) * divider_column ) ) / ( num_columns + 1 );
difference()
{
MakeRoundedCubeAxis( [ width, height, depth ], 4, [t, t, t, t], k_z);
if ( num_columns != -1 )
for (c = [ 0 : num_columns ] )
{
translate( [ divider_column + (divider_column + gap_size) * c, divider_bottom, 0])
MakeRoundedCubeAxis( [ gap_size, height - divider_bottom - divider_top, depth ], 4, k_z);
}
}
// TAB
difference()
{
height_overlap = tab_radius;
title_pos = [ tab_offset, height - height_overlap, 0];
// tab shape
translate( title_pos )
{
MakeRoundedCubeAxis( [ tab_width, tab_height + height_overlap, depth], 4, [f, f, t, t], k_z);
}
// words
text_pos = title_pos + [ tab_width/2, font_size * 2, 0 ];
text_width = len(title) > number_of_letters_before_scale_to_fit ? tab_width * 0.8 : 0;
translate( text_pos)
resize([ text_width,0, 0 ], auto=[ true, true, false])
linear_extrude( depth )
text(text = title,
font = font,
size = font_size,
valign = "top",
halign = "center",
spacing = font_spacing,
$fn = fn);
}
}
}
module MakeBox( box )
{
m_num_components = len( box );
m_box_label = __value( box, LABEL, default = "");
m_box_is_spacer = ( __type( box ) == SPACER ) || g_b_fit_test;
m_box_is_stackable = __value( box, BOX_STACKABLE_B, default = false );
m_wall_thickness = g_b_fit_test ? 0.5 : __value( box, "wall_thickness", default = g_wall_thickness ); // needs work to change if no lid
m_lid = __value( box, BOX_LID, default = [] );
m_lid_fit_under = __value( m_lid, LID_FIT_UNDER_B, default = true );
m_lid_solid = __value( m_lid, LID_SOLID_B, default = false );
m_lid_inset = m_box_is_stackable || __value( m_lid, LID_INSET_B, default = false );
// the part of the lid that overlaps the box
m_lid_wall_height = __value( m_lid, LID_HEIGHT, default = m_lid_inset ? 2.0 : 4.0 );
m_lid_wall_thickness = m_lid_inset ? 2*m_wall_thickness : m_wall_thickness/2;
m_lid_thickness = m_wall_thickness;
m_box_has_lid = !__value( box, BOX_NO_LID_B, default = false );
m_box_size = ( g_b_fit_test && m_box_has_lid ) ?
[ __element_dimensions( box )[ k_x ],
__element_dimensions( box )[ k_y ],
__element_dimensions( box )[ k_z ] + __lid_external_size( k_z )
] :
__element_dimensions( box );
function __notch_length( D ) = m_box_size[ D ] / 5.0;
function __lid_notch_depth() = m_wall_thickness / 2;
function __lid_external_size( D )= D == k_z ? m_lid_thickness + m_lid_wall_height :
m_box_size[ D ];
m_has_solid_lid = m_lid_solid || g_b_vis_actual;
m_lid_has_labels = !!__value( m_lid, LABEL, default = false );
function __lid_internal_size( D )= D == k_z ? m_lid_wall_height :
__lid_external_size( D ) - 2*m_lid_wall_thickness;
m_lid_cutout_sides = __value( m_lid, LID_CUTOUT_SIDES_4B, default = [f,f,f,f]);
m_lid_is_inverted = __value( m_lid, LID_LABELS_INVERT_B, default = false );
m_lid_label_depth = __value( m_lid, LID_SOLID_LABELS_DEPTH, default = m_lid_thickness / 2 );
m_lid_tab_sides = __value( m_lid, LID_TABS_4B, default = [t,t,t,t]);
m_lid_label_bg_thickness = __value( m_lid, LID_LABELS_BG_THICKNESS, default = 2.0 );
m_lid_label_border_thickness = __value( m_lid, LID_LABELS_BORDER_THICKNESS, default = 0.3 );
m_lid_stripe_width = __value( m_lid, LID_STRIPE_WIDTH, default = 0.5 );
m_lid_stripe_space = __value( m_lid, LID_STRIPE_SPACE, default = 1 );
m_lid_pattern_n1 = __value( m_lid, LID_PATTERN_N1, default = 6 );
m_lid_pattern_n2 = __value( m_lid, LID_PATTERN_N2, default = 6 );
m_lid_pattern_angle = __value( m_lid, LID_PATTERN_ANGLE, default = 30 );
m_lid_pattern_row_offset = __value( m_lid, LID_PATTERN_ROW_OFFSET, default = 50 );
m_lid_pattern_col_offset = __value( m_lid, LID_PATTERN_COL_OFFSET, default = 100 );
m_lid_pattern_thickness = __value( m_lid, LID_PATTERN_THICKNESS, default = 0.5 );
m_lid_pattern_radius = __value( m_lid, LID_PATTERN_RADIUS, default = 4.0 );
m_tab_width_x = max( m_box_size[ k_x ]/4, g_detent_spacing * 2 );
m_tab_width_y = max( m_box_size[ k_y ]/4, g_detent_spacing * 2 );
m_lid_tab = [ max( m_box_size[ k_x ]/4, g_detent_spacing * 2 ), m_wall_thickness, __lid_external_size( k_z ) + 1];
m_box_inner_position_min = [ m_wall_thickness, m_wall_thickness, m_wall_thickness ];
m_box_inner_position_max = m_box_size - m_box_inner_position_min;
module MakeCorners( mod = 0 )
{
difference()
{
cube([ m_box_size[ k_x ], m_box_size[ k_y ], m_box_size[ k_z ] + __lid_external_size( k_z ) ]); // outer box
translate( [ 0, m_corner_width + mod , 0])
cube([ m_box_size[ k_x ], m_box_size[ k_y ] - 2*( m_corner_width + mod ), m_box_size[ k_z ] + __lid_external_size( k_z ) ]); // middle y
translate( [ m_corner_width + mod, 0, 0])
cube([ m_box_size[ k_x ] - 2*( m_corner_width + mod ), m_box_size[ k_y ], m_box_size[ k_z ] + __lid_external_size( k_z ) ]);// middle x
translate( [ m_wall_thickness, m_wall_thickness, 0]) // innerbox
cube([ m_box_size[ k_x ] - 2*m_wall_thickness, m_box_size[ k_y ] - 2*m_wall_thickness, m_box_size[ k_z ] + __lid_external_size( k_z ) ]);
}
}
if ( m_box_is_spacer )
{
MakeLayer( layer = "layer_spacer" );
}
else
{
if( g_b_print_lid && m_box_has_lid )
{
MakeLayer( layer = "layer_lid");
}
if ( g_b_print_box )
{
difference()
{
// carve out the compartments from the box
difference()
{
MakeLayer( layer = "outerbox" );
difference() // we want to preserve the corners regardless
{
// create a negative of the component
for( i = [ 0: m_num_components - 1 ] )
{
if ( box[ i ][ k_key ] == BOX_COMPONENT )
{
component = box[ i ][ k_value ];
union()
{
difference()
{
MakeLayer( component , layer = "component_subtractions");
MakeLayer( component, layer = "component_additions" );
}
MakeLayer( component, layer = "final_component_subtractions" );
}
}
}
}
}
// lid carve outs
MakeLayer( layer = "lid_substractions" );
}
}
}
module MakeLayer( component, layer = "" )
{
m_is_outerbox = layer == "outerbox";
m_is_lid = layer == "layer_lid";
m_is_spacer = layer == "layer_spacer";
m_is_lid_subtractions = layer == "lid_substractions";
// we don't use position for the box or the lid. Only for components.
m_ignore_position = m_is_outerbox || m_is_lid || m_is_spacer || m_is_lid_subtractions;
m_is_component_subtractions = layer == "component_subtractions";
m_is_component_additions = layer == "component_additions";
m_is_final_component_subtractions = layer == "final_component_subtractions";
m_push_base = __value( component, CMP_PEDESTAL_BASE_B, default = f );
function __compartment_size( D ) = __value( component, CMP_COMPARTMENT_SIZE_XYZ, default = [10.0, 10.0, 10.0] )[ D ];
function __compartments_num( D ) = __value( component, CMP_NUM_COMPARTMENTS_XY, default = [1,1] )[ D ];
function __component_rotation() = __value( component, ROTATION, default = 0 );
function __is_component_enabled() = __value( component, ENABLED_B, default = true);
/////////
m_component_margin_side = __value( component, CMP_MARGIN_FBLR, default = [0,0,0,0] );
function __component_margin_start_axis( D ) = D == k_x ? m_component_margin_side[ k_left ] :
D == k_y ? m_component_margin_side[ k_front ] :
0;
function __component_margins_sum( D ) = D == k_x ? m_component_margin_side[ k_left ] + m_component_margin_side[ k_right ] :
D == k_y ? m_component_margin_side[ k_front ] + m_component_margin_side[ k_back ] :
0;
m_component_cutout_side = __value( component, CMP_CUTOUT_SIDES_4B, default = [false, false, false, false] );
m_component_has_side_cutouts = m_component_cutout_side[ k_front ] ||
m_component_cutout_side[ k_back ] ||
m_component_cutout_side[ k_left ] ||
m_component_cutout_side[ k_right ];
m_component_cutout_corner = __value( component, CMP_CUTOUT_CORNERS_4B, default = [false, false, false, false] );
m_component_cutout_type = __value( component, CMP_CUTOUT_TYPE, default = BOTH );
m_component_cutout_bottom = __value( component, CMP_CUTOUT_BOTTOM_B, default = false );
m_component_cutout_bottom_percent = __value( component, CMP_CUTOUT_BOTTOM_PCT, default = 80) / 100;
m_actually_cutout_the_bottom = !__component_is_fillet() && m_component_cutout_bottom && !m_push_base;
m_component_has_exactly_one_cutout =
(m_component_cutout_side[ k_front ]?1:0) +
(m_component_cutout_side[ k_back ]?1:0) +
(m_component_cutout_side[ k_left ]?1:0) +
(m_component_cutout_side[ k_right ]?1:0) == 1;
m_cutout_height_pct = __value( component, CMP_CUTOUT_HEIGHT_PCT, default = 100 ) / 100;
m_cutout_size_frac_aligned = __value( component, CMP_CUTOUT_DEPTH_PCT, default = 25 ) / 100;
m_cutout_size_frac_perpindicular = __value( component, CMP_CUTOUT_WIDTH_PCT, default = 50 ) / 100;
function __component_padding( D ) = __value( component, CMP_PADDING_XY, default = [1.0, 1.0] )[ D ];
function __component_padding_height_adjust( D ) = __value( component, CMP_PADDING_HEIGHT_ADJUST_XY, default = [0.0, 0.0] )[ D ];
function __component_shape() = __value( component, CMP_SHAPE, default = SQUARE );
function __component_shape_rotated_90() = __value( component, CMP_SHAPE_ROTATED_B, default = false );
function __component_shape_vertical() = __value( component, CMP_SHAPE_VERTICAL_B, default = false );
function __component_is_hex() = __component_shape() == HEX;
function __component_is_hex2() = __component_shape() == HEX2;
function __component_is_oct() = __component_shape() == OCT;
function __component_is_oct2() = __component_shape() == OCT2;
function __component_is_round() = __component_shape() == ROUND;
function __component_is_square() = __component_shape() == SQUARE;
function __component_is_fillet() = __component_shape() == FILLET;
function __component_fillet_radius() = __value( component, CMP_FILLET_RADIUS, default = min( __compartment_size( k_z ), 10) );
function __component_shear( D ) = __value( component, CMP_SHEAR, default = [0.0, 0.0] )[ D ];
///////////
function __partition_height_scale( D ) = D == __Y2() ? __req_lower_partitions() ? 0.5 : 1.00 : 1.00;
m_component_base_height = m_box_size[ k_z ] - __component_size( k_z ) - m_wall_thickness;
// DERIVED VARIABLES
///////// __component_position helpers
function __p_i_c( D) = __c_p_raw()[ D ] == CENTER;
function __p_i_m( D) = __c_p_raw()[ D ] == MAX;
function __c_p_c( D ) = ( m_box_size[ D ] - __component_size( D )) / 2;
function __c_p_max( D ) = m_box_size[ D ] - m_wall_thickness - __component_size( D );
/////////
function __c_p_raw() = __value( component, POSITION_XY, default = [ CENTER, CENTER ]);
function __component_position( D ) = __p_i_c( D ) ? __c_p_c( D ):
__p_i_m( D ) ? __c_p_max( D ):
__c_p_raw()[ D ] + m_wall_thickness;
function __component_position_max( D ) = __component_position( D ) + __component_size( D );
function __compartment_smallest_dimension() = ( __compartment_size( k_x ) < __compartment_size( k_y ) ) ? __compartment_size( k_x ) : __compartment_size( k_y );
function __compartment_largest_dimension() = ( __compartment_size( k_x ) > __compartment_size( k_y ) ) ? __compartment_size( k_x ) : __compartment_size( k_y );
function __partitions_num( D )= __compartments_num( D ) - 1;
// calculated __element local dimensions
function __component_size( D )= ( D == k_z ) ? __compartment_size( k_z ) :
( __compartment_size( D ) * __compartments_num( D )) +
( __partitions_num( D ) * __component_padding( D )
+ __component_margins_sum( D ));
function __partition_height( D ) = __component_size( k_z ) + __component_padding_height_adjust( D );
function __smallest_partition_height() = min( __partition_height( k_x ), __partition_height( k_y ) );
module ContainWithinBox()
{
b_needs_trimming = false;//m_is_component_additions;
if ( b_needs_trimming &&
(
__component_position( k_x ) < m_box_inner_position_min[ k_x ]
|| __component_position( k_y ) < m_box_inner_position_min[ k_y ]
|| __component_position( k_x ) + __component_size( k_x ) > m_box_inner_position_max[ k_x ]
|| __component_position( k_y ) + __component_size( k_y ) > m_box_inner_position_max[ k_y ]
))
{
echo( "<br><font color='red'>WARNING: Components in RED do not fit in box. If this is not intentional then adjustments are required or pieces won't fit.</font><br>");
color([1,0,0])
children();
}
else
{
children();
}
}
/////////////////////////////////////////
/////////////////////////////////////////
module __ColorComponent()
{
r = !g_b_colorize ? 0.7 : pow( sin( pow( __component_position(k_x),5) ), 0.5);
g = !g_b_colorize ? 0.8 :pow( sin( pow( __component_position(k_y), 5) ), 0.3);
b = !g_b_colorize ? 0.5 :pow( cos( pow( __component_size(k_z), 5) ), 0.5);
color( [r, g, b] )
children();
}
/////////////////////////////////////////
module PositionInnerLayer()
{
ContainWithinBox()
RotateAboutPoint( __component_rotation(), [0,0,1], [__component_position( k_x ) + __component_size( k_x )/2, __component_position( k_y )+ __component_size( k_y )/2, 0] )
translate( [ __component_position( k_x ), __component_position( k_y ), m_wall_thickness ] )
Shear( __component_shear( k_x ), __component_shear( k_y ), __component_size( k_z ) )
children();
}
if ( __is_component_enabled() )
{
// we only want the labels for an mmu pass
if ( g_print_mmu_layer == "mmu_label_layer" )
{
if ( m_is_lid )
{
color([0,0,1])
MakeDetachedLidLabels();
color([0,0,1])
MakeAllBoxLabels();
}
else if ( layer == "component_additions" )
{
PositionInnerLayer()
if ( !g_b_no_labels_actual)
{
color([0,0,1])
LabelEachCompartment();
}
}
}
else if ( m_is_spacer )
{
MakeSpacer();
}
else if ( m_is_lid )
{
MakeLid();
}
else if ( m_is_outerbox )
{
// 'outerbox' is the insert. It may contain one or more 'components' that each
// define a repeated compartment type.
//
difference()
{
if ( !m_lid_inset )
{
MakeBoxShell();
}
else
MakeBoxShellWithNewLidBits();
color([0,0,1])
MakeAllBoxLabels();
}
}
else if ( m_is_lid_subtractions )
{
// box top lid accommodation
if ( !m_lid_inset && m_box_has_lid )
{
translate( [ 0,0, m_box_size[ k_z ] - __lid_internal_size( k_z ) ] )
MirrorAboutPoint( v=[0,0,1], pt=[0,0,__lid_external_size(k_z)/2])
MakeLidBase_Cap();
notch_pos_z = m_box_size[ k_z ] - m_lid_wall_height + __lid_notch_depth();
if ( m_lid_notches )
translate([ 0, 0, notch_pos_z])
MakeLidCornerNotches();
}
// bottom of the box
if ( m_lid_inset && m_box_is_stackable )
{
difference()
{
cube( [ m_box_size[ k_x ], m_box_size[ k_y ], __lid_external_size(k_z) ]);
MirrorAboutPoint( v=[0,0,1], pt=[0,0,__lid_external_size(k_z)/2])
MakeLidBase_Inset( tolerance = g_tolerance, tolerance_detent_pos = g_tolerance_detent_pos, omit_detents = !m_lid_inset );
}
}
if ( m_lid_fit_under && m_box_has_lid )
{
if ( m_lid_inset )
translate( [ 0, 0, - __lid_external_size( k_z) ] ) // move it down
MakeLidTabs( mod = 0 );
else
{
translate( [ 0, 0, - m_lid_thickness ] )
MakeLidBase_Cap( omit_detents = false );
}
}
}
else
{
PositionInnerLayer()
InnerLayer();
}
}
module MakeLidBase_Inset( tolerance = 0, tolerance_detent_pos = 0, omit_detents = false )
{
difference()
{
// main __element
cube([__lid_external_size( k_x ), __lid_external_size( k_y ), __lid_external_size( k_z )]);
// lid exterior lip
translate( [ 0, 0, __lid_external_size( k_z )/2 ])
{
cube([ __lid_external_size( k_x ), __lid_notch_depth() + tolerance, __lid_external_size( k_z )/1]);
cube([ __lid_notch_depth() + tolerance, __lid_external_size( k_y ), __lid_external_size( k_z )/1]);
MirrorAboutPoint( v=[0,1,0], pt= [__lid_external_size( k_x )/2, __lid_external_size( k_y )/2, m_lid_wall_height/2 ] )
MirrorAboutPoint( v=[1,0,0], pt= [__lid_external_size( k_x )/2, __lid_external_size( k_y )/2, m_lid_wall_height/2 ] )
{
cube([ __lid_external_size( k_x ), __lid_notch_depth() + tolerance, __lid_external_size( k_z )/1]);
cube([ __lid_notch_depth() + tolerance, __lid_external_size( k_y ), __lid_external_size( k_z )/1]);
}
}
difference()
{
MakeLidEdges( extra_depth = tolerance, extra_height = __lid_external_size( k_z ) );
translate( [ 0, 0, 0 ] )
MirrorAboutPoint( v=[0,0,1], pt= [__lid_external_size( k_x )/2, __lid_external_size( k_y )/2, 0] )
hull()
{
MakeLidEdges( extra_depth = tolerance );
translate( [ 0, 0, -__lid_notch_depth() ] )
MakeLidEdges( offset = m_wall_thickness/2 + tolerance, extra_depth = tolerance);
}
}
}
//detents
if ( !omit_detents )
{
detent_height = ( __lid_external_size( k_z ) + __lid_notch_depth() )/2 + tolerance_detent_pos;
translate([ 0, 0, detent_height ]) // lower because tolerance
MakeDetents( mod = -tolerance, offset = tolerance );
}
}
module MakeLidBase_Cap( tolerance = 0, tolerance_detent_pos = 0, omit_detents = false )
{
difference()
{
// main __element
cube([__lid_external_size( k_x ), __lid_external_size( k_y ), __lid_external_size( k_z )]);
// #TODO: modulize this!
translate( [ 0, 0, __lid_external_size( k_z ) - __lid_notch_depth() ] )
hull()
{
translate( [ __lid_notch_depth() , __lid_notch_depth(), 0 ] )
cube([__lid_internal_size( k_x ), __lid_internal_size( k_y ), 1]);
translate( [ 0, 0, __lid_notch_depth() ] )
cube([__lid_external_size( k_x ), __lid_external_size( k_y ), 0.01]);
}
// big hollow