21. 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.

21.1. Functions defined in module fileread

fileread.readPGF(filename, count=-1)[source]

Read a pyFormex Geometry File.

A pyFormex Geometry File can store multiple geometrical objects in a native format that can be efficiently read back into pyformex. The format is portable over different pyFormex versions and even to other software, as the format is stable and documented. This reader can restore subclasses of Geometry: Formex, Mesh, TriSurface, PolyLine, BezierSpline, NurbsCurve, with their stored attributes (name, color, field variables).

Parameters:
  • filename (path_like) – The name of an existing pyFormex Geometry File. If the filename ends on ‘.gz’ or ‘.bz2’, it is considered to be a gzipped, resp. bzipped file and it will transparently be uncompressed during reading.

  • count (int, optional) – If specified, no more than this number of objects will be read.

Returns:

dict – A dictionary with the geometric objects read from the file. If the file contains the object names, these are used as the keys. Else, default names are provided. If the file contains attributes (colors, fields) for an object, these will also be restored.

fileread.readOFF(filename)[source]

Read polygon meshes from an OFF file.

Parameters:
  • filename (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.

  • mplex (int, optional) – The maximum plexitude of the output polygons. If provided, polygons with a plexitude higher that mplex will be split into smaller ones. For example, with mplex=4, polygons with 5 or more vertices will be split into quads and triangles. Likewise, mplex=3 will split all polygons into triangles.

Returns:

Polygons – A Polygons with the polygons read from the file.

Examples

>>> from .filewrite import writeOFF
>>> f = Path('test_filewrite.off')
>>> M = Mesh(eltype='quad4').convert('tri3-u')
>>> writeOFF(f, M)
>>> poly = readOFF(f)
>>> print(poly)
Polygons: nnodes: 4, nelems: 2, nplex: min 3, max 3, eltype: polygon
  BBox: [0. 0. 0.], [1. 1. 0.]
  Size: [1. 1. 0.]
>>> print(poly.coords)
[[0. 0. 0.]
 [1. 0. 0.]
 [1. 1. 0.]
 [0. 1. 0.]]
>>> print(poly.elems)
Varray (nrows=2, width=3..3)
  [0 1 2]
  [2 3 0]
fileread.readOBJ(filename)[source]

Read a mesh from an OBJ file.

Reads the mesh data from a wavefront .obj file.

Parameters:

filename (path_like) – The name of a file in OBJ format, commonly having a suffix ‘.obj’. If the name ends with ‘.obj.gz’ or ‘.obj.bz2’, the file will transparently be uncompressed during reading.

Returns:

Polygons – The polygons read from the .obj file.

Notes

Currently only handles polygons. It does not handle relative indexing, subobjects, groups, lines, splines, beziers, materials. Normals and texture coordinates are read but not returned.

Examples

>>> from .filewrite import writeOBJ
>>> f = Path('test_filewrite.obj')
>>> M = Mesh(eltype='quad4')
>>> writeOBJ(f, M)
>>> poly = readOBJ(f)
>>> print(poly)
Polygons: nnodes: 4, nelems: 1, nplex: min 4, max 4, eltype: polygon
  BBox: [0. 0. 0.], [1. 1. 0.]
  Size: [1. 1. 0.]
>>> print(poly.coords)
[[0.  0.  0.]
 [1.  0.  0.]
 [1.  1.  0.]
 [0.  1.  0.]]
>>> print(poly.elems)
Varray (nrows=1, width=4..4)
  [0 1 2 3]
fileread.readPLY(filename, check=True)[source]

Read polygons from a PLY file.

Reads the polygon data from a stanford .ply file and possibly splits the high plexitude polygons into smaller ones.

Parameters:

filename (path_like) – The name of a file in PLY format, commonly having a suffix ‘.ply’. Ascii as well as binary formats are supported If the name ends with ‘.ply.gz’ or ‘.ply.bz2’, the file will transparently be uncompressed during reading.

Returns:

Polygons – The polygons read from the PLY file.

Notes

This uses plyfile from https://github.com/dranjan/python-plyfile to read the PLY file.

Examples

>>> from .filewrite import writePLY
>>> f = Path('test_filewrite.ply')
>>> M = Mesh(eltype='quad4')
>>> writePLY(f, M)
>>> poly = readPLY(f)
>>> print(poly)
Polygons: nnodes: 4, nelems: 1, nplex: min 4, max 4, eltype: polygon
  BBox: [0. 0. 0.], [1. 1. 0.]
  Size: [1. 1. 0.]
>>> print(poly.coords)
[[0.  0.  0.]
 [1.  0.  0.]
 [1.  1.  0.]
 [0.  1.  0.]]
>>> print(poly.elems)
Varray (nrows=1, width=4..4)
  [0 1 2 3]
>>> M = Mesh(eltype='quad4').convert('tri3-u')
>>> writePLY(f, M, binary=True)
>>> P = readPLY(f, check=False)
>>> print(P)
Polygons: nnodes: 4, nelems: 2, nplex: min 3, max 3, eltype: polygon
  BBox: [0. 0. 0.], [1. 1. 0.]
  Size: [1. 1. 0.]
>>> print(P.coords)
[[0. 0. 0.]
 [1. 0. 0.]
 [1. 1. 0.]
 [0. 1. 0.]]
>>> print(P.elems)
Varray (nrows=2, width=3..3)
  [0 1 2]
  [2 3 0]
fileread.readGTS(filename)[source]

Read a surface mesh from a GTS file.

Parameters:

filename (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]
 [2 0]
 [3 0]
 [1 2]
 [2 3]]
