Return WrapLibrary from read() and readfile()

This commit is contained in:
Jan Petykiewicz 2023-01-26 19:28:10 -08:00 committed by jan
parent a35bf9770a
commit 42ee4db989
3 changed files with 21 additions and 18 deletions

View File

@ -18,7 +18,7 @@ import ezdxf
from .utils import is_gzipped, tmpfile from .utils import is_gzipped, tmpfile
from .. import Pattern, Ref, PatternError, Label from .. import Pattern, Ref, PatternError, Label
from ..library import Library, WrapROLibrary from ..library import Library, WrapROLibrary, WrapLibrary
from ..shapes import Shape, Polygon, Path from ..shapes import Shape, Polygon, Path
from ..repetition import Grid from ..repetition import Grid
from ..utils import rotation_matrix_2d, layer_t from ..utils import rotation_matrix_2d, layer_t
@ -148,7 +148,7 @@ def readfile(
filename: Union[str, pathlib.Path], filename: Union[str, pathlib.Path],
*args, *args,
**kwargs, **kwargs,
) -> Tuple[Dict[str, Pattern], Dict[str, Any]]: ) -> Tuple[WrapLibrary, Dict[str, Any]]:
""" """
Wrapper for `dxf.read()` that takes a filename or path instead of a stream. Wrapper for `dxf.read()` that takes a filename or path instead of a stream.
@ -172,7 +172,7 @@ def readfile(
def read( def read(
stream: TextIO, stream: TextIO,
) -> Tuple[Dict[str, Pattern], Dict[str, Any]]: ) -> Tuple[WrapLibrary, Dict[str, Any]]:
""" """
Read a dxf file and translate it into a dict of `Pattern` objects. DXF `Block`s are Read a dxf file and translate it into a dict of `Pattern` objects. DXF `Block`s are
translated into `Pattern` objects; `LWPolyline`s are translated into polygons, and `Insert`s translated into `Pattern` objects; `LWPolyline`s are translated into polygons, and `Insert`s
@ -189,16 +189,19 @@ def read(
lib = ezdxf.read(stream) lib = ezdxf.read(stream)
msp = lib.modelspace() msp = lib.modelspace()
npat = _read_block(msp) top_name, top_pat = _read_block(msp)
patterns_dict = dict( mlib = WrapLibrary({top_name: top_pat})
[npat] + [_read_block(bb) for bb in lib.blocks if bb.name != '*Model_Space'] for bb in lib.blocks:
) if bb.name == '*Model_Space':
continue
name, pat = _read_block(bb)
mlib[name] = pat
library_info = dict( library_info = dict(
layers=[ll.dxfattribs() for ll in lib.layers], layers=[ll.dxfattribs() for ll in lib.layers],
) )
return patterns_dict, library_info return mlib, library_info
def _read_block(block) -> Tuple[str, Pattern]: def _read_block(block) -> Tuple[str, Pattern]:

View File

@ -161,7 +161,7 @@ def readfile(
filename: Union[str, pathlib.Path], filename: Union[str, pathlib.Path],
*args, *args,
**kwargs, **kwargs,
) -> Tuple[Dict[str, Pattern], Dict[str, Any]]: ) -> Tuple[WrapLibrary, Dict[str, Any]]:
""" """
Wrapper for `read()` that takes a filename or path instead of a stream. Wrapper for `read()` that takes a filename or path instead of a stream.
@ -186,7 +186,7 @@ def readfile(
def read( def read(
stream: IO[bytes], stream: IO[bytes],
raw_mode: bool = True, raw_mode: bool = True,
) -> Tuple[Dict[str, Pattern], Dict[str, Any]]: ) -> Tuple[WrapLibrary, Dict[str, Any]]:
""" """
# TODO check GDSII file for cycles! # TODO check GDSII file for cycles!
Read a gdsii file and translate it into a dict of Pattern objects. GDSII structures are Read a gdsii file and translate it into a dict of Pattern objects. GDSII structures are
@ -209,15 +209,15 @@ def read(
""" """
library_info = _read_header(stream) library_info = _read_header(stream)
patterns_dict = {} mlib = WrapLibrary()
found_struct = records.BGNSTR.skip_past(stream) found_struct = records.BGNSTR.skip_past(stream)
while found_struct: while found_struct:
name = records.STRNAME.skip_and_read(stream) name = records.STRNAME.skip_and_read(stream)
pat = read_elements(stream, raw_mode=raw_mode) pat = read_elements(stream, raw_mode=raw_mode)
patterns_dict[name.decode('ASCII')] = pat mlib[name.decode('ASCII')] = pat
found_struct = records.BGNSTR.skip_past(stream) found_struct = records.BGNSTR.skip_past(stream)
return patterns_dict, library_info return mlib, library_info
def _read_header(stream: IO[bytes]) -> Dict[str, Any]: def _read_header(stream: IO[bytes]) -> Dict[str, Any]:

View File

@ -206,7 +206,7 @@ def readfile(
filename: Union[str, pathlib.Path], filename: Union[str, pathlib.Path],
*args, *args,
**kwargs, **kwargs,
) -> Tuple[Dict[str, Pattern], Dict[str, Any]]: ) -> Tuple[WrapLibrary, Dict[str, Any]]:
""" """
Wrapper for `oasis.read()` that takes a filename or path instead of a stream. Wrapper for `oasis.read()` that takes a filename or path instead of a stream.
@ -230,7 +230,7 @@ def readfile(
def read( def read(
stream: IO[bytes], stream: IO[bytes],
) -> Tuple[Dict[str, Pattern], Dict[str, Any]]: ) -> Tuple[WrapLibrary, Dict[str, Any]]:
""" """
Read a OASIS file and translate it into a dict of Pattern objects. OASIS cells are Read a OASIS file and translate it into a dict of Pattern objects. OASIS cells are
translated into Pattern objects; Polygons are translated into polygons, and Placements translated into Pattern objects; Polygons are translated into polygons, and Placements
@ -261,7 +261,7 @@ def read(
layer_map[str(layer_name.nstring)] = layer_name layer_map[str(layer_name.nstring)] = layer_name
library_info['layer_map'] = layer_map library_info['layer_map'] = layer_map
patterns_dict = {} mlib = WrapLibrary()
for cell in lib.cells: for cell in lib.cells:
if isinstance(cell.name, int): if isinstance(cell.name, int):
cell_name = lib.cellnames[cell.name].nstring.string cell_name = lib.cellnames[cell.name].nstring.string
@ -463,9 +463,9 @@ def read(
for placement in cell.placements: for placement in cell.placements:
pat.refs.append(_placement_to_ref(placement, lib)) pat.refs.append(_placement_to_ref(placement, lib))
patterns_dict[cell_name] = pat mlib[cell_name] = pat
return patterns_dict, library_info return mlib, library_info
def _mlayer2oas(mlayer: layer_t) -> Tuple[int, int]: def _mlayer2oas(mlayer: layer_t) -> Tuple[int, int]: