16. fileread — Read geometry from files in a number of formats.

This module defines basic routines to read geometrical data from a file and the specialized importers to read files in a number of well known standardized formats.

16.1. Functions defined in module fileread

fileread.readOFF(fn)[source]

Read a surface mesh from an OFF file.

Note

All the faces in the file should have 3 vertices.

Parameters:fn (path_like) – The name of a file in OFF format, commonly having a suffix ‘.off’. If the name ends with ‘.off.gz’ or ‘.off.bz2’, then the file will transparently be uncompressed during reading.
Returns:
  • coords (float array (ncoords, 3)) – The coordinates of all vertices.
  • elems (int array (nelems,3)) – The element connectivity table.

Examples

>>> from .filewrite import writeOFF
>>> f = Path('test_filewrite.off')
>>> M = Mesh(eltype='quad4').convert('tri3-u')
>>> writeOFF(f, M)
>>> coords, elems = readOFF(f)
>>> print(coords)
[[ 0.  0.  0.]
 [ 1.  0.  0.]
 [ 1.  1.  0.]
 [ 0.  1.  0.]]
>>> print(elems)
[[0 1 2]
 [2 3 0]]
fileread.readGTS(fn)[source]

Read a surface mesh from a GTS file.

Parameters:fn (path_like) – The name of a file in GTS format, commonly having a suffix ‘.gts’. If the name ends with ‘.gts.gz’ or ‘.gts.bz2’, then the file will transparently be uncompressed during reading.
Returns:
  • coords (float array (ncoords, 3)) – The coordinates of all vertices.
  • edges (int array (nedges,2)) – The edges to nodes connectivity table.
  • faces (int array (nfaces,2)) – The faces to edges connectivity table.

Examples

>>> from .filewrite import writeGTS
>>> f = Path('test_filewrite.gts')
>>> M = Mesh(eltype='quad4').convert('tri3-u')
>>> writeGTS(f, M.toSurface())
>>> coords, edges, faces = readGTS(f)
>>> print(coords)
[[ 0.  0.  0.]
 [ 1.  0.  0.]
 [ 1.  1.  0.]
 [ 0.  1.  0.]]
>>> print(edges)
[[0 1]
 [1 2]
 [2 0]
 [2 3]
 [3 0]]
>>> print(faces)
[[0 1 2]
 [3 4 2]]
fileread.readSTL(fn)[source]

Read a surface mesh from an STL file.

Parameters:fn (path_like) – The name of a file in STL format, commonly having a suffix ‘.stl’. If the name ends with ‘.gz’ or ‘.bz2’, then the file will transparently be uncompressed during reading.
Returns:
  • coords (float array (ncoords, 3)) – The coordinates of all vertices.
  • edges (int array (nedges,2)) – The edges to nodes connectivity table.
  • faces (int array (nfaces,2)) – The faces to edges connectivity table.

Notes

STL files come in ascii and binary formats. As there is no simple way to detect the format, a binary read is tried first, and if unsuccessful, the ascii read is tried next.

Examples

>>> from .filewrite import writeSTL
>>> f = Path('test_filewrite.stl')
>>> M = Mesh(eltype='quad4').convert('tri3-u')
>>> writeSTL(f, M.toFormex().coords, binary=True, color=[255,0,0,128])
>>> coords, normals, color = readSTL(f)
>>> print(coords)
[[[ 0.  0.  0.]
  [ 1.  0.  0.]
  [ 1.  1.  0.]]
<BLANKLINE>
 [[ 1.  1.  0.]
  [ 0.  1.  0.]
  [ 0.  0.  0.]]]
>>> print(normals)
[[ 0.  0.  1.]
 [ 0.  0.  1.]]
>>> print(color)
(1.0, 0.0, 0.0)
>>> writeSTL(f, M.toFormex().coords, binary=False)
>>> coords, normals, color = readSTL(f)
>>> print(coords)
[[[ 0.  0.  0.]
  [ 1.  0.  0.]
  [ 1.  1.  0.]]
<BLANKLINE>
 [[ 1.  1.  0.]
  [ 0.  1.  0.]
  [ 0.  0.  0.]]]
>>> print(normals)
[[ 0.  0.  1.]
 [ 0.  0.  1.]]
