Skip to content

Instantly share code, notes, and snippets.

@aJulop aJulop/svgPathToDat.py
Last active Mar 14, 2018

Embed
What would you like to do?
see from mayzja commit (this should not be used anymore)
import svgpathtools
import xml.etree.ElementTree as ET
import sys
import re
file = sys.argv[1]
# only need paths
paths, _ = svgpathtools.svg2paths(file)
pointPaths = []
distance = 0.1
for path in paths:
verts = []
holes = []
pathSegs = [re.split("([mM])", path.d())[1:]][0]
pathSegs = [pathSegs[i] + pathSegs[i + 1] for i in range(0, len(pathSegs), 2)]
pathSegs = [svgpathtools.parse_path(x) for x in pathSegs]
num_samples = path.length() / distance
change = 1 / num_samples
for segment in pathSegs:
i = 0;
current = []
while i < 1:
point = segment.point(i)
pointX = point.real
pointY = point.imag
current.append((pointX, pointY))
i += change
if len(verts) == 0:
verts.extend(current)
else:
holes.append(current)
pointPaths.append((verts, *holes))
with open(file + ".dat", 'w+') as f:
string = ''
for path in pointPaths:
for subPath in path:
for position in subPath:
string += f"{position[0]},{position[1]},"
string += ";"
string += "\n"
f.write(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.