2 """svg2bnd.py -- Converts a path in an SVG file to a boundary for Voro++."""
5 from svgfig
import svg
, pathdata
9 print 'Usage: svg2bnd <file>'
13 obj
= svg
.load(sys
.argv
[1])
15 for key
, elem
in obj
.walk():
16 if isinstance(elem
, svg
.SVG
):
17 if elem
.tag
== 'path':
20 commandStr
= elem
[u
'd']
21 commandStr
= re
.sub('([0-9])-', ' -', commandStr
)
22 # HACK: Add some spaces to ease along parsing.
23 commands
= pathdata
.parse(commandStr
)
31 if d
== u
'L' or d
== u
'M': # Absolute position line
33 elif d
== u
'l' or d
== u
'm': # Relative position line
37 elif d
== u
'H': # Absolute position horizontal motion
39 elif d
== u
'h': # Relative position horizontal motion
41 elif d
== u
'V': # Absolute position vertical motion
43 elif d
== u
'v': # Relative position vertical motion
45 if (redo
== 0 or (sqrt((x
-xs
[-1])**2+(y
-ys
[-1])**2) > 1e-5)):
50 if sqrt((xs
[-1]-xs
[0])**2+(ys
[-1]-ys
[0])**2) < 1e-5:
54 for i
in xrange(len(xs
)):
55 print i
+semitotal
, xs
[i
], ys
[i
]
59 elif elem
.tag
== 'polygon':
62 pointsStr
= elem
['points']
63 pointses
= pointsStr
.split()
68 if (redo
==0 or (sqrt((x
-xs
[-1])**2+(y
-ys
[-1])**2) > 1e-5)):
73 if sqrt((xs
[-1]-xs
[0])**2+(ys
[-1]-ys
[0])**2) < 1e-5:
77 for i
in xrange(len(xs
)):
78 print i
+ semitotal
, xs
[i
], ys
[i
]
83 # print 'Unsupported element tag: %s'%elem.tag