summaryrefslogtreecommitdiff
path: root/dxf_export
diff options
context:
space:
mode:
authorMichael Schwarz <michi.schwarz@gmail.com>2014-12-07 14:54:54 +0100
committerMichael Schwarz <michi.schwarz@gmail.com>2014-12-07 14:54:54 +0100
commitc02392b65243b3842e273cec36468e21ee376787 (patch)
tree7586f4bce4a40f4da49d87409078146c020b3bf0 /dxf_export
parent606d27c4384a8ad9b2e8b532dcf0c6c38169e057 (diff)
parent785aec3aa6e2deda9acfefa8a9114ea3e7d39f44 (diff)
downloadpogojig-c02392b65243b3842e273cec36468e21ee376787.tar.gz
pogojig-c02392b65243b3842e273cec36468e21ee376787.tar.bz2
pogojig-c02392b65243b3842e273cec36468e21ee376787.zip
Merge commit '785aec3aa6e2deda9acfefa8a9114ea3e7d39f44' into no-examples
Diffstat (limited to 'dxf_export')
-rw-r--r--dxf_export/__main__.py95
-rwxr-xr-xdxf_export/better_dxf_outlines.py4
-rwxr-xr-xdxf_export/main.sh38
3 files changed, 95 insertions, 42 deletions
diff --git a/dxf_export/__main__.py b/dxf_export/__main__.py
new file mode 100644
index 0000000..ea6c7ad
--- /dev/null
+++ b/dxf_export/__main__.py
@@ -0,0 +1,95 @@
+import sys, os, xml.etree.ElementTree, subprocess, tempfile, contextlib, shutil
+import better_dxf_outlines
+
+
+@contextlib.contextmanager
+def TemporaryDirectory():
+ dir = tempfile.mkdtemp()
+
+ try:
+ yield dir
+ finally:
+ shutil.rmtree(dir)
+
+
+def _export_dxf(in_path, out_path):
+ dxf_export = better_dxf_outlines.MyEffect()
+ dxf_export.affect(args = [in_path], output = False)
+
+ with open(out_path, 'w') as file:
+ file.write(dxf_export.dxf)
+
+
+def _get_inkscape_layer_count(svg_path):
+ document = xml.etree.ElementTree.parse(svg_path)
+ layers = document.findall(
+ '{http://www.w3.org/2000/svg}g[@{http://www.inkscape.org/namespaces/inkscape}groupmode="layer"]')
+
+ return len(layers)
+
+
+def _command(args):
+ process = subprocess.Popen(args)
+ process.wait()
+
+ assert not process.returncode
+
+
+def _inkscape(svg_path, verbs):
+ def iter_args():
+ yield os.environ['INKSCAPE']
+
+ for i in verbs:
+ yield '--verb'
+ yield i
+
+ yield svg_path
+
+ _command(list(iter_args()))
+
+
+def _unfuck_svg_document(temp_svg_path):
+ """
+ Unfucks an SVG document so is can be processed by the better_dxf_export plugin.
+ """
+
+ layers_count = _get_inkscape_layer_count(temp_svg_path)
+
+ def iter_inkscape_verbs():
+ yield 'LayerUnlockAll'
+ yield 'LayerShowAll'
+
+ # Go to the first layer
+ for _ in range(layers_count):
+ yield 'LayerPrev'
+
+ for _ in range(layers_count):
+ yield 'EditSelectAll'
+ yield 'ObjectToPath'
+ yield 'EditSelectAll'
+ yield 'SelectionUnGroup'
+ yield 'EditSelectAll'
+ yield 'StrokeToPath'
+ yield 'EditSelectAll'
+ yield 'SelectionUnion'
+ yield 'LayerNext'
+
+ yield 'FileSave'
+ yield 'FileClose'
+ yield 'FileQuit'
+
+ _inkscape(temp_svg_path, list(iter_inkscape_verbs()))
+
+
+def main(in_path, out_path):
+ with TemporaryDirectory() as temp_dir:
+ temp_svg_path = os.path.join(temp_dir, 'temp.svg')
+
+ shutil.copyfile(in_path, temp_svg_path)
+
+ _unfuck_svg_document(temp_svg_path)
+
+ _export_dxf(temp_svg_path, out_path)
+
+
+main(*sys.argv[1:])
diff --git a/dxf_export/better_dxf_outlines.py b/dxf_export/better_dxf_outlines.py
index 142dc8f..19ec6c8 100755
--- a/dxf_export/better_dxf_outlines.py
+++ b/dxf_export/better_dxf_outlines.py
@@ -121,7 +121,3 @@ class MyEffect(inkex.Effect):
self.dxf_path_to_point(layer,p)
self.dxf_add( dxf_templates.r14_footer )
-
-e = MyEffect()
-e.affect()
-
diff --git a/dxf_export/main.sh b/dxf_export/main.sh
deleted file mode 100755
index 4a7f3f3..0000000
--- a/dxf_export/main.sh
+++ /dev/null
@@ -1,38 +0,0 @@
-#! /usr/bin/env bash
-
-set -e -o pipefail
-
-in_file=$1
-out_file=$2
-
-# If environment variable DXF_EXPORT_DEBUG is set, the temporary file that is modified using Inkscape is saved in the same directory as the source file and not removed.
-if [ "$DXF_EXPORT_DEBUG" ]; then
- temp_file="$(dirname "$in_file")/$(basename "$in_file" '.svg')~temp.svg"
-else
- temp_dir=$(mktemp -d)
- temp_file="$temp_dir/temp.svg"
-fi
-
-script_path=$(dirname "$BASH_SOURCE")
-
-cp "$in_file" "$temp_file"
-
-# Run a few commands using Inkscape on the SVG file to get in into a shape that makes a successful conversion to DXF more likely.
-"$INKSCAPE" \
- --verb UnlockAllInAllLayers \
- --verb EditSelectAllInAllLayers \
- --verb ObjectToPath \
- --verb EditSelectAllInAllLayers \
- --verb SelectionUnGroup \
- --verb EditSelectAllInAllLayers \
- --verb StrokeToPath \
- --verb FileSave \
- --verb FileClose \
- "$temp_file"
-
-# Convert the SVG to a DXF file.
-python2 "$script_path/better_dxf_outlines.py" "$temp_file" > "$out_file"
-
-if ! [ "$DXF_EXPORT_DEBUG" ]; then
- rm -rf "$temp_dir"
-fi