-
Notifications
You must be signed in to change notification settings - Fork 0
/
STLTools.py
294 lines (234 loc) · 10.6 KB
/
STLTools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# copied from OpenCAMLib Google code project, Anders Wallin says it was originally from Julian Todd. License unknown, likely to be GPL
# python stl file tools
import re
import struct
import math
import sys
###########################################################################
def TriangleNormal(x0, y0, z0, x1, y1, z1, x2, y2, z2):
# calculate facet normal
v01 = (x1 - x0, y1 - y0, z1 - z0)
v02 = (x2 - x0, y2 - y0, z2 - z0)
n = ( v01[1] * v02[2] - v01[2] * v02[1],
v01[2] * v02[0] - v01[0] * v02[2],
v01[0] * v02[1] - v01[1] * v02[0])
ln = math.sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2])
if ln > 0.0:
return (n[0] / ln, n[1] / ln, n[2] / ln)
###########################################################################
class reader:
def __init__(self, fn = None):
self.fn = fn
if self.fn:
fl = open(self.fn, "r")
self.isascii = self.IsAscii(fl)
fl.close()
self.little_endian = (struct.unpack("<f", struct.pack("@f", 140919.00))[0] == 140919.00)
# print "computer is little endian: ", self.little_endian
# print "file is ascii: ", self.isascii
self.nfacets = 0
self.ndegenerate = 0
self.mr = MeasureBoundingBox()
def IsAscii(self, fdata):
l = fdata.readline(1024)
isascii = l[:5] == "solid" and (len(l) == 5 or (re.search("[^A-Za-z0-9\,\.\/\;\:\'\"\+\-\s\r\n]", l[6:]) == None)) # some files say 'solid' but are binary files, we try to find a non alphanumerical character in the rest to the first line
fdata.seek(0)
return isascii
def ReadVertex(self, l):
l = l.replace(",", ".") # Catia writes ASCII STL with , as decimal point
if re.search("facet", l) or re.search("outer", l) or re.search("endloop", l) or re.search("endfacet", l):
return
vertex = re.search("vertex\s*([\d\-+\.EeDd]+)\s*([\d\-+\.EeDd]+)\s*([\d\-+\.EeDd]+)", l)
if vertex:
return (float(vertex.group(1)), float(vertex.group(2)), float(vertex.group(3)))
def BinaryReadFacets(self, fl, fs = None):
# 80 bytes of header
hdr = fl.read(80)
# 4 bytes for number of facets
self.nfacets = struct.unpack("<i", fl.read(4))[0]
nfacets = 0
# we dont loop over self.nfacets because then we can recover any broken headers that show the wrong number of facets
while True:
try:
#50 byte records with normals and vertices per facet
fl.read(12) # override normal
xyz = struct.unpack("<9f", fl.read(36)) # little endian
if TriangleNormal(xyz[0], xyz[1], xyz[2], xyz[3], xyz[4], xyz[5], xyz[6], xyz[7], xyz[8]) == None:
self.ndegenerate = self.ndegenerate + 1
if (fs):
fs.PushTriangle(xyz[0], xyz[1], xyz[2], xyz[3], xyz[4], xyz[5], xyz[6], xyz[7], xyz[8])
self.mr.PushTriangle(xyz[0], xyz[1], xyz[2], xyz[3], xyz[4], xyz[5], xyz[6], xyz[7], xyz[8])
fl.read(2) # padding
nfacets += 1
except struct.error, e:
break
if self.nfacets != nfacets:
sys.stderr.write("Number of facets according to header: %d, number of facets read: %d\n" % (self.nfacets, nfacets))
self.nfacets = nfacets
def AsciiReadFacets(self, fl, fs = None):
lines = fl.readlines()
xyz = []
for l in lines:
tpl = self.ReadVertex(l)
if tpl:
xyz.append(tpl[0])
xyz.append(tpl[1])
xyz.append(tpl[2])
if len(xyz) == 9:
if not TriangleNormal(xyz[0], xyz[1], xyz[2], xyz[3], xyz[4], xyz[5], xyz[6], xyz[7], xyz[8]):
self.ndegenerate += 1
if (fs):
fs.PushTriangle(xyz[0], xyz[1], xyz[2], xyz[3], xyz[4], xyz[5], xyz[6], xyz[7], xyz[8])
self.nfacets += 1
self.mr.PushTriangle(xyz[0], xyz[1], xyz[2], xyz[3], xyz[4], xyz[5], xyz[6], xyz[7], xyz[8])
xyz = []
################################################################################
class writer:
def __init__(self, fn, write_ascii = False):
self.fn = fn
self.ascii = write_ascii
self.scale = 1.0
def write(self, fc):
self.fl = open(self.fn, "w")
self.WriteHeader(self.fl, fc.nfacets)
for t in xrange(fc.nfacets):
x0, y0, z0, x1, y1, z1, x2, y2, z2 = fc.GetFacet(t)
self.WriteFacet(x0, y0, z0, x1, y1, z1, x2, y2, z2)
self.WriteFooter(self.fl)
self.fl.flush()
self.fl.close()
def WriteHeader(self, fl, nfacets):
if self.ascii:
fl.write("solid\n")
else:
str = "Stereolithography "
assert(len(str) == 80)
fl.write(str)
fl.write(struct.pack("<i", nfacets))
def WriteFacet(self, x0, y0, z0, x1, y1, z1, x2, y2, z2, skip_degenerated = True):
if self.scale != 1.0:
x0 *= self.scale
y0 *= self.scale
z0 *= self.scale
x1 *= self.scale
y1 *= self.scale
z1 *= self.scale
x2 *= self.scale
y2 *= self.scale
z2 *= self.scale
# calculate facet normal
n = TriangleNormal(x0, y0, z0, x1, y1, z1, x2, y2, z2)
if n == None:
if skip_degenerated: return
n = (0.0, 0.0, 0.0)
if self.ascii:
self.fl.write("facet normal %f %f %f\n" % n)
self.fl.write("outer loop\n vertex %f %f %f\n vertex %f %f %f\n vertex %f %f %f\nendloop\nendfacet\n" %
(x0, y0, z0, x1, y1, z1, x2, y2, z2))
else:
self.fl.write(struct.pack("<12f2c", n[0], n[1], n[2], x0, y0, z0, x1, y1, z1, x2, y2, z2, " ", " "))
def WriteFooter(self, fl):
if self.ascii:
fl.write("endsolid\n")
def PushTriangle(self, x0, y0, z0, x1, y1, z1, x2, y2, z2):
self.WriteFacet(x0, y0, z0, x1, y1, z1, x2, y2, z2)
################################################################################
class MeasureBoundingBox:
def __init__(self):
self.xlo = None
self.xhi = None
self.ylo = None
self.yhi = None
self.zlo = None
self.zhi = None
def PushTriangle(self, x0, y0, z0, x1, y1, z1, x2, y2, z2):
for v in [(x0, y0, z0), (x1, y1, z1), (x2, y2, z2)]:
if self.xlo is None or v[0] < self.xlo:
self.xlo = v[0]
if self.ylo is None or v[1] < self.ylo:
self.ylo = v[1]
if self.zlo is None or v[2] < self.zlo:
self.zlo = v[2]
if self.xhi is None or v[0] > self.xhi:
self.xhi = v[0]
if self.yhi is None or v[1] > self.yhi:
self.yhi = v[1]
if self.zhi is None or v[2] > self.zhi:
self.zhi = v[2]
###########################################################################
class converter(reader):
def __init__(self, fin = None):
reader.__init__(self, fin)
# read to find number of facets, but substract degenerated facets
self.wr = None
def convert(self, fout, freadfrom = None):
if self.fn:
rmod = self.isascii and "r" or "rb"
fl = open(self.fn, rmod)
if self.isascii:
self.AsciiReadFacets(fl)
else:
self.BinaryReadFacets(fl)
fl.close()
elif freadfrom:
if self.isascii:
self.AsciiReadFacets(freadfrom)
else:
self.BinaryReadFacets(freadfrom)
freadfrom.seek(0) # rewind to start
self.wr = writer(fout, not self.isascii)
wmod = self.isascii and "wb" or "w"
self.fpout = open(fout, wmod)
self.wr.fl = self.fpout
self.wr.WriteHeader(self.fpout, self.nfacets - self.ndegenerate)
self.ndegenerate = 0
if self.fn:
rmod = self.isascii and "r" or "rb"
fl = open(self.fn, rmod)
if self.isascii:
self.AsciiReadFacets(fl, self)
else:
self.BinaryReadFacets(fl, self)
fl.close()
elif freadfrom:
if self.isascii:
self.AsciiReadFacets(freadfrom, self)
else:
self.BinaryReadFacets(freadfrom, self)
self.wr.WriteFooter(self.fpout)
self.fpout.close()
def PushTriangle(self, x0, y0, z0, x1, y1, z1, x2, y2, z2):
if self.wr != None:
self.wr.WriteFacet(x0, y0, z0, x1, y1, z1, x2, y2, z2)
###########################################################################
# use all the options flag. could have -tk which causes it to import using tk, -in, -out
# design all the settings so it works as pipe, or as files.
# stltools --in=file.stl --out=fil1.stl -b/-a if --out missing then piping
# stltools --tk does the following.
# stltools --in=file.stl --stats prints bounding box etc.
if __name__ == '__main__':
import tkFileDialog
fin = tkFileDialog.askopenfilename(
defaultextension = '*.stl',
filetypes = [('Stereolithography','*.stl'),('all files','*.*')],
title = "Open STL")
a = converter(fin)
t = a.isascii and "Save as STL (Binary format)" or "Save as STL (ASCII format)"
fout = tkFileDialog.asksaveasfilename(
defaultextension = '*.stl',
filetypes = [('Stereolithography','*.stl'),('all files','*.*')],
title = t)
a.convert(fout)
# Example STL ascii file:
#
# solid
# ...
# facet normal 0.00 0.00 1.00
# outer loop
# vertex 2.00 2.00 0.00
# vertex -1.00 1.00 0.00
# vertex 0.00 -1.00 0.00
# endloop
# endfacet
# ...
# endsolid