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 inkex, simplestyle, pturtle
21 def stripme(s):
22 return s.strip()
24 class LSystem(inkex.Effect):
25 def __init__(self):
26 inkex.Effect.__init__(self)
27 self.OptionParser.add_option("-o", "--order",
28 action="store", type="int",
29 dest="order", default=3,
30 help="number of iteration")
31 self.OptionParser.add_option("-a", "--angle",
32 action="store", type="float",
33 dest="angle", default=16.0,
34 help="angle for turn commands")
35 self.OptionParser.add_option("-s", "--step",
36 action="store", type="float",
37 dest="step", default=25.0,
38 help="step size")
39 self.OptionParser.add_option("-x", "--axiom",
40 action="store", type="string",
41 dest="axiom", default="++F",
42 help="initial state of system")
43 self.OptionParser.add_option("-r", "--rules",
44 action="store", type="string",
45 dest="rules", default="F=FF-[-F+F+F]+[+F-F-F]",
46 help="replacement rules")
47 self.stack = []
48 self.turtle = pturtle.pTurtle()
49 def iterate(self):
50 self.rules = dict([map(stripme, i.split("=")) for i in self.options.rules.upper().split(";") if i.count("=")==1])
51 string = self.__recurse(self.options.axiom.upper(),0)
52 self.__compose_path(string)
53 return self.turtle.getPath()
54 def __compose_path(self, string):
55 for c in string:
56 if c in 'ABCDEF':
57 self.turtle.pd()
58 self.turtle.fd(self.options.step)
59 elif c in 'GHIJKL':
60 self.turtle.pu()
61 self.turtle.fd(self.options.step)
62 elif c == '+':
63 self.turtle.lt(self.options.angle)
64 elif c == '-':
65 self.turtle.rt(self.options.angle)
66 elif c == '|':
67 self.turtle.lt(180)
68 elif c == '[':
69 self.stack.append([self.turtle.getpos(), self.turtle.getheading()])
70 elif c == ']':
71 self.turtle.pu()
72 pos,heading = self.stack.pop()
73 self.turtle.setpos(pos)
74 self.turtle.setheading(heading)
76 def __recurse(self,rule,level):
77 level_string = ''
78 for c in rule:
79 if level < self.options.order:
80 try:
81 level_string = level_string + self.__recurse(self.rules[c],level+1)
82 except KeyError:
83 level_string = level_string + c
84 else:
85 level_string = level_string + c
86 return level_string
88 def effect(self):
89 new = self.document.createElement('svg:path')
90 s = {'stroke-linejoin': 'miter', 'stroke-width': '1.0px',
91 'stroke-opacity': '1.0', 'fill-opacity': '1.0',
92 'stroke': '#000000', 'stroke-linecap': 'butt',
93 'fill': 'none'}
94 new.setAttribute('style', simplestyle.formatStyle(s))
95 new.setAttribute('d', self.iterate())
96 self.document.documentElement.appendChild(new)
98 e = LSystem()
99 e.affect()