-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnodes_edges.scad
More file actions
390 lines (330 loc) · 10.2 KB
/
Copy pathnodes_edges.scad
File metadata and controls
390 lines (330 loc) · 10.2 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
/* Nodes and Edges */
/*
* TODO:
*
* Shape of edge rims is not as good as it could be for RoundedOut
* Get rid of the magic 2 related to OutRadius
* Redo math so that when SpaceX == SpaceY, and EdgeLengthXFactor == EdgeLengthXYFactor, it all looks good
* Add additional rim types / styles, eg solid
* Add optional diagonal "X" that is useful when OffSetOdd is false
* Rename rim to be more general
* Connect nodes in odd rows in Y direction
* Ability to print a subset as a fancy hexagon
* Fix base edge size calculations to work for all node shapes
*/
// Node radius
_NodeSize = 9;
// Node shape (0 for circle, , 4 for square, 6 for hexagon)
_NodeShape = 0; // [0, 4, 6]
// Edge shape
_EdgeShape = "Rectangle"; // ["Rectangle", "RoundedOut"]
// Offset odd rows
_OffsetOdd = true;
// Column count
_CountX = 7;
// Row count
_CountY = 7;
// Row spacing
_SpaceY = 30;
// Column spacing
_SpaceX = 32;
// Edge width
_EdgeWidth = 6;
// Percentage of full length for edges in X direction
_EdgeLengthXFactor = 0.9;
// Percentage of full length for edges in XY direction
_EdgeLengthXYFactor = 0.9;
/* [Rim] */
// Rim thickness
_RimThickness = 0.5;
// Node rim count
_NodeRimCount = 3;
// Edge rim count
_EdgeRimCount = 3;
// Node rim spacing
_NodeRimSpacing = 2.0;
// Edge rim spacing
_EdgeRimSpacing = 1.5;
// Base extra (X and Y)
_BaseExtra = 10.0;
/* [Heights] */
// Base height
_BaseHeight = 0.2;
// Node height
_NodeHeight = 1.2;
// Edge height
_EdgeHeight = 1.2;
// Node rim height
_NodeRimHeight = 0.4;
// Edge rim height
_EdgeRimHeight = 0.4;
/* [Extruders] */
// Node extruder
_NodeExtruder = 1;
// Edge extruder
_EdgeExtruder = 2;
// Node rim extruder
_NodeRimExtruder = 3;
// Edge rim extruder
_EdgeRimExtruder = 4;
// Base extruder
_BaseExtruder = 0;
// [Extruder to render]
_WhichExtruder = "All"; // ["All", 1, 2, 3, 4, 5]
// Map a value of _WhichExtruder to an OpenSCAD color
function ExtruderColor(Extruder) =
(Extruder == 1 ) ? "red" :
(Extruder == 2 ) ? "green" :
(Extruder == 3 ) ? "blue" :
(Extruder == 4 ) ? "pink" :
(Extruder == 5 ) ? "yellow" :
"purple" ;
/* End of customization */
module __Customizer_Limit__ () {}
// Possible shift for odd rows
_OddShiftX = _OffsetOdd ? (_SpaceX / 2) : 0;
// Node rotation
NodeRotation = (_NodeShape == 0) ? 0 : /* Circle */
(_NodeShape == 4) ? 45 : /* Square */
(_NodeShape == 6) ? 30 : /* Hexagon */
0;
// If _WhichExtruder is "All" or is not "All" and matches the
// requested extruder, render the child nodes.
module Extruder(DoExtruder)
{
color(ExtruderColor(DoExtruder))
{
if (_WhichExtruder == "All" || DoExtruder == _WhichExtruder)
{
children();
}
}
}
// Render a node, with a rim
module Node(NodeShape, Radius, Height, RimHeight, RimThickness, RimCount, RimSpacing, NodeExtruder, RimExtruder)
{
rotate([0, 0, NodeRotation])
{
union()
{
/* Node */
Extruder(NodeExtruder)
{
linear_extrude(Height)
{
circle(Radius, $fn=NodeShape);
}
}
/* Rim */
Extruder(RimExtruder)
{
translate([0, 0, Height])
{
for (R = [0 : RimCount - 1])
{
dd = R * RimSpacing;
if ((Radius - dd) > 0)
{
linear_extrude(RimHeight)
{
difference()
{
circle(Radius - dd, $fn=NodeShape);
offset(delta=-RimThickness)
{
circle(Radius - dd, $fn=NodeShape);
}
}
}
}
}
}
}
}
}
}
// Render the element that makes up the edge
//
// Length is desired length when Shape is Rectangle
// LengthFull is desired length when shape is RoundedOut
//
module EdgeElement(Shape, Length, Width, LengthFull, OutRadius)
{
if (Shape == "Rectangle")
{
if (Length > 0)
{
square([Length, Width], center=true);
}
}
if (Shape == "RoundedOut")
{
if (LengthFull > 0)
{
difference()
{
square([LengthFull, Width], center=true);
union()
{
translate([-LengthFull, 0, 0]) circle(OutRadius, $fn=99);
translate([LengthFull, 0, 0]) circle(OutRadius, $fn=99);
}
}
}
}
}
// Render an edge, with a rim
module Edge(EdgeShape, Length, LengthFull, Width, Height, RimHeight, RimThickness, RimCount, RimSpacing, EdgeExtruder, RimExtruder, OutRadius)
{
union()
{
/* Edge */
{
Extruder(EdgeExtruder)
{
linear_extrude(Height)
{
EdgeElement(EdgeShape, Length, Width, LengthFull, OutRadius);
}
}
}
/* Rim */
Extruder(RimExtruder)
{
for (R = [0 : RimCount - 1])
{
dd = R * RimSpacing;
if (((LengthFull - dd) > 0) && ((Width - dd) > 0))
{
translate([0, 0, Height])
{
linear_extrude(RimHeight)
{
difference()
{
EdgeElement(EdgeShape, LengthFull - dd, Width - dd, LengthFull, OutRadius);
offset(delta=-RimThickness)
{
EdgeElement(EdgeShape, LengthFull - dd, Width - dd, LengthFull, OutRadius);
}
}
}
}
}
}
}
}
}
// Compute center of a node (x,y)
function NodeX(x, y, OddShiftX, SpaceX) = ((y % 2) == 1) ? OddShiftX + (x * SpaceX) : (x * SpaceX);
function NodeY(x, y, SpaceY) = y * SpaceY;
// Render nodes and edges
module NodesAndEdges(CountX, CountY, SpaceX, SpaceY, AngA, OddShiftX, OffsetOdd, NodeShape, EdgeShape, NodeSize, NodeHeight, NodeRimHeight, NodeRimCount, NodeRimSpacing, EdgeLengthX, EdgeLengthXY, EdgeWidth, EdgeHeight, EdgeRimHeight, EdgeRimCount, EdgeRimSpacing, RimThickness, NodeExtruder, EdgeExtruder, NodeRimExtruder, EdgeRimExtruder)
{
/* Nodes */
for (x = [0 : CountX - 1])
{
for (y = [0 : CountY - 1])
{
PtX = NodeX(x, y, OddShiftX, SpaceX);
PtY = NodeY(x, y, SpaceY);
translate([PtX, PtY, 0])
{
Node(NodeShape, NodeSize, NodeHeight, NodeRimHeight, RimThickness, NodeRimCount, NodeRimSpacing, NodeExtruder, NodeRimExtruder);
}
}
}
/* Edges */
OutRadius = NodeSize + 2;
for (x = [0 : CountX - 1])
{
for (y = [0 : CountY - 1])
{
StartPtX = NodeX(x, y, OddShiftX, SpaceX);
StartPtY = NodeY(x, y, SpaceY);
/* Edges along X axis */
if (x != CountX - 1)
{
EndPtX = NodeX(x + 1, y, OddShiftX, SpaceX);
EndPtY = NodeY(x, y, SpaceY);
MidPtX = (EndPtX - StartPtX) / 2;
MidPtY = (EndPtY - StartPtY) / 2;
translate([StartPtX + MidPtX, StartPtY + MidPtY, 0])
{
Edge(EdgeShape, EdgeLengthX, EdgeLengthX, EdgeWidth, EdgeHeight, EdgeRimHeight, RimThickness, EdgeRimCount, EdgeRimSpacing, EdgeExtruder, EdgeRimExtruder, OutRadius);
}
}
/* Forward edges between Y and Y+1 */
if (y < (CountY - 1))
{
if ((!OffsetOdd) ||
((y % 2) == 0) ||
(((y % 2) == 1) && (x != (CountX - 1))))
{
XXX = (OffsetOdd && (y % 2) == 1) ? x + 1: x;
FwdEndPtX = NodeX(XXX, y + 1, OddShiftX, SpaceX);
FwdEndPtY = NodeY(XXX, y + 1, SpaceY);
FwdMidPtX = (FwdEndPtX - StartPtX) / 2;
FwdMidPtY = (FwdEndPtY - StartPtY) / 2;
translate([StartPtX + FwdMidPtX, StartPtY + FwdMidPtY, 0])
{
rotate([0, 0, AngA])
{
Edge(EdgeShape, EdgeLengthXY, EdgeLengthXY, EdgeWidth, EdgeHeight, EdgeRimHeight, RimThickness, EdgeRimCount, EdgeRimSpacing, EdgeExtruder, EdgeRimExtruder, OutRadius);
}
}
}
}
/* Backward edges between Y and Y+1 */
if (OffsetOdd && (y < (CountY - 1)))
{
if ((x != 0) ||
((x == 0) && ((y % 2) == 1)))
{
XXX = ((y % 2) == 1) ? x : x - 1;
BwdEndPtX = NodeX(XXX, y + 1, OddShiftX, SpaceX);
BwdEndPtY = NodeY(XXX, y + 1, SpaceY);
BwdMidPtX = (BwdEndPtX - StartPtX) / 2;
BwdMidPtY = (BwdEndPtY - StartPtY) / 2;
translate([StartPtX + BwdMidPtX, StartPtY + BwdMidPtY, 0])
{
rotate([0, 0, 180-AngA])
{
Edge(EdgeShape, EdgeLengthXY, EdgeLengthXY, EdgeWidth, EdgeHeight, EdgeRimHeight, RimThickness, EdgeRimCount, EdgeRimSpacing, EdgeExtruder, EdgeRimExtruder, OutRadius);
}
}
}
}
}
}
}
module main(CountX, CountY, SpaceX, SpaceY, OddShiftX, OffsetOdd, NodeShape, EdgeShape, NodeSize, NodeHeight, NodeRimHeight, NodeRimCount, NodeRimSpacing, EdgeWidth, EdgeHeight, EdgeRimHeight, EdgeRimCount, EdgeRimSpacing, EdgeLengthXFactor, EdgeLengthXYFactor, BaseHeight, BaseExtra, RimThickness, BaseExtruder, NodeExtruder, EdgeExtruder, NodeRimExtruder, EdgeRimExtruder)
{
/* Compute angle for edges, special case if not offsetting odd rows */
C = SpaceX / 2;
A = SpaceY;
B = sqrt(A^2 + C^2 - (2 * A * C) * cos(90));
AngA = OffsetOdd ? acos((B^2 + C^2 - A^2) / (2 * B * C)) : 90;
/* Compute edge lengths */
EdgeLengthX = (SpaceX - 2 * NodeSize) * EdgeLengthXFactor;
EdgeLengthXY = (B - 2 * NodeSize) * EdgeLengthXYFactor;
echo("EdgeLengthX", EdgeLengthX);
echo("EdgeLengthXY", EdgeLengthXY);
/* Render nodes and edges to connect them */
NodesAndEdges(CountX, CountY, SpaceX, SpaceY, AngA, OddShiftX, OffsetOdd, NodeShape, EdgeShape, NodeSize, NodeHeight, NodeRimHeight, NodeRimCount, NodeRimSpacing, EdgeLengthX, EdgeLengthXY, EdgeWidth, EdgeHeight, EdgeRimHeight, EdgeRimCount, EdgeRimSpacing, RimThickness, NodeExtruder, EdgeExtruder, NodeRimExtruder, EdgeRimExtruder);
/* Render optional base */
if (BaseHeight > 0)
{
/* Compute size of base */
TotalX = (CountX - 1) * SpaceX + (SpaceX / 2) + NodeSize + NodeSize;
TotalY = (CountY - 1) * SpaceY + (SpaceY / 2);
translate([-(NodeSize + BaseExtra / 2), -(NodeSize + BaseExtra / 2), - BaseHeight])
{
Extruder(BaseExtruder)
{
cube([TotalX + BaseExtra, TotalY + BaseExtra, BaseHeight]);
}
}
}
}
main(_CountX, _CountY, _SpaceX, _SpaceY, _OddShiftX, _OffsetOdd, _NodeShape, _EdgeShape, _NodeSize, _NodeHeight, _NodeRimHeight, _NodeRimCount, _NodeRimSpacing, _EdgeWidth, _EdgeHeight, _EdgeRimHeight, _EdgeRimCount,_EdgeRimSpacing, _EdgeLengthXFactor, _EdgeLengthXYFactor, _BaseHeight, _BaseExtra, _RimThickness, _BaseExtruder, _NodeExtruder, _EdgeExtruder, _NodeRimExtruder, _EdgeRimExtruder);