A random link Home Compute Inkscape Contact   
   
   
   

Connect the Dots

This script converts the selected path into a connect the dots picture by replacing each vertex in the path with a dot and labeling it with a number. While it could certainly be improved, it serves as a simple example of accessing and altering path data and adding nodes to an svg document.

#!/usr/bin/env python 
import sys,inkex

class Dots(inkex.Effect):
	def effect(self):
		for id, node in self.selected.iteritems():
			if node.tagName == 'path':
				a =[]
				d = node.attributes.getNamedItem('d')
				p = inkex.parsePath(d.value)
				num = 1
				for cmd,params in p:
					a.append(['M',params[-2:]])
					a.append(['L',params[-2:]])
					self.addText(node,params[-2],params[-1],num)
					num += 1
				d.value = inkex.formatPath(a)

				style = node.attributes.getNamedItem('style')
				s = inkex.parseStyle(style.value)
				s['stroke-linecap']='round'
				s['stroke-width']='5px'
				style.value = inkex.formatStyle(s)
				
	def addText(self,node,x,y,text):
				new = self.document.createElement('svg:text')
				s = {'stroke-linejoin': 'miter', 'font-size': '20', 
					'stroke-width': '1.0px', 'stroke-opacity': '1.0', 
					'fill-opacity': '1.0', 'stroke': 'none', 'stroke-linecap': 'butt', 
					'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'}
				new.setAttribute('style', inkex.formatStyle(s))
				new.setAttribute('x', str(x))
				new.setAttribute('y', str(y))
				#TODO: prevent id collisions?
				new.setAttribute('id', 'dots'+str(text))
				new.appendChild(self.document.createTextNode(str(text)))
				node.parentNode.appendChild(new)

e = Dots()
e.affect()

Files:

     
   
   
Home Compute Inkscape Contact