External File Formats

STL

class petrify.formats.stl.STL(path, scale)[source]

Bases: object

A STL-formatted file at the given path with the specified file scale:

>>> import tempfile
>>> from petrify import u
>>> output = STL.read('tests/fixtures/svg.stl', 1 * u.mm / u.file)
>>> output = STL.read('tests/fixtures/svg.stl', 'mm')
>>> with tempfile.NamedTemporaryFile() as fp:
...     STL(fp.name, 'inches').write(output)
classmethod read(path, scale)[source]

Read a Node from a STL-formatted file:

>>> from petrify import u
>>> e = STL.read('tests/fixtures/svg.stl', 1 * u.inch / u.file)
>>> len(e.polygons)
40
>>> e.units
<Unit('inch')>
write(solid)[source]

Save a Node to a STL-formatted file.

>>> from petrify import u
>>> from petrify.solid import Box, Point, Vector
>>> from tempfile import NamedTemporaryFile
>>> b = Box(Point.origin, Vector(1, 1, 1))
>>> with NamedTemporaryFile() as fp:
...     STL(fp.name, 1 * u.mm / u.file).write(b.as_unit('inches'))

The input geometry must have a length unit tag:

>>> STL('/tmp/error.stl', 1 * u.mm / u.file).write(b)
Traceback (most recent call last):
...
AssertionError: object does not have unit tag: Box(Point(0, 0, 0), Vector(1, 1, 1))
exception petrify.formats.stl.StlEndOfFileException[source]

Bases: Exception

Exception class for reaching the end of the STL file while reading.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception petrify.formats.stl.StlMalformedLineException[source]

Bases: Exception

Exception class for malformed lines in the STL file being read.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

petrify.formats.stl.read_polys_from_stl_file(filename)[source]

Read array of triangle polygons from an STL file. filename - Name fo the STL file to read from.

petrify.formats.stl.save_polys_to_stl_file(polys, filename, binary=True)[source]

Save polygons in STL file. polys - list of Polygons. filename - Name fo the STL file to save to. binary - if true (default), file is written in binary STL format. Otherwise ASCII STL format.

SVG

Experimental SVG read support.

This currently only works with SVG paths without certain complications:

  • usage of fill-rule resulting in a single non-terminated (with the Z command) path resulting in unfilled areas.
  • motion commands that move the “pen” without terminating the path.
class petrify.formats.svg.SVG[source]

Bases: object

Basic reader for the SVG file format:

>>> from petrify import u
>>> paths = SVG.read('tests/fixtures/example.svg', u.inches / (90 * u.file))
>>> box = paths['rect'].m_as(u.inches)
class petrify.formats.svg.Path(transforms, data)[source]

Bases: object

An individual path object within a SVG file.

polygon(min_length=<Quantity(1.0, 'file')>)[source]

Returns a ComplexPolygon formed from this path:

>>> from petrify import u
>>> paths = SVG.read('tests/fixtures/example.svg', u.inches / (90 * u.file))
>>> box = paths['rect'].m_as(u.inches)
>>> len(box.polygon().segments())
8
>>> len(box.polygon().exterior)
1
>>> len(box.polygon().interior)
1

Converts all curves to lines.

min_length :
The length used to linearize all curves. For example, a curve with a length of 4.5 file units and a min_length of 2 * u.file would be broken into three line segments.
polygons(min_length=<Quantity(1.0, 'file')>)[source]

Returns all the simple Polygon objects formed from this path:

>>> from petrify import u
>>> paths = SVG.read('tests/fixtures/example.svg', u.inches / (90 * u.file))
>>> box = paths['rect'].m_as(u.inches)
>>> len(box.polygons())
2
>>> len(box.polygons()[0].segments())
4

Converts all curves to lines.

min_length :
The length used to linearize all curves. For example, a curve with a length of 4.5 file units and a min_length of 2 * u.file would be broken into three line segments.