Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 65 additions & 157 deletions gridstrip_creator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
'''
Grid Strip Creator v1.0 (30/11/2014)

Expand Down Expand Up @@ -26,26 +26,16 @@
# see also http://codespeak.net/lxml/dev/tutorial.html#namespaces for XML namespaces manipulation


# # # extension's begining # # #

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

# We will use the inkex module with the predefined Effect base class.
# These two lines are only needed if you don't put the script directly into the installation directory
import inkex

from xml.etree import ElementTree as ET

# for printing debugging output
from lxml import etree
import gettext
_ = gettext.gettext

def printDebug(string):
inkex.errormsg(_(string))


class GridStrip_Creator(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
Expand All @@ -56,130 +46,53 @@ def __init__(self):
# Call the base class constructor.
inkex.Effect.__init__(self)

# Define string option "--length" with default value '0.0'.
self.OptionParser.add_option('--length',
action = 'store',type = 'float',
dest = 'length',default = 230.0,
help = 'Length of strip')

# Define string option "--width" with default value '0.0'.
self.OptionParser.add_option('--width',
action = 'store',type = 'float',
dest = 'width',default = 20.0,
help = 'Width of strip')

# Define string option "--cellheight" with default value '0.0'.
self.OptionParser.add_option('--cellheight',
action = 'store',type = 'float',
dest = 'cellheight',default = 12.5,
help = 'height of cell')

# Define string option "--cellwidth" with default value '0.0'.
self.OptionParser.add_option('--cellwidth',
action = 'store',type = 'float',
dest = 'cellwidth',default = 12.5,
help = 'Width of cell')

# Define string option "--scalecells" with default value False.
self.OptionParser.add_option('--scalecells',
action = 'store',type = 'inkbool',
dest = 'scalecells',default = False,
help = 'Scale cells over length')

# Define string option "--cellnumx" with default value '0.0'.
self.OptionParser.add_option('--cellnumx',
action = 'store',type = 'int',
dest = 'cellnumx',default = 11,
help = 'Number of cells x')

# Define string option "--cellnumy" with default value '0.0'.
self.OptionParser.add_option('--cellnumy',
action = 'store',type = 'int',
dest = 'cellnumy',default = 10,
help = 'Number of cells y')

# Define string option "--notchdepth" with default value '0.0'.
self.OptionParser.add_option('--notchdepth',
action = 'store',type = 'float',
dest = 'notchdepth',default = 1.0,
help = 'Depth of notch')

# Define string option "--notchwidth" with default value '0.0'.
self.OptionParser.add_option('--notchwidth',
action = 'store',type = 'float',
dest = 'notchwidth',default = 10.0,
help = 'Width of notch')

# Define string option "--notchhorizontal" with default value False.
self.OptionParser.add_option('--notchhorizontal',
action = 'store',type = 'inkbool',
dest = 'notchhorizontal',default = False,
help = 'Make notches on horizontal strip')

# Define string option "--notchvertical" with default value False.
self.OptionParser.add_option('--notchvertical',
action = 'store',type = 'inkbool',
dest = 'notchvertical',default = False,
help = 'Make notches on vertical strip')

# Define string option "--notch2depth" with default value '0.0'.
# self.OptionParser.add_option('--notch2depth',
# action = 'store',type = 'float',
# dest = 'notch2depth',default = 10.0,
# help = 'Depth of notch')

# Define string option "--notch2width" with default value '0.0'.
self.OptionParser.add_option('--notch2width',
action = 'store',type = 'float',
dest = 'notch2width',default = 3.0,
help = 'Width of notch')

# Define string option "--notchxcorner" with default value False.
self.OptionParser.add_option('--notchxcorner',
action = 'store',type = 'inkbool',
dest = 'notchxcorner',default = False,
help = 'Make notches on corner of horizontal strip')

# Define string option "--notchycorner" with default value False.
self.OptionParser.add_option('--notchycorner',
action = 'store',type = 'inkbool',
dest = 'notchycorner',default = False,
help = 'Make notches on corner of vertical strip')

self.arg_parser.add_argument('--length', type = float, default = 230.0, help = 'Length of strip')
self.arg_parser.add_argument('--width', type = float, default = 20.0, help = 'Width of strip')
self.arg_parser.add_argument('--cellheight', type = float, default = 12.5, help = 'height of cell')
self.arg_parser.add_argument('--cellwidth', type = float, default = 12.5, help = 'Width of cell')
self.arg_parser.add_argument('--scalecells', type = inkex.Boolean, default = False, help = 'Scale cells over length')
self.arg_parser.add_argument('--cellnumx', type = int, default = 11, help = 'Number of cells x')
self.arg_parser.add_argument('--cellnumy', type = int, default = 10, help = 'Number of cells y')
self.arg_parser.add_argument('--notchdepth', type = float, default = 1.0, help = 'Depth of notch')
self.arg_parser.add_argument('--notchwidth', type = float, default = 10.0, help = 'Width of notch')
self.arg_parser.add_argument('--notchhorizontal', type = inkex.Boolean, default = False, help = 'Make notches on horizontal strip')
self.arg_parser.add_argument('--notchvertical', type = inkex.Boolean, default = False, help = 'Make notches on vertical strip')
self.arg_parser.add_argument('--notch2width', type = float, default = 3.0, help = 'Width of notch')
self.arg_parser.add_argument('--notchxcorner', type = inkex.Boolean, default = False, help = 'Make notches on corner of horizontal strip')
self.arg_parser.add_argument('--notchycorner', type = inkex.Boolean, default = False, help = 'Make notches on corner of vertical strip')


def effect(self):
# Get access to main SVG document element and get its dimensions.
svg = self.document.getroot()
# getting the parent tag of the guide
nv = self.document.xpath('/svg:svg/sodipodi:namedview',namespaces=inkex.NSS)[0]

documentUnits = inkex.addNS('document-units', 'inkscape')
# print >> sys.stderr, nv.get(documentUnits)
# inkex.utils.debug(nv.get(documentUnits))
uunits = nv.get(documentUnits)
message="Units="+uunits
inkex.debug(message)
# inkex.utils.debug(message)

# Get script's options value.
stripwidth=self.unittouu(str(self.options.width)+uunits)
striplength=self.unittouu(str(self.options.length)+uunits)
stripwidth=self.svg.unittouu(str(self.options.width)+uunits)
striplength=self.svg.unittouu(str(self.options.length)+uunits)

cellheight=self.unittouu(str(self.options.cellheight)+uunits)
cellwidth=self.unittouu(str(self.options.cellwidth)+uunits)
cellheight=self.svg.unittouu(str(self.options.cellheight)+uunits)
cellwidth=self.svg.unittouu(str(self.options.cellwidth)+uunits)

scalecells=(self.options.scalecells)

cellnumx=(self.options.cellnumx)
cellnumy=(self.options.cellnumy)

notchdepth=self.unittouu(str(self.options.notchdepth)+uunits)
notchwidth=self.unittouu(str(self.options.notchwidth)+uunits)
notchdepth=self.svg.unittouu(str(self.options.notchdepth)+uunits)
notchwidth=self.svg.unittouu(str(self.options.notchwidth)+uunits)

notchhorizontal=(self.options.notchhorizontal)
notchvertical=(self.options.notchvertical)

# notch2depth=self.unittouu(str(self.options.notch2depth)+uunits)
notch2width=self.unittouu(str(self.options.notch2width)+uunits)
# notch2depth=self.svg.unittouu(str(self.options.notch2depth)+uunits)
notch2width=self.svg.unittouu(str(self.options.notch2width)+uunits)

notch2depth= stripwidth/2

Expand All @@ -193,7 +106,7 @@ def effect(self):
notchycorner=False


linewidth=self.unittouu(str(0.01)+uunits)
linewidth=self.svg.unittouu(str(0.2)+uunits)

distx=(striplength-cellnumx*cellwidth)/2
disty=(striplength-cellnumy*cellheight)/2
Expand All @@ -202,54 +115,54 @@ def effect(self):
celldisty=(cellheight-notch2width)/2

# getting the width and height attributes of the canvas
width = float(self.unittouu(svg.attrib['width']))
height = float(self.unittouu(svg.attrib['height']))
width = float(self.svg.unittouu(svg.attrib['width']))
height = float(self.svg.unittouu(svg.attrib['height']))

# maxlength=max(width,height)
# if striplength > maxlength:
# factor=striplength/maxlength+1

inkex.debug("document width="+str(self.uutounit(width,uunits)))
inkex.debug("document height="+str(self.uutounit(height,uunits)))
# inkex.utils.debug("document width="+str(self.uutounit(width,uunits)))
# inkex.utils.debug("document height="+str(self.uutounit(height,uunits)))

inkex.debug("strip length="+str(self.uutounit(striplength,uunits)))
inkex.debug("strip width="+str(self.uutounit(stripwidth,uunits)))
# inkex.utils.debug("strip length="+str(self.uutounit(striplength,uunits)))
# inkex.utils.debug("strip width="+str(self.uutounit(stripwidth,uunits)))

inkex.debug("cell width="+str(self.uutounit(cellwidth,uunits)))
inkex.debug("cell height="+str(self.uutounit(cellheight,uunits)))
# inkex.utils.debug("cell width="+str(self.uutounit(cellwidth,uunits)))
# inkex.utils.debug("cell height="+str(self.uutounit(cellheight,uunits)))

inkex.debug("Number of cells horizontal="+str(cellnumx))
inkex.debug("Number of cells vertical ="+str(cellnumy))
# inkex.utils.debug("Number of cells horizontal="+str(cellnumx))
# inkex.utils.debug("Number of cells vertical ="+str(cellnumy))

inkex.debug("Depth of extra notch="+str(self.uutounit(notchdepth,uunits)))
inkex.debug("Width of extra notch="+str(self.uutounit(notchwidth,uunits)))
# inkex.utils.debug("Depth of extra notch="+str(self.uutounit(notchdepth,uunits)))
# inkex.utils.debug("Width of extra notch="+str(self.uutounit(notchwidth,uunits)))

inkex.debug("Depth of notch for grid="+str(self.uutounit(notchdepth,uunits)))
inkex.debug("Width of notch for grid="+str(self.uutounit(notchwidth,uunits)))
# inkex.utils.debug("Depth of notch for grid="+str(self.uutounit(notchdepth,uunits)))
# inkex.utils.debug("Width of notch for grid="+str(self.uutounit(notchwidth,uunits)))

inkex.debug("distx="+str(self.uutounit(distx,uunits)))
inkex.debug("disty="+str(self.uutounit(disty,uunits)))
# inkex.utils.debug("distx="+str(self.uutounit(distx,uunits)))
# inkex.utils.debug("disty="+str(self.uutounit(disty,uunits)))

inkex.debug("celldistx="+str(self.uutounit(celldistx,uunits)))
inkex.debug("celldisty="+str(self.uutounit(celldisty,uunits)))
# inkex.utils.debug("celldistx="+str(self.uutounit(celldistx,uunits)))
# inkex.utils.debug("celldisty="+str(self.uutounit(celldisty,uunits)))


parent = self.current_layer
parent = self.svg.get_current_layer()
layername=''
if notchhorizontal:
layername=layername+'VLED '
if notchvertical:
layername=layername+'HLED '

# Create a new layer
layer = inkex.etree.SubElement(svg,'g')
layer = etree.SubElement(svg,'g')
layer.set(inkex.addNS('label', 'inkscape'),layername+'Long strips')
layer.set(inkex.addNS('groupmode','inkscape'), 'layer')


grp_name = 'group_horizontal_strip_long'
grp_attribs = {inkex.addNS('label','inkscape'):grp_name}
grp = inkex.etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in
grp = etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in

style = { 'stroke': '#000000', 'stroke-width':str(linewidth), 'fill': 'none' }

Expand Down Expand Up @@ -301,18 +214,18 @@ def effect(self):

strip_transform= 'rotate(' + str(90)+')'
strip_transform+=' translate('+str(stripwidth*num)+','+str(1)+')'
strip_attribs = {'style':simplestyle.formatStyle(style),
strip_attribs = {'style':str(inkex.Style(style)),
inkex.addNS('label','inkscape'):"strip horizontal long",
'transform': strip_transform,
'd':pathstring}
inkex.etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )
etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )


celldisty=(cellheight-notch2width-notchwidth)/2

grp_name = 'group_vertical_strip_long'
grp_attribs = {inkex.addNS('label','inkscape'):grp_name}
grp = inkex.etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in
grp = etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in


for num in range(0,2):
Expand Down Expand Up @@ -353,20 +266,20 @@ def effect(self):
pathstring+=' L '+str(1)+','+str(1)+' z'

strip_transform= 'translate('+str(num*stripwidth)+','+str(1)+')'
strip_attribs = {'style':simplestyle.formatStyle(style),
strip_attribs = {'style':str(inkex.Style(style)),
inkex.addNS('label','inkscape'):"strip vertical long",
'transform': strip_transform,
'd':pathstring}
inkex.etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )
etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )

# Create a new layer
layer = inkex.etree.SubElement(svg,'g')
layer = etree.SubElement(svg,'g')
layer.set(inkex.addNS('label', 'inkscape'), layername+'Horizontal strips short')
layer.set(inkex.addNS('groupmode','inkscape'), 'layer')

grp_name = 'group horizontal_strip_short'
grp_attribs = {inkex.addNS('label','inkscape'):grp_name}
grp = inkex.etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in
grp = etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in
striplength=cellnumx*cellwidth+4*notch2width
distx=(striplength-cellnumx*cellwidth)/2
disty=(striplength-cellnumy*cellheight)/2
Expand Down Expand Up @@ -410,21 +323,21 @@ def effect(self):
strip_transform='rotate(' + str(90)+')'
strip_transform+=' translate('+str((num+1)*stripwidth+2)+','+str(1)+')'
stripname="strip horizontal short"+str(num)
strip_attribs = {'style':simplestyle.formatStyle(style),
strip_attribs = {'style':str(inkex.Style(style)),
inkex.addNS('label','inkscape'):stripname,
'transform': strip_transform,
'd':pathstring}
inkex.etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )
etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )


# Create a new layer
layer = inkex.etree.SubElement(svg,'g')
layer = etree.SubElement(svg,'g')
layer.set(inkex.addNS('label', 'inkscape'), layername+'Vertical strips short')
layer.set(inkex.addNS('groupmode','inkscape'), 'layer')

grp_name = 'group vertical_strip_short'
grp_attribs = {inkex.addNS('label','inkscape'):grp_name}
grp = inkex.etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in
grp = etree.SubElement(layer, 'g', grp_attribs) #the group to put everything in

striplength=cellnumx*cellwidth+4*notch2width
distx=(striplength-cellnumx*cellwidth)/2
Expand Down Expand Up @@ -466,16 +379,11 @@ def effect(self):

strip_transform= 'translate('+str((num+1)*stripwidth+10)+','+str(1)+')'
stripname="strip vertical short"+str(num)
strip_attribs = {'style':simplestyle.formatStyle(style),
strip_attribs = {'style':str(inkex.Style(style)),
inkex.addNS('label','inkscape'):stripname,
'transform': strip_transform,
'd':pathstring}
inkex.etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )


if __name__ == '__main__': #pragma: no cover
# Create effect instance and apply it.
effect = GridStrip_Creator()
effect.affect()

## end of file gridstrip_creator.py ##
etree.SubElement(grp, inkex.addNS('path','svg'), strip_attribs )

if __name__ == '__main__':
GridStrip_Creator().run()