diff options
author | Michael Schwarz <michi.schwarz@gmail.com> | 2014-12-06 14:40:00 +0100 |
---|---|---|
committer | Michael Schwarz <michi.schwarz@gmail.com> | 2014-12-06 14:40:00 +0100 |
commit | 785aec3aa6e2deda9acfefa8a9114ea3e7d39f44 (patch) | |
tree | f5b18da6ee3caf3e91c2e4dbc3f509ef0e2717ff | |
parent | 9da7f66feec3ec222760e5d34ff83e43ab41af39 (diff) | |
download | pogojig-785aec3aa6e2deda9acfefa8a9114ea3e7d39f44.tar.gz pogojig-785aec3aa6e2deda9acfefa8a9114ea3e7d39f44.tar.bz2 pogojig-785aec3aa6e2deda9acfefa8a9114ea3e7d39f44.zip |
Replaced Script used for DXF export by more advanced version working with Inkscape 0.99.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | dxf_export/__main__.py | 95 | ||||
-rwxr-xr-x | dxf_export/better_dxf_outlines.py | 4 | ||||
-rwxr-xr-x | dxf_export/main.sh | 38 |
4 files changed, 96 insertions, 43 deletions
@@ -36,7 +36,7 @@ $(foreach i,$(COMPILED_SCAD_FILES),$(eval $(i): $(filter $(dir $(i))%,$(LIBRARY_ # Rule to convert an SVG file to a DXF file. %.dxf: %.svg - dxf_export/main.sh $< $@ + python2 dxf_export $< $@ # Rule to compile an OpenSCAD file to an STL file. %.stl: %.scad 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 |