Read a DXF drawing

Read a DXF drawing#

DXF is the dominant 2D interchange format: mechanical drawings, PCB outlines, architectural floor plans. This example loads a real DXF drawing from the ezdxf project (MIT) whose entities span three named layers (0, BLUE, RED), and shows how every drawable entity becomes a cell on a single pyvista.PolyData with its originating layer preserved on the cad.layer cell-data array.

Why this matters: knowing how to inspect DXF layers is the entry point for any 2D CAM, PCB, or architectural workflow that starts from a DXF.

import pyvista as pv

import pyvista_cad
from pyvista_cad.examples import downloads

Read the real multi-layer drawing.

PolyData (0x7f6faf5e5780)
  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


DXF layer metadata round-trips via the .cad accessor.

['0', 'BLUE', 'RED']

Split the drawing by layer and render each in its own color: the typical first move for triaging an unknown DXF.

by_layer = drawing.cad.split_by_layer()
palette = ['steelblue', 'indianred', 'goldenrod', 'mediumseagreen', 'orchid', 'slategray']

pl = pv.Plotter()
for i, (name, block) in enumerate(zip(by_layer.keys(), by_layer)):
    if block is None or block.n_cells == 0:
        continue
    pl.add_mesh(block, color=palette[i % len(palette)], line_width=2, label=str(name))
pl.add_legend()
pl.view_xy()
pl.show()
read dxf

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