fileread.read_stl_bin(fil)[source]

Read a binary STL file.

Note

This is a low level routine for use in readSTL. It is not intended to be used directly.

Parameters:fil (open file) – File opened in binary read mode, holding binary STL data.
Returns:
  • coords (Coords (ntri,3,3)) – A Coords with ntri triangles. Each triangle consists of 3 vertices.
  • normals (Coords (ntri,3)) – A Coords with ntri vectors: the outer normals on the triangles.
  • color (None | float array (3,)) – If the STL file header contained a color in Materialise (TM) format, the RGB color components are returned as OpenGL color components. The alpha value is currently not returned.
fileread.read_stl_asc(fil)[source]

Read an ascii STL file.

Note

This is a low level routine for use in readSTL. It is not intended to be used directly.

Parameters:fil (open file) – File opened in binary read mode, holding ascii STL data.
Returns:
  • coords (Coords (ntri,3,3)) – A Coords with ntri triangles. Each triangle consists of 3 vertices.
  • normals (Coords (ntri,3)) – A Coords with ntri vectors: the outer normals on the triangles.
fileread.read_stl_cvt(fn, intermediate=None)[source]

Read a surface from .stl file.

This is done by first coverting the .stl to .gts or .off format. The name of the intermediate file may be specified. If not, it will be generated by changing the extension of fn to ‘.gts’ or ‘.off’ depending on the setting of the ‘surface/stlread’ config setting.

Return a coords,edges,faces or a coords,elems tuple, depending on the intermediate format.

fileread.stlConvert(stlname, outname=None, binary=False, options='-d')[source]

Convert an .stl file to .off or .gts or binary .stl format.

Parameters:
  • stlname (path_like) – Name of an existing .stl file (either ascii or binary).
  • outname (str or Path) – Name or suffix of the output file. The suffix defines the format and should be one of ‘.off’, ‘.gts’, ‘.stl’, ‘.stla’, or .stlb’. If a suffix only is given (other than ‘.stl’), then the outname will be constructed by changing the suffix of the input stlname. If not specified, the suffix of the configuration variable ‘surface/stlread’ is used.
  • binary (bool) – Only used if the extension of outname is ‘.stl’. Defines whether the output format is a binary or ascii STL format.
  • options (str) –
Returns:

  • outname (Path) – The name of the output file.
  • status (int) – The exit status (0 if successful) of the conversion program.
  • stdout (str) – The output of running the conversion program or a ‘file is already up to date’ message.

Notes

If the outname file exists and its mtime is more recent than the stlname, the outname file is considered up to date and the conversion program will not be run.

The conversion program will be choosen depending on the extension. This uses the external commands ‘admesh’ or ‘stl2gts’.

fileread.getParams(line)[source]

Strip the parameters from a comment line

fileread.readNodes(fil)[source]

Read a set of nodes from an open mesh file

fileread.readElems(fil, nplex)[source]

Read a set of elems of plexitude nplex from an open mesh file

fileread.readEsets(fil)[source]

Read the eset data of type generate

fileread.readMeshFile(fn)[source]

Read a nodes/elems model from file.

Returns a dict:

  • ‘coords’: a Coords with all nodes
  • ‘elems’: a list of Connectivities
  • ‘esets’: a list of element sets
fileread.extractMeshes(d)[source]

Extract the Meshes read from a .mesh file.

fileread.convertInp(fn)[source]

Convert an Abaqus .inp to a .mesh set of files

fileread.readInpFile(filename)[source]

Read the geometry from an Abaqus/Calculix .inp file

This is a replacement for the convertInp/readMeshFile combination. It uses the ccxinp plugin to provide a direct import of the Finite Element meshes from an Abaqus or Calculix input file. Currently still experimental and limited in functionality (aimed primarily at Calculix). But also many simple meshes from Abaqus can already be read.

Returns an dict.

fileread.read_gambit_neutral(fn, eltype='tri')[source]

Read a triangular/hexahedral surface mesh in Gambit neutral format.

eltype = ‘tri’ for triangular, ‘hex’ for hexahedral mesh. The .neu file nodes are numbered from 1! Returns a nodes,elems tuple.