Update DXF reading

This commit is contained in:
Jan Petykiewicz 2024-07-28 20:04:48 -07:00
parent 8035daee7e
commit 5614eea3b4

View File

@ -16,6 +16,7 @@ import gzip
import numpy import numpy
import ezdxf import ezdxf
from ezdxf.enums import TextEntityAlignment from ezdxf.enums import TextEntityAlignment
from ezdxf.entities import LWPolyline, Polyline, Text, Insert
from .utils import is_gzipped, tmpfile from .utils import is_gzipped, tmpfile
from .. import Pattern, Ref, PatternError, Label from .. import Pattern, Ref, PatternError, Label
@ -39,7 +40,7 @@ def write(
top_name: str, top_name: str,
stream: TextIO, stream: TextIO,
*, *,
dxf_version='AC1024', dxf_version: str = 'AC1024',
) -> None: ) -> None:
""" """
Write a `Pattern` to a DXF file, by first calling `.polygonize()` to change the shapes Write a `Pattern` to a DXF file, by first calling `.polygonize()` to change the shapes
@ -205,16 +206,15 @@ def read(
return mlib, library_info return mlib, library_info
def _read_block(block) -> tuple[str, Pattern]: def _read_block(block: ezdxf.layouts.BlockLayout | ezdxf.layouts.Modelspace) -> tuple[str, Pattern]:
name = block.name name = block.name
pat = Pattern() pat = Pattern()
for element in block: for element in block:
eltype = element.dxftype() if isinstance(element, LWPolyline | Polyline):
if eltype in ('POLYLINE', 'LWPOLYLINE'): if isinstance(element, LWPolyline):
if eltype == 'LWPOLYLINE': points = numpy.array(element.get_points())
points = numpy.array(tuple(element.lwpoints)) elif isinstance(element, Polyline):
else: points = numpy.array(element.points())[:, :2]
points = numpy.array(tuple(element.points()))
attr = element.dxfattribs() attr = element.dxfattribs()
layer = attr.get('layer', DEFAULT_LAYER) layer = attr.get('layer', DEFAULT_LAYER)
@ -239,9 +239,9 @@ def _read_block(block) -> tuple[str, Pattern]:
pat.shapes[layer].append(shape) pat.shapes[layer].append(shape)
elif eltype in ('TEXT',): elif isinstance(element, Text):
args = dict( args = dict(
offset=numpy.array(element.get_pos()[1])[:2], offset=numpy.array(element.get_placement()[1])[:2],
layer=element.dxfattribs().get('layer', DEFAULT_LAYER), layer=element.dxfattribs().get('layer', DEFAULT_LAYER),
) )
string = element.dxfattribs().get('text', '') string = element.dxfattribs().get('text', '')
@ -252,7 +252,7 @@ def _read_block(block) -> tuple[str, Pattern]:
pat.label(string=string, **args) pat.label(string=string, **args)
# else: # else:
# pat.shapes[args['layer']].append(Text(string=string, height=height, font_path=????)) # pat.shapes[args['layer']].append(Text(string=string, height=height, font_path=????))
elif eltype in ('INSERT',): elif isinstance(element, Insert):
attr = element.dxfattribs() attr = element.dxfattribs()
xscale = attr.get('xscale', 1) xscale = attr.get('xscale', 1)
yscale = attr.get('yscale', 1) yscale = attr.get('yscale', 1)
@ -337,10 +337,10 @@ def _mrefs_to_drefs(
def _shapes_to_elements( def _shapes_to_elements(
block: ezdxf.layouts.BlockLayout | ezdxf.layouts.Modelspace, block: ezdxf.layouts.BlockLayout | ezdxf.layouts.Modelspace,
shapes: dict[layer_t, list[Shape]], shapes: dict[layer_t, list[Shape]],
polygonize_paths: bool = False,
) -> None: ) -> None:
# Add `LWPolyline`s for each shape. # Add `LWPolyline`s for each shape.
# Could set do paths with width setting, but need to consider endcaps. # Could set do paths with width setting, but need to consider endcaps.
# TODO: can DXF do paths?
for layer, sseq in shapes.items(): for layer, sseq in shapes.items():
attribs = dict(layer=_mlayer2dxf(layer)) attribs = dict(layer=_mlayer2dxf(layer))
for shape in sseq: for shape in sseq: