Skip to content

Instantly share code, notes, and snippets.

@darktrojan
Created December 4, 2015 02:13
Show Gist options
  • Save darktrojan/43e6f5ad31cbf7362108 to your computer and use it in GitHub Desktop.
Save darktrojan/43e6f5ad31cbf7362108 to your computer and use it in GitHub Desktop.
Guides creator extension for Inkscape
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>My Guides Creator</_name>
<id>net.darktrojan.myguidescreator</id>
<dependency type="executable" location="extensions">my_guides_creator.py</dependency>
<dependency type="executable" location="extensions">inkex.py</dependency>
<param name="vertical_guides" type="int" min="1" max="1000" _gui-text="Columns:">2</param>
<param name="horizontal_guides" type="int" min="1" max="1000" _gui-text="Rows:">3</param>
<param name="guide_margin" type="int" min="0" max="20" _gui-text="Margin around guides (mm)">10</param>
<param name="start_from_edges" type="boolean" _gui-text="Start from edges">true</param>
<param name="delete_existing_guides" type="boolean" _gui-text="Delete existing guides">true</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu _name="Render"/>
</effects-menu>
</effect>
<script>
<command reldir="extensions" interpreter="python">my_guides_creator.py</command>
</script>
</inkscape-extension>
#!/usr/bin/env python
import inkex
class Guides_Creator(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option(
'--vertical_guides',
action='store', type='string',
dest='vertical_guides', default=1,
help='Vertical guides each:'
)
self.OptionParser.add_option(
'--horizontal_guides',
action='store', type='string',
dest='horizontal_guides', default=1,
help='Horizontal guides each:'
)
self.OptionParser.add_option(
'--guide_margin',
action='store', type='string',
dest='guide_margin', default=10
)
self.OptionParser.add_option(
'--start_from_edges',
action='store', type='inkbool',
dest='start_from_edges', default=False,
help='Start from edges'
)
self.OptionParser.add_option(
'--delete_existing_guides',
action='store', type='inkbool',
dest='delete_existing_guides', default=False,
help='Delete existing guides'
)
def effect(self):
svg = self.document.getroot()
self.width = self.unittouu(svg.get('width')) / self.unittouu('1px')
self.height = self.unittouu(svg.get('height')) / self.unittouu('1px')
self.from_edges = self.options.start_from_edges
self.margin = self.unittouu(self.options.guide_margin + 'mm')
delete_existing = self.options.delete_existing_guides
v_division = int(self.options.vertical_guides)
h_division = int(self.options.horizontal_guides)
if (delete_existing):
self.deleteAllGuides()
self.drawVerticalGuides(v_division)
self.drawHorizontalGuides(h_division)
def deleteAllGuides(self):
nv = self.document.xpath('/svg:svg/sodipodi:namedview', namespaces=inkex.NSS)[0]
children = self.document.xpath(
'/svg:svg/sodipodi:namedview/sodipodi:guide', namespaces=inkex.NSS
)
for element in children:
nv.remove(element)
def drawVerticalGuides(self, division):
if division <= 0:
return
for v in range(0, division + 1):
if not self.from_edges and (v == 0 or v == division):
continue
position_x = v * (self.width / division)
self.createGuide(round(position_x, 4), 0, 90, color='rgb(255,0,255)')
if self.margin > 0:
if v != 0:
self.createGuide(round(position_x - self.margin, 4), 0, 90)
if v != division:
self.createGuide(round(position_x + self.margin, 4), 0, 90)
def drawHorizontalGuides(self, division):
if (division <= 0):
return
for h in range(0, division + 1):
if not self.from_edges and (h == 0 or h == division):
continue
position_y = h * (self.height / division)
self.createGuide(0, round(position_y, 4), 0, color='rgb(255,0,255)')
if self.margin > 0:
if h != 0:
self.createGuide(0, round(position_y - self.margin, 4), 0)
if h != division:
self.createGuide(0, round(position_y + self.margin, 4), 0)
def createGuide(self, position_x, position_y, orientation, color=None):
attrs = {
'position': '%s,%s' % (str(position_x), str(position_y)),
'orientation': '1,0' if orientation == 90 else '0,1'
}
if color is not None:
attrs['{http://www.inkscape.org/namespaces/inkscape}color'] = color
parent = self.document.find(inkex.addNS('namedview', 'sodipodi'))
element_qname = '{http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd}guide'
inkex.etree.SubElement(parent, element_qname, attrs)
if __name__ == '__main__':
effect = Guides_Creator()
effect.affect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment