r12writer

The fast file/stream writer creates simple DXF R12 drawings with just an ENTITIES section. The HEADER, TABLES and BLOCKS sections are not present except FIXED-TABLES are written. Only LINE, CIRCLE, ARC, TEXT, POINT, SOLID, 3DFACE and POLYLINE entities are supported. FIXED-TABLES is a predefined TABLES section, which will be written, if the init argument fixed_tables of R12FastStreamWriter is True.

The R12FastStreamWriter writes the DXF entities as strings direct to the stream without creating an in-memory drawing and therefore the processing is very fast.

Because of the lack of a BLOCKS section, BLOCK/INSERT can not be used. Layers can be used, but this layers have a default setting color = 7 (black/white) and linetype = 'Continuous'. If writing the FIXED-TABLES, some predefined text styles and line types are available, else text style is always 'STANDARD' and line type is always 'ByLayer'.

If using FIXED-TABLES, following predefined line types are available:

  • CONTINUOUS

  • CENTER ____ _ ____ _ ____ _ ____ _ ____ _ ____

  • CENTERX2 ________  __  ________  __  ________

  • CENTER2 ____ _ ____ _ ____ _ ____ _ ____

  • DASHED __ __ __ __ __ __ __ __ __ __ __ __ __ _

  • DASHEDX2 ____  ____  ____  ____  ____  ____

  • DASHED2 _ _ _ _ _ _ _ _ _ _ _ _ _ _

  • PHANTOM ______  __  __  ______  __  __  ______

  • PHANTOMX2 ____________    ____    ____    ____________

  • PHANTOM2 ___ _ _ ___ _ _ ___ _ _ ___ _ _ ___

  • DASHDOT __ . __ . __ . __ . __ . __ . __ . __

  • DASHDOTX2 ____  .  ____  .  ____  .  ____

  • DASHDOT2 _ . _ . _ . _ . _ . _ . _ . _

  • DOT .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

  • DOTX2 .    .    .    .    .    .    .    .

  • DOT2 . . . . . . . . . . . . . . . . . . .

  • DIVIDE __ . . __ . . __ . . __ . . __ . . __

  • DIVIDEX2 ____  . .  ____  . .  ____  . .  ____

  • DIVIDE2 _ . _ . _ . _ . _ . _ . _ . _

If using FIXED-TABLES, following predefined text styles are available:

  • OpenSans

  • OpenSansCondensed-Light

Tutorial

A simple example with different DXF entities:

from random import random
from ezdxf.addons import r12writer

with r12writer("quick_and_dirty_dxf_r12.dxf") as dxf:
    dxf.add_line((0, 0), (17, 23))
    dxf.add_circle((0, 0), radius=2)
    dxf.add_arc((0, 0), radius=3, start=0, end=175)
    dxf.add_solid([(0, 0), (1, 0), (0, 1), (1, 1)])
    dxf.add_point((1.5, 1.5))

    # 2d polyline, new in v0.12
    dxf.add_polyline_2d([(5, 5), (7, 3), (7, 6)])

    # 2d polyline with bulge value, new in v0.12
    dxf.add_polyline_2d([(5, 5), (7, 3, 0.5), (7, 6)], format='xyb')

    # 3d polyline only, changed in v0.12
    dxf.add_polyline([(4, 3, 2), (8, 5, 0), (2, 4, 9)])

    dxf.add_text("test the text entity", align="MIDDLE_CENTER")

A simple example of writing really many entities in a short time:

from random import random
from ezdxf.addons import r12writer

MAX_X_COORD = 1000.0
MAX_Y_COORD = 1000.0
CIRCLE_COUNT = 1000000

with r12writer("many_circles.dxf") as dxf:
    for i in range(CIRCLE_COUNT):
        dxf.add_circle((MAX_X_COORD*random(), MAX_Y_COORD*random()), radius=2)

Show all available line types:

import ezdxf

LINETYPES = [
    'CONTINUOUS', 'CENTER', 'CENTERX2', 'CENTER2',
    'DASHED', 'DASHEDX2', 'DASHED2', 'PHANTOM', 'PHANTOMX2',
    'PHANTOM2', 'DASHDOT', 'DASHDOTX2', 'DASHDOT2', 'DOT',
    'DOTX2', 'DOT2', 'DIVIDE', 'DIVIDEX2', 'DIVIDE2',
]

with r12writer('r12_linetypes.dxf', fixed_tables=True) as dxf:
    for n, ltype in enumerate(LINETYPES):
        dxf.add_line((0, n), (10, n), linetype=ltype)
        dxf.add_text(ltype, (0, n+0.1), height=0.25, style='OpenSansCondensed-Light')

Reference

ezdxf.addons.r12writer.r12writer(stream: TextIO | BinaryIO | str, fixed_tables=False, fmt='asc') R12FastStreamWriter

Context manager for writing DXF entities to a stream/file. stream can be any file like object with a write() method or just a string for writing DXF entities to the file system. If fixed_tables is True, a standard TABLES section is written in front of the ENTITIES section and some predefined text styles and line types can be used.

Set argument fmt to “asc” to write ASCII DXF file (default) or “bin” to write Binary DXF files. ASCII DXF require a TextIO stream and Binary DXF require a BinaryIO stream.

class ezdxf.addons.r12writer.R12FastStreamWriter(stream: TextIO, fixed_tables=False)

Fast stream writer to create simple DXF R12 drawings.

Parameters:
  • stream – a file like object with a write() method.

  • fixed_tables – if fixed_tables is True, a standard TABLES section is written in front of the ENTITIES section and some predefined text styles and line types can be used.

close() None

Writes the DXF tail. Call is not necessary when using the context manager r12writer().

