home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / share / extensions / hpgl_output.py < prev    next >
Text File  |  2011-07-08  |  5KB  |  114 lines

  1. #!/usr/bin/env python 
  2. '''
  3. Copyright (C) 2008 Aaron Spike, aaron@ekips.org
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. '''
  19. import inkex, simpletransform, cubicsuperpath, simplestyle, cspsubdiv
  20.  
  21. class MyEffect(inkex.Effect):
  22.     def __init__(self):
  23.         inkex.Effect.__init__(self)
  24.         self.OptionParser.add_option("-f", "--flatness",
  25.                         action="store", type="float", 
  26.                         dest="flat", default=0.2,
  27.                         help="Minimum flatness of the subdivided curves")
  28.         self.OptionParser.add_option("-m", "--mirror",
  29.                         action="store", type="inkbool", 
  30.                         dest="mirror", default="FALSE",
  31.                         help="Mirror Y-Axis")
  32.         self.OptionParser.add_option("-x", "--xOrigin",
  33.                         action="store", type="float", 
  34.                         dest="xOrigin", default=0.0,
  35.                         help="X Origin (pixels)")
  36.         self.OptionParser.add_option("-y", "--yOrigin",
  37.                         action="store", type="float", 
  38.                         dest="yOrigin", default=0.0,
  39.                         help="Y Origin (pixels)")
  40.         self.OptionParser.add_option("-r", "--resolution",
  41.                         action="store", type="int", 
  42.                         dest="resolution", default=1016,
  43.                         help="Resolution (dpi)")
  44.         self.OptionParser.add_option("-n", "--pen",
  45.                         action="store", type="int",
  46.                         dest="pen", default=1,
  47.                         help="Pen number")
  48.         self.OptionParser.add_option("-p", "--plotInvisibleLayers",
  49.                         action="store", type="inkbool", 
  50.                         dest="plotInvisibleLayers", default="FALSE",
  51.                         help="Plot invisible layers")
  52.  
  53.     def output(self):
  54.         print ''.join(self.hpgl)
  55.  
  56.     def process_path(self, node, mat):
  57.         d = node.get('d')
  58.         if d:
  59.             p = cubicsuperpath.parsePath(d)
  60.             trans = node.get('transform')
  61.             if trans:
  62.                 mat = simpletransform.composeTransform(mat, simpletransform.parseTransform(trans))
  63.             simpletransform.applyTransformToPath(mat, p)
  64.             cspsubdiv.cspsubdiv(p, self.options.flat)
  65.             for sp in p:
  66.                 first = True
  67.                 for csp in sp:
  68.                     cmd = 'PD'
  69.                     if first:
  70.                         cmd = 'PU'
  71.                     first = False
  72.                     self.hpgl.append('%s%d,%d;' % (cmd,csp[1][0],csp[1][1]))
  73.  
  74.     def process_group(self, group):
  75.         style = group.get('style')
  76.         if style:
  77.             style = simplestyle.parseStyle(style)
  78.             if style.has_key('display'):
  79.                 if style['display']=='none':
  80.                     if not self.options.plotInvisibleLayers:
  81.                         return
  82.         trans = group.get('transform')
  83.         if trans:
  84.             self.groupmat.append(simpletransform.composeTransform(self.groupmat[-1], simpletransform.parseTransform(trans)))
  85.         for node in group:
  86.             if node.tag == inkex.addNS('path','svg'):
  87.                 self.process_path(node, self.groupmat[-1])
  88.             if node.tag == inkex.addNS('g','svg'):
  89.                 self.process_group(node)
  90.         if trans:
  91.             self.groupmat.pop()
  92.  
  93.     def effect(self):
  94.         self.hpgl = ['IN;SP%d;' % self.options.pen]
  95.         x0 = self.options.xOrigin
  96.         y0 = self.options.yOrigin
  97.         scale = float(self.options.resolution)/90
  98.         self.options.flat *= scale
  99.         mirror = 1.0
  100.         if self.options.mirror:
  101.             mirror = -1.0
  102.             if self.document.getroot().get('height'):
  103.                 y0 -= float(self.document.getroot().get('height'))
  104.         self.groupmat = [[[scale, 0.0, -x0*scale], [0.0, mirror*scale, -y0*scale]]]
  105.         doc = self.document.getroot()
  106.         self.process_group(doc)
  107.         self.hpgl.append('PU;')
  108.  
  109. if __name__ == '__main__':   #pragma: no cover
  110.     e = MyEffect()
  111.     e.affect()
  112.  
  113. # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
  114.