]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/io/@svg/parsePath.py
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / io / @svg / parsePath.py
1 #!/usr/bin/env python
2
3 ## Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
4 ##
5 ##    This program is free software: you can redistribute it and/or modify
6 ##    it under the terms of the GNU General Public License as published by
7 ##    the Free Software Foundation, either version 3 of the License, or
8 ##    any later version.
9 ##
10 ##    This program is distributed in the hope that it will be useful,
11 ##    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ##    GNU General Public License for more details.
14 ##
15 ##    You should have received a copy of the GNU General Public License
16 ##    along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import inkex, simplepath
19 import sys
20 #import getopt
21
22 def parsePaths (filen=None):
23
24   svg = inkex.Effect ()
25   svg.parse (filen)
26
27   paths = svg.document.xpath ('//svg:path', namespaces=inkex.NSS)
28   for path in paths:
29     D = simplepath.parsePath (path.attrib['d'])
30     cmdlst = [];
31     parlst = [];
32     for cmd,params in D:
33       cmdlst.append(cmd)
34       parlst.append(params)
35
36     print 'svgpath = struct("cmd","{0}","data",{{{1}}});' \
37         .format(''.join(cmdlst),str(parlst).replace('[[','[').replace(']]',']'))
38
39     print 'svgpathid = "{0}"; $'.format(path.attrib['id'])
40
41
42
43 # ----------------------------
44
45 if __name__=="__main__":
46   '''
47     try:
48         optlist,args = getopt.getopt(sys.argv[1:],"thdp")
49     except getopt.GetoptError:
50         usage()
51         sys.exit(2)
52
53     doHelp = 0
54     c = Context()
55     c.doPrint = 1
56     for opt in optlist:
57         if opt[0] == "-d":  c.debug = 1
58         if opt[0] == "-p":  c.plot  = 1
59         if opt[0] == "-t":  c.triangulate = 1
60         if opt[0] == "-h":  doHelp = 1
61
62     if not doHelp:
63         pts = []
64         fp = sys.stdin
65         if len(args) > 0:
66             fp = open(args[0],'r')
67         for line in fp:
68             fld = line.split()
69             x = float(fld[0])
70             y = float(fld[1])
71             pts.append(Site(x,y))
72         if len(args) > 0: fp.close()
73
74     if doHelp or len(pts) == 0:
75         usage()
76         sys.exit(2)
77   '''
78   svg = sys.argv[1]
79   parsePaths(svg)