error handling for paste length extension

This commit is contained in:
leyghisbb 2020-08-30 11:40:20 +02:00
parent 3cedfeafd9
commit f7ebf7d33d

View File

@ -76,21 +76,16 @@ class PasteLengthEffect(inkex.Effect):
def getLength(self, cspath, tolerance): def getLength(self, cspath, tolerance):
parts = self.getPartsFromCubicSuper(cspath) parts = self.getPartsFromCubicSuper(cspath)
curveLen = 0 curveLen = 0
for i, part in enumerate(parts): for i, part in enumerate(parts):
for j, seg in enumerate(part): for j, seg in enumerate(part):
curveLen += bezier.bezierlength((seg[0], seg[1], seg[2], seg[3]), tolerance = tolerance) curveLen += bezier.bezierlength((seg[0], seg[1], seg[2], seg[3]), tolerance = tolerance)
return curveLen return curveLen
def effect(self): def effect(self):
scale = self.options.scale scale = self.options.scale
scaleFrom = self.options.scaleFrom scaleFrom = self.options.scaleFrom
tolerance = 10 ** (-1 * self.options.precision) tolerance = 10 ** (-1 * self.options.precision)
selections = self.svg.selected
printOut = False
selections = self.svg.selected
pathNodes = self.document.xpath('//svg:path',namespaces=inkex.NSS) pathNodes = self.document.xpath('//svg:path',namespaces=inkex.NSS)
outStrs = [str(len(pathNodes))] outStrs = [str(len(pathNodes))]
@ -103,12 +98,14 @@ class PasteLengthEffect(inkex.Effect):
for key, cspath in paths: for key, cspath in paths:
curveLen = self.getLength(cspath, tolerance) curveLen = self.getLength(cspath, tolerance)
self.scaleCubicSuper(cspath, scaleFactor = scale * (srclen / curveLen), \ self.scaleCubicSuper(cspath, scaleFactor = scale * (srclen / curveLen), scaleFrom = scaleFrom)
scaleFrom = scaleFrom) try: #we wrap this in try-except because if the elements are within groups it will cause errors
selections[key].set('d', CubicSuperPath(cspath)) selections[key].set('d', CubicSuperPath(cspath))
except:
pass
else: else:
inkex.errormsg("Please select at least two paths, with the path whose \ inkex.errormsg("Please select at least two paths, with the path whose \
length is to be copied at the top. You may have to convert the shape \ length is to be copied at the top. You may have to convert the shape \
to path with path->Object to Path.") to path with path->Object to Path.")
PasteLengthEffect().run() PasteLengthEffect().run()