Skip to content

Instantly share code, notes, and snippets.

@vovcacik
Last active December 27, 2017 13:06
Show Gist options
  • Save vovcacik/e70f68cbee9cbda0565577ca3675e7f3 to your computer and use it in GitHub Desktop.
Save vovcacik/e70f68cbee9cbda0565577ca3675e7f3 to your computer and use it in GitHub Desktop.
off2polyhedron.sh

This script automates the most laborious part of STL to OpenSCAD script conversion.

Details

OpenSCAD is able to import object from STL files, however this capability is not preserved in thingiverse.com Customizer. As a result of this many OpenSCAD scripts can't be used directly from thingiverse.com. However you are allowed to inline STL model, include it in your OpenSCAD script and then upload it to thingiverse.com.

The steps:

  • get your STL file and open it in OpenSCAD
  • select File > Export > Export to OFF...
  • run this script on the OFF file
    • e.g. ./off2polyhedron.sh model.off > model.scad
  • the SCAD file is OpenSCAD script and can be used as is or copy-pasted to another SCAD file
Why OFF?

I have looked at STL, OFF and AMF file formats. All of them are text based and could be used for this conversion but:

  • the STL format seems to support loops and also it may designate zero as 1.69406589450876e-18. I wanted to avoid parsing the content.
  • OFF is the simplest. It nicely separates points and faces.
  • AMF is very similar to OFF, but it is XML file.
#! /bin/bash
# Sanity check
[ ! -f "$1" ] && {
echo "Usage: $0 example.off"
exit 1
}
# Parse parameter
BASENAME=$(basename "$1")
EXTENSION="${BASENAME##*.}"
FILENAME="${BASENAME%.*}"
# Convert line endings
sed -i 's/\r$//' "$1"
# Output OpenSCAD code
echo "// converted from $BASENAME" with `basename "$0"`
echo
echo "${FILENAME}Points = ["
awk 'NR>3 && /^([0-9.-]+ ?){3}$/ {print "\t[" $1 ", " $2 ", " $3 "],"};' "$1"
echo "];"
echo
echo "${FILENAME}Faces = ["
awk 'NR>3 && /^3 ([0-9-]+ ?){3}$/ {print "\t[" $4 ", " $3 ", " $2 "],"};' "$1"
echo "];"
echo
echo "module ${FILENAME}() {"
echo -e "\tpolyhedron(points = ${FILENAME}Points, faces = ${FILENAME}Faces);"
echo "}"
echo
echo "${FILENAME}();"
echo
exit 0
@vovcacik
Copy link
Author

Examples tagged with off2polyhedronsh.

@vovcacik
Copy link
Author

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