Draft: importSVG.py, improved Pythonic style, 4 space indentation

This commit is contained in:
vocx-fc
2019-08-02 11:34:53 -05:00
committed by Yorik van Havre
parent c4822fc30b
commit 3738ad67f3

View File

@@ -1474,68 +1474,68 @@ class svgHandler(xml.sax.ContentHandler):
return vec.add(v)
def getMatrix(self, tr):
"""Return a FreeCAD matrix from an SVG transform attribute.
"""Return a FreeCAD matrix from an SVG transform attribute.
Parameters
----------
tr : str
The type of transform: 'matrix', 'translate', 'scale',
'rotate', 'skewX', 'skewY' and its value
Parameters
----------
tr : str
The type of transform: 'matrix', 'translate', 'scale',
'rotate', 'skewX', 'skewY' and its value
Returns
-------
Base::Matrix4D
The translated matrix.
"""
transformre = re.compile('(matrix|translate|scale|rotate|skewX|skewY)\s*?\((.*?)\)', re.DOTALL)
m = FreeCAD.Matrix()
for transformation, arguments in transformre.findall(tr):
argsplit = [float(arg) for arg in arguments.replace(',', ' ').split()]
# m.multiply(FreeCAD.Matrix(1, 0, 0, 0, 0, -1))
# print('%s:%s %s %d' % (transformation, arguments, argsplit, len(argsplit)))
if transformation == 'translate':
tx = argsplit[0]
ty = argsplit[1] if len(argsplit) > 1 else 0.0
m.move(Vector(tx, -ty, 0))
elif transformation == 'scale':
sx = argsplit[0]
sy = argsplit[1] if len(argsplit) > 1 else sx
m.scale(Vector(sx, sy, 1))
elif transformation == 'rotate':
cx = 0
cy = 0
angle = argsplit[0]
if len(argsplit) >= 3:
cx = argsplit[1]
cy = argsplit[2]
m.move(Vector(cx, -cy, 0))
# Mirroring one axis is equal to changing the direction
# of rotation
m.rotateZ(math.radians(-angle))
if len(argsplit) >= 3:
m.move(Vector(-cx, cy, 0))
elif transformation == 'skewX':
m = m.multiply(FreeCAD.Matrix(1,
-math.tan(math.radians(argsplit[0]))))
elif transformation == 'skewY':
m = m.multiply(FreeCAD.Matrix(1, 0, 0, 0,
-math.tan(math.radians(argsplit[0]))))
elif transformation == 'matrix':
# transformation matrix:
# FreeCAD SVG
# (+A -C +0 +E) (A C 0 E)
# (-B +D -0 -F) = (-Y) * (B D 0 F) *(-Y)
# (+0 -0 +1 +0) (0 0 1 0)
# (+0 -0 +0 +1) (0 0 0 1)
m = m.multiply(FreeCAD.Matrix(argsplit[0], -argsplit[2],
0, argsplit[4],
-argsplit[1], argsplit[3],
0, -argsplit[5]))
# else:
# print 'SKIPPED %s' % transformation
# print "m= ",m
# print "generating transformation: ",m
return m
Returns
-------
Base::Matrix4D
The translated matrix.
"""
transformre = re.compile('(matrix|translate|scale|rotate|skewX|skewY)\s*?\((.*?)\)', re.DOTALL)
m = FreeCAD.Matrix()
for transformation, arguments in transformre.findall(tr):
argsplit = [float(arg) for arg in arguments.replace(',', ' ').split()]
# m.multiply(FreeCAD.Matrix(1, 0, 0, 0, 0, -1))
# print('%s:%s %s %d' % (transformation, arguments, argsplit, len(argsplit)))
if transformation == 'translate':
tx = argsplit[0]
ty = argsplit[1] if len(argsplit) > 1 else 0.0
m.move(Vector(tx, -ty, 0))
elif transformation == 'scale':
sx = argsplit[0]
sy = argsplit[1] if len(argsplit) > 1 else sx
m.scale(Vector(sx, sy, 1))
elif transformation == 'rotate':
cx = 0
cy = 0
angle = argsplit[0]
if len(argsplit) >= 3:
cx = argsplit[1]
cy = argsplit[2]
m.move(Vector(cx, -cy, 0))
# Mirroring one axis is equal to changing the direction
# of rotation
m.rotateZ(math.radians(-angle))
if len(argsplit) >= 3:
m.move(Vector(-cx, cy, 0))
elif transformation == 'skewX':
m = m.multiply(FreeCAD.Matrix(1,
-math.tan(math.radians(argsplit[0]))))
elif transformation == 'skewY':
m = m.multiply(FreeCAD.Matrix(1, 0, 0, 0,
-math.tan(math.radians(argsplit[0]))))
elif transformation == 'matrix':
# transformation matrix:
# FreeCAD SVG
# (+A -C +0 +E) (A C 0 E)
# (-B +D -0 -F) = (-Y) * (B D 0 F) *(-Y)
# (+0 -0 +1 +0) (0 0 1 0)
# (+0 -0 +0 +1) (0 0 0 1)
m = m.multiply(FreeCAD.Matrix(argsplit[0], -argsplit[2],
0, argsplit[4],
-argsplit[1], argsplit[3],
0, -argsplit[5]))
# else:
# print 'SKIPPED %s' % transformation
# print "m= ",m
# print "generating transformation: ",m
return m
# getMatrix
# class svgHandler