-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgridflock.scad
More file actions
1372 lines (1254 loc) · 73.3 KB
/
gridflock.scad
File metadata and controls
1372 lines (1254 loc) · 73.3 KB
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
include <gridfinity-rebuilt-openscad/src/core/gridfinity-baseplate.scad>
use <gridfinity-rebuilt-openscad/src/helpers/list.scad>
include <paths/puzzle.scad>
// The size of the grid plate to generate
plate_size = [371, 254];
// The bed size of the printer, e.g. 250x220 for the Prusa Core One
bed_size = [250, 220];
/* [Magnets] */
// Whether to enable friction-fit magnets for each grid cell
magnets = false;
// Magnet style
magnet_style = 1; // [0:Glue from top, 1:Press-Fit, 2:Glue from bottom]
// Style of the magnet level
magnet_frame_style = 1; // [0:Solid (incompatible with press-fit), 1:Round Corners]
// Diameter of the magnet slot
magnet_diameter = 5.9; // 0.01
// Height of the magnet slot
magnet_height = 2.25; // 0.25
// Wall above the magnet. Should be small for maximum magnet strength
magnet_top = 0.5; // 0.25
// Floor below the magnet. Not structurally important, should be small to minimize filament use
magnet_bottom = 0.75; // 0.25
// Horizontal strength of the frame holding the magnet in place
magnet_border = 2; // 0.5
// Width of the magnet release slot
magnet_release_width = 3; // 0.5
/* [Click Latch (Experimental)] */
// Enable the click latch. WARNING: The plastic can deform over time, do not use PLA! PETG might be fine, but there are no long-term tests yet
click = false;
// Style of the click latch. The traditional arc style is more robust. The newer ClickGroove design can reduce creep when used with compatible bins, but still supports standard bins.
click_style = 1; // [0:Arc, 1:ClickGroove]
// Distance that the click latch extends into the bin area
click1_distance = 1; // .1
// Steepness of the click latch arc
click1_steepness = 1; // .1
// Length of the full click latch
click1_outer_length = 30;
// Length of the straight piece in the middle of the click latch. The arced pieces take up the remaining space
click1_inner_length = 0;
// Height of the click latch
click1_height = 3; // .1
// Thickness of the click latch. This is measured from the bottom of the baseplate profile
click1_strength = 1.6; // .1
// Thickness of the non-bending wall behind the click latch. This wall provides stability and prevents the click latch from bending too far
click1_wall_strength = 1; // .1
// Length of the gap behind the click latch
clickgroove_gap_length = 25;
// Length of the tab that engages with the bin groove
clickgroove_tab_length = 10;
// Thickness of the click latch. This is measured from the bottom of the baseplate profile
clickgroove_strength = 1.4; // .1
// Thickness of the non-bending wall behind the click latch. This wall provides stability and prevents the click latch from bending too far
clickgroove_wall_strength = 1; // .1
// How far the tab protudes from the normal baseplate profile. Increasing this produces a more secure fit
clickgroove_depth = 0.9; // .05
/* [Intersection Puzzle Connector] */
// Enable the intersection puzzle plate connector. This is similar to GridPlates/GRIPS. Small puzzle connectors are added cell intersections.
connector_intersection_puzzle = true;
// A value from 0 to 1 to modify the fit of the intersection puzzle connector. 0 is a loose fit, 1 is a tight fit.
intersection_puzzle_fit = 1; // [0:0.1:1]
/* [Edge Puzzle Connector] */
// Enable the edge puzzle plate connector. This connector is a bit cleaner, but is harder to print, especially when magnets are disabled (not enough vertical space). It's also more customizable, so you can tune the fit to your printer.
connector_edge_puzzle = false;
// Number of puzzle connectors per cell
edge_puzzle_count = 1;
// Dimensions of the male puzzle connector (main piece)
edge_puzzle_dim = [10, 2.5]; // 0.1
// Dimensions of the male puzzle connector (bridge to plate)
edge_puzzle_dim_c = [3, 1.2]; // 0.1
// Clearance of the puzzle connector. The female side is larger than the above dimensions by this amount
edge_puzzle_gap = 0.15; // 0.05
// If magnets are enabled, use the vertical space to add a border to the female puzzle socket, for added stability and better printability
edge_puzzle_magnet_border = true;
// Size of the added border
edge_puzzle_magnet_border_width = 2.5; // 0.1
// Height of the edge puzzle connector (female side, male is smaller by edge_puzzle_height_male_delta). You can set this to the full height, but make sure that no pieces of the segment remain unconnected!
edge_puzzle_height_female = 2.25; // 0.25
// Male side of the edge puzzle connector is smaller than the female side by this amount
edge_puzzle_height_male_delta = 0.25; // 0.25
/* [Filler] */
// When there is not enough room for a full cell, fill the remaining space with a half-width or dynamic width cell (x direction)
filler_x = 1; // [0:None, 1:Integer Fraction, 2:Dynamic]
// When there is not enough room for a full cell, fill the remaining space with a half-width or dynamic width cell (y direction)
filler_y = 1; // [0:None, 1:Integer Fraction, 2:Dynamic]
// Integer fraction of the reduced size cells, e.g. a value of 2 produces half-width cells, a value of 3 produces third-width cells
filler_fraction = [2, 2];
// Minimum size of filler cells in dynamic mode. If a filler cell would be smaller than this value, it is added to the previous cell instead, producing a cell that is larger than 42mm
filler_minimum_size = [15, 15];
// Padding alignment. The first value is the x direction (east/west), the second value the y direction (north/south). When padding is added to the build plate, this alignment is used to distribute it. A lower value will move the grid towards the west/south direction, adding more padding to the east/north
alignment = [0.5, 0.5]; // [0:0.1:1]
/* [Numbering] */
// Enable numbering of the segements, embossed in a corner
numbering = true;
// Depth of the embossing
number_depth = 0.5; // 0.25
// Font size of the numbers
number_size = 3; // 0.5
// Font
number_font = "sans-serif";
// When a segment is very narrow, use this reduced number size. Should rarely be relevant
number_squeeze_size = 2; // 0.5
/* [Chamfer] */
// Chamfer at the bottom edge of the plate. Configurable for each edge individually (clockwise: north, east, south, west)
bottom_chamfer = [0, 0, 0, 0]; // 0.1
// Chamfer at the top edge of the plate. Configurable for each edge individually (clockwise: north, east, south, west)
top_chamfer = [0, 0, 0, 0]; // 0.1
/* [Plate wall] */
// Plate wall thickness. Can be specified for each direction individually (north, east, south, west). Note that this is *added* to the plate_size
plate_wall_thickness = [0, 0, 0, 0]; // 0.5
// Plate wall height. The first value is the height above the plate, the second value the height below the plate
plate_wall_height = [0, 0];
/* [Vertical Screws] */
// Radius of vertical screws
vertical_screw_diameter = 3.2; // 0.1
// Top countersink dimension. First value is the diameter of the screw head, second value the height
vertical_screw_countersink_top = [0, 0]; // 0.1
// Top counterbore dimension. First value is the diameter of the screw head, second value the height
vertical_screw_counterbore_top = [0, 0];
// Enable screws at *plate* corners
vertical_screw_plate_corners = false;
// Distance from the edge (in number of cells) for an intersection to qualify as a plate corner
vertical_screw_plate_corner_inset = [1, 1];
// Enable screws at *plate* edges
vertical_screw_plate_edges = false;
// Enable screws at *segment* corners that are not also plate corners
vertical_screw_segment_corners = false;
// Distance from the edge (in number of cells) for an intersection to qualify as a segment corner
vertical_screw_segment_corner_inset = [1, 1];
// Enable screws at *segment* edges (will interfere with intersection connectors!)
vertical_screw_segment_edges = false;
// Enable screws at all other intersections
vertical_screw_other = false;
/* [Thumb Screw] */
// Generate thumb screw cutouts compatible with 'Gridfinity Refined'. This requires solid_base or magnets with solid frame style
thumbscrews = false;
// Thumb screw cutout diameter
thumbscrew_diameter = 15.8; // 0.1
/* [Segmentation] */
// Select the algorithm for splitting the baseplate into segments along the x axis. The default ideal algorithm splits the plate into roughly equally-sized segments. The incremental algorithm produces as many maximum-size segments as possible, and one smaller segment for the remaining cells.
x_segment_algorithm = 0; // [0:Ideal, 1:Incremental]
// In the y direction, segment sizes are determined by a simple algorithm that only resizes the first and last segments. The number of rows for the first segment alternate to avoid 4-way intersections. You can override the number of rows in the start segment for the odd and even columns with this property
y_row_count_first = [0, 0];
// If the 'incremental' x segment algorithm is chosen, this can be used to override the column count in the first segment.
x_column_count_first = 0;
/* [Stacked Print] */
// Enable stacked printing of segments in a single print
stacked_print = false;
// Layer height for stacked prints. GridFlock will ensure that each segment is placed at a multiple of this layer height, for consistent printing.
stacked_print_layer_height = 0.2; // 0.05
// The minimum vertical gap between segments, in layers (as per stacked_print_layer_height). The actual gap will be somewhere between stacked_print_min_gap and 1+stacked_print_min_gap layers
stacked_print_min_gap = 0.5; // 0.1
// How many times each segment should appear in the stack. If you set this to 2 for example, you can assemble two full baseplates from the final result.
stacked_print_duplicates = 1;
// Flip the first (lowest) segment?
stacked_print_flip_first = 0; // [0:Do not flip, 1:Flip east-west, 2:Flip north-south]
// Flip the other segments?
stacked_print_flip = 1; // [0:Do not flip, 1:Flip east-west, 2:Flip north-south]
// In order to get better contact area for the stacked print, cut off this much from the top of the baseplate.
stacked_print_slice = 0.3; // 0.05
/* [Advanced] */
// Thickness of the optional solid base
solid_base = 0;
// Corner radius of the generated plate. The default of 4mm matches the corner radius of the gridfinity cell
plate_corner_radius = 4;
// Edge adjustment values (clockwise: north, east, south, west). These values are *added* to the plate size as padding, i.e. the final plate will end up different than configured in plate_size. This allows you to customize the padding to be asymmetrical. You can also use negative values to "cut" the plate edges if you want to squeeze an extra square out of limited space.
edge_adjust = [0, 0, 0, 0];
// Override the content of individual cells. Each character in this string modifies one cell. The order goes from west to east, then south to north. A 'c' stands for a normal cell. An 's' stands for a solid plate without a cell cutout. An 'e' stands for an empty square
cell_override = "";
// Cut off a bit of the top of the baseplate. This can be used to increase contact area when printing the plate upside down.
top_slice = 0; // 0.05
// Test patterns
test_pattern = 0; // [0:None, 1:Half, 2:Padding, 3:Numbering, 4:Wall, 5:Click]
/* [Hidden] */
// Resolution of the click latch.
click1_steps = 15;
_MAGNET_GLUE_TOP = 0;
_MAGNET_PRESS_FIT = 1;
_MAGNET_GLUE_BOTTOM = 2;
_MAGNET_SOLID = 0;
_MAGNET_ROUND_CORNERS = 1;
assert(!magnets || magnet_frame_style != _MAGNET_SOLID || magnet_style != _MAGNET_PRESS_FIT, "'Solid' magnet frame style is not compatible with press-fit magnets.");
assert(!thumbscrews || solid_base > 0 || (magnets && magnet_frame_style == _MAGNET_SOLID), "Thumbscrew holes require some sort of solid base, such as magnet_style solid, or an explicit solid_base.");
$fn=40;
// dimensions of the magnet extraction slot
_magnet_extraction_dim = [magnet_release_width, magnet_diameter/2+2];
// dimensions of the magnet extraction slot in negative mode. This is used to cut out slots out of the edge puzzle connector. This is a bit smaller to make the edge puzzle connector less frail
_magnet_extraction_dim_negative = [magnet_release_width, magnet_diameter/2];
// actual height of a gridfinity profile with no extra clearance.
// gridfinity rebuilt adds extra clearance at the bottom, we cut that out. This is the height for z>0
_profile_height_raw = 4.65;
// for stacked prints, we cut off a sliver at the top to get a better contact area
_profile_height = _profile_height_raw - top_slice - (stacked_print ? stacked_print_slice : 0);
// height of the magnet level
_magnet_level_height = (magnet_style != _MAGNET_GLUE_TOP ? magnet_top : 0) + (magnet_style != _MAGNET_GLUE_BOTTOM ? magnet_bottom : 0) + magnet_height;
// total height of the non-bin levels (magnets, solid base). These are placed at z<0
_extra_height = (magnets ? _magnet_level_height : 0) + solid_base;
_total_height = _profile_height + _extra_height;
// gap between segments in output
_segment_gap = 10;
_NORTH = 0;
_EAST = 1;
_SOUTH = 2;
_WEST = 3;
// distance of each magnet from the side of the base plate grid cell
_magnet_location = 0.25 + 2.15 + 0.8 + 4.8;
// Distance between edge connector pieces, if multiple configured
_edge_puzzle_stagger = edge_puzzle_dim.x + 2;
// Which edges have male connectors?
_edge_puzzle_direction_male = [true, true, false, false];
_CELL_STYLE_NORMAL = "c";
_CELL_STYLE_SOLID = "s";
_CELL_STYLE_EMPTY = "e";
_SEGMENT_ALGORITHM_IDEAL = 0;
_SEGMENT_ALGORITHM_INCREMENTAL = 1;
_FILLER_NONE = 0;
_FILLER_INTEGER = 1;
_FILLER_DYNAMIC = 2;
_CLICK1 = 0;
_CLICK2 = 1;
_FLIP_OFF = 0;
_FLIP_X = 1;
_FLIP_Y = 2;
firstview = $vpr==[55,0,25];
$vpr = firstview ? [0, 0, 0] : $vpr;
/**
* @Summary Run some code in each corner, with proper rotation, to add magnets
* @Details From the children's perspective, we are centered at the corner, and
* the center of the cell is in the north-east (+x and +y)
* @param unit_size Size of the cell, in grid units, in each direction
*/
module each_cell_corner(unit_size) {
size = [BASEPLATE_DIMENSIONS.x*unit_size.x, BASEPLATE_DIMENSIONS.y*unit_size.y];
if (unit_size.x < 1 && unit_size.y < 1) {
translate([-size.x/2, size.y/2]) rotate([0, 0, 270]) children();
} else if (unit_size.x < 1) {
// these corners are chosen so that a half-size bin will fit both a vertical and a horizontal half-width slot
translate([size.x/2, -size.y/2]) rotate([0, 0, 90]) children();
translate([-size.x/2, size.y/2]) rotate([0, 0, 270]) children();
} else if (unit_size.y < 1) {
translate([-size.x/2, -size.y/2]) children();
translate([size.x/2, size.y/2]) rotate([0, 0, 180]) children();
} else {
translate([-size.x/2, -size.y/2]) children();
translate([size.x/2, -size.y/2]) rotate([0, 0, 90]) children();
translate([-size.x/2, size.y/2]) rotate([0, 0, 270]) children();
translate([size.x/2, size.y/2]) rotate([0, 0, 180]) children();
}
}
module each_cell_side(unit_size, enabled = [true, true, true, true]) {
size = [BASEPLATE_DIMENSIONS.x*unit_size.x, BASEPLATE_DIMENSIONS.y*unit_size.y];
if (unit_size.x == 1) {
if (enabled[_SOUTH]) translate([0, -size.y/2, 0]) rotate([90, 0, -90]) children();
if (enabled[_NORTH]) translate([0, size.y/2, 0]) rotate([90, 0, 90]) children();
}
if (unit_size.y == 1) {
if (enabled[_WEST]) translate([-size.x/2, 0, 0]) rotate([90, 0, 180]) children();
if (enabled[_EAST]) translate([size.x/2, 0, 0]) rotate([90, 0, 0]) children();
}
}
module cutter(size, below=0) {
cutter_height = _profile_height_raw + below + 0.001;
translate([0, 0, _profile_height_raw - cutter_height]) {
if (cutter_height >= BASEPLATE_HEIGHT) {
baseplate_cutter(size, cutter_height);
} else if (cutter_height >= _total_height) {
// we can simply move down a little bit, the additional cutting will only cut air anyway.
translate([0, 0, cutter_height - BASEPLATE_HEIGHT]) baseplate_cutter(size, BASEPLATE_HEIGHT);
} else {
// we need to manually remove the dead space from the cut
intersection() {
translate([0, 0, cutter_height - BASEPLATE_HEIGHT]) baseplate_cutter(size, BASEPLATE_HEIGHT);
translate([-size.x/2, -size.y/2]) cube([size.x, size.y, cutter_height]);
}
}
}
}
/**
* @Summary Draw a grid cell centered on 0,0
* @param unit_size Size of the cell, in grid units, in each direction
* @param connector Is there a connector right next to this cell? (by direction)
* @param bottom_chamfer_takes How much of this cell a bottom chamfer will eat up (by direction)
* @param positive This flag is false when this cell is used for cutting instead of additively. When cutting, we can simplify the geometry in ways that would waste filament for additive mode
*/
module cell(unit_size=[1, 1], connector=[false, false, false, false], bottom_chamfer_takes=[0, 0, 0, 0], positive=true) {
size = [BASEPLATE_DIMENSIONS.x*unit_size.x, BASEPLATE_DIMENSIONS.y*unit_size.y];
difference() {
union() {
enable_clickgroove = function(direction) positive && bottom_chamfer_takes[direction] < clickgroove_wall_strength && !(connector_edge_puzzle && connector[direction] && (direction == _SOUTH || direction == _WEST));
difference() {
translate([-size.x/2, -size.y/2, -_extra_height]) cube([size.x, size.y, _total_height]);
cutter(size, below=_extra_height - solid_base);
if (click && click_style == _CLICK1) translate([0, 0, _profile_height/2]) {
if (unit_size.x == 1) cube([click1_outer_length, size.y-click1_wall_strength*2, _profile_height], center=true);
if (unit_size.y == 1) cube([size.x-click1_wall_strength*2, click1_outer_length, _profile_height], center=true);
}
if (click && click_style == _CLICK2) {
each_cell_side(unit_size, enabled=[for (dir = [0:3]) enable_clickgroove(dir)]) translate([-2.85+clickgroove_strength, 0, -clickgroove_gap_length/2]) cube([2.85-clickgroove_wall_strength-clickgroove_strength, _profile_height, clickgroove_gap_length]);
}
}
if (click && click_style == _CLICK1) {
each_cell_side(unit_size) do_sweep(_click1_sweep, convexity=4);
}
if (click && click_style == _CLICK2) {
height = clickgroove_depth*2;
center_y = 0.8+1.8/2;
max_y = 2.5;
inset = 0.01;
each_cell_side(unit_size, enabled=[for (dir = [0:3]) enable_clickgroove(dir)]) translate([-2.15, 0.8+1.8/2]) linear_extrude(clickgroove_tab_length, center=true) polygon([
[-clickgroove_depth, 0], // peak
[0, min(height/2, max_y - center_y)], // top corner
[0.01, 0], // slight dent to make sure the polygon is connected to the edge
[0, -height/2] // bottom corner
]);
}
if (magnets) {
translate([0, 0, -_magnet_level_height]) linear_extrude(height = _magnet_level_height) {
if (positive && magnet_frame_style != _MAGNET_SOLID) {
// round corners
if (magnet_frame_style == _MAGNET_ROUND_CORNERS) {
each_cell_corner(unit_size) {
total_bounds = _magnet_location + magnet_diameter/2 + magnet_border;
square([_magnet_location, total_bounds]);
square([total_bounds, _magnet_location]);
translate([_magnet_location, _magnet_location]) circle(r=magnet_diameter/2+magnet_border);
}
}
// if we have a female edge connector here, add a bar for stability (edge_puzzle_magnet_border)
if (connector_edge_puzzle && edge_puzzle_magnet_border) {
bw = edge_puzzle_dim.y + edge_puzzle_dim_c.y + edge_puzzle_magnet_border_width;
translate(-size/2) {
if (connector[_SOUTH] && !_edge_puzzle_direction_male[_SOUTH]) square([size.x, bw]);
if (connector[_WEST] && !_edge_puzzle_direction_male[_WEST]) square([bw, size.y]);
}
if (connector[_NORTH] && !_edge_puzzle_direction_male[_NORTH]) translate([-size.x/2, size.y/2-bw]) square([size.x, bw]);
if (connector[_EAST] && !_edge_puzzle_direction_male[_EAST]) translate([size.x/2-bw, -size.y/2]) square([bw, size.y]);
}
} else {
// for negative mode, we don't care about extra geometry
// this also runs for _MAGNET_SOLID style
translate(-size/2) square(size);
}
}
}
}
if (magnets) {
each_cell_corner(unit_size) {
translate([_magnet_location, _magnet_location]) {
rot_slot = (unit_size == [1, 1] || (unit_size.x < 1 && unit_size.y < 1)) ? -45 : -90;
// magnet slot
if (magnet_style == _MAGNET_GLUE_BOTTOM) {
translate([0, 0, -_extra_height]) cylinder(d=magnet_diameter, h=_extra_height-magnet_top);
} else {
translate([0, 0, -_magnet_level_height + magnet_bottom]) linear_extrude(magnet_height) {
circle(d=magnet_diameter);
if (magnet_style == _MAGNET_PRESS_FIT) rotate([0, 0, rot_slot]) translate([-magnet_diameter/2, 0]) square([magnet_diameter, magnet_diameter/2 + magnet_border]);
}
}
// magnet extraction slot
if (magnet_style == _MAGNET_PRESS_FIT) {
// horizontal slot
extraction_dim = positive ? _magnet_extraction_dim : _magnet_extraction_dim_negative;
if (extraction_dim.x > 0 && extraction_dim.y > 0) {
rotate([0, 0, rot_slot]) translate([0, 0, -_extra_height]) linear_extrude(_extra_height - _magnet_level_height + magnet_bottom + magnet_height) {
translate([-extraction_dim.x/2, -extraction_dim.y]) square(extraction_dim);
translate([0, -extraction_dim.y]) circle(extraction_dim.x/2);
circle(extraction_dim.x/2);
}
}
} else {
// vertical slot
if (magnet_release_width > 0) {
translate([0, 0, -_extra_height]) cylinder(d=magnet_release_width, h=_extra_height);
}
}
}
}
}
if (thumbscrews && unit_size == [1, 1]) rotate_extrude() {
top = magnets && magnet_frame_style != _MAGNET_SOLID ? -_magnet_level_height : 0;
height = 100;
polygon([[0, top], [thumbscrew_diameter/2, top], [thumbscrew_diameter/2 + height, top-height], [0, top-height]]);
}
}
}
/**
* Raw polygon for the male puzzle connector. (Note: Only one half)
*/
module puzzle_male_0() {
scale(1/128*4) translate([-128, -128]) polygon(svg_path_puzzle_svg_male_tight * intersection_puzzle_fit + svg_path_puzzle_svg_male_loose * (1 - intersection_puzzle_fit));
}
/**
* Cleaned polygon for the male puzzle connector. In particular, we need
* to cut the parts of the polygon that overlap with the bin.
*/
module puzzle_male(positive) {
difference() {
if (positive) {
puzzle_male_0();
} else {
hull() puzzle_male_0();
}
translate([-4, -4]) circle(4);
}
}
/**
* Raw polygon for the female connector. (Note: Only one half)
*/
module puzzle_female_0() {
mirror([1, 0]) scale(1/128*4) translate([-128, -128]) polygon(svg_path_puzzle_svg_female_tight * intersection_puzzle_fit + svg_path_puzzle_svg_female_loose * (1 - intersection_puzzle_fit));
}
/**
* Cleaned polygon for the female puzzle connector. The female polygon
* is a negative, so we need to do some inversion here.
*/
module puzzle_female(positive) {
if (positive) {
difference() {
hull() puzzle_female_0();
puzzle_female_0();
}
} else {
puzzle_female_0();
}
}
/**
* @Summary Prepare geometry for do_sweep, which sweeps a polygon along a path
* @param polygon The polygon to sweep. Array of 2D points
* @param path The path to sweep along. Array of 3D points
* @return Geometry data to pass to do_sweep
*/
function prepare_sweep(polygon, path) = let(
ring_faces = function (base_index) [
for (i = [0:len(polygon)-1]) [
base_index + (i + 1) % len(polygon),
base_index + len(polygon) + (i + 1) % len(polygon),
base_index + len(polygon) + i,
base_index + i
]
],
points = [for (pt_path = path) each [for (pt_poly = polygon) pt_path + [pt_poly.x, pt_poly.y, 0]]],
first_face = reverse([each [0:len(polygon)-1]]),
last_face = [each [len(polygon)*(len(path)-1):len(polygon)*len(path)-1]],
faces = [
first_face,
for (i = [0:len(path)-2]) each ring_faces(i * len(polygon)),
last_face
]
) [points, faces];
/**
* @Summary Display a sweep prepared by prepare_sweep
* @param sweep The value returned by prepare_sweep
*/
module do_sweep(prep, convexity=2) {
polyhedron(points = prep[0], faces = prep[1], convexity = convexity);
}
/**
* @Summary Clip a polygon along an edge (one step of the Sutherland-Hodgman algorithm)
* @param polygon The input polygon to clip
* @param contains A lambda taking a single point that returns whether that point is clipped or not
* @param find_intersection A lambda taking a point inside and outside the result area (in that order), that returns the point where the line between those points intersects the start of the clipping region
* @return The clipped polygon, potentially with duplicate points
*/
function clip_polygon_edge(polygon, contains, find_intersection) =
[for (i = [0:len(polygon)-1]) let (
here = polygon[i],
prev = i == 0 ? polygon[len(polygon) - 1] : polygon[i - 1],
here_inside = contains(here),
prev_inside = contains(prev),
) each
here_inside ?
prev_inside ? [here] : [find_intersection(here, prev), here] :
prev_inside ? [find_intersection(prev, here)] : []
];
/**
* @Summary Clip a polygon using the Sutherland-Hodgman algorithm so that all resulting points satisfy `pt.x <= max.x && pt.y <= max.y`
* @param polygon The polygon to clip
* @param max The bounds to clip to
* @return The clipped polygon, with no duplicate points
*/
function clip_polygon_max(polygon, max) = let(
step = function(dimension, pg) clip_polygon_edge(pg, function (pt) pt[dimension] <= max[dimension], function (inside, outside) let (factor = (max[dimension] - inside[dimension]) / (outside[dimension] - inside[dimension])) inside + (outside - inside) * factor),
clipped = step(0, step(1, polygon)),
deduplicated = [for (i = 0, prev = clipped[len(clipped) - 1]; i < len(clipped); prev = clipped[i], i = i + 1) each clipped[i] == prev ? [] : [clipped[i]]]
) deduplicated;
// the maximum width of the baseplate profile (at the very bottom of the profile)
_baseplate_max_strength = _BASEPLATE_PROFILE[3].x;
// the full polygon of the baseplate profile
_baseplate_polygon = [
[0, 0],
for (pt = _BASEPLATE_PROFILE) pt + [-_baseplate_max_strength, 0]
];
_click1_polygon = let(shiftx = _baseplate_max_strength-click1_strength) [for (pt = clip_polygon_max([for (pt = _baseplate_polygon) [pt.x+shiftx, pt.y]], [0, click1_height])) [pt.x-shiftx, pt.y]];
/**
* @Summary Logistic function used for the click1 arc
* @param x coordinate
*/
function click1_path_base(x) = 1/(1+exp(-click1_steepness*x));
_click1_path = let (
arc_length = (click1_outer_length - click1_inner_length) / 2,
low = click1_path_base(-arc_length/2),
high = click1_path_base(arc_length/2),
scale = click1_distance / (high - low),
arc = [for (x = [-arc_length/2:arc_length/click1_steps:arc_length/2]) [-(click1_path_base(x)-low)*scale, 0, x - (click1_outer_length-arc_length)/2]]
) [
each arc,
for (pt = reverse(arc)) [pt.x, pt.y, -pt.z]
];
_click1_sweep = prepare_sweep(_click1_polygon, _click1_path);
/**
* @Summary Cumulate the entries of an array. e.g. [1, 1, 0.5] -> [0, 1, 2, 2.5]
*/
function cumulate(trace) =
assert(is_list(trace))
[for (i = 0, cumulated = 0; i <= len(trace); cumulated = cumulated + (i >= len(trace) ? 0 : assert(is_num(trace[i])) trace[i]), i = i + 1) cumulated];
/**
* @Summary Get the position of a particular cell on one axis (1D)
* @param dimension The cell dimension on this axis
* @param size The segment size on this axis
* @param trace The array of cell sizes on this axis
* @param padding_start The start padding on this axis
* @param index The index of this cell on this axis (may be < 0 or >= size also)
*/
function cell_axis_position(dimension, size, trace, padding_start, index) =
let(
cumulated = cumulate(trace),
start_pos_norm =
index < 0 ? index :
index >= len(trace) ? cumulated[len(trace)] + (index - len(trace)) :
cumulated[index],
own_size_norm = index < 0 || index >= len(trace) ? 1 : trace[index],
final = -size/2 + padding_start + (start_pos_norm + own_size_norm / 2) * dimension
) final;
/**
* @Summary In the segment coordinate system, translate to the center of a particular cell
* @param size The size of the segment
* @param trace The cell sizes on each axis
* @param padding The padding of the segment (for each edge)
* @param index The index of the cell (can also be negative or >= count)
*/
module navigate_cell(size, trace, padding, index) {
translate([
cell_axis_position(BASEPLATE_DIMENSIONS.x, size.x, trace.x, padding[_WEST], index.x),
cell_axis_position(BASEPLATE_DIMENSIONS.y, size.y, trace.y, padding[_SOUTH], index.y)
]) children();
}
/**
* @Summary In the segment coordinate system, translate to a corner of a particular cell
* @param size The size of the segment
* @param trace The cell sizes on each axis
* @param padding The padding of the segment (for each edge)
* @param index The index of the cell
* @param diry The y direction of the corner (north or south)
* @param dirx The x direction of the corner (east or west)
*/
module navigate_corner(size, trace, padding, index, diry, dirx) {
assert(diry == _NORTH || diry == _SOUTH);
assert(dirx == _EAST || dirx == _WEST);
navigate_cell(size, trace, padding, index) translate([
(dirx == _WEST ? -1 : 1) * BASEPLATE_DIMENSIONS.x * (index.x < 0 || index.x >= len(trace.x) ? 1 : trace.x[index.x]) / 2,
(diry == _SOUTH ? -1 : 1) * BASEPLATE_DIMENSIONS.y * (index.y < 0 || index.y >= len(trace.y) ? 1 : trace.y[index.y]) / 2
]) children();
}
/**
* @Summary In the segment coordinate system, translate to an edge of a particular cell
* @param size The size of the segment
* @param trace The cell sizes on each axis
* @param padding The padding of the segment (for each edge)
* @param index The index of the cell
* @param dir The edge to navigate to (N/E/S/W)
*/
module navigate_edge(size, trace, padding, index, dir) {
navigate_cell(size, trace, padding, index) translate([
(dir == _WEST ? -1 : dir == _EAST ? 1 : 0) * BASEPLATE_DIMENSIONS.x * (index.x < 0 || index.x >= len(trace.x) ? 1 : trace.x[index.x]) / 2,
(dir == _SOUTH ? -1 : dir == _NORTH ? 1 : 0) * BASEPLATE_DIMENSIONS.y * (index.y < 0 || index.y >= len(trace.y) ? 1 : trace.y[index.y]) / 2
]) children();
}
/**
* @Summary Draw the segment intersection connectors (2D)
* @Details Draw all the segment connectors in 2D, once for the whole segment.
* This is done in two passes (negative and positive): The negative
* pass cuts out room from the plate, and the positive pass adds the
tabs.
* @param positive true if this is the positive pass
* @param trace The cell sizes on each axis
* @param size The size of the segment (incl. padding)
* @param padding The padding of the segment (for each edge)
* @param connector The connector configuration (for each edge)
*/
module segment_intersection_connectors(positive, trace, size, padding, connector) {
last = [len(trace.x) - 1, len(trace.y) - 1];
// for the normal case, we iterate over the cells at the edge of the segment, and add two half-connectors for each cell.
for (ix = [0:1:last.x]) {
// north and south connectors
skip_first = ix == 0 && connector[_WEST];
skip_last = ix == last.x && connector[_EAST];
if (connector[_SOUTH]) {
if (!skip_first) navigate_corner(size, trace, padding, [ix, 0], _SOUTH, _WEST) mirror([1, 0]) rotate([0, 0, -90]) puzzle_female(positive);
if (!skip_last) navigate_corner(size, trace, padding, [ix, 0], _SOUTH, _EAST) rotate([0, 0, -90]) puzzle_female(positive);
}
if (connector[_NORTH]) {
if (!skip_first) navigate_corner(size, trace, padding, [ix, last.y], _NORTH, _WEST) rotate([0, 0, 90]) puzzle_male(positive);
if (!skip_last) navigate_corner(size, trace, padding, [ix, last.y], _NORTH, _EAST) mirror([1, 0]) rotate([0, 0, 90]) puzzle_male(positive);
}
}
for (iy = [0:1:last.y]) {
// east and west connectors
if (connector[_WEST]) {
navigate_corner(size, trace, padding, [0, iy], _SOUTH, _WEST) rotate([0, 0, 180]) puzzle_female(positive);
navigate_corner(size, trace, padding, [0, iy], _NORTH, _WEST) mirror([0, 1]) rotate([0, 0, 180]) puzzle_female(positive);
}
if (connector[_EAST]) {
navigate_corner(size, trace, padding, [last.x, iy], _SOUTH, _EAST) mirror([0, 1]) puzzle_male(positive);
navigate_corner(size, trace, padding, [last.x, iy], _NORTH, _EAST) puzzle_male(positive);
}
}
// At the corners of the segment, we now only have half-connectors. But if we have padding, there may be space for a full connector after all.
// We add half-connectors at the corners and cut them to fit the plate.
// Size includes plate wall. We don't want to interfere with that.
calculate_plate_wall = function (side) connector[side] ? 0 : plate_wall_thickness[side];
bounds_min = [
-size.x/2 + calculate_plate_wall(_WEST),
-size.y/2 + calculate_plate_wall(_SOUTH)
];
bounds_max = [
size.x/2 - calculate_plate_wall(_EAST),
size.y/2 - calculate_plate_wall(_NORTH)
];
intersection() {
translate([bounds_min.x, -size.y/2 - 20]) square([bounds_max.x - bounds_min.x, size.y + 40]);
union() {
if (!connector[_WEST]) {
if (connector[_SOUTH]) navigate_corner(size, trace, padding, [0, 0], _SOUTH, _WEST) rotate([0, 0, -90]) puzzle_female(positive);
if (connector[_NORTH]) navigate_corner(size, trace, padding, [0, last.y], _NORTH, _WEST) mirror([1, 0]) rotate([0, 0, 90]) puzzle_male(positive);
}
if (!connector[_EAST]) {
if (connector[_SOUTH]) navigate_corner(size, trace, padding, [last.x, 0], _SOUTH, _EAST) mirror([1, 0]) rotate([0, 0, -90]) puzzle_female(positive);
if (connector[_NORTH]) navigate_corner(size, trace, padding, [last.x, last.y], _NORTH, _EAST) rotate([0, 0, 90]) puzzle_male(positive);
}
}
}
intersection() {
translate([-size.x/2 - 20, bounds_min.y]) square([size.x + 40, bounds_max.y - bounds_min.y]);
union() {
if (!connector[_SOUTH]) {
if (connector[_WEST]) navigate_corner(size, trace, padding, [0, 0], _SOUTH, _WEST) mirror([0, 1]) rotate([0, 0, 180]) puzzle_female(positive);
if (connector[_EAST]) navigate_corner(size, trace, padding, [last.x, 0], _SOUTH, _EAST) puzzle_male(positive);
}
if (!connector[_NORTH]) {
if (connector[_WEST]) navigate_corner(size, trace, padding, [0, last.y], _NORTH, _WEST) rotate([0, 0, 180]) puzzle_female(positive);
if (connector[_EAST]) navigate_corner(size, trace, padding, [last.x, last.y], _NORTH, _EAST) mirror([0, 1]) puzzle_male(positive);
}
}
}
}
/**
* @Summary Draw the segment edge connectors (2D)
* @Details Draw all the segment connectors in 2D, once for the whole segment.
* This is done in two passes (negative and positive): The negative
* pass cuts out room from the plate, and the positive pass adds the
tabs.
* @param positive true if this is the positive pass
* @param trace The cell sizes on each axis
* @param size The size of the segment (incl. padding)
* @param padding The padding of the segment (for each edge)
* @param connector The connector configuration (for each edge)
*/
module segment_edge_connectors(positive, trace, size, padding, connector) {
last = [len(trace.x) - 1, len(trace.y) - 1];
for (ix = [0:1:last.x]) {
if (connector[_SOUTH]) navigate_edge(size, trace, padding, [ix, 0], _SOUTH) edge_puzzle(positive, _edge_puzzle_direction_male[_SOUTH], trace.x[ix]);
if (connector[_NORTH]) navigate_edge(size, trace, padding, [ix, last.y], _NORTH) mirror([0, 1]) edge_puzzle(positive, _edge_puzzle_direction_male[_NORTH], trace.x[ix]);
}
for (iy = [0:1:last.y]) {
if (connector[_WEST]) navigate_edge(size, trace, padding, [0, iy], _WEST) mirror([1, 0]) rotate([0, 0, 90]) edge_puzzle(positive, _edge_puzzle_direction_male[_WEST], trace.y[iy]);
if (connector[_EAST]) navigate_edge(size, trace, padding, [last.x, iy], _EAST) rotate([0, 0, 90]) edge_puzzle(positive, _edge_puzzle_direction_male[_EAST], trace.y[iy]);
}
}
/**
* @Summary Draw a rounded bar
* @Details This is a rectangle that is rounded at the start and end of the x direction. The full bar fits into the given size
* @param size The bounds of the bar
*/
module round_bar_x(size) {
translate([size.y/2, 0]) square([size.x - size.y, size.y]);
translate([size.y/2, size.y/2]) circle(size.y/2);
translate([size.x-size.y/2, size.y/2]) circle(size.y/2);
}
/**
* @Summary Draw a negative rounded bar
* @Details This is a rectangle that is *negatively* rounded (i.e. circles cut out) at the start and end of the x direction. The full bar fits into the given size
* @param size The bounds of the bar
*/
module round_bar_x_neg(size) {
difference() {
translate([-size.y/2, 0]) square([size.x + size.y, size.y]);
translate([-size.y/2, size.y/2]) circle(size.y/2);
translate([size.x+size.y/2, size.y/2]) circle(size.y/2);
}
}
/**
* @Summary Draw the edge puzzle connector for a single cell side (2D)
* @Details This function operates as if on the south edge: The male side extends into the south direction, the female goes into the north direction. For other orientations, rotate/mirror as needed
* @param positive Whether this is the positive pass of the edge puzzle drawing
* @param male Whether this side has a male connector
* @param size Size of this cell edge in grid units
*/
module edge_puzzle(positive, male, size) {
count_here = size < 1 ? max(1, floor(edge_puzzle_count/2)) : edge_puzzle_count;
for (i = [0:1:count_here-1]) translate([(-(count_here-1)/2+i)*_edge_puzzle_stagger, 0]) {
if (male) {
if (positive) {
translate([-edge_puzzle_dim_c.x/2, -edge_puzzle_dim_c.y]) round_bar_x_neg([edge_puzzle_dim_c.x, edge_puzzle_dim_c.y]);
translate([-edge_puzzle_dim.x/2, -edge_puzzle_dim_c.y-edge_puzzle_dim.y]) round_bar_x([edge_puzzle_dim.x, edge_puzzle_dim.y]);
}
} else {
if (!positive) {
translate([-edge_puzzle_dim_c.x/2-edge_puzzle_gap, 0]) round_bar_x_neg([edge_puzzle_dim_c.x+edge_puzzle_gap*2, edge_puzzle_dim_c.y-edge_puzzle_gap]);
translate([-edge_puzzle_dim.x/2-edge_puzzle_gap, edge_puzzle_dim_c.y-edge_puzzle_gap]) round_bar_x([edge_puzzle_dim.x+edge_puzzle_gap, edge_puzzle_dim.y+edge_puzzle_gap]);
}
}
}
//translate([-0.5,0]) square([1, 15]); // for visually checking alignment
}
/**
* @Summary Draw a vertical screw at the current coordinate
*/
module vertical_screw() {
// Additional space to clear above the screw. There shouldn't be anything here, but this guards against rounding errors
clear_upwards = 0.01;
rotate_extrude() {
translate([0, -_extra_height]) square([vertical_screw_diameter/2, _total_height + clear_upwards]);
translate([0, _profile_height - vertical_screw_counterbore_top.y]) {
// counterbore
square([vertical_screw_counterbore_top.x/2, vertical_screw_counterbore_top.y+clear_upwards]);
// countersink
polygon([
[0, 0],
[0, clear_upwards],
[vertical_screw_countersink_top.x/2, clear_upwards],
[vertical_screw_countersink_top.x/2, 0],
[vertical_screw_diameter/2, -vertical_screw_countersink_top.y]
]);
}
}
}
/**
* @Summary Draw the shape of a segment corner depending on connector configuration (2D)
* @Details The corner is square if there is an adjacent connector, and rounded if there is not
* @param posy The y corner position (north or south)
* @param posx The x corner position (east or west)
* @param connector The connector configuration
* @param radius Function to compute the corner radius by side
*/
module segment_corner(posy=_NORTH, posx=_WEST, connector=[false, false, false, false], radius) {
assert(posy == _NORTH || posy == _SOUTH);
assert(posx == _EAST || posx == _WEST);
radii = [radius(posx), radius(posy)];
if (connector[posx] || connector[posy]) {
square(size = radii * 2, center=true);
} else {
// ellipse
scale(radii) circle(r = 1);
}
}
/**
* @Summary Draw the 2D shape of a segment, including rounded corners
* @param size The size of the segment
* @param connector The connector configuration
* @param include_wall If false, the plate_wall_thickness is not included in the rectangle
*/
module segment_rectangle(size, connector=[false, false, false, false], include_wall=false) {
// wall thickness to cut off, by side
wall_t = function (side) include_wall || connector[side] ? 0 : plate_wall_thickness[side];
// corner radius by side
compute_radius = function (side) max(0.01, plate_corner_radius - wall_t(side));
bounds_offset = function (side) compute_radius(side) + wall_t(side);
bounds_min = [
-size.x/2 + bounds_offset(_WEST),
-size.y/2 + bounds_offset(_SOUTH)
];
bounds_max = [
size.x/2 - bounds_offset(_EAST),
size.y/2 - bounds_offset(_NORTH)
];
hull() {
translate([bounds_min.x, bounds_min.y]) segment_corner(_SOUTH, _WEST, connector, compute_radius);
translate([bounds_max.x, bounds_min.y]) segment_corner(_SOUTH, _EAST, connector, compute_radius);
translate([bounds_max.x, bounds_max.y]) segment_corner(_NORTH, _EAST, connector, compute_radius);
translate([bounds_min.x, bounds_max.y]) segment_corner(_NORTH, _WEST, connector, compute_radius);
};
}
module chamfer_triangle() {
extend = 20;
// extend far out into -x and -y to make sure we cut everything
polygon([[-extend, -extend], [1 + extend, -extend], [-extend, 1 + extend]]);
}
function compute_segment_size(trace, padding) = [
BASEPLATE_DIMENSIONS.x * cumulate(trace.x)[len(trace.x)] + padding[_EAST] + padding[_WEST],
BASEPLATE_DIMENSIONS.y * cumulate(trace.y)[len(trace.y)] + padding[_NORTH] + padding[_SOUTH],
];
/**
* @Summary Model a segment, which is piece of the plate without breaks
* @param trace The cell sizes, in grid units, on each axis
* @param padding The padding, for each side
* @param connector Whether to add a connector, for each side
* @param global_segment_index If applicable, the global index of this segment. This is used to emboss numbering
* @param global_cell_index If applicable, the global cell index of this segment. This is used for vertical screws at plate corners
* @param global_cell_count If applicable, the global cell count. This is used for vertical screws at plate corners
*/
module segment(trace=[[1], [1]], padding=[0, 0, 0, 0], connector=[false, false, false, false], global_segment_index=undef, global_cell_index=[0, 0], global_cell_count=[0, 0]) {
size = compute_segment_size(trace, padding);
_edge_puzzle_height_male = edge_puzzle_height_female - edge_puzzle_height_male_delta;
// whether to cut the male edge puzzle connector to make room for the bin in the next cell. For really short connectors this is not necessary, but there's also no good reason to turn this off, so it's not user configurable at the moment
_edge_puzzle_overlap = true;
last = [len(trace.x)-1, len(trace.y)-1];
difference() {
union() {
intersection() {
translate([0, 0, -_extra_height]) linear_extrude(height = _total_height) difference() {
// basic plate with rounded corners
segment_rectangle(size, connector, include_wall=false);
if (connector_intersection_puzzle) {
segment_intersection_connectors(false, trace, size, padding, connector);
}
}
union() {
// padding cubes
translate([0, 0, -_extra_height]) {
if (padding[_NORTH] > 0) translate([-size.x/2, size.y/2-padding[_NORTH]]) cube([size.x, padding[_NORTH], _total_height]);
if (padding[_EAST] > 0) translate([size.x/2-padding[_EAST], -size.y/2]) cube([padding[_EAST], size.y, _total_height]);
if (padding[_SOUTH] > 0) translate([-size.x/2, -size.y/2]) cube([size.x, padding[_SOUTH], _total_height]);
if (padding[_WEST] > 0) translate([-size.x/2, -size.y/2]) cube([padding[_WEST], size.y, _total_height]);
}
// cells
for (ix = [0:1:last.x]) for (iy = [0:1:last.y]) navigate_cell(size, trace, padding, [ix, iy]) {
cell_size = [trace.x[ix], trace.y[iy]];
seq_index = (iy + global_cell_index.y) * ceil(global_cell_count.x) + (ix + global_cell_index.x);
cell_style = len(cell_override) <= seq_index ? _CELL_STYLE_NORMAL : cell_override[seq_index];
if (cell_style == _CELL_STYLE_NORMAL) {
at_edge = [iy == last.y, ix == last.x, iy == 0, ix == 0];
cell(
cell_size,
connector=[for (direction = [0:3]) connector[direction] && at_edge[direction]],
bottom_chamfer_takes=[for (direction = [0:3]) at_edge[direction] && !connector[direction] ? max(0, bottom_chamfer[direction] - padding[direction]) : 0]
);
} else if (cell_style == _CELL_STYLE_EMPTY) {
} else if (cell_style == _CELL_STYLE_SOLID) {
dim = [BASEPLATE_DIMENSIONS.x * cell_size.x, BASEPLATE_DIMENSIONS.y * cell_size.y];
translate([-dim.x/2, -dim.y/2, -_extra_height]) cube([dim.x, dim.y, _total_height]);
} else {
assert(false, str("Unknown cell style: '", cell_style, "'"));
}
};
};
};
if (plate_wall_thickness != [0,0,0,0]) translate([0, 0, -_extra_height-plate_wall_height[1]]) linear_extrude(_total_height + plate_wall_height[0] + plate_wall_height[1]) difference() {
segment_rectangle(size, connector, include_wall=true);
segment_rectangle(size, connector, include_wall=false);
}
if (connector_intersection_puzzle) translate([0, 0, -_extra_height]) linear_extrude(height = _total_height) segment_intersection_connectors(true, trace, size, padding, connector);
if (connector_edge_puzzle) {
intersection() {
translate([0, 0, -_extra_height]) linear_extrude(height = _extra_height+_edge_puzzle_height_male) segment_edge_connectors(true, trace, size, padding, connector);
if (_edge_puzzle_overlap) union() {
for (ix = [0:1:last.x]) {
if (connector[_SOUTH]) navigate_cell(size, trace, padding, [ix, -1]) cell([trace.x[ix], 1], positive=false);
if (connector[_NORTH]) navigate_cell(size, trace, padding, [ix, last.y+1]) cell([trace.x[ix], 1], positive=false);
}
for (iy = [0:1:last.y]) {
if (connector[_WEST]) navigate_cell(size, trace, padding, [-1, iy]) cell([1, trace.y[iy]], positive=false);
if (connector[_EAST]) navigate_cell(size, trace, padding, [last.x+1, iy]) cell([1, trace.y[iy]], positive=false);
}
}
}
}
}
if (connector_edge_puzzle) {
translate([0, 0, -_extra_height]) linear_extrude(height = _extra_height+edge_puzzle_height_female) segment_edge_connectors(false, trace, size, padding, connector);
}
if (numbering && global_segment_index != undef) {
squeeze = len(trace.x) <= 1;
navigate_cell(size, trace, padding, [0, 0]) translate([BASEPLATE_DIMENSIONS.x*trace.x[0]/2-(squeeze?2.95/2:0), -BASEPLATE_DIMENSIONS.y/2+4, -_extra_height]) linear_extrude(number_depth) mirror([0, 1]) rotate([0, 0, 90]) text(str(global_segment_index + 1), size = squeeze ? number_squeeze_size : number_size, halign="right", valign = "center", font = number_font);
}
// extend a bit beyond the segment edges to make sure we cut any overhang
extend = 10;
if (bottom_chamfer[_SOUTH] > 0 && !connector[_SOUTH]) translate([-size.x/2 - extend, -size.y/2, -_extra_height]) rotate([0, 90, 0]) rotate([0, 0, 90]) linear_extrude(size.x + extend * 2) scale(bottom_chamfer[_SOUTH]) chamfer_triangle();
if (bottom_chamfer[_WEST] > 0 && !connector[_WEST]) translate([-size.x/2, -size.y/2 - extend, -_extra_height]) rotate([-90, 0, 0]) rotate([0, 0, -90]) linear_extrude(size.y + extend * 2) scale(bottom_chamfer[_WEST]) chamfer_triangle();
if (bottom_chamfer[_NORTH] > 0 && !connector[_NORTH]) translate([size.x/2 + extend, size.y/2, -_extra_height]) rotate([0, -90, 0]) rotate([0, 0, -90]) linear_extrude(size.x + extend * 2) scale(bottom_chamfer[_NORTH]) chamfer_triangle();
if (bottom_chamfer[_EAST] > 0 && !connector[_EAST]) translate([size.x/2, size.y/2 + extend, -_extra_height]) rotate([90, 0, 0]) rotate([0, 0, 90]) linear_extrude(size.y + extend * 2) scale(bottom_chamfer[_EAST]) chamfer_triangle();
if (top_chamfer[_SOUTH] > 0 && !connector[_SOUTH]) translate([-size.x/2 - extend, -size.y/2, _profile_height]) rotate([0, 90, 0]) linear_extrude(size.x + extend * 2) scale(top_chamfer[_SOUTH]) chamfer_triangle();
if (top_chamfer[_WEST] > 0 && !connector[_WEST]) translate([-size.x/2, -size.y/2 - extend, _profile_height]) rotate([-90, 0, 0]) linear_extrude(size.y + extend * 2) scale(top_chamfer[_WEST]) chamfer_triangle();
if (top_chamfer[_NORTH] > 0 && !connector[_NORTH]) translate([-size.x/2, size.y/2, _profile_height]) rotate([0, 90, 0]) rotate([0, 0, -90]) linear_extrude(size.x + extend * 2) scale(top_chamfer[_NORTH]) chamfer_triangle();
if (top_chamfer[_EAST] > 0 && !connector[_EAST]) translate([size.x/2, size.y/2 + extend, _profile_height]) rotate([90, -90, 0]) rotate([0, 0, 90]) linear_extrude(size.y + extend * 2) scale(top_chamfer[_EAST]) chamfer_triangle();
// vertical screw holes
is_edge_axis = function (index, bounds, inset=0) (index == inset && index <= ceil(bounds - 0.25) - inset) || (index == ceil(bounds - 0.25) - inset && index >= inset);
is_edge = function (index, bounds) is_edge_axis(index.x, bounds.x) || is_edge_axis(index.y, bounds.y);
is_corner = function (index, bounds, inset) is_edge_axis(index.x, bounds.x, inset.x) && is_edge_axis(index.y, bounds.y, inset.y);
for (ix = [0:1:last.x+1]) for (iy = [0:1:last.y+1]) navigate_corner(size, trace, padding, [ix, iy], _SOUTH, _WEST) {
segment_index = [ix, iy];
if (is_corner(segment_index + global_cell_index, global_cell_count, vertical_screw_plate_corner_inset)) {
if (vertical_screw_plate_corners) vertical_screw();
} else if (is_edge(segment_index + global_cell_index, global_cell_count)) {
if (vertical_screw_plate_edges) vertical_screw();
} else if (is_corner(segment_index, [len(trace.x), len(trace.y)], vertical_screw_segment_corner_inset)) {
if (vertical_screw_segment_corners) vertical_screw();
} else if (is_edge(segment_index, [len(trace.x), len(trace.y)])) {
if (vertical_screw_segment_edges) vertical_screw();
} else {
if (vertical_screw_other) vertical_screw();
}
}
}
}