User:Sergeybe

From Inkscape Wiki
Jump to navigation Jump to search

This extension draws old-fashioned rotary dialer

rotarydialer.inx

<inkscape-extension>
<_name>Rotary Dialer</_name>
    <id>RotaryDialer</id>
    <dependency type="executable" location="extensions">rotarydialer.py</dependency>
    <dependency type="executable" location="extensions">inkex.py</dependency>
    <param name="radius1" type="int" _gui-text="Radius1" min="0" max="2000">300</param>
    <param name="radius2" type="int" _gui-text="Radius2" min="0" max="2000">55</param>
    <param name="angle" type="int" _gui-text="Angle" min="0" max="360">28</param>
    <effect>
        <object-type>all</object-type>
        <effects-menu>
            <submenu _name="Render"/>
        </effects-menu>
    </effect>
    <script>
        <command reldir="extensions" interpreter="python">rotarydialer.py</command>
    </script>
</inkscape-extension>

rotarydialer.py

#!/usr/bin/env python
#
# -*- coding: utf-8 -*-

import sys

if sys.platform in ('win32','win16', 'os2', 'dos'):
  	sys.path.append('C:\Program Files\Inkscape\share\extensions')
else:
	sys.path.append('/usr/share/inkscape/extensions')

import inkex
from simplestyle import *
import math

