Skip to content

Instantly share code, notes, and snippets.

@zopieux
Last active February 11, 2024 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zopieux/1844ba0963f1b8c79f99f9f052945859 to your computer and use it in GitHub Desktop.
Save zopieux/1844ba0963f1b8c79f99f9f052945859 to your computer and use it in GitHub Desktop.
Modernize version of the Living Hinge extension that works on recent Inkscape versions
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Living Hinge</_name>
<id>org.lvl1.living_hinge</id>
<dependency type="executable" location="extensions">living_hinge.py</dependency>
<param name="unit" _gui-text="Unit" type="enum">
<_item value="mm">mm</_item>
<_item value="cm">cm</_item>
<_item value="in">in</_item>
<_item value="px">px</_item>
</param>
<param name="cut_length" type="float" precision="2" _gui-text="cut length (y)" min="1" max="1000">19.0</param>
<param name="gap_length" type="float" precision="2" _gui-text="gap length (y)" min="1" max="1000">3.0</param>
<param name="sep_distance" type="float" precision="2" _gui-text="separation distance (x)" min="1" max="1000">1.5</param>
<param name="help_text" type="description">This extension renders the lines to make the cuts for a living hinge. The cuts run in the y-direction.
cut length: the length of each cut in the y-direction.
gap length: the separation between cut lines in the y-direction.
separation distance: the separation distance in the x-direction between adjacent sets of cut lines.
The entered value for cut length will be adjusted so that an integer number of whole cut lines lies in the y-direction. The entered value for separation distance will be adjusted so that in integer number of cut lines lies in the x-direction.
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="Render" />
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">living_hinge.py</command>
</script>
</inkscape-extension>
#! /usr/bin/env python
"""
hinge_cuts.py
A module for creating lines to laser cut living hinges
Copyright (C) 2013 Mark Endicott; drphonon@gmail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License
write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
"""
Change in version 0.2.
Changed self.unittouu to self.unittouu
and self.uutounit to self.uutounit
to make it work with Inkscape 0.91
Thanks to Pete Prodoehl for pointing this out.
"""
__version__ = "0.2"
import sys,inkex,simplestyle,gettext
from lxml import etree
_ = gettext.gettext
def drawS(parent, XYstring): # Draw lines from a list
name='part'
style = { 'stroke': '#000000', 'fill': 'none', 'stroke-width': self.unittouu("0.1 mm") }
drw = {'style':simplestyle.formatStyle(style),inkex.addNS('label','inkscape'):name,'d':XYstring}
etree.SubElement(parent, inkex.addNS('path','svg'), drw )
return
class HingeCuts(inkex.Effect):
def __init__(self):
# Call the base class constructor.
inkex.Effect.__init__(self)
# Define options - Must match to the <param> elements in the .inx file
self.OptionParser.add_option('--unit',action='store',type='string', dest='unit',default='mm',help='units of measurement')
self.OptionParser.add_option('--cut_length',action='store',type='float', dest='cut_length',default=0,help='length of the cuts for the hinge.')
self.OptionParser.add_option('--gap_length',action='store',type='float', dest='gap_length',default=0,help='separation distance between successive hinge cuts.')
self.OptionParser.add_option('--sep_distance',action='store',type='float', dest='sep_distance',default=0,help='distance between successive lines of hinge cuts.')
def effect(self):
unit=self.options.unit
# starting cut length. Will be adjusted for get an integer number of cuts in the y-direction.
l = self.unittouu(str(self.options.cut_length) + unit)
# cut separation in the y-direction
d = self.unittouu(str(self.options.gap_length) + unit)
# starting separation between lines of cuts in the x-direction. Will be adjusted to get an integer
# number of cut lines in the x-direction.
dd = self.unittouu(str(self.options.sep_distance) + unit)
# get selected nodes
if self.selected:
# put lines on the current layer
parent = self.current_layer
for id, node in self.selected.items():
# inkex.debug("id:" + id)
# for key in node.attrib.keys():
# inkex.debug(key + ": " + node.get(key))
x = float(node.get("x"))
dx = float(node.get("width"))
y = float(node.get("y"))
dy = float(node.get("height"))
# calculate the cut lines for the hinge
lines, l_actual, d_actual, dd_actual = self.calcCutLines(x, y, dx, dy, l, d, dd)
# all the lines are one path. Prepare the string that describes the path.
s = ''
for line in lines:
s = s + "M %s, %s L %s, %s " % (line['x1'], line['y1'], line['x2'], line['y2'])
# add the path to the document
style = { 'stroke': '#000000', 'fill': 'none', 'stroke-width': self.unittouu("0.1 mm")}
drw = {'style':simplestyle.formatStyle(style), 'd': s}
hinge = etree.SubElement(parent, inkex.addNS('path', 'svg'), drw)
# add a description element to hold the parameters used to create the cut lines
desc = etree.SubElement(hinge, inkex.addNS('desc', 'svg'))
desc.text = "Hinge cut parameters: actual(requested)\n" \
"cut length: %.2f %s (%.2f %s)\n" \
"gap length: %.2f %s (%.2f %s)\n" \
"separation distance: %.2f %s (%.2f %s)" % (self.uutounit(l_actual, unit), unit, self.uutounit(l, unit), unit,
self.uutounit(d_actual, unit), unit, self.uutounit(d, unit), unit,
self.uutounit(dd_actual, unit), unit, self.uutounit(dd, unit), unit)
else:
inkex.debug("No rectangle(s) have been selected.")
def calcCutLines(self, x, y, dx, dy, l, d, dd):
"""
Return a list of cut lines as dicts. Each dict contains the end points for one cut line.
[{x1, y1, x2, y2}, ... ]
Parameters
x, y: the coordinates of the lower left corner of the bounding rect
dx, dy: width and height of the bounding rect
l: the nominal length of a cut line
d: the separation between cut lines in the y-direction
dd: the nominal separation between cut lines in the x-direction
l will be adjusted so that there is an integral number of cuts in the y-direction.
dd will be adjusted so that there is an integral number of cuts in the x-direction.
"""
ret = []
# use l as a starting guess. Adjust it so that we get an integer number of cuts in the y-direction
# First compute the number of cuts in the y-direction using l. This will not in general be an integer.
p = (dy-d)/(d+l)
#round p to the nearest integer
p = round(p)
#compute the new l that will result in p cuts in the y-direction.
l = (dy-d)/p - d
# use dd as a starting guess. Adjust it so that we get an even integer number of cut lines in the x-direction.
p = dx/dd
p = round(p)
if p % 2 == 1:
p = p + 1
dd = dx/p
#
# Column A cuts
#
currx = 0
donex = False
while not donex:
doney = False
starty = 0
endy = (l + d)/2.0
while not doney:
if endy >= dy:
endy = dy
# Add the end points of the line
ret.append({'x1' : x + currx, 'y1' : y + starty, 'x2': x + currx, 'y2': y + endy})
starty = endy + d
endy = starty + l
if starty >= dy:
doney = True
currx = currx + dd * 2.0
if currx - dx > dd:
donex = True
# inkex.debug("lastx: " + str(lastx) + "; currx: " + str(currx))
#
#Column B cuts
#
currx = dd
donex = False
while not donex:
doney = False
starty = d
endy = starty + l
while not doney:
if endy >= dy:
endy = dy
# create a line
ret.append({'x1' : x + currx, 'y1' : y + starty, 'x2': x + currx, 'y2': y + endy})
starty = endy + d
endy = starty + l
if starty >= dy:
doney = True
currx = currx + dd*2.0
if currx > dx:
donex = True
return (ret, l, d, dd)
# Create effect instance and apply it.
effect = HingeCuts()
effect.affect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment