add Library.map_layers

dicty
jan 9 months ago
parent 140d344e77
commit 41add31c03

@ -20,10 +20,11 @@ import numpy
from numpy.typing import ArrayLike from numpy.typing import ArrayLike
from .error import LibraryError, PatternError from .error import LibraryError, PatternError
from .utils import rotation_matrix_2d from .utils import rotation_matrix_2d, layer_t
from .shapes import Shape, Polygon from .shapes import Shape, Polygon
from .label import Label from .label import Label
from .abstract import Abstract from .abstract import Abstract
from .pattern import map_layers
if TYPE_CHECKING: if TYPE_CHECKING:
from .pattern import Pattern from .pattern import Pattern
@ -547,6 +548,28 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
del pattern.refs[old_target] del pattern.refs[old_target]
return self return self
def map_layers(
self,
map_layer: Callable[[layer_t], layer_t],
) -> Self:
"""
Move all the elements in all patterns from one layer onto a different layer.
Can also handle multiple such mappings simultaneously.
Args:
map_layer: Callable which may be called with each layer present in `elements`,
and should return the new layer to which it will be mapped.
A simple example which maps `old_layer` to `new_layer` and leaves all others
as-is would look like `lambda layer: {old_layer: new_layer}.get(layer, layer)`
Returns:
self
"""
for pattern in self.values():
pattern.shapes = map_layers(pattern.shapes, map_layer)
pattern.labels = map_layers(pattern.labels, map_layer)
return self
def mkpat(self, name: str) -> tuple[str, 'Pattern']: def mkpat(self, name: str) -> tuple[str, 'Pattern']:
""" """
Convenience method to create an empty pattern, add it to the library, Convenience method to create an empty pattern, add it to the library,

Loading…
Cancel
Save