add_line(start: Sequence[float], end: Sequence[float], layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a LINE entity from start to end.

Parameters:
  • start – start vertex as (x, y[, z]) tuple

  • end – end vertex as as (x, y[, z]) tuple

  • layer – layer name as string, without a layer definition the assigned color = 7 (black/white) and line type is 'Continuous'.

  • color – color as AutoCAD Color Index (ACI) in the range from 0 to 256, 0 is ByBlock and 256 is ByLayer, default is ByLayer which is always color = 7 (black/white) without a layer definition.

  • linetype – line type as string, if FIXED-TABLES are written some predefined line types are available, else line type is always ByLayer, which is always 'Continuous' without a LAYERS table.

add_circle(center: Sequence[float], radius: float, layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a CIRCLE entity.

Parameters:
add_arc(center: Sequence[float], radius: float, start: float = 0, end: float = 360, layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add an ARC entity. The arc goes counter-clockwise from start angle to end angle.

Parameters:
  • center – arc center point as (x, y) tuple

  • radius – arc radius as float

  • start – arc start angle in degrees as float

  • end – arc end angle in degrees as float

  • layer – layer name as string see add_line()

  • color – color as AutoCAD Color Index (ACI) see add_line()

  • linetype – line type as string see add_line()

add_point(location: Sequence[float], layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a POINT entity.

Parameters:
add_3dface(vertices: Iterable[Sequence[float]], invisible: int = 0, layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a 3DFACE entity. 3DFACE is a spatial area with 3 or 4 vertices, all vertices have to be in the same plane.

Parameters:
  • vertices – iterable of 3 or 4 (x, y, z) vertices.

  • invisible

    bit coded flag to define the invisible edges,

    1. edge = 1

    2. edge = 2

    3. edge = 4

    4. edge = 8

    Add edge values to set multiple edges invisible, 1. edge + 3. edge = 1 + 4 = 5, all edges = 15

  • layer – layer name as string see add_line()

  • color – color as AutoCAD Color Index (ACI) see add_line()

  • linetype – line type as string see add_line()

add_solid(vertices: Iterable[Sequence[float]], layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a SOLID entity. SOLID is a solid filled area with 3 or 4 edges and SOLID is a 2D entity.

Parameters:
add_polyline_2d(points: Iterable[Sequence], format: str = 'xy', closed: bool = False, start_width: float = 0, end_width: float = 0, layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a 2D POLYLINE entity with start width, end width and bulge value support.

Format codes:

x

x-coordinate

y

y-coordinate

s

start width

e

end width

b

bulge value

v

(x, y) tuple (z-axis is ignored)

Parameters:
  • points – iterable of (x, y, [start_width, [end_width, [bulge]]]) tuple, value order according to the format string, unset values default to 0

  • format – format: format string, default is 'xy'

  • closedTrue creates a closed polyline

  • start_width – default start width, default is 0

  • end_width – default end width, default is 0

  • layer – layer name as string see add_line()

  • color – color as AutoCAD Color Index (ACI) see add_line()

  • linetype – line type as string see add_line()

add_polyline(vertices: Iterable[Sequence[float]], closed: bool = False, layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a 3D POLYLINE entity.

Parameters:
add_polyface(vertices: Iterable[Sequence[float]], faces: Iterable[Sequence[int]], layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a POLYFACE entity. The POLYFACE entity supports only faces of maximum 4 vertices, more indices will be ignored. A simple square would be:

v0 = (0, 0, 0)
v1 = (1, 0, 0)
v2 = (1, 1, 0)
v3 = (0, 1, 0)
dxf.add_polyface(vertices=[v0, v1, v2, v3], faces=[(0, 1, 2, 3)])

All 3D form functions of the ezdxf.render.forms module return MeshBuilder objects, which provide the required vertex and face lists.

See sphere example: https://github.com/mozman/ezdxf/blob/master/examples/r12writer.py

Parameters:
add_polymesh(vertices: Iterable[Sequence[float]], size: tuple[int, int], closed=(False, False), layer: str = '0', color: int | None = None, linetype: str | None = None) None

Add a POLYMESH entity. A POLYMESH is a mesh of m rows and n columns, each mesh vertex has its own x-, y- and z coordinates. The mesh can be closed in m- and/or n-direction. The vertices have to be in column order: (m0, n0), (m0, n1), (m0, n2), (m1, n0), (m1, n1), (m1, n2), …

See example: https://github.com/mozman/ezdxf/blob/master/examples/r12writer.py

Parameters:
  • vertices – iterable of (x, y, z) tuples, in column order

  • size – mesh dimension as (m, n)-tuple, requirement: len(vertices) == m*n

  • closed – (m_closed, n_closed) tuple, for closed mesh in m and/or n direction

  • layer – layer name as string see add_line()

  • color – color as AutoCAD Color Index (ACI) see add_line()

  • linetype – line type as string see add_line()

add_text(text: str, insert: Sequence[float] = (0, 0), height: float = 1.0, width: float = 1.0, align: str = 'LEFT', rotation: float = 0.0, oblique: float = 0.0, style: str = 'STANDARD', layer: str = '0', color: int | None = None) None

Add a one line TEXT entity.

Parameters:
  • text – the text as string

  • insert – insert location as (x, y) tuple

  • height – text height in drawing units

  • width – text width as factor

  • align – text alignment, see table below

  • rotation – text rotation in degrees as float

  • oblique – oblique in degrees as float, vertical = 0 (default)

  • style – text style name as string, if FIXED-TABLES are written some predefined text styles are available, else text style is always 'STANDARD'.

  • layer – layer name as string see add_line()

  • color – color as AutoCAD Color Index (ACI) see add_line()

Vert/Horiz

Left

Center

Right

Top

TOP_LEFT

TOP_CENTER

TOP_RIGHT

Middle

MIDDLE_LEFT

MIDDLE_CENTER

MIDDLE_RIGHT

Bottom

BOTTOM_LEFT

BOTTOM_CENTER

BOTTOM_RIGHT

Baseline

LEFT

CENTER

RIGHT

The special alignments ALIGNED and FIT are not available.