Code

add extension 'Convert to Dashes'
authoralvinpenner <alvinpenner@users.sourceforge.net>
Fri, 15 May 2009 23:02:52 +0000 (23:02 +0000)
committeralvinpenner <alvinpenner@users.sourceforge.net>
Fri, 15 May 2009 23:02:52 +0000 (23:02 +0000)
share/extensions/Makefile.am
share/extensions/convert2dashes.inx [new file with mode: 0644]
share/extensions/convert2dashes.py [new file with mode: 0644]

index 70f820eae1ffee3f8b9acca7a0a6ffa8aafc7222..9d6762d4e4ccf650d0b90906357db979961ecb5d 100644 (file)
@@ -37,6 +37,7 @@ extensions = \
        color_removered.py\
        color_rgbbarrel.py\
        color_replace.py\
+       convert2dashes.py\
        cspsubdiv.py \
        cubicsuperpath.py \
        dia2svg.sh \
@@ -160,6 +161,7 @@ modules = \
        color_removered.inx\
        color_rgbbarrel.inx\
        color_replace.inx\
+       convert2dashes.inx\
        dia.inx \
        dimension.inx \
        dots.inx \
diff --git a/share/extensions/convert2dashes.inx b/share/extensions/convert2dashes.inx
new file mode 100644 (file)
index 0000000..9f3ff36
--- /dev/null
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">\r
+       <_name>Convert to Dashes</_name>\r
+       <id>com.vaxxine.filter.dashes</id>\r
+       <dependency type="executable" location="extensions">convert2dashes.py</dependency>\r
+       <dependency type="executable" location="extensions">inkex.py</dependency>\r
+       <effect>\r
+               <object-type>path</object-type>\r
+               <effects-menu>\r
+                       <submenu _name="Modify Path"/>\r
+               </effects-menu>\r
+       </effect>\r
+       <script>\r
+               <command reldir="extensions" interpreter="python">convert2dashes.py</command>\r
+       </script>\r
+</inkscape-extension>\r
diff --git a/share/extensions/convert2dashes.py b/share/extensions/convert2dashes.py
new file mode 100644 (file)
index 0000000..b779617
--- /dev/null
@@ -0,0 +1,89 @@
+#!/usr/bin/env python 
+'''
+This extension converts a path into a dashed line using 'stroke-dasharray'
+It is a modification of the file addnodes.py
+
+Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org
+Copyright (C) 2009 Alvin Penner, penner@vaxxine.com
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+'''
+
+import inkex, cubicsuperpath, bezmisc, simplestyle
+
+def tpoint((x1,y1), (x2,y2), t = 0.5):
+    return [x1+t*(x2-x1),y1+t*(y2-y1)]
+def cspbezsplit(sp1, sp2, t = 0.5):
+    m1=tpoint(sp1[1],sp1[2],t)
+    m2=tpoint(sp1[2],sp2[0],t)
+    m3=tpoint(sp2[0],sp2[1],t)
+    m4=tpoint(m1,m2,t)
+    m5=tpoint(m2,m3,t)
+    m=tpoint(m4,m5,t)
+    return [[sp1[0][:],sp1[1][:],m1], [m4,m,m5], [m3,sp2[1][:],sp2[2][:]]]
+def cspbezsplitatlength(sp1, sp2, l = 0.5, tolerance = 0.001):
+    bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
+    t = bezmisc.beziertatlength(bez, l, tolerance)
+    return cspbezsplit(sp1, sp2, t)
+def cspseglength(sp1,sp2, tolerance = 0.001):
+    bez = (sp1[1][:],sp1[2][:],sp2[0][:],sp2[1][:])
+    return bezmisc.bezierlength(bez, tolerance)    
+
+class SplitIt(inkex.Effect):
+    def __init__(self):
+        inkex.Effect.__init__(self)
+
+    def effect(self):
+        for id, node in self.selected.iteritems():
+            if node.tag == inkex.addNS('path','svg'):
+                dashes = []
+                style = simplestyle.parseStyle(node.get('style'))
+                if style.has_key('stroke-dasharray'):
+                    if style['stroke-dasharray'].find(',') > 0:
+                        dashes = [float (dash) for dash in style['stroke-dasharray'].split(',')]
+                if dashes:
+                    p = cubicsuperpath.parsePath(node.get('d'))
+                    new = []
+                    for sub in p:
+                        idash = 0
+                        dash = dashes[0]
+                        length = 0
+                        new.append([sub[0][:]])
+                        i = 1
+                        while i < len(sub):
+                            dash = dash - length
+                            length = cspseglength(new[-1][-1], sub[i])
+                            while dash < length:
+                                new[-1][-1], next, sub[i] = cspbezsplitatlength(new[-1][-1], sub[i], dash/length)
+                                if idash % 2:           # create a gap
+                                    new.append([next[:]])
+                                else:                   # splice the curve
+                                    new[-1].append(next[:])
+                                length = length - dash
+                                idash = (idash + 1) % len(dashes)
+                                dash = dashes[idash]
+                            if idash % 2:
+                                new.append([sub[i]])
+                            else:
+                                new[-1].append(sub[i])
+                            i+=1
+                    node.set('d',cubicsuperpath.formatPath(new))
+
+if __name__ == '__main__':
+    e = SplitIt()
+    e.affect()
+
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99