23. filewrite — Write geometry to file in a whole number of formats. — pyFormex 1.0.5 documentation

# 23. `filewrite` — Write geometry to file in a whole number of formats.

This module defines bothe the basic routines to write geometrical data to a file and the specialized exporters to write files in a number of well known standardized formats.

The basic routines are very versatile as well as optimized (using the version in the pyFormex C-library) and allow to easily create new exporters for other formats.

## 23.1. Functions defined in module filewrite

`filewrite.` `writeData`(fil, data, sep='', fmt=None, end='')

  Write an array of numerical data to an open file.

  Parameters:

  - fil: an open file object

  - data: a numerical array of int or float type

  - sep: a string to be used as separator in case no fmt is specified. If an empty string, the data are written in binary mode. This is the default. For any other string, the data are written in ascii mode with the specified string inserted as separator between any two items, and a newline appended at the end. In both cases, the data are written using the numpy.tofile function.

  - fmt: a format string compatible with the array data type. If specified, the sep argument is ignored and the data are written according to the specified format. This uses the pyFormex functions misc.tofile_int32 or misc.tofile_float32, which have accelerated versions in the pyFormex C library. This also means that the data arrays will be forced to type float32 or int32 before writing.

    The format string should contain a valid format converter for a a single data item in both Python and C. They should also contain the necessary spacing or separator. Examples are ‘%5i ‘ for int data and ‘%f,’ or ‘%10.3e’ for float data. The array will be converted to a 2D array, keeping the length of the last axis. Then all elements will be written row by row using the specified format string, and the end string will be added after each row.

  - end: a string to be written at the end of the data block (if no fmt) or at the end of each row (with fmt). The default value is a newline character.

`filewrite.` `writeIData`(data, fil, fmt, ind=1)

  Write an indexed array of numerical data to an open file.

  ind = i: autoindex from i
    array: use these indices

`filewrite.` `writeOFF`(fn, mesh)

  Write a mesh of polygons to a file in OFF format.

  Parameters:

  - fn: file name, by preference ending on ‘.off’
  - mesh: a Mesh

`filewrite.` `writeOBJ`(fn, mesh, name=None)

  Write a mesh of polygons to a file in OBJ format.

  Parameters:

  - fn: file name, by preference ending on ‘.obj’
  - mesh: a Mesh
  - name: name of the Mesh to be written into the file. If None, and the Mesh has an .attrib.name, that name will be used.

`filewrite.` `writePLY`(fn, mesh, comment=None)

  Write a mesh to a file in PLY format.

  Parameters:

  - fn: file name, by preference ending on ‘.ply’
  - mesh: a Mesh
  - comment: an extra comment to add in the file header.

`filewrite.` `writeGTS`(fn, coords, edges, faces)

  Write a mesh of triangles to a file in GTS format.

  Parameters:

  - fn: file name, by preference ending on ‘.gts’
  - coords: float array with shape (ncoords,3), with the coordinates of ncoords vertices
  - edges: int array with shape (nedges,2), with the definition of nedges edges in function of the vertex indices
  - faces: int array with shape (nfaces,3), with the definition of nfaces triangles in function of the edge indices

`filewrite.` `writeSTL`(f, x, n=None, binary=False, color=None)

  Write a collection of triangles to an STL file.

  Parameters:

  - fn: file name, by preference ending with ‘.stl’ or ‘.stla’
  - x: (ntriangles,3,3) shaped array with the vertices of the triangles
  - n: (ntriangles,3) shaped array with the normals of the triangles. If not specified, they will be calculated.
  - binary: if True, the output file format will be a binary STL. The default is an ascii STL. Note that creation of a binary STL requires the extermal program ‘admesh’.
  - color: a single color can be passed to a binary STL and will be stpored in the header.

`filewrite.` `write_stl_bin`(fn, x, color=None)

  Write a binary stl.

  Parameters:

  - x: (ntri,4,3) float array describin ntri triangles. The first item of each triangle is the normal, the other three are the vertices.
  - color: (4,) int array with values in the range 0..255. These are the red, green, blue and alpha components of the color. This is a single color for all the triangles, and will be stored in the header of the STL file.

`filewrite.` `write_stl_asc`(fn, x)

  Write a collection of triangles to an ascii .stl file.

  Parameters:

  - fn: file name, by preference ending with ‘.stl’ or ‘.stla’
  - x: (ntriangles,3,3) shaped array with the vertices of the triangles