-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLego compatible brick.scad
executable file
·246 lines (196 loc) · 4.9 KB
/
Lego compatible brick.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
/***
* LEGO® compatible brick
*/
include <_conf.scad>;
module lego_braces(x, y, z) {
l = x * LEGO_UNIT_WIDTH;
w = y * LEGO_UNIT_WIDTH;
h = z ? z * LEGO_UNIT_HEIGHT * LEGO_UNITS_Z_PER_BLOCK : LEGO_UNIT_HEIGHT;
alternate = min(x, y) > 1 && max(x, y) > 3;
module wall() {
translate([
-LEGO_PIN_POST_BRACE_THICKNESS / 2,
(x < y ? l : w) / -2,
LEGO_PIN_POST_BRACE_INSET
])
cube([
LEGO_PIN_POST_BRACE_THICKNESS,
x < y ? l : w,
h - LEGO_PIN_POST_BRACE_INSET
]);
}
if (z && ((min(x, y) == 1 && max(x, y) > 1) || max(x, y) > 2)) {
intersection() {
lego_cube(x, y, z);
difference() {
union() {
for (i = [1 : max(x, y) - 1]) {
if (!alternate || i % 2 == 0) {
translate([
x < y ? l / 2 : i * LEGO_UNIT_WIDTH,
x < y ? i * LEGO_UNIT_WIDTH : w / 2,
0
])
rotate([0, 0, x < y ? -90 : 0])
wall();
}
}
}
lego_internals(x, y, z, true);
}
}
}
}
module lego_cavity(x, y, z, wall_thickness = LEGO_WALL_THICKNESS) {
translate([wall_thickness, wall_thickness, -1])
cube([
x * LEGO_UNIT_WIDTH - (wall_thickness * 2),
y * LEGO_UNIT_WIDTH - (wall_thickness * 2),
(z ? z * LEGO_UNIT_HEIGHT * LEGO_UNITS_Z_PER_BLOCK : LEGO_UNIT_HEIGHT) - wall_thickness + 1
]);
}
module lego_compatible_brick(
// units long
x = 1,
// units wide
y = 1,
// units high (typically 0 or 1)
z = 0,
// studded or flat
studs = true,
// material conservation - thinner walls, using ridges for stud contact
ridge_width = 0.6,
wall_thickness = 0.6 // pass 0 for default
) {
// defaults
wall_thickness = wall_thickness ? min(LEGO_WALL_THICKNESS, wall_thickness) : LEGO_WALL_THICKNESS;
// assemble
union() {
// brick hull
difference() {
lego_cube(x, y, z);
lego_cavity(x, y, z, wall_thickness);
}
// flat/studs
if (studs) {
lego_studs(x, y, z, wall_thickness);
}
// (internal) ridges
lego_ridges(x, y, z, ridge_width, wall_thickness);
// pins/posts
lego_internals(x, y, z);
// pin/post bracing
lego_braces(x, y, z);
}
}
module lego_cube(x, y, z) {
translate([LEGO_WIDTH_SPACING / 2, LEGO_WIDTH_SPACING / 2, 0])
cube([
x * LEGO_UNIT_WIDTH - LEGO_WIDTH_SPACING,
y * LEGO_UNIT_WIDTH - LEGO_WIDTH_SPACING,
z ? z * LEGO_UNIT_HEIGHT * LEGO_UNITS_Z_PER_BLOCK : LEGO_UNIT_HEIGHT
]);
}
module lego_internals(x, y, z, bounds) {
inset = LEGO_UNIT_WIDTH;
// posts
if (y > 1 && x > 1) {
for (i = [0 : x - 2], j = [0 : y - 2]) {
translate([
i * LEGO_UNIT_WIDTH + inset,
j * LEGO_UNIT_WIDTH + inset,
0])
lego_post(z, bounds);
}
// pins
} else if ((y > 1 || x > 1)) {
for (i = [1 : max(x, y) - 1], j = [0 : y]) {
translate([
x > 1 ? i * LEGO_UNIT_WIDTH : x * LEGO_UNIT_WIDTH / 2,
y > 1 ? j * LEGO_UNIT_WIDTH : y * LEGO_UNIT_WIDTH / 2,
0
])
lego_pin(z, bounds);
}
}
}
module lego_pin(z, bounds) {
$fs = 0.1;
h = z ? z * LEGO_UNIT_HEIGHT * LEGO_UNITS_Z_PER_BLOCK : LEGO_UNIT_HEIGHT;
difference() {
cylinder(h - LEGO_WALL_THICKNESS, r = (LEGO_UNIT_WIDTH - LEGO_STUD_DIAMETER) / 2);
if (!bounds) {
translate([0, 0, -1])
cylinder(h + 2, r = (LEGO_UNIT_WIDTH - LEGO_STUD_DIAMETER - LEGO_POST_WALL_THICKNESS * 2) / 2);
}
}
}
module lego_post(z, bounds) {
h = z ? z * LEGO_UNIT_HEIGHT * LEGO_UNITS_Z_PER_BLOCK : LEGO_UNIT_HEIGHT;
difference() {
cylinder(h - LEGO_WALL_THICKNESS, r = LEGO_POST_DIAMETER / 2, $fs = 0.1);
if (!bounds) {
translate([0, 0, -1])
cylinder(h + 2, r = (LEGO_POST_DIAMETER - LEGO_POST_WALL_THICKNESS * 2) / 2, $fs = 1);
}
}
}
module lego_ridges(x, y, z, width, wall_thickness) {
l = x * LEGO_UNIT_WIDTH;
w = y * LEGO_UNIT_WIDTH;
h = z ? z * LEGO_UNIT_HEIGHT * LEGO_UNITS_Z_PER_BLOCK : LEGO_UNIT_HEIGHT;
depth = LEGO_WALL_THICKNESS - wall_thickness;
inset = LEGO_UNIT_WIDTH / 2;
module ridge() {
translate([-width / 2, 0, 0])
cube([width, depth, h]);
}
for (i = [0 : x - 1], j = [0 : y - 1]) {
translate([
i * LEGO_UNIT_WIDTH + inset,
LEGO_WALL_THICKNESS - depth,
0
])
ridge();
translate([
i * LEGO_UNIT_WIDTH + inset,
w - LEGO_WALL_THICKNESS,
0
])
ridge();
translate([
LEGO_WALL_THICKNESS,
j * LEGO_UNIT_WIDTH + inset,
0
])
rotate([0, 0, 90])
ridge();
translate([
l - LEGO_WALL_THICKNESS + depth,
j * LEGO_UNIT_WIDTH + inset,
0
])
rotate([0, 0, 90])
ridge();
}
}
module lego_stud(wall_thickness) {
difference() {
cylinder(LEGO_STUD_HEIGHT, r = LEGO_STUD_DIAMETER / 2 , $fs = 0.1);
if (wall_thickness) {
translate([0, 0, -1])
cylinder(LEGO_STUD_HEIGHT - wall_thickness + 1, r = LEGO_STUD_DIAMETER / 2 - wall_thickness, $fs = 1);
}
}
}
module lego_studs(x, y, z, wall_thickness) {
inset = LEGO_UNIT_WIDTH / 2;
for (i = [0 : x - 1], j = [0 : y - 1]) {
translate([
i * LEGO_UNIT_WIDTH + inset,
j * LEGO_UNIT_WIDTH + inset,
z ? z * LEGO_UNIT_HEIGHT * LEGO_UNITS_Z_PER_BLOCK : LEGO_UNIT_HEIGHT
])
lego_stud(wall_thickness);
}
}