-
Notifications
You must be signed in to change notification settings - Fork 6
/
living_hinge.py
executable file
·484 lines (407 loc) · 15.2 KB
/
living_hinge.py
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
#!/usr/bin/env python
# These two lines are only needed if you don't put the script directly into
# the installation directory
import sys
sys.path.append("/usr/share/inkscape/extensions")
# We will use the inkex module with the predefined Effect base class.
import inkex
# The simplestyle module provides functions for style parsing.
from simplestyle import *
cut_colour = '#ff0000'
engrave_colour = '#0000ff'
class Generator(object):
"""A generic generator, subclassed for each different lattice style."""
def __init__(self, x, y, width, height, stroke_width, svg, e_length, p_spacing):
self.x = x
self.y = y
self.width = width
self.height = height
self.stroke_width = stroke_width
self.svg = svg
self.canvas = self.svg.get_current_layer()
self.e_length = e_length
self.e_height = 0 # Provided by sub-classes.
self.p_spacing = p_spacing
self.fixed_commands = ""
def draw_one(self, x, y):
return "M %f,%f %s" % (x, y, self.fixed_commands)
def parameter_text(self):
return "length: %.1f spacing: %.f" % (
self.e_length,
self.p_spacing,
)
def draw_swatch(self):
border = self.canvas.add(inkex.PathElement())
# Curve radius
cr = self.svg.unittouu('10mm')
# Swatch padding
sp = self.svg.unittouu('30mm')
# Handle length
hl = cr/2
path_command = (
'm %f,%f l %f,%f c %f,%f %f,%f %f,%f'
'l %f,%f c %f,%f %f,%f %f,%f '
'l %f,%f c %f,%f %f,%f %f,%f '
'l %f,%f c %f,%f %f,%f %f,%f ') % (
cr, 0,
self.width - 2*cr, 0,
hl, 0,
cr, cr-hl,
cr, cr,
0, self.height - 2*cr + 1.5*sp,
0, cr/2,
0-cr+hl, cr,
0-cr, cr,
0-self.width + 2*cr, 0,
0-hl, 0,
0-cr, 0-cr+hl,
0-cr, 0-cr,
0, 0-self.height - 1.5*sp + 2*cr,
0, 0-hl,
cr-hl, 0-cr,
cr, 0-cr
)
style = {
"stroke": cut_colour,
"stroke-width": str(self.stroke_width),
"fill": "none",
}
border.update(**{"style": style, "inkscape:label": "lattice_border", "d": path_command})
c = self.canvas.add(inkex.Circle(
style=str(inkex.Style(style)),
cx=str(cr),
cy=str(cr),
r=str(self.svg.unittouu('4mm'))))
self.y += sp
text_style = {
'fill': engrave_colour,
'font-size': '9px',
'font-family': 'sans-serif',
'text-anchor': 'middle',
'text-align': 'center',
}
text = self.canvas.add(
inkex.TextElement(
style=str(inkex.Style(text_style)),
x=str(self.x + self.width/2),
y=str(self.y - sp/2)))
text.text = "Style: %s" % self.name
text_style['font-size'] = "3px"
text = self.canvas.add(
inkex.TextElement(
style=str(inkex.Style(text_style)),
x=str(self.x + self.width/2),
y=str(self.y - sp/4)))
text.text = self.parameter_text()
text = self.canvas.add(
inkex.TextElement(
style=str(inkex.Style(text_style)),
x=str(self.x + self.width/2),
y=str(self.y +self.height + sp/4)))
text.text = "https://github.com/buxtronix/living-hinge"
def generate(self, swatch):
if swatch:
self.draw_swatch()
# Round width/height to integer number of patterns.
x_patterns = max(round(self.width / self.e_length), 1.0)
y_patterns = max(round(self.height / self.e_height), 1.0)
self.e_length = self.width / x_patterns
self.e_height = self.height / y_patterns
self.prerender()
style = {
"stroke": cut_colour,
"stroke-width": str(self.stroke_width),
"fill": "none",
}
path_command = ""
y = self.y
for _ in range (y_patterns):
x = self.x
for _ in range(x_patterns):
path_command = "%s %s " % (path_command, self.draw_one(x, y))
x += self.e_length
y += self.e_height
link = self.canvas.add(inkex.PathElement())
link.update(**{"style": style, "inkscape:label": "lattice", "d": path_command})
link.desc = "%s hinge %s" % (self.name, self.parameter_text())
class StraightLatticeGenerator(Generator):
def __init__(self, *args, **kwargs):
super(StraightLatticeGenerator, self).__init__(*args)
self.link_gap = kwargs['link_gap']
self.e_height = 2 * self.p_spacing
self.name = "straight"
def prerender(self):
self.e_height = 2 * self.p_spacing
w = self.e_length
lg = self.link_gap
if lg < 0.1:
# Single line for 0 height gap.
self.fixed_commands = " m %f,%f h %f m %f,%f h %f m %f,%f h %f" % (
0, self.e_height / 2,
w * 2 / 5,
0 - w / 5, 0 - self.e_height / 2,
w * 3 / 5,
0 - w / 5, self.e_height / 2,
w * 2 / 5,
)
else:
self.fixed_commands = (
" m %f,%f h %f v %f h %f"
" m %f,%f h %f v %f h %f v %f"
" m %f,%f h %f v %f h %f "
) % (
0,
self.e_height / 2,
w * 2 / 5,
lg,
0 - w * 2 / 5,
w / 8,
0 - lg - self.e_height / 2,
w * 3 / 4,
lg,
0 - w * 3 / 4,
0 - lg,
w * 7 / 8,
lg + self.e_height / 2,
0 - w * 2 / 5,
0 - lg,
w * 2 / 5,
)
def parameter_text(self):
text = super(StraightLatticeGenerator, self).parameter_text()
return "%s element_height: %.1f" % (text, self.link_gap)
class DiamondLatticeGenerator(Generator):
def __init__(self, *args, **kwargs):
super(DiamondLatticeGenerator, self).__init__(*args)
self.e_height = self.p_spacing
self.diamond_curve = kwargs['diamond_curve']
self.name = "diamond"
def prerender(self):
h = self.e_height
w = self.e_length
# Diamond curve
dc = 0-self.diamond_curve
# Horiz handle length.
hhl = abs(dc * w * 0.2)
# Endpoint horiz handle length
ehhl = hhl if dc > 0 else 0
# Vert handle length
vhl = abs(dc * h / 8) if dc < 0 else 0
# Left
self.fixed_commands = " m %f,%f c %f,%f %f,%f %f,%f c %f,%f %f,%f %f,%f " % (
0, h / 4,
hhl, 0,
w * 0.4 - ehhl, h / 4 - vhl,
w * 0.4, h / 4,
0 - ehhl, vhl,
0 - (w * 0.4 - hhl), h / 4,
0 - w * 0.4, h / 4,
)
# Bottom
self.fixed_commands = "%s m %f,%f c %f,%f %f,%f %f,%f s %f,%f %f,%f " % (
self.fixed_commands,
w * 0.1, h / 4,
ehhl, 0 - vhl,
w * 0.4 - hhl, 0 - h / 4,
w * 0.4, 0 - h / 4,
w * 0.4 - ehhl, h / 4 - vhl,
w * 0.4, h / 4,
)
# Top
self.fixed_commands = "%s m %f,%f c %f,%f %f,%f %f,%f s %f,%f %f,%f " % (
self.fixed_commands,
0 - w * 0.8, 0 - h,
ehhl, vhl,
w * 0.4 - hhl, h / 4,
w * 0.4, h / 4,
w * 0.4 - ehhl, 0 - h / 4 + vhl,
w * 0.4, 0 - h / 4,
)
# Right
self.fixed_commands = "%s m %f,%f c %f,%f %f,%f %f,%f c %f,%f %f,%f %f,%f " % (
self.fixed_commands,
w * 0.1, h *0.75,
0 - hhl, 0,
(0 - w * 0.4) + ehhl, 0 - h / 4 + vhl,
0 - w * 0.4, 0 - h / 4,
ehhl, 0 - vhl,
w * 0.4 - hhl, 0 - h / 4,
w * 0.4, 0 - h / 4,
)
def draw_one(self, x, y):
return "M %f,%f %s" % (x, y, self.fixed_commands)
def parameter_text(self):
text = super(DiamondLatticeGenerator, self).parameter_text()
return "%s curve: %.1f" % (text, self.diamond_curve)
class CrossLatticeGenerator(Generator):
def __init__(self, *args):
super(CrossLatticeGenerator, self).__init__(*args)
self.e_height = self.p_spacing
self.name = "cross"
def prerender(self):
l = self.e_length
h = self.e_height
self.fixed_commands = (
"m %f,%f l %f,%f l %f,%f m %f,%f l %f,%f"
"m %f,%f l %f,%f l %f,%f l %f,%f "
"m %f,%f l %f,%f l %f,%f l %f,%f "
"m %f,%f l %f,%f l %f,%f m %f,%f l %f,%f"
) % (
# Left
0, h * 0.5,
l * 0.2, 0,
l * 0.2, 0 - h * 0.3,
0 - l * 0.2, h * 0.3,
l * 0.2, h * 0.3,
# Top
0 - l * 0.3, 0 - h * 0.5,
l * 0.2, 0 - h * 0.3,
l * 0.4, 0,
l * 0.2, h * 0.3,
# Bottom
0, h * 0.4,
0 - l * 0.2, h * 0.3,
0 - l * 0.4, 0,
0 - l * 0.2, 0 - h * 0.3,
# Right
l * 0.5, 0 - h * 0.5,
l * 0.2, h * 0.3,
0 - l * 0.2, h * 0.3,
l * 0.2, 0 - h * 0.3,
l * 0.2, 0,
)
class WavyLatticeGenerator(Generator):
def __init__(self, *args, **kwargs):
super(WavyLatticeGenerator, self).__init__(*args)
self.e_height = self.p_spacing
self.name = "wavy"
def prerender(self):
h = self.e_height
w = self.e_length
self.fixed_commands = (
" m %f,%f h %f c %f,%f %f,%f %f,%f h %f "
"m %f,%f h %f c %f,%f %f,%f %f,%f h %f "
) % (
0, h, # Start of element (left)
w * 0.1, # Short horiz line.
w * 0.1, 0, # Control 1
w * 3 / 40, 0 - h / 2, # Control 2
w * 0.2, 0 - h / 2, # Curve top.
w * 0.175, # Top horiz line.
0 - w * 0.1, 0 - h / 2, # Move to higher line.
w * 0.3, # Long higher horiz line.
w / 5, 0, # Control 1
w / 10, h, # Control 2
w * 0.25, h, # Curve down.
w * 0.075, # End horiz line.
)
class LivingHingeEffect(inkex.EffectExtension):
"""
Extension to create laser cut bend lattices.
"""
def add_arguments(self, pars):
pars.add_argument("--tab", help="Bend pattern to generate")
pars.add_argument("--unit", help="Units for dimensions")
pars.add_argument("--swatch", type=inkex.Boolean, help="Draw as a swatch card")
pars.add_argument("--width", type=float, default=300, help="Width of pattern")
pars.add_argument("--height", type=float, default=100, help="Height of pattern")
pars.add_argument("--sl_length", type=int, default=20, help="Length of links")
pars.add_argument("--sl_gap", type=float, default=0.5, help="Gap between links")
pars.add_argument(
"--sl_spacing", type=float, default=20, help="Spacing of links"
)
pars.add_argument(
"--dl_curve", type=float, default=0.5, help="Curve of diamonds"
)
pars.add_argument(
"--dl_length", type=float, default=24, help="Length of diamonds"
)
pars.add_argument(
"--dl_spacing", type=float, default=4, help="Spacing of diamonds"
)
pars.add_argument("--cl_length", type=float, default=24, help="Length of combs")
pars.add_argument(
"--cl_spacing", type=float, default=6, help="Spacing of combs"
)
pars.add_argument("--wl_length", type=int, default=20, help="Length of links")
pars.add_argument(
"--wl_interval", type=int, default=30, help="Interval between links"
)
pars.add_argument(
"--wl_spacing", type=float, default=0.5, help="Spacing between links"
)
def convert(self, value):
return self.svg.unittouu(str(value) + self.options.unit)
def convertmm(self, value):
return self.svg.unittouu('%fmm' % value)
def effect(self):
"""
Effect behaviour.
"""
stroke_width = self.svg.unittouu("0.2mm")
self.options.width = self.convert(self.options.width)
self.options.height = self.convert(self.options.height)
def draw_one(x, y):
if self.options.tab == "straight_lattice":
generator = StraightLatticeGenerator(
x,
y,
self.options.width,
self.options.height,
stroke_width,
self.svg,
self.convertmm(self.options.sl_length),
self.convertmm(self.options.sl_spacing),
link_gap=self.convertmm(self.options.sl_gap),
)
elif self.options.tab == "diamond_lattice":
generator = DiamondLatticeGenerator(
x,
y,
self.options.width,
self.options.height,
stroke_width,
self.svg,
self.convertmm(self.options.dl_length),
self.convertmm(self.options.dl_spacing),
diamond_curve=self.options.dl_curve,
)
elif self.options.tab == "cross_lattice":
generator = CrossLatticeGenerator(
x,
y,
self.options.width,
self.options.height,
stroke_width,
self.svg,
self.convertmm(self.options.cl_length),
self.convertmm(self.options.cl_spacing),
)
elif self.options.tab == "wavy_lattice":
generator = WavyLatticeGenerator(
x,
y,
self.options.width,
self.options.height,
stroke_width,
self.svg,
self.convertmm(self.options.wl_length),
self.convertmm(self.options.wl_spacing),
)
else:
inkex.errormsg(_("Select a valid pattern tab before rendering."))
return
generator.generate(self.options.swatch)
if self.options.swatch or not self.svg.selected:
draw_one(0, 0)
else:
for elem in self.svg.selected.values():
# Determine width and height based on the selected object's bounding box.
bbox = elem.bounding_box()
self.options.width = bbox.width
self.options.height = bbox.height
x = bbox.x.minimum
y = bbox.y.minimum
draw_one(x, y)
# Create effect instance and apply it.
LivingHingeEffect().run()