Write a DXF drawing

Write a DXF drawing#

Round-trip a real 2D drawing through PyVista back out to DXF. The layer assignment of every cell is preserved on the cad.layer array.

This example loads a real multi-layer DXF from the ezdxf project (MIT), with entities on layers 0, BLUE, RED, writes it back to a new DXF, and renders the source and the round-tripped result side by side so any loss of detail is immediately visible.

from pathlib import Path
import tempfile

import pyvista as pv

import pyvista_cad
from pyvista_cad.examples import downloads

Load the real drawing.

PolyData (0x7f6fa81d57e0)
  N Cells:    68
  N Points:   710
  N Strips:   0
  X Bounds:   4.001e+01, 1.750e+02
  Y Bounds:   6.339e+01, 1.254e+02
  Z Bounds:   0.000e+00, 0.000e+00
  N Arrays:   7


Write back. layer_array='cad.layer' is the default; every cell returns to its originating layer.

PolyData (0x7f6fa81d5420)
  N Cells:    68
  N Points:   710
  N Strips:   0
  X Bounds:   4.001e+01, 1.750e+02
  Y Bounds:   6.339e+01, 1.254e+02
  Z Bounds:   0.000e+00, 0.000e+00
  N Arrays:   7


Render side by side so any geometric drift is immediately visible.

pl = pv.Plotter(shape=(1, 2))
pl.subplot(0, 0)
pl.add_mesh(src, color='steelblue', show_edges=True, line_width=2)
pl.view_xy()
pl.subplot(0, 1)
pl.add_mesh(rt, color='indianred', show_edges=True, line_width=2)
pl.view_xy()
pl.link_views()
pl.show()
write dxf

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