<filename>convert_binary.py
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from subprocess import Popen
import os, errno
import struct

def silent_remove(filename):
    try:
        os.remove(filename)
    except OSError as e: # this would be "except OSError, e:" before Python 2.6
        if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
            raise # re-raise exception if a different error occurred


def build_datafile(stl_path, file_type):
    last_slash = stl_path.rfind('/') + 1

    csv_path = stl_path[:last_slash]

    stl_name = stl_path[last_slash:]

    csv_name = stl_name.split('.')[0] + file_type
    
    csv = csv_path+csv_name

    silent_remove(csv)

    return csv

def convert_binary(filename):
    binary = open(filename, 'rb')
    ascii_stl = build_datafile(filename, '.stl')
    out = open(ascii_stl, 'w')

    data = binary.read()

    out.write("solid ")

    for x in range(0,80):
        if not ord(data[x]) == 0:
            out.write(struct.unpack('c', data[x])[0])
        else:
            pass
    out.write("\n")

    number = data[80] + data[81] + data[82] + data[83] 
    faces = struct.unpack('I',number)[0]

    for x in range(0,faces):
        out.write("facet normal ")

        xc = data[84+x*50] + data[85+x*50] + data[86+x*50] + data[87+x*50]
        yc = data[88+x*50] + data[89+x*50] + data[90+x*50] + data[91+x*50]
        zc = data[92+x*50] + data[93+x*50] + data[94+x*50] + data[95+x*50]

        out.write(str(struct.unpack('f',xc)[0]) + " ")
        out.write(str(struct.unpack('f',yc)[0]) + " ")
        out.write(str(struct.unpack('f',zc)[0]) + "\n")

        out.write("outer loop\n")

        for y in range(1,4):
            out.write("vertex ")

            xc = data[84+y*12+x*50] + data[85+y*12+x*50] + data[86+y*12+x*50] + data[87+y*12+x*50]
            yc = data[88+y*12+x*50] + data[89+y*12+x*50] + data[90+y*12+x*50] + data[91+y*12+x*50]
            zc = data[92+y*12+x*50] + data[93+y*12+x*50] + data[94+y*12+x*50] + data[95+y*12+x*50]

            out.write(str(struct.unpack('f',xc)[0]) + " ")
            out.write(str(struct.unpack('f',yc)[0]) + " ")
            out.write(str(struct.unpack('f',zc)[0]) + "\n")

        out.write("endloop\n")
        out.write("endfacet\n")

    out.close()
    print('end')
<filename>convert_binary.py
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from subprocess import Popen
import os, errno
import struct

def silent_remove(filename):
    try:
        os.remove(filename)
    except OSError as e: # this would be "except OSError, e:" before Python 2.6
        if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
            raise # re-raise exception if a different error occurred


def build_datafile(stl_path, file_type):
    last_slash = stl_path.rfind('/') + 1

    csv_path = stl_path[:last_slash]

    stl_name = stl_path[last_slash:]

    csv_name = stl_name.split('.')[0] + file_type
    
    csv = csv_path+csv_name

    silent_remove(csv)

    return csv

def convert_binary(filename):
    binary = open(filename, 'rb')
    ascii_stl = build_datafile(filename, '.stl')
    out = open(ascii_stl, 'w')

    data = binary.read()

    out.write("solid ")

    for x in range(0,80):
        if not ord(data[x]) == 0:
            out.write(struct.unpack('c', data[x])[0])
        else:
            pass
    out.write("\n")

    number = data[80] + data[81] + data[82] + data[83] 
    faces = struct.unpack('I',number)[0]

    for x in range(0,faces):
        out.write("facet normal ")

        xc = data[84+x*50] + data[85+x*50] + data[86+x*50] + data[87+x*50]
        yc = data[88+x*50] + data[89+x*50] + data[90+x*50] + data[91+x*50]
        zc = data[92+x*50] + data[93+x*50] + data[94+x*50] + data[95+x*50]

        out.write(str(struct.unpack('f',xc)[0]) + " ")
        out.write(str(struct.unpack('f',yc)[0]) + " ")
        out.write(str(struct.unpack('f',zc)[0]) + "\n")

        out.write("outer loop\n")

        for y in range(1,4):
            out.write("vertex ")

            xc = data[84+y*12+x*50] + data[85+y*12+x*50] + data[86+y*12+x*50] + data[87+y*12+x*50]
            yc = data[88+y*12+x*50] + data[89+y*12+x*50] + data[90+y*12+x*50] + data[91+y*12+x*50]
            zc = data[92+y*12+x*50] + data[93+y*12+x*50] + data[94+y*12+x*50] + data[95+y*12+x*50]

            out.write(str(struct.unpack('f',xc)[0]) + " ")
            out.write(str(struct.unpack('f',yc)[0]) + " ")
            out.write(str(struct.unpack('f',zc)[0]) + "\n")

        out.write("endloop\n")
        out.write("endfacet\n")

    out.close()
    print('end')
# this would be "except OSError, e:" before Python 2.6 # errno.ENOENT = no such file or directory # re-raise exception if a different error occurred