#!/usr/bin/env python

import sys
sys.path.append('c:\Program Files\inkscape\share\extensions')
import inkex
from simplestyle import *

class GridAlignEffect(inkex.Effect):
    def __init_self(self):
        inkex.Effect.__init__(self)

        self.OptionParser.add_option('-gw','--gw', action='store',
                                     type="int", dest='gw', default='64',
                                     help='What is the grid width (px)?')

        self.OptionParser.add_option('-gh','--gh', action='store',
                                     type="int", dest='gh', default='64',
                                     help='What is the grid height (px)?')

        self.writeLog("ARGV="+str(sys.argv))

    def writeLog(self,msg):
        fp=open("c:\\tmp\\inkscape.log","w+")
        fp.write(msg+"\n")
        fp.close()


    def effect(self):

        svg=self.document.getroot()

        self.writeLog("OPTIONS="+str(self.options))

        # we want to preserve aspect ratio
        widthStep=svg.get('width')/int(self.options.gw)
        heightStep=svg.get('height')/int(self.options.gh)

        # nudge all ellipses onto grid boundaries
        for node in inkex.etree.Element("ellipse"):
            node.set('x', round(node.get('x') / widthStep, 0))
            node.set('y', round(node.get('y') / heightStep, 0))


effect=GridAlignEffect()
effect.affect()