Merge commit '785aec3aa6' into no-examples
This commit is contained in:
commit
c02392b652
4 changed files with 96 additions and 43 deletions
2
Makefile
2
Makefile
|
|
@ -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
|
||||
|
|
|
|||
95
dxf_export/__main__.py
Normal file
95
dxf_export/__main__.py
Normal file
|
|
@ -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:])
|
||||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue