Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / share / extensions / dots.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 inkex, simplestyle, simplepath, math
21 class Dots(inkex.Effect):
23     def __init__(self):
24         inkex.Effect.__init__(self)
25         self.OptionParser.add_option("-d", "--dotsize",
26                         action="store", type="string",
27                         dest="dotsize", default="10px",
28                         help="Size of the dots placed at path nodes")
29         self.OptionParser.add_option("-f", "--fontsize",
30                         action="store", type="string",
31                         dest="fontsize", default="20",
32                         help="Size of node label numbers")
33         self.OptionParser.add_option("-s", "--start",
34                         action="store", type="int",
35                         dest="start", default="1",
36                         help="First number in the sequence, assigned to the first node")
37         self.OptionParser.add_option("-t", "--step",
38                         action="store", type="int",
39                         dest="step", default="1",
40                         help="Numbering step between two nodes")
41         self.OptionParser.add_option("--tab",
42                         action="store", type="string",
43                         dest="tab",
44                         help="The selected UI-tab when OK was pressed")
46     def effect(self):
47         selection = self.selected
48         if (selection):
49             for id, node in selection.iteritems():
50                 if node.tag == inkex.addNS('path','svg'):
51                     self.addDot(node)
52         else:
53             inkex.errormsg("Please select an object.")
55     def separateLastAndFirst(self, p):
56         # Separate the last and first dot if they are togheter
57         lastDot = -1
58         if p[lastDot][1] == []: lastDot = -2
59         if round(p[lastDot][1][-2]) == round(p[0][1][-2]) and \
60                 round(p[lastDot][1][-1]) == round(p[0][1][-1]):
61                 x1 = p[lastDot][1][-2]
62                 y1 = p[lastDot][1][-1]
63                 x2 = p[lastDot-1][1][-2]
64                 y2 = p[lastDot-1][1][-1]
65                 dx = abs( max(x1,x2) - min(x1,x2) )
66                 dy = abs( max(y1,y2) - min(y1,y2) )
67                 dist = math.sqrt( dx**2 + dy**2 )
68                 x = dx/dist
69                 y = dy/dist
70                 if x1 > x2: x *= -1
71                 if y1 > y2: y *= -1
72                 p[lastDot][1][-2] += x * inkex.unittouu(self.options.dotsize)
73                 p[lastDot][1][-1] += y * inkex.unittouu(self.options.dotsize)
75     def addDot(self, node):
76         self.group = inkex.etree.SubElement( node.getparent(), inkex.addNS('g','svg') )
77         self.dotGroup = inkex.etree.SubElement( self.group, inkex.addNS('g','svg') )
78         self.numGroup = inkex.etree.SubElement( self.group, inkex.addNS('g','svg') )
79         
80         try:
81             t = node.get('transform')
82             self.group.set('transform', t)
83         except:
84             pass
86         style = simplestyle.formatStyle({ 'stroke': 'none', 'fill': '#000' })
87         a = []
88         p = simplepath.parsePath(node.get('d'))
90         self.separateLastAndFirst(p)
92         num = self.options.start
93         for cmd,params in p:
94             if cmd != 'Z' and cmd != 'z':
95                 dot_att = {
96                   'style': style,
97                   'r':  str( inkex.unittouu(self.options.dotsize) / 2 ),
98                   'cx': str( params[-2] ),
99                   'cy': str( params[-1] )
100                 }
101                 inkex.etree.SubElement(
102                   self.dotGroup,
103                   inkex.addNS('circle','svg'),
104                   dot_att )
105                 self.addText(
106                   self.numGroup,
107                   params[-2] + ( inkex.unittouu(self.options.dotsize) / 2 ),
108                   params[-1] - ( inkex.unittouu(self.options.dotsize) / 2 ),
109                   num )
110                 num += self.options.step
111         node.getparent().remove( node )
113     def addText(self,node,x,y,text):
114                 new = inkex.etree.SubElement(node,inkex.addNS('text','svg'))
115                 s = {'font-size': self.options.fontsize, 'fill-opacity': '1.0', 'stroke': 'none',
116                     'font-weight': 'normal', 'font-style': 'normal', 'fill': '#999'}
117                 new.set('style', simplestyle.formatStyle(s))
118                 new.set('x', str(x))
119                 new.set('y', str(y))
120                 new.text = str(text)
122 if __name__ == '__main__':
123     e = Dots()
124     e.affect()
127 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99