# Convert STL file to other formats like .msh (Gmsh) and .inp (Abaqus)

After reading some stuff on STL files, I realized that they do no contain information about a mesh (connectivity for instance) , but they represent the surface geometry of the part to be meshed.

However, some available tools like the open source Gmsh, could import the STL file and create the mesh from the defined vertices within the STL file. Can anyone guide to some algorithm that performs such conversion ?

• I believe Gmsh is not the only tool that can mesh STL models. For example, open source Netgen can also do that. Commented Aug 13, 2013 at 15:58

What you need to do is to analyze the set of input vertices (each triangle contributes three of these) and assign consecutive indices to them so that vertices that appear multiple times (i.e. are shared by different triangles) are assigned the same index every time. This leaves you with a list of (unique) vertices ${\bf x}_i$, and a list of index triples $(i, j, k)$ indexing into the vertex list so that the $({\bf x}_i, {\bf x}_j, {\bf x}_k)$'s represent the original triangles.
In practice, the coordinates of equivalent vertices will possibly not be exactly identical but differ by small amounts due to roundoff problems. In this case, discretize your input data with a grid resolution of $\delta$ (which should be a small number corresponding to the maximum expected roundoff error, e.g. $10^{-6}$). Use the grid coordinates of your vertices thus obtained (rather than their 3D positions) as keys in your hashtable to find the associated vertex index, but make sure to check whether any one of the neighboring grid cells was assigned an index earlier before generating a new index. The last step ensures that close vertices will receive the same index.