Skip to content

Instantly share code, notes, and snippets.

@foosel
Created December 7, 2021 13:59
Show Gist options
  • Save foosel/02c8e77ae87a32986f051b0de9ef62fb to your computer and use it in GitHub Desktop.
Save foosel/02c8e77ae87a32986f051b0de9ef62fb to your computer and use it in GitHub Desktop.
STL to SVG conversion script using OpenSCAD. Takes an STL and projects it to the defined plane (default: XY), then saves the result as SVG.
FileToLoad = "";
Plane=0; // 0: XY, 1: XZ, 2: YZ
echo("File:", FileToLoad);
projection() {
if (Plane == 0) {
echo("Plane: XY");
import(FileToLoad);
} else if (Plane == 1) {
echo("Plane: XZ");
rotate([90, 0, 0]) import(FileToLoad);
} else if (Plane == 2) {
echo("Plane: YZ");
rotate([0, 90, 0]) import(FileToLoad);
}
}
#!/bin/bash
### Usage ###
# stl_to_svg.sh [-p {xy|xz|yz}] input.stl [output.svg]
#
# -p = Projection plane, xy, xz or yz
# input.stl = Input file
# output.svg = Output file, if not provided will default to input.stl.svg
#
#set -xe
OPENSCAD="/path/to/openscad"
SCAD="/path/to/stl_to_svg.scad"
plane=0
while getopts "p:" opt; do
case $opt in
p)
case $OPTARG in
xy)
plane=0
;;
xz)
plane=1
;;
yz)
plane=2
;;
*)
echo "Invalid plane definition, accepted values are xy, xz and yz"
exit -1
;;
esac
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
STL="$1"
SVG="$2"
[ -n "$SVG" ] || SVG="$STL.svg"
"$OPENSCAD" -o "$SVG" -D FileToLoad="\"$STL\"" -D Plane=$plane "$SCAD"
@buchnema
Copy link

Nice and useful, thanks a lot. Since Prusaslicer now allows to modify models with SVGs this comes quite handy!

@TheophileWalter
Copy link

Works really well, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment