Snippets

Iakov Davydov add_size extension for inkscape

Created by Iakov Davydov
#!/usr/bin/env python
from __future__ import print_function

import sys

# These two lines are only needed if you don't put the script directly into
# the installation directory
import sys
sys.path.append('/usr/share/inkscape/extensions')

# We will use the inkex module with the predefined Effect base class.
import inkex
# The simplestyle module provides functions for style parsing.
from simplestyle import *

class AddSizeEffect(inkex.Effect):
    def __init__(self):
        # Call the base class constructor.
        inkex.Effect.__init__(self)

        # Define string option "--what" with "-w" shortcut and default value "World".
        self.OptionParser.add_option('-H', '--height', action = 'store',
          type = 'float', dest = 'height', default = 0,
          help = 'Increase height by')

        self.OptionParser.add_option('-w', '--width', action = 'store',
          type = 'float', dest = 'width', default = 0,
          help = 'Increase width by')

        self.OptionParser.add_option('-u', '--unit', action = 'store',
          type = 'str', dest = 'unit', default = '1.',
          help = 'Base unit')

    def effect(self):
        svg = self.document.getroot()
        unit = self.options.unit 
        if '/' in unit:
            num, den = (float(v) for v in unit.split('/'))
            unit = num / den
        else:
            unit = float(unit)
        dw = self.options.width / unit
        dh = self.options.height / unit

        for i, o in self.selected.iteritems():
            o.attrib['width'] = str(float(o.get('width')) + dw)
            o.attrib['height']= str(float(o.get('height')) + dh)

# Create effect instance and apply it.
effect = AddSizeEffect()
effect.affect()
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
  <_name>Add size</_name>
  <id>org.ekips.filter.add_size</id>
  <dependency type="executable" location="extensions">add_size.py</dependency>
  <dependency type="executable" location="extensions">inkex.py</dependency>
  <param name="height" type="float" min="-9999.00" max="9999.00" _gui-text="Add to height">0</param>
  <param name="width" type="float" min="-9999.00" max="9999.00" _gui-text="Add to width">0</param>
  <param name="unit" type="enum" _gui-text="Base unit">
    <_item value="72./90">pt</_item>
    <_item value="1./15">pc</_item>
    <_item value="1.">px</_item>
    <_item value="25.4/90">mm</_item>
    <_item value="2.54/90">cm</_item>
    <_item value=".0254/90">m</_item>
    <_item value="1./90">in</_item>
    <_item value="1./1080">ft</_item>
  </param>

  <effect>
    <object-type>all</object-type>
    <effects-menu>
       <submenu _name="My"/>
    </effects-menu>
  </effect>
  <script>
    <command reldir="extensions" interpreter="python">add_size.py</command>
  </script>
</inkscape-extension>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.