From Inkscape Wiki
Jump to navigation Jump to search

This module specifies the method for drawing an SVG object from parameters entered by the "command line". It contains:

  1. A dictionary holding possible parameters (except general style paramters - these are found in the global association module), along with the type of the parameter and the default value, which will be used if the user doesn't specify. For example, if the user enters circle r:50, the circle will be centred at (0,0).
  2. A function called draw_SVG(), which puts the parameters into the correct position in the SVG, and draws it using, in this case, the inkex helper module.
  3. A function used to perform overrides for redundant parameters. For example, in the circle case, the radius only is used in drawing the circle. However, if the user enters a diameter and not radius, the default radius (100) would be used unless this function sets the radius to the diameter/2.

Draft Code

#!/usr/bin/env python 


This script contains routines for drawing circles using command line parameters
formatted by command_line.py

import inkex, simplestyle
import command_line

command_dictionary = { 'circle':{
                    'r':{'t':'scalar',  'd':100},   #radius     if r given, overrides diameter
                    'd':{'t':'scalar'},             #diameter   
                    'c':{'t':'point' ,  'd':(0,0)} #centre     uses the inkscape position, if given

def draw_SVG(params, parent):
    style = command_line.create_SVG_style_dict(params)
    attribs = {'style':simplestyle.formatStyle(style),
                'cx':           str(params['c'][0]),
                'cy':           str(params['c'][1]),
                'r':            str(params['r'])
    inkex.etree.SubElement(parent, inkex.addNS('circle','svg'), attribs )

def set_defaults(params):

	#r overrides d
	if 'd' in params and 'r' not in params: 
		params['r'] = params['d']/2.0