1
- from vpython import *
2
-
3
- # Convert 3D .stl file ("stereo lithography") to VPython 7 object.
4
-
5
- # Limitations:
6
- # Code for binary files needs to be updated to VPython 7.
7
- # Does not deal with color.
8
- # Does not assign texpos values to vertex objects,
9
- # so cannot add a meaningful texture to the final compound object.
10
-
11
- # Original converter and STLbot by Derek Lura 10/06/09
12
- # Be sure to look at the bottom of the STLbot figure!
13
- # Part1.stl found at 3Dcontentcentral.com; also see 3dvia.com
14
-
15
- # Factory function and handling of binary files by Bruce Sherwood 1/26/10
16
- # Conversion to VPython 7 by Bruce Sherwood 2018 May 8
17
-
18
- # Give this factory function an .stl file and it returns a compound object,
19
- # to permit moving and rotating.
20
-
21
- # Specify the file as a file name.
22
-
23
- # See http://en.wikipedia.org/wiki/STL_(file_format)
24
- # Text .stl file starts with a header that begins with the word "solid".
25
- # Binary .stl file starts with a header that should NOT begin with the word "solid",
26
- # but this rule seems not always to be obeyed.
27
- # Currently the 16-bit unsigned integer found after each triangle in a binary
28
- # file is ignored; some versions of .stl files put color information in this value.
29
-
30
- def stl_to_triangles (fileinfo ): # specify file
31
- # Accept a file name or a file descriptor; make sure mode is 'rb' (read binary)
32
- fd = open (fileinfo , mode = 'rb' )
33
- text = fd .read ()
34
- tris = [] # list of triangles to compound
35
- if False : # prevent executing code for binary file
36
- pass
37
- # The following code for binary files must be updated:
38
- # if chr(0) in text: # if binary file
39
- # text = text[84:]
40
- # L = len(text)
41
- # N = 2*(L//25) # 25/2 floats per point: 4*3 float32's + 1 uint16
42
- # triPos = []
43
- # triNor = []
44
- # n = i = 0
45
- # while n < L:
46
- # triNor[i] = fromstring(text[n:n+12], float32)
47
- # triPos[i] = fromstring(text[n+12:n+24], float32)
48
- # triPos[i+1] = fromstring(text[n+24:n+36], float32)
49
- # triPos[i+2] = fromstring(text[n+36:n+48], float32)
50
- # colors = fromstring(text[n+48:n+50], uint16)
51
- # if colors != 0:
52
- # print '%x' % colors
53
- # if triNor[i].any():
54
- # triNor[i] = triNor[i+1] = triNor[i+2] = norm(vector(triNor[i]))
55
- # else:
56
- # triNor[i] = triNor[i+1] = triNor[i+2] = \
57
- # norm(cross(triPos[i+1]-triPos[i],triPos[i+2]-triPos[i]))
58
- # n += 50
59
- # i += 3
60
- else :
61
- fd .seek (0 )
62
- fList = fd .readlines ()
63
-
64
- # Decompose list into vertex positions and normals
65
- vs = []
66
- for line in fList :
67
- FileLine = line .split ( )
68
- if FileLine [0 ] == b'facet' :
69
- N = vec (float (FileLine [2 ]), float (FileLine [3 ]), float (FileLine [4 ]))
70
- elif FileLine [0 ] == b'vertex' :
71
- vs .append ( vertex (pos = vec (float (FileLine [1 ]), float (FileLine [2 ]), float (FileLine [3 ])), normal = N , color = color .white ) )
72
- if len (vs ) == 3 :
73
- tris .append (triangle (vs = vs ))
74
- vs = []
75
-
76
- return compound (tris )
77
-
78
- if __name__ == '__main__' :
79
- man = stl_to_triangles ('STLbot.stl' )
80
- man .pos = vec (- 200 ,0 ,0 )
81
- man .color = color .cyan
82
- part = stl_to_triangles ('Part1.stl' )
83
- part .size *= 200
84
- part .pos = vec (250 ,0 ,0 )
85
- part .color = color .orange
1
+ from vpython import *
2
+
3
+ # Convert 3D .stl file ("stereo lithography") to VPython 7 object.
4
+
5
+ # Limitations:
6
+ # Code for binary files needs to be updated to VPython 7.
7
+ # Does not deal with color.
8
+ # Does not assign texpos values to vertex objects,
9
+ # so cannot add a meaningful texture to the final compound object.
10
+
11
+ # Original converter and STLbot by Derek Lura 10/06/09
12
+ # Be sure to look at the bottom of the STLbot figure!
13
+ # Part1.stl found at 3Dcontentcentral.com; also see 3dvia.com
14
+
15
+ # Factory function and handling of binary files by Bruce Sherwood 1/26/10
16
+ # Conversion to VPython 7 by Bruce Sherwood 2018 May 8
17
+
18
+ # Give this factory function an .stl file and it returns a compound object,
19
+ # to permit moving and rotating.
20
+
21
+ # Specify the file as a file name.
22
+
23
+ # See http://en.wikipedia.org/wiki/STL_(file_format)
24
+ # Text .stl file starts with a header that begins with the word "solid".
25
+ # Binary .stl file starts with a header that should NOT begin with the word "solid",
26
+ # but this rule seems not always to be obeyed.
27
+ # Currently the 16-bit unsigned integer found after each triangle in a binary
28
+ # file is ignored; some versions of .stl files put color information in this value.
29
+
30
+ def stl_to_triangles (fileinfo ): # specify file
31
+ # Accept a file name or a file descriptor; make sure mode is 'rb' (read binary)
32
+ fd = open (fileinfo , mode = 'rb' )
33
+ text = fd .read ()
34
+ tris = [] # list of triangles to compound
35
+ if False : # prevent executing code for binary file
36
+ pass
37
+ # The following code for binary files must be updated:
38
+ # if chr(0) in text: # if binary file
39
+ # text = text[84:]
40
+ # L = len(text)
41
+ # N = 2*(L//25) # 25/2 floats per point: 4*3 float32's + 1 uint16
42
+ # triPos = []
43
+ # triNor = []
44
+ # n = i = 0
45
+ # while n < L:
46
+ # triNor[i] = fromstring(text[n:n+12], float32)
47
+ # triPos[i] = fromstring(text[n+12:n+24], float32)
48
+ # triPos[i+1] = fromstring(text[n+24:n+36], float32)
49
+ # triPos[i+2] = fromstring(text[n+36:n+48], float32)
50
+ # colors = fromstring(text[n+48:n+50], uint16)
51
+ # if colors != 0:
52
+ # print '%x' % colors
53
+ # if triNor[i].any():
54
+ # triNor[i] = triNor[i+1] = triNor[i+2] = norm(vector(triNor[i]))
55
+ # else:
56
+ # triNor[i] = triNor[i+1] = triNor[i+2] = \
57
+ # norm(cross(triPos[i+1]-triPos[i],triPos[i+2]-triPos[i]))
58
+ # n += 50
59
+ # i += 3
60
+ else :
61
+ fd .seek (0 )
62
+ fList = fd .readlines ()
63
+
64
+ # Decompose list into vertex positions and normals
65
+ ret = [] # will return a list of compounds if necessary
66
+ vs = []
67
+ vertices = 0
68
+ for line in fList :
69
+ FileLine = line .split ( )
70
+ if FileLine [0 ] == b'facet' :
71
+ N = vec (float (FileLine [2 ]), float (FileLine [3 ]), float (FileLine [4 ]))
72
+ elif FileLine [0 ] == b'vertex' :
73
+ vertices += 1
74
+ vs .append ( vertex (pos = vec (float (FileLine [1 ]), float (FileLine [2 ]), float (FileLine [3 ])), normal = N , color = color .white ) )
75
+ if len (vs ) == 3 :
76
+ tris .append (triangle (vs = vs ))
77
+ vs = []
78
+ if vertices > 64000 :
79
+ print (vertices )
80
+ ret .append (compound (tris ))
81
+ tris = []
82
+ vertices = 0
83
+ if len (tris ) > 0 : ret .append (compound (tris ))
84
+ if len (ret ) == 1 : return ret [0 ]
85
+ else : return ret
86
+
87
+ if __name__ == '__main__' :
88
+ man = stl_to_triangles ('z-as.stl' )
89
+ print ('Done' )
90
+ # man.pos = vec(-200,0,0)
91
+ # man.color = color.cyan
92
+ # part = stl_to_triangles('Part1.stl')
93
+ # part.size *= 200
94
+ # part.pos = vec(250,0,0)
95
+ # part.color = color.orange
0 commit comments