Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-09 10:47:17

0001 # -*- encoding: utf-8 -*-
0002 # This script comes from https://github.com/IsseiMori/binary-stl-toASCII/blob/master/BinaryToASCII.py
0003 # Converted to Python 3
0004 
0005 import struct
0006 import sys
0007 
0008 file = sys.argv[1]
0009 file_with_no_extension = file[:-4]
0010 
0011 with open(sys.argv[1], 'rb') as infile, open(file_with_no_extension + '_ASCII.stl', 'w') as out:
0012     data = infile.read()
0013 
0014     out.write("solid ")
0015 
0016     for x in range(80):
0017         if data[x] != 0:
0018             out.write(data[x:x+1].decode('utf-8'))
0019     out.write("\n")
0020 
0021     number = data[80:84]
0022     faces = struct.unpack('I', number)[0]
0023 
0024     for x in range(faces):
0025         out.write("facet normal ")
0026         base = 84 + x * 50
0027 
0028         nx = struct.unpack('f', data[base:base+4])[0]
0029         ny = struct.unpack('f', data[base+4:base+8])[0]
0030         nz = struct.unpack('f', data[base+8:base+12])[0]
0031 
0032         out.write(f"{nx} {ny} {nz}\n")
0033         out.write("outer loop\n")
0034 
0035         for y in range(3):
0036             vertex_base = base + 12 + y * 12
0037             vx = struct.unpack('f', data[vertex_base:vertex_base+4])[0]
0038             vy = struct.unpack('f', data[vertex_base+4:vertex_base+8])[0]
0039             vz = struct.unpack('f', data[vertex_base+8:vertex_base+12])[0]
0040             out.write(f"vertex {vx} {vy} {vz}\n")
0041 
0042         out.write("endloop\n")
0043         out.write("endfacet\n")
0044 
0045     out.write("endsolid\n")
0046 
0047 print("end")
0048