chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / fabmetheus_utilities / fabmetheus_tools / interpret_plugins / stl.py
1 """
2 This page is in the table of contents.
3 The stl.py script is an import translator plugin to get a carving from an stl file.
4
5 An import plugin is a script in the interpret_plugins folder which has the function getCarving.  It is meant to be run from the interpret tool.  To ensure that the plugin works on platforms which do not handle file capitalization properly, give the plugin a lower case name.
6
7 The getCarving function takes the file name of an stl file and returns the carving.
8
9 STL is an inferior triangle surface format, described at:
10 http://en.wikipedia.org/wiki/STL_(file_format)
11
12 A good triangle surface format is the GNU Triangulated Surface format which is described at:
13 http://gts.sourceforge.net/reference/gts-surfaces.html#GTS-SURFACE-WRITE
14
15 """
16
17
18 from __future__ import absolute_import
19
20 from fabmetheus_utilities.geometry.geometry_tools import face
21 from fabmetheus_utilities.geometry.solids import triangle_mesh
22 from fabmetheus_utilities.vector3 import Vector3
23 from fabmetheus_utilities import archive
24 from struct import unpack
25
26 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
27 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
28 __date__ = '$Date: 2008/21/04 $'
29 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
30
31
32 def addFacesGivenBinary( stlData, triangleMesh, vertexIndexTable ):
33         "Add faces given stl binary."
34         numberOfVertexes = ( len( stlData ) - 84 ) / 50
35         vertexes = []
36         for vertexIndex in xrange( numberOfVertexes ):
37                 byteIndex = 84 + vertexIndex * 50
38                 vertexes.append( getVertexGivenBinary( byteIndex + 12, stlData ) )
39                 vertexes.append( getVertexGivenBinary( byteIndex + 24, stlData ) )
40                 vertexes.append( getVertexGivenBinary( byteIndex + 36, stlData ) )
41         addFacesGivenVertexes( triangleMesh, vertexIndexTable, vertexes )
42
43 def addFacesGivenText( stlText, triangleMesh, vertexIndexTable ):
44         "Add faces given stl text."
45         lines = archive.getTextLines( stlText )
46         vertexes = []
47         for line in lines:
48                 if line.find('vertex') != - 1:
49                         vertexes.append( getVertexGivenLine(line) )
50         addFacesGivenVertexes( triangleMesh, vertexIndexTable, vertexes )
51
52 def addFacesGivenVertexes( triangleMesh, vertexIndexTable, vertexes ):
53         "Add faces given stl text."
54         for vertexIndex in xrange( 0, len(vertexes), 3 ):
55                 triangleMesh.faces.append( getFaceGivenLines( triangleMesh, vertexIndex, vertexIndexTable, vertexes ) )
56
57 def getCarving(fileName=''):
58         "Get the triangle mesh for the stl file."
59         if fileName == '':
60                 return None
61         stlData = archive.getFileText(fileName, True, 'rb')
62         if stlData == '':
63                 return None
64         triangleMesh = triangle_mesh.TriangleMesh()
65         vertexIndexTable = {}
66         numberOfVertexStrings = stlData.count('vertex')
67         requiredVertexStringsForText = max( 2, len( stlData ) / 8000 )
68         if numberOfVertexStrings > requiredVertexStringsForText:
69                 addFacesGivenText( stlData, triangleMesh, vertexIndexTable )
70         else:
71 #       A binary stl should never start with the word "solid".  Because this error is common the file is been parsed as binary regardless.
72                 addFacesGivenBinary( stlData, triangleMesh, vertexIndexTable )
73         return triangleMesh
74
75 def getFaceGivenLines( triangleMesh, vertexStartIndex, vertexIndexTable, vertexes ):
76         "Add face given line index and lines."
77         faceGivenLines = face.Face()
78         faceGivenLines.index = len( triangleMesh.faces )
79         for vertexIndex in xrange( vertexStartIndex, vertexStartIndex + 3 ):
80                 vertex = vertexes[vertexIndex]
81                 vertexUniqueIndex = len( vertexIndexTable )
82                 if str(vertex) in vertexIndexTable:
83                         vertexUniqueIndex = vertexIndexTable[ str(vertex) ]
84                 else:
85                         vertexIndexTable[ str(vertex) ] = vertexUniqueIndex
86                         triangleMesh.vertexes.append(vertex)
87                 faceGivenLines.vertexIndexes.append( vertexUniqueIndex )
88         return faceGivenLines
89
90 def getFloat(floatString):
91         "Get the float, replacing commas if necessary because an inferior program is using a comma instead of a point for the decimal point."
92         try:
93                 return float(floatString)
94         except:
95                 return float( floatString.replace(',', '.') )
96
97 def getFloatGivenBinary( byteIndex, stlData ):
98         "Get vertex given stl vertex line."
99         return unpack('f', stlData[ byteIndex : byteIndex + 4 ] )[0]
100
101 def getVertexGivenBinary( byteIndex, stlData ):
102         "Get vertex given stl vertex line."
103         return Vector3( getFloatGivenBinary( byteIndex, stlData ), getFloatGivenBinary( byteIndex + 4, stlData ), getFloatGivenBinary( byteIndex + 8, stlData ) )
104
105 def getVertexGivenLine(line):
106         "Get vertex given stl vertex line."
107         splitLine = line.split()
108         return Vector3( getFloat(splitLine[1]), getFloat( splitLine[2] ), getFloat( splitLine[3] ) )