pyvista_cad.write_step

On this page

pyvista_cad.write_step#

write_step(dataset, path, /, *, write_assembly=True, **_)[source]#

Write a pyvista.PolyData or pyvista.MultiBlock as STEP.

Each input triangle is converted to a planar face; the faces are sewn with BRepBuilderAPI_Sewing so a watertight mesh becomes a single closed shell, which is promoted to a TopoDS_Solid when closed. The output is a faceted STEP, not a B-rep reconstruction: analytic surface information is not recovered.

When dataset is a pyvista.MultiBlock and write_assembly is True (the default), a labelled OCAF assembly is emitted – one STEP product per block, the block key used as the component name, and nested MultiBlocks written as nested sub-assemblies (recursion is unbounded). A per-block field_data['cad.transform'] (raveled row-major 4x4) is applied as the component placement. The placement must be affine: only the top three rows are read into the OCCT gp_Trsf. The projective bottom row is assumed to be [0, 0, 0, 1] and is silently dropped, so a non-affine 4x4 does not round-trip. When write_assembly is False or dataset is a single pyvista.PolyData, the geometry is collapsed into one flat TopoDS_Compound.

Round-trip metadata: a read_step() of the written file with merge=False recovers per-part cad.color, cad.label and cad.transform. With merge=True the parts are flattened by pyvista.MultiBlock.combine(), which keeps only the root cad.source_format / cad.units / cad.reader / cad.backend; per-part cad.color, cad.label and cad.transform do not survive the merge.

Chordal-deviation bound: the faceted STEP is the input triangulation, vertex-for-vertex, except that polydata_to_topods() sews faces with a tolerance of max(diag * 1e-6, 1e-9) (diag = bounding-box diagonal), so vertices closer together than diag * 1e-6 may be welded. Apart from that welding, deviation from the original analytic surface stays bounded by the linear_deflection used when that surface was tessellated into dataset (see read_step()); this writer introduces no further geometric error.

Parameters:
datasetpyvista.PolyData or pyvista.MultiBlock

Source dataset.

pathstr or os.PathLike

Destination .step or .stp path.

write_assemblybool, default: True

Emit a labelled OCAF assembly for MultiBlock inputs. When False, MultiBlock inputs are flattened into one compound.

**_Any

Forward-compat keyword arguments are accepted and ignored.

Raises:
pyvista_cad.CadWriteError

If dataset is an unsupported type, if no geometry is found, or if one or more blocks cannot be faceted (every dropped block is named in the message; no block is silently dropped).

pyvista_cad.OptionalDependencyError

If OCP is not installed.

Parameters:
Return type:

None

Examples

>>> import tempfile, pathlib
>>> import pyvista as pv
>>> import pyvista_cad  # noqa: F401
>>> asm = pv.MultiBlock()
>>> asm['Base'] = pv.Box()
>>> asm['Pin'] = pv.Sphere()
>>> with tempfile.TemporaryDirectory() as d:
...     out = pathlib.Path(d) / 'asm.step'
...     pyvista_cad.write_step(asm, out)
...     out.exists()
True