e97dbe3cccc8b873b529cd251a26684fec2bac96apenner#!/usr/bin/env python
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner# coding=utf-8
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner'''
5051b59af15dfa3ae44b4bf4f10981649d37ff0eSebastian WüstCopyright (C) 2013 Sebastian Wüst, sebi@timewaster.de
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerThis program is free software; you can redistribute it and/or modify
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerit under the terms of the GNU General Public License as published by
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerthe Free Software Foundation; either version 2 of the License, or
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner(at your option) any later version.
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerThis program is distributed in the hope that it will be useful,
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerbut WITHOUT ANY WARRANTY; without even the implied warranty of
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerGNU General Public License for more details.
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerYou should have received a copy of the GNU General Public License
e97dbe3cccc8b873b529cd251a26684fec2bac96apenneralong with this program; if not, write to the Free Software
107e00c8104649437b9520d0ba298dba659e7cd7JazzyNicoFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner'''
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner
5051b59af15dfa3ae44b4bf4f10981649d37ff0eSebastian Wüst# standard libraries
933dce1d63c0de5b9b9323e3c6ac3ed6fc50c342Sebastian Wüstimport sys
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerfrom StringIO import StringIO
5051b59af15dfa3ae44b4bf4f10981649d37ff0eSebastian Wüst# local libraries
5051b59af15dfa3ae44b4bf4f10981649d37ff0eSebastian Wüstimport hpgl_decoder
5051b59af15dfa3ae44b4bf4f10981649d37ff0eSebastian Wüstimport inkex
5051b59af15dfa3ae44b4bf4f10981649d37ff0eSebastian Wüstimport sys
e97dbe3cccc8b873b529cd251a26684fec2bac96apennerinkex.localize()
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner
86776d459a74b8432bbeee60fe796db6403afe7eSebastian Wüst
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner# parse options
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüstparser = inkex.optparse.OptionParser(usage='usage: %prog [options] HPGLfile', option_class=inkex.InkOption)
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüstparser.add_option('--resolutionX', action='store', type='float', dest='resolutionX', default=1016.0, help='Resolution X (dpi)')
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüstparser.add_option('--resolutionY', action='store', type='float', dest='resolutionY', default=1016.0, help='Resolution Y (dpi)')
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüstparser.add_option('--showMovements', action='store', type='inkbool', dest='showMovements', default='FALSE', help='Show Movements between paths')
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner(options, args) = parser.parse_args(inkex.sys.argv[1:])
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst
86776d459a74b8432bbeee60fe796db6403afe7eSebastian Wüst# needed to initialize the document
b8a1484510f4f74b0384356d7924c2ddd61318c5Sebastian Wüstoptions.docWidth = 210.0 # 210mm (DIN A4)
b8a1484510f4f74b0384356d7924c2ddd61318c5Sebastian Wüstoptions.docHeight = 297.0 # 297mm (DIN A4)
e97dbe3cccc8b873b529cd251a26684fec2bac96apenner
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian Wüst# read file
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian Wüstfobj = open(args[0], 'r')
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian WüsthpglString = []
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian Wüstfor line in fobj:
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian Wüst hpglString.append(line.strip())
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian Wüstfobj.close()
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian Wüst# combine all lines
8b65722621bfa3a76feef34a9d3b7de60544140fSebastian WüsthpglString = ';'.join(hpglString)
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst# interpret HPGL data
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian WüstmyHpglDecoder = hpgl_decoder.hpglDecoder(hpglString, options)
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüsttry:
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst doc, warnings = myHpglDecoder.getSvg()
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüstexcept Exception as inst:
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst if inst.args[0] == 'NO_HPGL_DATA':
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst # issue error if no hpgl data found
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst inkex.errormsg(_("No HPGL data found."))
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüst exit(1)
7eeb834ef21e18fe242fcc0b7ab161daa31bb0fcSebastian Wüst else:
933dce1d63c0de5b9b9323e3c6ac3ed6fc50c342Sebastian Wüst type, value, traceback = sys.exc_info()
933dce1d63c0de5b9b9323e3c6ac3ed6fc50c342Sebastian Wüst raise ValueError, ("", type, value), traceback
86776d459a74b8432bbeee60fe796db6403afe7eSebastian Wüst
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüst# issue warning if unknown commands where found
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüstif 'UNKNOWN_COMMANDS' in warnings:
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüst inkex.errormsg(_("The HPGL data contained unknown (unsupported) commands, there is a possibility that the drawing is missing some content."))
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüst
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüst# deliver document to inkscape
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüstdoc.write(inkex.sys.stdout)
7cc5285e0a99d8e121e75abfdfea3474dcd6a86dSebastian Wüst
86776d459a74b8432bbeee60fe796db6403afe7eSebastian Wüst# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99