>>> print(faces)
[[0 3 1]
 [4 2 1]]
fileread.readSTL(filename)[source]

Read a surface mesh from an STL file.

Parameters:

filename (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.]]

 [[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.]]

 [[1.  1.  0.]
  [0.  1.  0.]
  [0.  0.  0.]]]
>>> print(normals)
[[0.  0.  1.]
 [0.  0.  1.]]
fileread.convertInp(filename)[source]

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

fileread.extractMeshes(d)[source]

Extract the Meshes read from a .mesh file.

fileread.getParams(line)[source]

Strip the parameters from a comment line

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.readINP(filename, tempdir=None)[source]

Read the geometry from an Abaqus/Calculix .inp file

This uses plugins.ccxinp.readInpFile() to import the Finite Element meshes from an Abaqus or Calculix INP format file.

Parameters:
  • filename (path_like) – The path of the input file, usually with a .inp suffix.

  • tempdir (path_like, optional) – The path of a directory where intermediary results can be stored. If provided but not existent, it will be created. If not provided, a temporary directory is used and intermediary data are not saved.

Returns:

dict – A dict where the keys are part names and the values are FeModel instances. If the .inp file does not contain parts, default part names are generated.

Warning

Element blocks with an unrecognized element type will raise an exception, unless plugins.ccxinp.skip_unknown_eltype is set to True.

fileread.readMeshFile(filename)[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.readNEU(filename, return_numbering=False)[source]

Read a Mesh from a Gambit neutral file.

Parameters:
  • filename (path_like) – The name of a file in Gambit neutral format, commonly having a suffix ‘.neu’. If the name ends with ‘.gz’ or ‘.bz2’, the file will transparently be uncompressed during reading.

  • return_numbering (bool) – If True, also returns the original node and element numbers as found in the file. The internal pyFormex numbers are always in order of appearance in the file.

Returns:

  • Mesh – The Mesh read from the Gambit neutral file, if reading was successful. None if the file could not be read. If the .neu file contains groups, the Mesh will have prop values equal to the material type numbers in the file.

  • nrs (int array) – The node numbers in the .neu file corrsponding to the nodes in Mesh.coords. Only returned if return_numbering is True.

  • enrs (in array) – The element numbers in the .neu file corresponding to the elements in Mesh.elems. Only returned if return_numbering is True.

Notes

Currently this function can only read Gambit files where all cells are of the same element type.

Examples

>>> from pyformex.plugins.neu_exp import writeNEU
>>> f = Path('test_filewrite.neu')
>>> M = Mesh(eltype='hex8').subdivide(3)
>>> writeNEU(f, M)
>>> M = readNEU(f)
Successfully read test_filewrite.neu
 ncoords=64; nelems=27;  nplex=8; eltype=Hex8
>>> print(M)
Mesh: nnodes: 64, nelems: 27, nplex: 8, level: 3, eltype: hex8
  BBox: [0.  0.  0.], [1.  1.  1.]
  Size: [1.  1.  1.]
  Area: 6.0  Volume: 1.0
fileread.readNodes(fil)[source]

Read a set of nodes from an open mesh file

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_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.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’.