summaryrefslogtreecommitdiff
path: root/support/dxf_export/__main__.py
diff options
context:
space:
mode:
authorMichael Schwarz <michi.schwarz@gmail.com>2014-12-10 22:13:34 +0100
committerMichael Schwarz <michi.schwarz@gmail.com>2014-12-10 22:13:34 +0100
commit8e5a1520cd5e901f514c382ad17e77d64727896a (patch)
tree184de9647c4776669f54fabf72e6f1f4bf2253a3 /support/dxf_export/__main__.py
parent785aec3aa6e2deda9acfefa8a9114ea3e7d39f44 (diff)
downloadpogojig-8e5a1520cd5e901f514c382ad17e77d64727896a.tar.gz
pogojig-8e5a1520cd5e901f514c382ad17e77d64727896a.tar.bz2
pogojig-8e5a1520cd5e901f514c382ad17e77d64727896a.zip
Moved DXF export scripts to separate directory.
Diffstat (limited to 'support/dxf_export/__main__.py')
-rw-r--r--support/dxf_export/__main__.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/support/dxf_export/__main__.py b/support/dxf_export/__main__.py
new file mode 100644
index 0000000..ea6c7ad
--- /dev/null
+++ b/support/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:])