-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridfinity_b123.py
More file actions
903 lines (776 loc) · 30.7 KB
/
Copy pathgridfinity_b123.py
File metadata and controls
903 lines (776 loc) · 30.7 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
"""
Ultra Light Gridfinity Bin generator — Python/build123d port of
UltraLightGridfinityBins.scad by HuMa_Meng (CC BY-NC-SA 4.0).
Reproduces the geometry of the OpenSCAD source so STL output matches
what users would get from running OpenSCAD on the original file.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Literal
from build123d import (
Cylinder,
Box,
Plane,
Location,
Pos,
Rot,
RectangleRounded,
Circle,
Mode,
Align,
Part,
Solid,
extrude,
loft,
export_stl,
Compound,
BuildSketch,
Polygon,
)
# ---------------------------------------------------------------------------
# Parameters
# ---------------------------------------------------------------------------
LabelPosition = Literal["Full", "Left", "Center", "Right"]
@dataclass
class GridfinityParams:
# General
grids_x: float = 1.0
grids_y: float = 2.0
grids_z: float = 3.0
# Half grid
half_grid_right: bool = True
half_grid_top: bool = True
half_grid_base: bool = False
# Ultra-light
wall_thickness: float = 1.0
ultra_light_base: bool = True
ultra_light_labels: bool = True
# Magnets
magnets: bool = False
magnet_diameter: float = 6.15
magnet_depth: float = 2.2
# Dividers
dividers: bool = False
dividers_x: int = 0
dividers_y: int = 1
# Labels
labels: bool = False
label_for_each_section: bool = True
label_position: LabelPosition = "Full"
label_width: float = 30.0
label_depth: float = 13.0
# Scoop
scoops: bool = False
scoop_radius: float = 15.0
# ---------------------------------------------------------------------------
# Constants from SCAD source
# ---------------------------------------------------------------------------
OFFSET_XY = 0.25
BASIC_UNIT_XY = 42.0
BASIC_UNIT_Z = 7.0
BASIC_RADIUS_1 = 4.0
BASIC_RADIUS_2 = 8.0
TOP_CLEARANCE_OFFSET = 0.6
STACKING_LIP_WIDTH = 2.6
LABEL_HEIGHT = 1.0
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def rounded_rect_frustum(
x0: float,
y0: float,
width_x: float,
width_y: float,
z0: float,
height: float,
r_bot: float,
r_top: float,
) -> Part:
"""Build the convex hull of 4 cylinders at the corners of a rectangle.
In the SCAD source this pattern is used everywhere via ``hull() { 4
cylinders }``. When all 4 cylinders share their bottom radius and their
top radius, the hull is exactly a prism whose horizontal cross-section is
a rectangle with rounded corners (rectangle's straight edges from the
cylinder positions, corner radius from the cylinder radius).
The arguments mirror the SCAD usage: ``(x0, y0)`` is the rectangle's
bottom-left, ``width_x`` and ``width_y`` are the outer rectangle size
(i.e. corners are at ``(x0+r, y0+r) ... (x0+W-r, y0+H-r)`` style — but we
expose the *rectangle* dimensions here, since that's how it composes).
"""
if height <= 0:
return Part()
cx = x0 + width_x / 2.0
cy = y0 + width_y / 2.0
# Both rounded-rects share the same outer rectangle; only corner radius
# changes between bottom and top.
if abs(r_bot - r_top) < 1e-9:
face = RectangleRounded(width_x, width_y, radius=r_bot).face()
face = Plane.XY.offset(z0) * face
return extrude(face, amount=height)
face_bot = RectangleRounded(width_x, width_y, radius=r_bot).face()
face_bot = Plane.XY.offset(z0) * face_bot
face_top = RectangleRounded(width_x, width_y, radius=r_top).face()
face_top = Plane.XY.offset(z0 + height) * face_top
return loft([face_bot, face_top])
def cyl(x: float, y: float, z: float, h: float, r_bot: float, r_top: float) -> Part:
"""Cylinder with possibly different bottom/top radius (= SCAD cylinder)."""
if h <= 0:
return Part()
if abs(r_bot - r_top) < 1e-9:
return Pos(x, y, z + h / 2.0) * Cylinder(r_bot, h)
# Truncated cone via loft of two circles. Build123d's Cylinder doesn't
# support different radii in 0.10, so use loft of two CircleArcs/Faces.
from build123d import Circle # local import to keep top tidy
f_bot = (Plane.XY.offset(z)) * Circle(r_bot).face()
f_top = (Plane.XY.offset(z + h)) * Circle(r_top).face()
return loft([f_bot, f_top])
def box(x0: float, y0: float, z0: float, w: float, d: float, h: float) -> Part:
"""Axis-aligned box at corner (x0, y0, z0), size (w, d, h)."""
return Pos(x0 + w / 2.0, y0 + d / 2.0, z0 + h / 2.0) * Box(w, d, h)
def _to_part(x):
"""Coerce build123d boolean op results (which may be ShapeList) into a
single shape with .volume / .moved() / boolean ops still working."""
if x is None:
return Part()
# Solid / Compound / Part already work
if isinstance(x, (Solid, Compound, Part)):
return x
# ShapeList or other iterable of shapes
try:
items = list(x)
except TypeError:
return x
if not items:
return Part()
if len(items) == 1:
return items[0]
return Compound(items)
def _union_all(parts: list) -> Part:
"""Boolean-union an arbitrary list of parts, dropping empty ones."""
parts = [p for p in parts if p is not None and getattr(p, "volume", 0) > 1e-9]
if not parts:
return Part()
if len(parts) == 1:
return parts[0]
res = parts[0]
for p in parts[1:]:
res = _to_part(res + p)
return res
def _diff(a, b):
"""Boolean-difference, coercing the result back to a usable shape."""
return _to_part(a - b)
# ---------------------------------------------------------------------------
# Bin pieces
# ---------------------------------------------------------------------------
def _make_magnet_holes(p: GridfinityParams, sx: float, sy: float) -> Part:
"""Magnet cylinder voids placed at the 4 inner corners of one base unit."""
r = p.magnet_diameter / 2.0
h = p.magnet_depth
parts = []
for cx, cy in (
(sx + BASIC_RADIUS_2, sy + BASIC_RADIUS_2),
(sx + BASIC_UNIT_XY - BASIC_RADIUS_2, sy + BASIC_RADIUS_2),
(sx + BASIC_RADIUS_2, sy + BASIC_UNIT_XY - BASIC_RADIUS_2),
(sx + BASIC_UNIT_XY - BASIC_RADIUS_2, sy + BASIC_UNIT_XY - BASIC_RADIUS_2),
):
parts.append(cyl(cx, cy, 0.0, h, r, r))
return _union_all(parts)
def _make_bin_base_single(
p: GridfinityParams,
start_x: float,
start_y: float,
width_x: float,
width_y: float,
) -> Part:
"""One Gridfinity base unit (full / half / quarter sized)."""
radius_0 = 1.05 - OFFSET_XY
radius_1 = 1.85 - OFFSET_XY
radius_2 = BASIC_RADIUS_1 - OFFSET_XY
h0 = 0.80
h1 = 1.80
h2 = 2.15
wall = p.wall_thickness
Wx = width_x * BASIC_UNIT_XY
Wy = width_y * BASIC_UNIT_XY
# Outer block
outer = box(start_x, start_y, 0.0, Wx, Wy, h0 + h1 + h2 + wall)
# Inner hollows (all built relative to the same outer rectangle of corners)
# rect_W, rect_H follow the spacing between the 4 corner cylinders, plus
# corner radius * 2 to give the rounded-rect frustum the right outer size.
# In SCAD the 4 cylinders are at corners (x0+R, y0+R) ... (x0+W-R, y0+W-R)
# so the rounded-rect's outer extents go to (x0, y0) ... (x0+W, y0+W) when
# corner radius == BASIC_RADIUS_1. For radii smaller than BASIC_RADIUS_1
# the outer extents are (x0 + (R-r), y0 + (R-r)) ... (x0 + W - (R-r), ...).
R = BASIC_RADIUS_1
def _frustum_at(z0: float, h: float, rb: float, rt: float) -> Part:
# Outer rectangle whose corners are inset by (R - max(rb, rt)) from the
# block edges — but actually each level has its own size based on its
# own radius. Since loft expects the same outer rectangle on both
# ends, we use the *cylinder centers* spacing plus radii.
# Centers spacing in X: (Wx - 2R) ; in Y: (Wy - 2R).
# Outer rect at the bottom: spacing + 2*rb (= Wx - 2R + 2*rb).
# Outer rect at the top: spacing + 2*rt (= Wx - 2R + 2*rt).
# Loft requires same XY size on both faces — so we need a different
# approach: build each level as a separate rounded-rect prism stack
# since the outer rectangle changes when r changes. But this is the
# convex hull of cylinders, which in 3D produces sloped sides.
#
# Workaround: build the frustum as a loft between two rounded-rects
# of *different* outer sizes — that *is* the convex hull when both
# endpoints share centerline and the rectangle dimensions only differ
# in their corner inflation. build123d's loft will linearly interp.
cx_spacing = Wx - 2 * R
cy_spacing = Wy - 2 * R
outer_w_bot = cx_spacing + 2 * rb
outer_h_bot = cy_spacing + 2 * rb
outer_w_top = cx_spacing + 2 * rt
outer_h_top = cy_spacing + 2 * rt
# Center of the rectangle
cx = start_x + Wx / 2.0
cy = start_y + Wy / 2.0
f_bot = Pos(cx, cy, z0) * RectangleRounded(
outer_w_bot, outer_h_bot, radius=rb
).face()
f_top = Pos(cx, cy, z0 + h) * RectangleRounded(
outer_w_top, outer_h_top, radius=rt
).face()
return loft([f_bot, f_top])
hollows: list[Part] = []
if p.ultra_light_base:
# Layer 1: from z=wall, height h0 - 0.5858*wall, radii r0-0.4142w → r1-w
hollows.append(
_frustum_at(
wall,
h0 - 0.5858 * wall,
radius_0 - 0.4142 * wall,
radius_1 - wall,
)
)
# Layer 2: z=h0+0.4142w, height h1, radii r1-w both ends (prism)
hollows.append(
_frustum_at(h0 + 0.4142 * wall, h1, radius_1 - wall, radius_1 - wall)
)
# Layer 3: z=h0+h1+0.4142w, height h2, radii r1-w → r2-w
hollows.append(
_frustum_at(
h0 + h1 + 0.4142 * wall,
h2,
radius_1 - wall,
radius_2 - wall,
)
)
# Layer 4: z=h0+h1+h2, height 0.4142w, radii r2-1.4142w → r2-w
hollows.append(
_frustum_at(
h0 + h1 + h2,
0.4142 * wall,
radius_2 - 1.4142 * wall,
radius_2 - wall,
)
)
# Layer 5: z=h0+h1+h2, height 1.4142w, radii r2-1.4142w → r2 (overlaps
# layer 4 by design — SCAD union())
hollows.append(
_frustum_at(
h0 + h1 + h2,
1.4142 * wall,
radius_2 - 1.4142 * wall,
radius_2,
)
)
# Magnets are a *void* in the base block — handled together with hollows
if (not p.half_grid_base) and p.magnets and width_x == 1.0 and width_y == 1.0:
hollows.append(_make_magnet_holes(p, start_x, start_y))
if hollows:
result = _diff(outer, _union_all(hollows))
else:
result = outer
# Magnet ring reinforcement — adds a thicker ring around the magnet hole
# so it has wall_thickness around it even after the hollow chops the base.
if (not p.half_grid_base) and p.magnets and width_x == 1.0 and width_y == 1.0:
ring_r_outer = (p.magnet_diameter / 2.0) + p.wall_thickness
ring_h = p.magnet_depth + p.wall_thickness
# Place 4 cylinders, then subtract the magnet holes from them.
c0 = (start_x + R + (BASIC_RADIUS_2 - R), start_y + R + (BASIC_RADIUS_2 - R))
c1 = (start_x + Wx - R - (BASIC_RADIUS_2 - R), start_y + R + (BASIC_RADIUS_2 - R))
c2 = (start_x + R + (BASIC_RADIUS_2 - R), start_y + Wy - R - (BASIC_RADIUS_2 - R))
c3 = (start_x + Wx - R - (BASIC_RADIUS_2 - R), start_y + Wy - R - (BASIC_RADIUS_2 - R))
rings = [cyl(cx, cy, 0.0, ring_h, ring_r_outer, ring_r_outer)
for (cx, cy) in (c0, c1, c2, c3)]
ring_part = _diff(_union_all(rings), _make_magnet_holes(p, start_x, start_y))
result = _union_all([result, ring_part])
return result
def make_bin_base(p: GridfinityParams, gx_: float, gy_: float) -> Part:
"""Tile base units to cover the requested grid footprint."""
parts: list[Part] = []
full_x = int(gx_)
full_y = int(gy_)
half_x = (gx_ - full_x) > 0
half_y = (gy_ - full_y) > 0
for ix in range(full_x):
for iy in range(full_y):
parts.append(
_make_bin_base_single(
p, ix * BASIC_UNIT_XY, iy * BASIC_UNIT_XY, 1.0, 1.0
)
)
if half_x:
for iy in range(full_y):
parts.append(
_make_bin_base_single(
p, full_x * BASIC_UNIT_XY, iy * BASIC_UNIT_XY, 0.5, 1.0
)
)
if half_y:
for ix in range(full_x):
parts.append(
_make_bin_base_single(
p, ix * BASIC_UNIT_XY, full_y * BASIC_UNIT_XY, 1.0, 0.5
)
)
if half_x and half_y:
parts.append(
_make_bin_base_single(
p,
full_x * BASIC_UNIT_XY,
full_y * BASIC_UNIT_XY,
0.5,
0.5,
)
)
return _union_all(parts)
def make_bin_body(p: GridfinityParams, gx_: float, gy_: float) -> Part:
"""Outer rounded-rect tube of the bin (above the base)."""
radius_0 = BASIC_RADIUS_1 - OFFSET_XY
h0 = 4.75
h1 = 7.0 - h0
h2 = (p.grids_z - 1) * BASIC_UNIT_Z
Wx = gx_ * BASIC_UNIT_XY
Wy = gy_ * BASIC_UNIT_XY
outer_w = Wx - 2 * BASIC_RADIUS_1 + 2 * radius_0
outer_h = Wy - 2 * BASIC_RADIUS_1 + 2 * radius_0
cx, cy = Wx / 2.0, Wy / 2.0
profile_outer = Pos(cx, cy, h0) * RectangleRounded(
outer_w, outer_h, radius=radius_0
).face()
outer = extrude(profile_outer, amount=h1 + h2)
inner_r = radius_0 - p.wall_thickness
inner_w = Wx - 2 * BASIC_RADIUS_1 + 2 * inner_r
inner_h = Wy - 2 * BASIC_RADIUS_1 + 2 * inner_r
profile_inner = Pos(cx, cy, h0) * RectangleRounded(
inner_w, inner_h, radius=inner_r
).face()
inner = extrude(profile_inner, amount=h1 + h2)
return _diff(outer, inner)
def make_bin_stacklip(p: GridfinityParams, gx_: float, gy_: float) -> Part:
"""Stacking lip on top of the bin (interlock profile)."""
radius_0 = 1.15
radius_1 = 1.85
radius_2 = BASIC_RADIUS_1 - OFFSET_XY
h0 = STACKING_LIP_WIDTH - p.wall_thickness
h1 = 0.60
h2 = 0.70
h3 = 1.80
h4 = 1.90
h5 = 0.75
h_start = (p.grids_z * BASIC_UNIT_Z) - (h0 + h1)
Wx = gx_ * BASIC_UNIT_XY
Wy = gy_ * BASIC_UNIT_XY
cx, cy = Wx / 2.0, Wy / 2.0
R = BASIC_RADIUS_1
def _ring(z0: float, h: float, rb: float, rt: float) -> Part:
# Outer block goes by radius_2 throughout (spans the full lip)
cx_spacing = Wx - 2 * R
cy_spacing = Wy - 2 * R
f_bot = Pos(cx, cy, z0) * RectangleRounded(
cx_spacing + 2 * rb, cy_spacing + 2 * rb, radius=rb
).face()
f_top = Pos(cx, cy, z0 + h) * RectangleRounded(
cx_spacing + 2 * rt, cy_spacing + 2 * rt, radius=rt
).face()
return loft([f_bot, f_top])
outer_total_h = h0 + h1 + h2 + h3 + h4
outer = _ring(h_start, outer_total_h, radius_2, radius_2)
# Inner subtractions
voids = []
voids.append(_ring(h_start, h0, radius_2 - p.wall_thickness, radius_0))
voids.append(_ring(h_start + h0, h1, radius_0, radius_0))
voids.append(_ring(h_start + h0 + h1, h2, radius_0, radius_1))
voids.append(_ring(h_start + h0 + h1 + h2, h3, radius_1, radius_1))
voids.append(_ring(h_start + h0 + h1 + h2 + h3, h4, radius_1, radius_2))
voids.append(
_ring(h_start + h0 + h1 + h2 + h3 + h4 - h5, h5, radius_2, radius_2)
)
return _diff(outer, _union_all(voids))
def make_bin_dividers(p: GridfinityParams, gx_: float, gy_: float) -> Part:
"""Vertical dividers inside the bin."""
if not p.dividers or (p.dividers_x <= 0 and p.dividers_y <= 0):
return Part()
radius_0 = 1.10
Wx = gx_ * BASIC_UNIT_XY
Wy = gy_ * BASIC_UNIT_XY
R = BASIC_RADIUS_1
wall = p.wall_thickness
radius_spacing_x = (
Wx - 2 * OFFSET_XY - 2 * wall - 2 * radius_0 - p.dividers_x * (wall + 2 * radius_0)
) / (p.dividers_x + 1)
radius_spacing_y = (
Wy - 2 * OFFSET_XY - 2 * wall - 2 * radius_0 - p.dividers_y * (wall + 2 * radius_0)
) / (p.dividers_y + 1)
start_xy = OFFSET_XY + wall + radius_0
# The "outer hull" is the bin interior up to (Grids_Z * 7 - clearance).
# Loft of 4 cylinders r=BASIC_RADIUS_1-OFFSET_XY at corner positions, from
# z=wall up to z=grids_z*7 - clearance. This is just an extrusion of a
# rounded rect (same radius top/bottom).
outer_r = BASIC_RADIUS_1 - OFFSET_XY
z0 = wall
z_h = (p.grids_z * BASIC_UNIT_Z) - TOP_CLEARANCE_OFFSET - wall
cx_spacing = Wx - 2 * R
cy_spacing = Wy - 2 * R
cx, cy = Wx / 2.0, Wy / 2.0
outer_face = Pos(cx, cy, z0) * RectangleRounded(
cx_spacing + 2 * outer_r, cy_spacing + 2 * outer_r, radius=outer_r
).face()
outer = extrude(outer_face, amount=z_h)
# Subtract divider sections — each is a rounded rect prism that fills a
# cell, leaving wall_thickness wide separators.
voids = []
full_h = p.grids_z * BASIC_UNIT_Z
for dx in range(p.dividers_x + 1):
for dy in range(p.dividers_y + 1):
# rounded rect with corner radius radius_0, dimensions
# (radius_spacing_x + 2*r0) x (radius_spacing_y + 2*r0)
cell_w = radius_spacing_x + 2 * radius_0
cell_h = radius_spacing_y + 2 * radius_0
ccx = (
start_xy
+ dx * (radius_spacing_x + 2 * radius_0 + wall)
+ cell_w / 2.0
- radius_0
)
ccy = (
start_xy
+ dy * (radius_spacing_y + 2 * radius_0 + wall)
+ cell_h / 2.0
- radius_0
)
face = Pos(ccx, ccy, 0.0) * RectangleRounded(
cell_w, cell_h, radius=radius_0
).face()
voids.append(extrude(face, amount=full_h))
return _diff(outer, _union_all(voids))
def _make_bin_label(
p: GridfinityParams,
gx_: float,
gy_: float,
start_x: float,
start_y: float,
width: float,
depth: float,
) -> Part:
"""One label tap (top slab + triangular rip below it)."""
z_top = (p.grids_z * BASIC_UNIT_Z) - TOP_CLEARANCE_OFFSET
# Top slab (the actual label surface, 1 mm thick)
slab = box(start_x, start_y - depth, z_top - LABEL_HEIGHT, width, depth, LABEL_HEIGHT)
# Triangular prism beneath the slab — equivalent to SCAD hull() of two
# thin rectangles. Cross section in YZ at any X is a right triangle with
# vertices:
# A = (start_y - depth, z_top - LH) (top-back)
# B = (start_y, z_top - LH) (top-front)
# C = (start_y, z_top - LH - depth)(bottom-front)
with BuildSketch(Plane.YZ) as sk:
Polygon(
(start_y - depth, z_top - LABEL_HEIGHT),
(start_y, z_top - LABEL_HEIGHT),
(start_y, z_top - LABEL_HEIGHT - depth),
align=None,
)
triangle = extrude(sk.sketch, amount=width)
# Plane.YZ extrudes along +X. Move the prism to start_x.
triangle = Pos(start_x, 0, 0) * triangle
# Optional rip cutouts in the triangle (slab is left intact).
if p.ultra_light_labels:
rips_max_bridge = 13.0
wall = p.wall_thickness
import math
if p.label_position == "Full" and p.dividers:
rips = math.ceil(
(gx_ * BASIC_UNIT_XY + p.dividers_x * wall - 2 * OFFSET_XY)
/ (rips_max_bridge * (p.dividers_x + 1))
) * (p.dividers_x + 1) - p.dividers_x
else:
rips = math.ceil(width / rips_max_bridge) + 1
if rips < 2:
rips = 2
rips_distance = (width - rips * wall) / (rips - 1)
rip_voids = []
for r in range(rips - 1):
rx = start_x + wall + r * (rips_distance + wall)
rip_voids.append(
box(rx, start_y - depth, z_top - LABEL_HEIGHT - depth,
rips_distance, depth, depth)
)
if rip_voids:
triangle = _diff(triangle, _union_all(rip_voids))
return _union_all([slab, triangle])
def make_bin_labels(p: GridfinityParams, gx_: float, gy_: float) -> Part:
"""All label taps for the bin."""
if not p.labels:
return Part()
dividers_x = p.dividers_x if (p.dividers and p.label_for_each_section) else 0
dividers_y = p.dividers_y if (p.dividers and p.label_for_each_section) else 0
Wx = gx_ * BASIC_UNIT_XY
Wy = gy_ * BASIC_UNIT_XY
wall = p.wall_thickness
parts: list[Part] = []
if p.label_position == "Full":
for dy in range(dividers_y + 1):
label_w = Wx - 2 * OFFSET_XY
label_d = (p.label_depth + STACKING_LIP_WIDTH) if dy == 0 else p.label_depth
sy = (
Wy * (dividers_y + 1 - dy)
- OFFSET_XY * (dividers_y + 1 - 2.0 * dy)
+ wall * dy
) / (dividers_y + 1)
parts.append(_make_bin_label(p, gx_, gy_, OFFSET_XY, sy, label_w, label_d))
elif p.label_position == "Left":
for dx in range(dividers_x + 1):
for dy in range(dividers_y + 1):
label_w = (p.label_width + STACKING_LIP_WIDTH) if dx == 0 else p.label_width
label_d = (p.label_depth + STACKING_LIP_WIDTH) if dy == 0 else p.label_depth
sx = OFFSET_XY + ((Wx - 2 * OFFSET_XY - wall) / (dividers_x + 1)) * dx
sy = (Wy - OFFSET_XY) - ((Wy - 2 * OFFSET_XY - wall) / (dividers_y + 1)) * dy
parts.append(_make_bin_label(p, gx_, gy_, sx, sy, label_w, label_d))
elif p.label_position == "Center":
for dx in range(dividers_x + 1):
for dy in range(dividers_y + 1):
label_w = p.label_width
label_d = (p.label_depth + STACKING_LIP_WIDTH) if dy == 0 else p.label_depth
cell_w = (Wx - 2 * OFFSET_XY - wall) / (dividers_x + 1)
sx = OFFSET_XY + cell_w * dx + cell_w / 2.0 - label_w / 2.0
sy = (Wy - OFFSET_XY) - ((Wy - 2 * OFFSET_XY - wall) / (dividers_y + 1)) * dy
parts.append(_make_bin_label(p, gx_, gy_, sx, sy, label_w, label_d))
elif p.label_position == "Right":
for dx in range(dividers_x + 1):
for dy in range(dividers_y + 1):
label_w = (
(p.label_width + STACKING_LIP_WIDTH) if dx == dividers_x else p.label_width
)
label_d = (p.label_depth + STACKING_LIP_WIDTH) if dy == 0 else p.label_depth
cell_w = (Wx - 2 * OFFSET_XY - wall) / (dividers_x + 1)
sx = OFFSET_XY + cell_w * dx + (cell_w + wall) - label_w
sy = (Wy - OFFSET_XY) - ((Wy - 2 * OFFSET_XY - wall) / (dividers_y + 1)) * dy
parts.append(_make_bin_label(p, gx_, gy_, sx, sy, label_w, label_d))
return _union_all(parts)
def _make_scoop_one(p: GridfinityParams, gx_: float, gy_: float, start_y: float, spacing: float) -> Part:
Wx = gx_ * BASIC_UNIT_XY
Wy = gy_ * BASIC_UNIT_XY
wall = p.wall_thickness
h0 = 0.6
h1 = 0 if p.ultra_light_base else 4.75
Sr = p.scoop_radius
# Outer wedge: cube extending the scoop region
outer = box(
OFFSET_XY,
start_y,
wall,
Wx - 2 * OFFSET_XY,
Sr + spacing,
(gy_ * BASIC_UNIT_Z) - wall - h0, # SCAD: Grids_Z_ * Basic_Unit_Z, but Grids_Z_ == Grids_Z
)
# SCAD source uses Grids_Z_, which equals Grids_Z when not half_grid_base.
# Recompute with the correct factor:
outer = box(
OFFSET_XY,
start_y,
wall,
Wx - 2 * OFFSET_XY,
Sr + spacing,
(p.grids_z * BASIC_UNIT_Z) - wall - h0,
)
# Subtract a cylinder along X (the scoop curve)
cx0 = OFFSET_XY # SCAD translates to this X then rotates 90 around Y axis
cyl_y = start_y + Sr + spacing
cyl_z = wall + Sr + h1
# Cylinder of radius Sr and length (Wx - 2*OFFSET_XY) along +X
from build123d import Cylinder, Rot
cyl_len = Wx - 2 * OFFSET_XY
scoop_cyl = (
Pos(cx0 + cyl_len / 2.0, cyl_y, cyl_z)
* Rot(0, 90, 0)
* Cylinder(Sr, cyl_len)
)
# And a box above the cylinder centerline (so the scoop opens upward)
above = box(
OFFSET_XY,
start_y + spacing,
wall + Sr + h1,
Wx - 2 * OFFSET_XY,
2 * Sr,
p.grids_z * BASIC_UNIT_Z,
)
return _diff(outer, _union_all([scoop_cyl, above]))
def make_bin_scoops(p: GridfinityParams, gx_: float, gy_: float) -> Part:
if not p.scoops or p.scoop_radius <= 0:
return Part()
dividers_y = p.dividers_y if p.dividers else 0
Wy = gy_ * BASIC_UNIT_XY
wall = p.wall_thickness
parts: list[Part] = []
for dy in range(dividers_y + 1):
spacing = STACKING_LIP_WIDTH if dy == 0 else wall
sy = OFFSET_XY + ((Wy - 2 * OFFSET_XY - wall) / (dividers_y + 1)) * dy
parts.append(_make_scoop_one(p, gx_, gy_, sy, spacing))
return _union_all(parts)
def _make_bin_clean_single(
p: GridfinityParams,
start_x: float,
start_y: float,
width_x: float,
width_y: float,
) -> Part:
"""Per-base 'clean' subtraction shape — chamfers the foot profile."""
radius_0 = 1.05 - OFFSET_XY
radius_1 = 1.85 - OFFSET_XY
radius_2 = BASIC_RADIUS_1 - OFFSET_XY
h0 = 0.80
h1 = 1.80
h2 = 2.15
Wx = width_x * BASIC_UNIT_XY
Wy = width_y * BASIC_UNIT_XY
R = BASIC_RADIUS_1
cx, cy = start_x + Wx / 2.0, start_y + Wy / 2.0
cx_spacing = Wx - 2 * R
cy_spacing = Wy - 2 * R
def _frustum(z0: float, h: float, rb: float, rt: float) -> Part:
f_b = Pos(cx, cy, z0) * RectangleRounded(
cx_spacing + 2 * rb, cy_spacing + 2 * rb, radius=rb
).face()
f_t = Pos(cx, cy, z0 + h) * RectangleRounded(
cx_spacing + 2 * rt, cy_spacing + 2 * rt, radius=rt
).face()
return loft([f_b, f_t])
outer = box(start_x, start_y, 0.0, Wx, Wy, h0 + h1 + h2)
voids = [
_frustum(0.0, h0, radius_0, radius_1),
_frustum(h0, h1, radius_1, radius_1),
_frustum(h0 + h1, h2, radius_1, radius_2),
]
res = _diff(outer, _union_all(voids))
if (not p.half_grid_base) and p.magnets and width_x == 1.0 and width_y == 1.0:
res = _union_all([res, _make_magnet_holes(p, start_x, start_y)])
return res
def make_bin_clean(p: GridfinityParams, gx_: float, gy_: float) -> Part:
"""Subtraction volume that gives the bin its rounded outer profile.
SCAD subtracts this from the union of base+body+lip+... at the end.
"""
parts: list[Part] = []
full_x = int(gx_)
full_y = int(gy_)
half_x = (gx_ - full_x) > 0
half_y = (gy_ - full_y) > 0
for ix in range(full_x):
for iy in range(full_y):
parts.append(
_make_bin_clean_single(
p, ix * BASIC_UNIT_XY, iy * BASIC_UNIT_XY, 1.0, 1.0
)
)
if half_x:
for iy in range(full_y):
parts.append(
_make_bin_clean_single(
p, full_x * BASIC_UNIT_XY, iy * BASIC_UNIT_XY, 0.5, 1.0
)
)
if half_y:
for ix in range(full_x):
parts.append(
_make_bin_clean_single(
p, ix * BASIC_UNIT_XY, full_y * BASIC_UNIT_XY, 1.0, 0.5
)
)
if half_x and half_y:
parts.append(
_make_bin_clean_single(
p,
full_x * BASIC_UNIT_XY,
full_y * BASIC_UNIT_XY,
0.5,
0.5,
)
)
base_clean = _union_all(parts)
# Outer "all-around" shape
radius_0 = BASIC_RADIUS_1 - OFFSET_XY
h0 = 4.40
Wx = gx_ * BASIC_UNIT_XY
Wy = gy_ * BASIC_UNIT_XY
R = BASIC_RADIUS_1
cx_spacing = Wx - 2 * R
cy_spacing = Wy - 2 * R
cx, cy = Wx / 2.0, Wy / 2.0
outer_face = Pos(cx, cy, 0.0) * RectangleRounded(
cx_spacing + 2 * radius_0, cy_spacing + 2 * radius_0, radius=radius_0
).face()
outer_extrude = extrude(outer_face, amount=p.grids_z * BASIC_UNIT_Z + h0)
outer_box = box(0, 0, 0, Wx, Wy, p.grids_z * BASIC_UNIT_Z + h0)
around = _diff(outer_box, outer_extrude)
below = box(0, 0, -12.0, Wx, Wy, 12.0)
return _union_all([base_clean, around, below])
# ---------------------------------------------------------------------------
# Top-level
# ---------------------------------------------------------------------------
def _mirror_xy(part: Part, mirror_x: bool, mirror_y: bool, Wx: float, Wy: float) -> Part:
"""Apply same mirroring transformations the SCAD source uses for half grids."""
if not mirror_x and not mirror_y:
return part
from build123d import Plane, Pos as _Pos
if mirror_x and not mirror_y:
# translate([Wx,0,0]) mirror([1,0,0])
part = part.mirror(Plane.YZ)
part = _Pos(Wx, 0, 0) * part
return part
if not mirror_x and mirror_y:
part = part.mirror(Plane.XZ)
part = _Pos(0, Wy, 0) * part
return part
# both
part = part.mirror(Plane.YZ).mirror(Plane.XZ)
part = _Pos(Wx, Wy, 0) * part
return part
def build_bin(p: GridfinityParams) -> Part:
"""Assemble the whole bin from its pieces."""
gx_ = 2.0 * p.grids_x if p.half_grid_base else p.grids_x
gy_ = 2.0 * p.grids_y if p.half_grid_base else p.grids_y
Wx = gx_ * BASIC_UNIT_XY
Wy = gy_ * BASIC_UNIT_XY
base = make_bin_base(p, gx_, gy_)
base = _mirror_xy(base, not p.half_grid_right, not p.half_grid_top, Wx, Wy)
body = make_bin_body(p, gx_, gy_)
lip = make_bin_stacklip(p, gx_, gy_)
pieces = [base, body, lip]
if p.dividers and (p.dividers_x > 0 or p.dividers_y > 0):
pieces.append(make_bin_dividers(p, gx_, gy_))
if p.labels:
pieces.append(make_bin_labels(p, gx_, gy_))
if p.scoops:
pieces.append(make_bin_scoops(p, gx_, gy_))
union_part = _union_all(pieces)
clean = make_bin_clean(p, gx_, gy_)
clean = _mirror_xy(clean, not p.half_grid_right, not p.half_grid_top, Wx, Wy)
result = _diff(union_part, clean)
# SCAD translates the whole thing so origin is centered on the footprint
result = Pos(-Wx / 2.0, -Wy / 2.0, 0) * result
return result
def export(p: GridfinityParams, path: str, tolerance: float = 0.1) -> None:
"""Generate the bin and export to STL."""
part = build_bin(p)
export_stl(part, path, tolerance=tolerance)
if __name__ == "__main__":
# Smoke test
import sys
p = GridfinityParams()
out = sys.argv[1] if len(sys.argv) > 1 else "/tmp/gridfinity_default.stl"
export(p, out)
print(f"Wrote {out}")