Write a 3MF package

Write a 3MF package#

Export a printable part to 3MF. The 3MF format carries per-object names, units, and (where supported) colors: everything a modern slicer needs to put the part on the build plate.

This example loads a real STEP part, the NIST AM Bench 2022 LPBF bridge specimen (public domain, https://doi.org/10.18434/mds2-2607), writes it to 3MF in millimetres, and verifies the round-trip preserves geometry and units.

from pathlib import Path
import tempfile

import numpy as np
import pyvista as pv

import pyvista_cad
from pyvista_cad.examples import downloads

Load the real part and tessellate it to a single surface.

mesh = pyvista_cad.read_step(downloads.step_part_path()).combine().extract_surface()
mesh.field_data['cad.label'] = np.array(['amb2022_bridge'])
mesh
/home/runner/work/pyvista-cad/pyvista-cad/examples/02_writers/write_3mf.py:29: PyVistaFutureWarning: The default value of `algorithm` for the filter
`UnstructuredGrid.extract_surface` will change in the future. It currently defaults to
`'dataset_surface'`, but will change to `None`. Explicitly set the `algorithm` keyword to
silence this warning.
  mesh = pyvista_cad.read_step(downloads.step_part_path()).combine().extract_surface()
PolyData (0x7f6fa8113d00)
  N Cells:    436
  N Points:   690
  N Strips:   0
  X Bounds:   0.000e+00, 7.500e+01
  Y Bounds:   -2.446e-13, 5.000e+00
  Z Bounds:   0.000e+00, 1.225e+01
  N Arrays:   8


Write to 3MF, preserving units explicitly.

path = Path(tempfile.mkdtemp()) / 'amb2022_bridge.3mf'
pyvista_cad.write_three_mf(mesh, path, units='mm')

Round-trip through read_three_mf to confirm the file is valid and the units survived.

MultiBlock (0x7f6fa81136a0)
  N Blocks:   1
  X Bounds:   0.000e+00, 7.500e+01
  Y Bounds:   0.000e+00, 5.000e+00
  Z Bounds:   0.000e+00, 1.225e+01


Render the round-tripped part.

surface = mb.combine().extract_surface()
surface['Z (mm)'] = surface.points[:, 2]

pl = pv.Plotter()
pl.add_mesh(surface, scalars='Z (mm)', cmap='viridis', show_edges=True, edge_color='black')
pl.show()
write 3mf
/home/runner/work/pyvista-cad/pyvista-cad/examples/02_writers/write_3mf.py:49: PyVistaFutureWarning: The default value of `algorithm` for the filter
`UnstructuredGrid.extract_surface` will change in the future. It currently defaults to
`'dataset_surface'`, but will change to `None`. Explicitly set the `algorithm` keyword to
silence this warning.
  surface = mb.combine().extract_surface()

Total running time of the script: (0 minutes 0.397 seconds)