Code

d50c498c07bcf8189ab3e06b9a9ad60a75ba911f
[inkscape.git] / share / extensions / kochify_load.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
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 2 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 '''
19 import math, tempfile, cPickle, inkex, simplepath
21 def findend(d):
22         end = []
23         subPathStart = []
24         for cmd,params in d:
25                 if cmd == 'M':
26                         subPathStart = params[-2:]
27                 if cmd == 'Z':
28                         end = subPathStart[:]
29                 else:
30                         end = params[-2:]
31         return end
33 class LoadKochify(inkex.Effect):
34         def effect(self):
35                 for id, node in self.selected.iteritems():
36                         if node.tagName == 'path':
37                                 d = simplepath.parsePath(node.attributes.getNamedItem('d').value)
38                                 start = d[0][1][-2:]
39                                 end = findend(d)
40                                 while start == end and len(d):
41                                         d = d[:-1]
42                                         end = findend(d)
43                                 if not end: 
44                                         break
45                                 dx = end[0]-start[0]
46                                 dy = end[1]-start[1]
47                                 length = math.sqrt((dx**2) + (dy**2))
48                                 angle = math.atan2(dy,dx)
49                                 endsinz = False
50                                 if d[-1][0]=='Z':
51                                         endsinz = True
52                                 path = {'start': start, 'end': end, 'endsinz': endsinz,
53                                         'length': length, 'angle': angle, 'path': d}
54                                 f = open(tempfile.gettempdir() + '/kochify.bin', 'w')
55                                 cPickle.dump(path, f)
56                                 f.close()                               
57                                 break
59 e = LoadKochify()
60 e.affect()