This commit is contained in:
Mario Voigt 2023-08-19 17:43:40 +02:00
parent 34d058deb3
commit 85f3281a53

View File

@ -20,7 +20,7 @@ write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
""" """
""" """
Change in version 0.2. Change in version 0.2.
Changed self.unittouu to self.svg.unittouu Changed self.unittouu to self.svg.unittouu
and self.uutounit to self.uutounit and self.uutounit to self.uutounit
@ -28,26 +28,27 @@ to make it work with Inkscape 0.91
Thanks to Pete Prodoehl for pointing this out. Thanks to Pete Prodoehl for pointing this out.
""" """
""" """
Change in version 0.3 Change in version 0.3
Add a direction option so the cuts can be done in the X or Y direction. Add a direction option so the cuts can be done in the X or Y direction.
Modification by Sylvain GARNAVAULT; garnav@wanadoo.fr Modification by Sylvain GARNAVAULT; garnav@wanadoo.fr
""" """
""" """
Change in version 0.4. Change in version 0.4.
Python3 / inkscape 1.0 migration Python3 / inkscape 1.0 migration
""" """
__version__ = "0.4" __version__ = "0.4"
import inkex import inkex
import gettext import gettext
_ = gettext.gettext _ = gettext.gettext
from lxml import etree from lxml import etree
from inkex import Rectangle
class LivingHinge(inkex.EffectExtension): class LivingHinge(inkex.EffectExtension):
def add_arguments(self, pars): def add_arguments(self, pars):
pars.add_argument('--direction',default='y',help='cuts direction') pars.add_argument('--direction',default='y',help='cuts direction')
pars.add_argument('--unit',default='mm',help='units of measurement') pars.add_argument('--unit',default='mm',help='units of measurement')
@ -56,7 +57,7 @@ class LivingHinge(inkex.EffectExtension):
pars.add_argument('--sep_distance',type=float, default=0,help='distance between successive lines of hinge cuts.') pars.add_argument('--sep_distance',type=float, default=0,help='distance between successive lines of hinge cuts.')
def effect(self): def effect(self):
unit=self.options.unit unit=self.options.unit
# which direction are we cutting # which direction are we cutting
dir = self.options.direction dir = self.options.direction
@ -67,17 +68,17 @@ class LivingHinge(inkex.EffectExtension):
# starting separation between lines of cuts in the x-direction. Will be adjusted to get an integer # 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. # number of cut lines in the x-direction.
dd = self.svg.unittouu(str(self.options.sep_distance) + unit) dd = self.svg.unittouu(str(self.options.sep_distance) + unit)
# get selected nodes # get selected nodes
if self.svg.selected: if self.svg.selected:
# put lines on the current layer # put lines on the current layer
parent = self.svg.get_current_layer() parent = self.svg.get_current_layer()
for id, node in self.svg.selected.items(): for id, node in self.svg.selection.filter(Rectangle).items():
# inkex.utils.debug("id:" + id) # inkex.utils.debug("id:" + id)
# for key in node.attrib.keys(): # for key in node.attrib.keys():
# inkex.utils.debug(key + ": " + node.get(key)) # inkex.utils.debug(key + ": " + node.get(key))
bbox = node.bounding_box() bbox = node.bounding_box()
# calculate the cut lines for the hinge # calculate the cut lines for the hinge
if (dir=="y"): if (dir=="y"):
lines, l_actual, d_actual, dd_actual = self.calcYCutLines(bbox.left, bbox.top, bbox.width, bbox.height, l, d, dd) lines, l_actual, d_actual, dd_actual = self.calcYCutLines(bbox.left, bbox.top, bbox.width, bbox.height, l, d, dd)
@ -94,29 +95,29 @@ class LivingHinge(inkex.EffectExtension):
desc.text = "Hinge cut parameters: actual(requested)\n" \ desc.text = "Hinge cut parameters: actual(requested)\n" \
"cut length: %.2f %s (%.2f %s)\n" \ "cut length: %.2f %s (%.2f %s)\n" \
"gap length: %.2f %s (%.2f %s)\n" \ "gap length: %.2f %s (%.2f %s)\n" \
"separation distance: %.2f %s (%.2f %s)" % (self.svg.uutounit(l_actual, unit), unit, self.svg.uutounit(l, unit), unit, "separation distance: %.2f %s (%.2f %s)" % (self.svg.uutounit(l_actual, unit), unit, self.svg.uutounit(l, unit), unit,
self.svg.uutounit(d_actual, unit), unit, self.svg.uutounit(d, unit), unit, self.svg.uutounit(d_actual, unit), unit, self.svg.uutounit(d, unit), unit,
self.svg.uutounit(dd_actual, unit), unit, self.svg.uutounit(dd, unit), unit) self.svg.uutounit(dd_actual, unit), unit, self.svg.uutounit(dd, unit), unit)
else: else:
inkex.utils.debug("No rectangle(s) have been selected.") inkex.utils.debug("No rectangle(s) have been selected.")
def calcYCutLines(self, x, y, dx, dy, l, d, dd): def calcYCutLines(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. Return a list of cut lines as dicts. Each dict contains the end points for one cut line.
[{x1, y1, x2, y2}, ... ] [{x1, y1, x2, y2}, ... ]
Parameters Parameters
x, y: the coordinates of the lower left corner of the bounding rect x, y: the coordinates of the lower left corner of the bounding rect
dx, dy: width and height of the bounding rect dx, dy: width and height of the bounding rect
l: the nominal length of a cut line l: the nominal length of a cut line
d: the separation between cut lines in the y-direction d: the separation between cut lines in the y-direction
dd: the nominal separation between cut lines in the x-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. 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. dd will be adjusted so that there is an integral number of cuts in the x-direction.
""" """
ret = [] ret = []
# use l as a starting guess. Adjust it so that we get an integer number of cuts in the y-direction # 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. # 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) p = (dy-d)/(d+l)
@ -126,14 +127,14 @@ class LivingHinge(inkex.EffectExtension):
if p == 0: if p == 0:
p = 1 #avoid divison by zero p = 1 #avoid divison by zero
l = (dy-d)/p - d 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. # 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 = dx/dd
p = round(p) p = round(p)
if p % 2 == 1: if p % 2 == 1:
p = p + 1 p = p + 1
dd = dx/p dd = dx/p
# #
# Column A cuts # Column A cuts
# #
@ -177,22 +178,22 @@ class LivingHinge(inkex.EffectExtension):
currx = currx + dd*2.0 currx = currx + dd*2.0
if currx > dx: if currx > dx:
donex = True donex = True
return (ret, l, d, dd) return (ret, l, d, dd)
def calcXCutLines(self, x, y, dx, dy, l, d, dd): def calcXCutLines(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. Return a list of cut lines as dicts. Each dict contains the end points for one cut line.
[{x1, y1, x2, y2}, ... ] [{x1, y1, x2, y2}, ... ]
Parameters Parameters
x, y: the coordinates of the lower left corner of the bounding rect x, y: the coordinates of the lower left corner of the bounding rect
dx, dy: width and height of the bounding rect dx, dy: width and height of the bounding rect
l: the nominal length of a cut line l: the nominal length of a cut line
d: the separation between cut lines in the x-direction d: the separation between cut lines in the x-direction
dd: the nominal separation between cut lines in the y-direction dd: the nominal separation between cut lines in the y-direction
l will be adjusted so that there is an integral number of cuts in the x-direction. l will be adjusted so that there is an integral number of cuts in the x-direction.
dd 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 y-direction.
""" """