~inkscape.dev/inkscape/trunk

12365 by apenner
extensions. hpgl input. new import routine by TimeWaster
1
#!/usr/bin/env python
2
# coding=utf-8
3
'''
12417.1.34 by Sebastian Wüst
better PEP 8 compatibility
4
Copyright (C) 2013 Sebastian Wüst, sebi@timewaster.de
12365 by apenner
extensions. hpgl input. new import routine by TimeWaster
5
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
'''
20
12417.1.34 by Sebastian Wüst
better PEP 8 compatibility
21
# standard libraries
12417.1.15 by Sebastian Wüst
parser can now work with multiple pens, better exception handling
22
import sys
12365 by apenner
extensions. hpgl input. new import routine by TimeWaster
23
from StringIO import StringIO
12417.1.34 by Sebastian Wüst
better PEP 8 compatibility
24
# local libraries
25
import hpgl_decoder
26
import inkex
27
import sys
12365 by apenner
extensions. hpgl input. new import routine by TimeWaster
28
inkex.localize()
29
12417.1.1 by Sebastian Wüst
moved main hpgl processing to new classes, moved plotter control to a extension dialog
30
12365 by apenner
extensions. hpgl input. new import routine by TimeWaster
31
# parse options
12417.1.3 by Sebastian Wüst
changed how decoder is called and errors are passed
32
parser = inkex.optparse.OptionParser(usage='usage: %prog [options] HPGLfile', option_class=inkex.InkOption)
33
parser.add_option('--resolutionX',   action='store', type='float',   dest='resolutionX',   default=1016.0,  help='Resolution X (dpi)')
34
parser.add_option('--resolutionY',   action='store', type='float',   dest='resolutionY',   default=1016.0,  help='Resolution Y (dpi)')
35
parser.add_option('--showMovements', action='store', type='inkbool', dest='showMovements', default='FALSE', help='Show Movements between paths')
12365 by apenner
extensions. hpgl input. new import routine by TimeWaster
36
(options, args) = parser.parse_args(inkex.sys.argv[1:])
12417.1.3 by Sebastian Wüst
changed how decoder is called and errors are passed
37
12417.1.1 by Sebastian Wüst
moved main hpgl processing to new classes, moved plotter control to a extension dialog
38
# needed to initialize the document
13314 by Sebastian Wüst
changed default unit for hpgl importer to real world unit mm
39
options.docWidth = 210.0 # 210mm (DIN A4)
40
options.docHeight = 297.0 # 297mm (DIN A4)
12417.1.1 by Sebastian Wüst
moved main hpgl processing to new classes, moved plotter control to a extension dialog
41
12417.1.4 by Sebastian Wüst
load whole file instead of one line.
42
# read file
43
fobj = open(args[0], 'r')
44
hpglString = []
45
for line in fobj:
46
    hpglString.append(line.strip())
47
fobj.close()
48
# combine all lines
49
hpglString = ';'.join(hpglString)
12417.1.3 by Sebastian Wüst
changed how decoder is called and errors are passed
50
51
# interpret HPGL data
52
myHpglDecoder = hpgl_decoder.hpglDecoder(hpglString, options)
53
try:
54
    doc, warnings = myHpglDecoder.getSvg()
55
except Exception as inst:
56
    if inst.args[0] == 'NO_HPGL_DATA':
57
        # issue error if no hpgl data found
58
        inkex.errormsg(_("No HPGL data found."))
12787 by Sebastian Wüst
fixed and optimized pyserial usage, added dmpl support, small stuff
59
        exit(1)
12417.1.3 by Sebastian Wüst
changed how decoder is called and errors are passed
60
    else:
12417.1.15 by Sebastian Wüst
parser can now work with multiple pens, better exception handling
61
        type, value, traceback = sys.exc_info()
62
        raise ValueError, ("", type, value), traceback
12417.1.1 by Sebastian Wüst
moved main hpgl processing to new classes, moved plotter control to a extension dialog
63
12787 by Sebastian Wüst
fixed and optimized pyserial usage, added dmpl support, small stuff
64
# issue warning if unknown commands where found
65
if 'UNKNOWN_COMMANDS' in warnings:
66
    inkex.errormsg(_("The HPGL data contained unknown (unsupported) commands, there is a possibility that the drawing is missing some content."))
67
68
# deliver document to inkscape
69
doc.write(inkex.sys.stdout)
70
12417.1.1 by Sebastian Wüst
moved main hpgl processing to new classes, moved plotter control to a extension dialog
71
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99