dimension.py revision a825bb04cf2b441c2cb2bd4770c4ed2c6d6b5e02
324N/A#!/usr/bin/env python
324N/A'''
324N/Adimension.py
324N/AAn Inkscape effect for adding CAD style dimensions to selected objects
943N/Ain a drawing.
324N/A
324N/AIt uses the selection's bounding box, so if the bounding box has empty
919N/Aspace in the x- or y-direction (such as with some stars) the results
919N/Awill look strange. Strokes might also overlap the edge of the
919N/Abounding box.
919N/A
919N/AThe dimension arrows aren't measured: use the "Visualize Path/Measure
919N/APath" effect to add measurements.
919N/A
919N/AThis code contains snippets from existing effects in the Inkscape
919N/Aextensions library, and marker data from markers.svg.
919N/A
919N/ACopyright (C) 2007 Peter Lewerin, peter.lewerin@tele2.se
919N/A
919N/AThis program is free software; you can redistribute it and/or modify
919N/Ait under the terms of the GNU General Public License as published by
919N/Athe Free Software Foundation; either version 2 of the License, or
919N/A(at your option) any later version.
919N/A
324N/AThis program is distributed in the hope that it will be useful,
324N/Abut WITHOUT ANY WARRANTY; without even the implied warranty of
324N/AMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
324N/AGNU General Public License for more details.
493N/A
324N/AYou should have received a copy of the GNU General Public License
970N/Aalong with this program; if not, write to the Free Software
970N/AFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
970N/A'''
970N/A
324N/Aimport sys, inkex, pathmodifier
493N/Afrom simpletransform import *
324N/Aimport gettext
911N/A_ = gettext.gettext
911N/A
911N/Aclass Dimension(pathmodifier.PathModifier):
911N/A def __init__(self):
324N/A inkex.Effect.__init__(self)
371N/A self.OptionParser.add_option("-x", "--xoffset",
324N/A action="store", type="float",
324N/A dest="xoffset", default=100.0,
328N/A help="x offset of the vertical dimension arrow")
493N/A self.OptionParser.add_option("-y", "--yoffset",
324N/A action="store", type="float",
970N/A dest="yoffset", default=100.0,
970N/A help="y offset of the horizontal dimension arrow")
970N/A
324N/A def addMarker(self, name, rotate):
324N/A defs = self.xpathSingle('/svg:svg//svg:defs')
if defs == None:
defs = inkex.etree.SubElement(self.document.getroot(),inkex.addNS('defs','svg'))
marker = inkex.etree.SubElement(defs ,inkex.addNS('marker','svg'))
marker.set('id', name)
marker.set('orient', 'auto')
marker.set('refX', '0.0')
marker.set('refY', '0.0')
marker.set('style', 'overflow:visible')
marker.set(inkex.addNS('stockid','inkscape'), name)
arrow = inkex.etree.Element("path")
arrow.set('d', 'M 0.0,0.0 L 5.0,-5.0 L -12.5,0.0 L 5.0,5.0 L 0.0,0.0 z ')
if rotate:
arrow.set('transform', 'scale(0.8) rotate(180) translate(12.5,0)')
else:
arrow.set('transform', 'scale(0.8) translate(12.5,0)')
arrow.set('style', 'fill-rule:evenodd;stroke:#000000;stroke-width:1.0pt;marker-start:none')
marker.append(arrow)
def dimHLine(self, y, xlat):
line = inkex.etree.Element("path")
x1 = self.bbox[0] - xlat[0] * self.xoffset
x2 = self.bbox[1]
y = y - xlat[1] * self.yoffset
line.set('d', 'M %f %f H %f' % (x1, y, x2))
return line
def dimVLine(self, x, xlat):
line = inkex.etree.Element("path")
x = x - xlat[0] * self.xoffset
y1 = self.bbox[2] - xlat[1] * self.yoffset
y2 = self.bbox[3]
line.set('d', 'M %f %f V %f' % (x, y1, y2))
return line
def effect(self):
self.xoffset = self.options.xoffset
self.yoffset = self.options.yoffset
self.bbox = computeBBox(self.selected.values())
# Avoid ugly failure on rects and texts.
try:
testing_the_water = self.bbox[0]
except TypeError:
sys.exit(_('Unable to process this object. Try changing it into a path first.'))
layer = self.current_layer
self.addMarker('Arrow1Lstart', False)
self.addMarker('Arrow1Lend', True)
group = inkex.etree.Element("g")
group.set('fill', 'none')
group.set('stroke', 'black')
line = self.dimHLine(self.bbox[2], [0, 1])
line.set('marker-start', 'url(#Arrow1Lstart)')
line.set('marker-end', 'url(#Arrow1Lend)')
line.set('stroke-width', '1')
group.append(line)
line = self.dimVLine(self.bbox[0], [0, 2])
line.set('stroke-width', '0.5')
group.append(line)
line = self.dimVLine(self.bbox[1], [0, 2])
line.set('stroke-width', '0.5')
group.append(line)
line = self.dimVLine(self.bbox[0], [1, 0])
line.set('marker-start', 'url(#Arrow1Lstart)')
line.set('marker-end', 'url(#Arrow1Lend)')
line.set('stroke-width', '1')
group.append(line)
line = self.dimHLine(self.bbox[2], [2, 0])
line.set('stroke-width', '0.5')
group.append(line)
line = self.dimHLine(self.bbox[3], [2, 0])
line.set('stroke-width', '0.5')
group.append(line)
for id, node in self.selected.iteritems():
group.append(node)
layer.append(group)
if __name__ == '__main__':
e = Dimension()
e.affect()
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99