class RotaryDialer(inkex.Effect):
	"""
	Init
	"""
	def __init__(self):
		inkex.Effect.__init__(self)
		
		self.OptionParser.add_option('-1', '--radius1', action = 'store',
		type = 'int', dest = 'radius1', default = 300,
		help = 'Radius1')
		
		self.OptionParser.add_option('-2', '--radius2', action = 'store',
		type = 'int', dest = 'radius2', default = 55,
		help = 'Radius2')
		
		self.OptionParser.add_option('-a', '--angle', action = 'store',
		type = 'int', dest = 'angle', default = 28,
		help = 'Angle')
		
	"""
	Effect
	"""
	def effect(self):
		# Set local variables
		radius1 = self.options.radius1
		radius2 = self.options.radius2
		angle = self.options.angle
		
		# Get document
		svg = self.document.getroot()
		width = inkex.unittouu(svg.get('width'))
		height = inkex.unittouu(svg.attrib['height'])
		
		# Create new layer
		layer = inkex.etree.SubElement(svg, 'g')
		layer.set(inkex.addNS('label', 'inkscape'), 'Sector 1 Layer')
		layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')
	
		# Add black rect
		rect = inkex.etree.SubElement(layer, inkex.addNS('rect','svg'))
		rect.set('x', '0')
		rect.set('y', '0')
		rect.set('width', str(width))
		rect.set('height', str(height))
		style = {
		'opacity': '1',
		'fill': '#000000',
		'fill-opacity': '1',
		'stroke': 'none',
		'stroke-width': '4',
		'stroke-miterlimit': '4',
		'stroke-dasharray': 'none',
		'stroke-opacity': '1'
		}
		rect.set('style', formatStyle(style))

		# Get defs
        	defs = self.xpathSingle('/svg:svg//svg:defs')
        	if not defs:
            		defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))
	
		
		# Create gradient in space
		gradient1 = inkex.etree.SubElement(defs, inkex.addNS('linearGradient','svg'))
		gradient1.set('id', 'Grad1')
		gradient1.set('gradientUnits', 'userSpaceOnUse')
		gradient1.set('x1', str(width / 2))
		gradient1.set('y1', str(height / 2 - radius1))
		gradient1.set('x2', str(width / 2))
		gradient1.set('y2', str(height / 2 + radius1))
		gradient1.set(inkex.addNS('collect', 'inkscape'), 'always')
		gradient1.set(inkex.addNS('href', 'xlink'), '#Grad2')

		defs.append(gradient1)

		# Create gradient in color
		
		gradient2 = inkex.etree.SubElement(defs, inkex.addNS('linearGradient','svg'))
		gradient2.set('id', 'Grad2')
		gradient2.set(inkex.addNS('collect', 'inkscape'), 'always')
		
		# Add 1 stop
		stop1 = inkex.etree.SubElement(gradient2, inkex.addNS('stop','svg'))
		stop1.set('id', 'stop1')
		stop1.set('offset', '0')

		style = {
		'stop-color': '#e7e7e7',
		'stop-opacity': '1'
		}
		stop1.set('style', formatStyle(style))
		gradient2.append(stop1)
		
		# Add 2 stop
		stop2 = inkex.etree.SubElement(gradient2, inkex.addNS('stop','svg'))
		stop2.set('id', 'stop2')
		stop2.set('offset', '1')

		style = {
		'stop-color': '#4c564d',
		'stop-opacity': '1'
		}
		stop2.set('style', formatStyle(style))
		gradient2.append(stop2)

		# Create gradient background

		gradient3 = inkex.etree.SubElement(defs, inkex.addNS('radialGradient','svg'))
		gradient3.set('id', 'Grad3')
		gradient3.set('r', '304')
		gradient3.set('cx', str(width / 2))
		gradient3.set('cy', str(height / 2))
		gradient3.set('fx', '300')
		gradient3.set('fy', '300')
		gradient3.set('r', '304')
		gradient3.set('gradientUnits', 'userSpaceOnUse')
		gradient3.set(inkex.addNS('collect', 'inkscape'), 'always')
		gradient3.set(inkex.addNS('href', 'xlink'), '#Grad2')
		
		# Add 1 stop
		stop1 = inkex.etree.SubElement(gradient3, inkex.addNS('stop','svg'))
		stop1.set('id', 'stop1')
		stop1.set('offset', '0')

		style = {
		'stop-color': '#ffffff',
		'stop-opacity': '0'
		}
		stop1.set('style', formatStyle(style))
		gradient3.append(stop1)
		
		# Add 2 stop
		stop2 = inkex.etree.SubElement(gradient3, inkex.addNS('stop','svg'))
		stop2.set('id', 'stop2')
		stop2.set('offset', '0.9')

		style = {
		'stop-color': '#ffffff',
		'stop-opacity': '1'
		}
		stop2.set('style', formatStyle(style))
		gradient3.append(stop2)

		# Add 3 stop
		stop3 = inkex.etree.SubElement(gradient3, inkex.addNS('stop','svg'))
		stop3.set('id', 'stop3')
		stop3.set('offset', '1')

		style = {
		'stop-color': '#ffffff',
		'stop-opacity': '1'
		}
		stop3.set('style', formatStyle(style))
		gradient3.append(stop3)

		# Append gradients to defs
		defs.append(gradient1)			
		defs.append(gradient2)
		defs.append(gradient3)

		# Create background 
		path = inkex.etree.SubElement(layer, 'path')
		path.set(inkex.addNS('type', 'sodipodi'), 'arc')
		path.set(inkex.addNS('cx', 'sodipodi'), str(width / 2))
		path.set(inkex.addNS('cy', 'sodipodi'), str(height / 2))
		path.set(inkex.addNS('rx', 'sodipodi'), str(radius1))
		path.set(inkex.addNS('ry', 'sodipodi'), str(radius1))

		style = {
		'fill': '#000000',
		'stroke': '#000000',
#		'stroke': 'url(#Grad1)',
		'stroke-width': '8',
		'stroke-miterlimit': '0',
		'stroke-opacity': '1',
		'stroke-dasharray': 'none',
		'fill-opacity': '1',
		'stroke-miterlimit': '4',
		'stroke-dasharray': 'none'
		}

		path.set('style', formatStyle(style))
		
		layer.append(path)

		# Create circle 
		path = inkex.etree.SubElement(layer, 'path')
		path.set(inkex.addNS('type', 'sodipodi'), 'arc')
		path.set(inkex.addNS('cx', 'sodipodi'), str(width / 2))
		path.set(inkex.addNS('cy', 'sodipodi'), str(height / 2))
		path.set(inkex.addNS('rx', 'sodipodi'), str(radius1))
		path.set(inkex.addNS('ry', 'sodipodi'), str(radius1))

		style = {
		'fill': '#7f7f7f',
#		'stroke': '#000000',
		'stroke': 'url(#Grad1)',
		'stroke-width': '8',
		'stroke-miterlimit': '0',
		'stroke-opacity': '0.5',
		'stroke-dasharray': 'none',
		'fill-opacity': '0.5',
		'stroke-miterlimit': '4',
		'stroke-dasharray': 'none'
		}

		path.set('style', formatStyle(style))
		
		layer.append(path)
		
		# Create inside circle
		path = inkex.etree.SubElement(layer, 'path')
		path.set(inkex.addNS('type', 'sodipodi'), 'arc')
		path.set(inkex.addNS('cx', 'sodipodi'), str(width / 2))
		path.set(inkex.addNS('cy', 'sodipodi'), str(height / 2))
		path.set(inkex.addNS('rx', 'sodipodi'), str(radius1 - 2 * radius2 - 20))
		path.set(inkex.addNS('ry', 'sodipodi'), str(radius1 - 2 * radius2 - 20))
		
		style = {
		'fill': '#7f7f7f',
		'stroke': '#000000',
		'stroke-width': '2',
		'stroke-opacity': '0.5',
		'fill-opacity': '0.5',
		'stroke-miterlimit': '4',
		'stroke-dasharray': 'none'
		}
		
		path.set('style', formatStyle(style))
		
		layer.append(path)
	
		# Create small circlies
		i = 0
		fi = 0
		
		# Alphabet
		alphabet = (
		"",
		"ABC",
		"DEF",
		"GHI",
		"JKL",
		"MN",
		"PRS",
		"TUV",
		"WXY",
		"OQ"
		)

		while (fi < 360 and i < 10):
			rad = math.radians(fi + 90)
			cx = width / 2 + math.cos(rad) * (radius1 - radius2 - 10)
			cy = height / 2 + math.sin(rad) * (radius1 - radius2 - 10)
			
			# Small circle
			path = inkex.etree.SubElement(layer, 'path')
			path.set(inkex.addNS('type', 'sodipodi'), 'arc')
			path.set(inkex.addNS('cx', 'sodipodi'), str(cx))
			path.set(inkex.addNS('cy', 'sodipodi'), str(cy))
			path.set(inkex.addNS('rx', 'sodipodi'), str(radius2))
			path.set(inkex.addNS('ry', 'sodipodi'), str(radius2))
			
			#style = {
			#'fill' : 'none',
			#'stroke': '#000000',
			#'stroke-width' : '1.0px',
			#'stroke-opacity' : '1.0'
			#}
			
			style = {
#			'fill': '#7f7f7f',
			'fill': '#Grad2',
			'stroke': '#000000',
			'stroke-width': '2px',
			'stroke-opacity': '0.5',
			'fill-opacity': '0.5',
			'stroke-miterlimit': '4px',
			'stroke-dasharray': 'none'
			}
			
			path.set('style', formatStyle(style))

			# Numbers
			text = inkex.etree.SubElement(layer, 'text')
			text.set('id', 'text' + str(i))
			text.set('x', str(cx))
			text.set('y', str(cy + 18))

			if i == 0:
				text.text = '0'
			else :
				text.text = str(10 - i)
			
			style = {
			'fill':'#ffffff',
			'font-size': '64px',
			'text-anchor': 'middle', 
			'text-align': 'center', 
			'-inkscape-font-specification': 'Agency FB Bold',
			'font-family': 'Agency FB',
			'font-weight': 'bold',
			'font-style': 'normal'
			}
			text.set('style', formatStyle(style))

			# Alphabets
			text = inkex.etree.SubElement(layer, 'text')
			text.set('id', 'textA' + str(i))
			text.set('x', str(cx))
			text.set('y', str(cy + 43))
			text.text = alphabet[9 - i]
			style = {
			'fill':'#ffffff',
			'font-size': '24px',
			'text-anchor': 'middle', 
			'text-align': 'center', 
			'-inkscape-font-specification': 'Agency FB Bold',
			'font-family': 'Agency FB',
			'font-weight': 'bold',
			'font-style': 'normal'
			}
			text.set('style', formatStyle(style))
			
			# Increase variables
			fi = fi + angle
			i = i + 1

effect = RotaryDialer()
effect.affect()