This commit is contained in:
Mario Voigt 2023-08-19 17:43:40 +02:00
parent 34d058deb3
commit 85f3281a53
1 changed files with 22 additions and 21 deletions

View File

@ -20,7 +20,7 @@ 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.svg.unittouu
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.
"""
"""
"""
Change in version 0.3
Add a direction option so the cuts can be done in the X or Y direction.
Modification by Sylvain GARNAVAULT; garnav@wanadoo.fr
"""
"""
"""
Change in version 0.4.
Python3 / inkscape 1.0 migration
"""
__version__ = "0.4"
__version__ = "0.4"
import inkex
import gettext
_ = gettext.gettext
from lxml import etree
from inkex import Rectangle
class LivingHinge(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument('--direction',default='y',help='cuts direction')
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.')
def effect(self):
unit=self.options.unit
# which direction are we cutting
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
# number of cut lines in the x-direction.
dd = self.svg.unittouu(str(self.options.sep_distance) + unit)
# get selected nodes
if self.svg.selected:
# put lines on the current layer
# put lines on the 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)
# for key in node.attrib.keys():
# inkex.utils.debug(key + ": " + node.get(key))
bbox = node.bounding_box()
# calculate the cut lines for the hinge
if (dir=="y"):
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" \
"cut 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(dd_actual, unit), unit, self.svg.uutounit(dd, unit), unit)
else:
inkex.utils.debug("No rectangle(s) have been selected.")
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.
[{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)
@ -126,14 +127,14 @@ class LivingHinge(inkex.EffectExtension):
if p == 0:
p = 1 #avoid divison by zero
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
#
@ -177,22 +178,22 @@ class LivingHinge(inkex.EffectExtension):
currx = currx + dd*2.0
if currx > dx:
donex = True
return (ret, 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.
[{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 x-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.
dd will be adjusted so that there is an integral number of cuts in the y-direction.
"""