chiark / gitweb /
Move SF into its own directory, to seperate SF and Cura. Rename newui to gui.
[cura.git] / Cura / cura_sf / fabmetheus_utilities / fabmetheus_tools / interpret_plugins / gts.py
1 """
2 This page is in the table of contents.
3 The gts.py script is an import translator plugin to get a carving from an gts 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 gts file and returns the carving.
8
9 The GNU Triangulated Surface (.gts) format is described at:
10 http://gts.sourceforge.net/reference/gts-surfaces.html#GTS-SURFACE-WRITE
11
12 Quoted from http://gts.sourceforge.net/reference/gts-surfaces.html#GTS-SURFACE-WRITE
13 "All the lines beginning with GTS_COMMENTS (#!) are ignored. The first line contains three unsigned integers separated by spaces. The first integer is the number of vertexes, nv, the second is the number of edges, ne and the third is the number of faces, nf.
14
15 Follows nv lines containing the x, y and z coordinates of the vertexes. Follows ne lines containing the two indices (starting from one) of the vertexes of each edge. Follows nf lines containing the three ordered indices (also starting from one) of the edges of each face.
16
17 The format described above is the least common denominator to all GTS files. Consistent with an object-oriented approach, the GTS file format is extensible. Each of the lines of the file can be extended with user-specific attributes accessible through the read() and write() virtual methods of each of the objects written (surface, vertexes, edges or faces). When read with different object classes, these extra attributes are just ignored."
18
19 """
20
21
22 from __future__ import absolute_import
23 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
24 import __init__
25
26 from fabmetheus_utilities.geometry.geometry_tools import face
27 from fabmetheus_utilities.geometry.solids import triangle_mesh
28 from fabmetheus_utilities.vector3 import Vector3
29 from fabmetheus_utilities import archive
30 from fabmetheus_utilities import gcodec
31
32 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
33 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
34 __date__ = '$Date: 2008/21/04 $'
35 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
36
37
38 def getCarving(fileName):
39         "Get the carving for the gts file."
40         return getFromGNUTriangulatedSurfaceText( archive.getFileText(fileName), triangle_mesh.TriangleMesh() )
41
42 def getFromGNUTriangulatedSurfaceText( gnuTriangulatedSurfaceText, triangleMesh ):
43         "Initialize from a GNU Triangulated Surface Text."
44         if gnuTriangulatedSurfaceText == '':
45                 return None
46         lines = archive.getTextLines( gnuTriangulatedSurfaceText )
47         linesWithoutComments = []
48         for line in lines:
49                 if len(line) > 0:
50                         firstCharacter = line[0]
51                         if firstCharacter != '#' and firstCharacter != '!':
52                                 linesWithoutComments.append(line)
53         splitLine = linesWithoutComments[0].split()
54         numberOfVertexes = int( splitLine[0] )
55         numberOfEdges = int(splitLine[1])
56         numberOfFaces = int( splitLine[2] )
57         faceTriples = []
58         for vertexIndex in xrange( numberOfVertexes ):
59                 line = linesWithoutComments[ vertexIndex + 1 ]
60                 splitLine = line.split()
61                 vertex = Vector3( float( splitLine[0] ), float(splitLine[1]), float( splitLine[2] ) )
62                 triangleMesh.vertexes.append(vertex)
63         edgeStart = numberOfVertexes + 1
64         for edgeIndex in xrange( numberOfEdges ):
65                 line = linesWithoutComments[ edgeIndex + edgeStart ]
66                 splitLine = line.split()
67                 vertexIndexes = []
68                 for word in splitLine[ : 2 ]:
69                         vertexIndexes.append( int(word) - 1 )
70                 edge = face.Edge().getFromVertexIndexes( edgeIndex, vertexIndexes )
71                 triangleMesh.edges.append( edge )
72         faceStart = edgeStart + numberOfEdges
73         for faceIndex in xrange( numberOfFaces ):
74                 line = linesWithoutComments[ faceIndex + faceStart ]
75                 splitLine = line.split()
76                 edgeIndexes = []
77                 for word in splitLine[ : 3 ]:
78                         edgeIndexes.append( int(word) - 1 )
79                 triangleMesh.faces.append( face.Face().getFromEdgeIndexes( edgeIndexes, triangleMesh.edges, faceIndex ) )
80         return triangleMesh