diff options
| author | Alvin Penner <penner@vaxxine.com> | 2011-12-21 00:24:38 +0000 |
|---|---|---|
| committer | apenner <penner@vaxxine.com> | 2011-12-21 00:24:38 +0000 |
| commit | 3ad96cd418b1331e0476b756178819c1a0cb0f2e (patch) | |
| tree | 34014d7ba3a4c600b7d43b2d1992a1b9b1baa758 /share | |
| parent | when resizing page, move the origin of the grids too. This way all objects wi... (diff) | |
| download | inkscape-3ad96cd418b1331e0476b756178819c1a0cb0f2e.tar.gz inkscape-3ad96cd418b1331e0476b756178819c1a0cb0f2e.zip | |
Extensions. Import .dhw file. new code by Kevin Lindsey, Nikita Kitaev, Chris Morgan.
(bzr r10788)
Diffstat (limited to 'share')
| -rw-r--r-- | share/extensions/Makefile.am | 2 | ||||
| -rw-r--r-- | share/extensions/dhw_input.inx | 15 | ||||
| -rw-r--r-- | share/extensions/dm2svg.py | 128 |
3 files changed, 145 insertions, 0 deletions
diff --git a/share/extensions/Makefile.am b/share/extensions/Makefile.am index e86852895..8718de825 100644 --- a/share/extensions/Makefile.am +++ b/share/extensions/Makefile.am @@ -44,6 +44,7 @@ extensions = \ cubicsuperpath.py \ dia2svg.sh \ dimension.py \ + dm2svg.py \ dots.py \ draw_from_triangle.py \ dxf_input.py \ @@ -218,6 +219,7 @@ modules = \ color_rgbbarrel.inx\ color_replace.inx\ convert2dashes.inx\ + dhw_input.inx \ dia.inx \ dimension.inx \ dots.inx \ diff --git a/share/extensions/dhw_input.inx b/share/extensions/dhw_input.inx new file mode 100644 index 000000000..91b52ff9e --- /dev/null +++ b/share/extensions/dhw_input.inx @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="UTF-8"?> +<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> + <_name>DHW file input</_name> + <id>org.inkscape.input.dhw</id> + <dependency type="executable" location="extensions">dm2svg.py</dependency> + <input> + <extension>.dhw</extension> + <mimetype>application/x-extension-DHW</mimetype> + <_filetypename>ACECAD Digimemo File (.dhw)</_filetypename> + <_filetypetooltip>Open files from ACECAD Digimemo</_filetypetooltip> + </input> + <script> + <command reldir="extensions" interpreter="python">dm2svg.py</command> + </script> +</inkscape-extension> diff --git a/share/extensions/dm2svg.py b/share/extensions/dm2svg.py new file mode 100644 index 000000000..908fedbad --- /dev/null +++ b/share/extensions/dm2svg.py @@ -0,0 +1,128 @@ +#!/usr/bin/env python +''' +dm2svg.py - import a DHW file from ACECAD DigiMemo + +Copyright (C) 2009 Kevin Lindsey, https://github.com/thelonious/DM2SVG +Copyright (C) 2011 Nikita Kitaev, https://github.com/nikitakit/DM2SVG +Copyright (C) 2011 Chris Morgan, https://gist.github.com/1471691 + +This program is free software; you can redistribute it and/or modify +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 +''' + +import struct + +def process_file(filename): + try: + f = open(filename, 'rb') + except IOError as e: + print >> sys.stderr, 'Unable to open %s: %s' % (filename, e) + return + + with f: + height = emit_header(f) + + layer = 'layer1' + timestamp = 0 + svg_element = '<g inkscape:groupmode="layer" id="%s">' % layer + + while True: + tag = f.read(1) + if tag == '': + break + + if ord(tag) > 128: + if tag == '\x90': + # Emit the current element and close the last layer + emit_element(svg_element) + emit_element('</g>') + + # Start a new layer + layer = 'layer%d' % (ord(f.read(1)) + 1) + timestamp = 0 + svg_element = '<g inkscape:groupmode="layer" id="%s">' % layer + elif tag == '\x88': + # Read the timestamp next + timestamp += ord(f.read(1)) * 20 + else: + # Emit the current svg element + emit_element(svg_element) + + coords = [] + + # Pen down + while True: + coords.append(read_point(f, height)) + if ord(f.read(1)) >= 128: + break + f.seek(-1, 1) # It wasn't the magic value, don't miss it + + # Pen up + coords.append(read_point(f, height)) + points = ' '.join(','.join(map(str, e)) for e in coords) + svg_element = '<polyline points="%s" dm:timestamp="%s" />' % (points, timestamp) + else: + print >> sys.stderr, 'Unsupported tag: %s\n' % tag + + # Emit the footer to finish it off + print '\n</svg>\n' + + +def read_point(f, ymax): + x1, x2, y1, y2 = map(ord, f.read(4)) + x = x1 | x2 << 7 + y = y1 | y2 << 7 + + return x, ymax - y + + +def emit_header(f): + id, version, width, height, page_type = struct.unpack('<32sBHHBxx', f.read(40)) + + print ''' +<svg viewBox="0 0 %(width)s %(height)s" fill="none" stroke="black" stroke-width="10" stroke-linecap="round" stroke-linejoin="round" + xmlns="http://www.w3.org/2000/svg" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + xmlns:dm="http://github.com/nikitakit/DM2SVG"> + <metadata> + <dm:page + id="%(id)s" + version="%(version)s" + width="%(width)s" + height="%(height)s" + page_type="%(page_type)s"> + </dm:page> + </metadata> + <rect width="%(width)s" height="%(height)s" fill="aliceblue"/> +''' % locals() + + return height + + +def emit_element(message): + if message: + print '%s\n' % message + + +if __name__ == '__main__': + import sys + + if len(sys.argv) == 2: + process_file(sys.argv[1]) + else: + print >> sys.stderr, 'Usage: %s <dhw-file>' % sys.argv[0] + +# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99 |
