measure.py revision 7ee7b8c1cab74de2b8ee22c617605a3dd48fa63d
'''
This extension module can measure arbitrary path and object length
It adds text to the selected path containing the length in a given unit.
Area and Center of Mass calculated using Green's Theorem:
Copyright (C) 2015 ~suv <suv-sf@users.sf.net>
Copyright (C) 2010 Alvin Penner
Copyright (C) 2006 Georg Wiora
Copyright (C) 2006 Nathan Hurst
Copyright (C) 2005 Aaron Spike, aaron@ekips.org
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
TODO:
* should use the standard attributes for text
* Implement option to keep text orientation upright
1. Find text direction i.e. path tangent,
2. check direction >90 or <-90 Degrees
3. rotate by 180 degrees around text center
'''
# standard library
import locale
# local library
import inkex
import simplestyle
import simpletransform
import cubicsuperpath
import bezmisc
# On darwin, fall back to C in cases of
# - incorrect locale IDs (see comments in bug #406662)
try:
# third party
try:
import numpy
except:
inkex.errormsg(_("Failed to import the numpy modules. These modules are required by this extension. Please install them and try again. On a Debian-like system this can be done with the command, sudo apt-get install python-numpy."))
exit()
return pointdistance(sp1[1], sp1[2]) + pointdistance(sp1[2], sp2[0]) + pointdistance(sp2[0], sp2[1])
total = 0
lengths = []
total += l
area = 0.0
return -area # require positive area for CCW
xc = 0.0
yc = 0.0
return 0, 0
vec_t = numpy.matrix([(vec_x*mat_cofm_0*vec_y.T)[0,0], (vec_x*mat_cofm_1*vec_y.T)[0,0], (vec_x*mat_cofm_2*vec_y.T)[0,0], (vec_x*mat_cofm_3*vec_y.T)[0,0]])
super = inkex.etree.SubElement(node, inkex.addNS('tspan', 'svg'), {'style': 'font-size:65%;baseline-shift:super'})
help="Type of measurement")
help="Text Orientation")
help="Preset text layout")
help="Text Offset along Path")
help="Text Offset along Path")
help="Text Anchor")
help="Text Position")
help="Angle")
help="Size of length lable text in px")
help="The distance above the curve")
help="The unit of the measurement")
help="Number of significant digits after decimal point")
help="Scale Factor (Drawing:Real Length)")
help="Keep orientation of text upright")
help="The selected UI-tab when OK was pressed")
help="dummy")
# get number of digits
factor = 1.0
# loop over all selected paths
else:
continue
# Format the length as string
lenstr = locale.format("%(len)25."+str(prec)+"f",{'len':round(stotal*factor*self.options.scale,prec)}).strip()
if startOffset == "custom":
self.addTextOnPath(self.group, 0, 0, lenstr+' '+self.options.unit, id, self.options.anchor, startOffset, self.options.offset)
else:
self.addTextOnPath(self.group, 0, 0, lenstr+' '+self.options.unit+'^2', id, self.options.anchor, startOffset, self.options.offset)
anchor = 'middle'
anchor = 'middle'
else: # default
anchor = 'start'
self.addTextWithTspan(self.group, tx, ty, lenstr+' '+self.options.unit, id, anchor, -int(self.options.angle), self.options.offset + self.options.fontsize/2)
else:
self.addTextWithTspan(self.group, tx, ty, lenstr+' '+self.options.unit+'^2', id, anchor, -int(self.options.angle), -self.options.offset + self.options.fontsize/2)
else:
# center of mass, no text
pass
# keep dict in sync with enum in INX file:
preset_dict = {
'default_length': ['"textonpath"', "50%", "start", None, None],
'default_area': ['"fixedtext"', None, None, "start", 0.0],
'default_cofm': [None, None, None, None, None],
'TaP_start': ['"textonpath"', "0%", "start", None, None],
'TaP_middle': ['"textonpath"', "50%", "middle", None, None],
'TaP_end': ['"textonpath"', "100%", "end", None, None],
'FT_start': ['"fixedtext"', None, None, "start", 0.0],
'FT_bbox': ['"fixedtext"', None, None, "center", 0.0],
'FT_mass': ['"fixedtext"', None, None, "mass", 0.0],
}
else:
node.set('d', 'm %s,%s %s,0 %s,0 m %s,%s 0,%s 0,%s' % (str(x-l), str(y), str(l), str(l), str(-l), str(-l), str(l), str(l)))
s = {'text-align': 'center', 'vertical-align': 'bottom',
'fill-opacity': '1.0', 'stroke': 'none',
'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'}
#new.append(tp)
else:
#node.set('transform','rotate(180,'+str(-x)+','+str(-y)+')')
new = inkex.etree.SubElement(node,inkex.addNS('tspan','svg'), {inkex.addNS('role','sodipodi'): 'line'})
s = {'text-align': 'center', 'vertical-align': 'bottom',
'fill-opacity': '1.0', 'stroke': 'none',
'font-weight': 'normal', 'font-style': 'normal', 'fill': '#000000'}
else:
if __name__ == '__main__':
e = Length()
e.affect()
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99