diff --git a/masque/builder/pather_mixin.py b/masque/builder/pather_mixin.py index f2b8229..bcf9d5e 100644 --- a/masque/builder/pather_mixin.py +++ b/masque/builder/pather_mixin.py @@ -1,4 +1,4 @@ -from typing import Self, TYPE_CHECKING +from typing import Self from collections.abc import Sequence, Iterator, Iterable import logging from contextlib import contextmanager @@ -537,6 +537,10 @@ class PortPather: self.pather[self.port].set_ptype(ptype) return self + def translate(self, *args, **kwargs) -> Self: + self.pather[self.port].translate(*args, **kwargs) + return self + def mirror(self, *args, **kwargs) -> Self: self.pather[self.port].mirror(*args, **kwargs) return self @@ -556,3 +560,13 @@ class PortPather: def rename_from(self, old_name: str) -> Self: self.pather.rename_ports({old_name: self.port}) return self + + def into_copy(self, new_name: str) -> Self: + self.pather.ports[new_name] = self.pather[self.port].copy() + self.port = new_name + return self + + def save_copy(self, new_name: str) -> Self: + self.pather.ports[new_name] = self.pather[self.port].copy() + return self + diff --git a/masque/builder/renderpather.py b/masque/builder/renderpather.py index 1665ed0..747a098 100644 --- a/masque/builder/renderpather.py +++ b/masque/builder/renderpather.py @@ -279,6 +279,7 @@ class RenderPather(PatherMixin): thru = thru, set_rotation = set_rotation, append = append, + ok_connections = ok_connections, ) return self diff --git a/masque/builder/tools.py b/masque/builder/tools.py index 1db156b..9c27a41 100644 --- a/masque/builder/tools.py +++ b/masque/builder/tools.py @@ -302,6 +302,9 @@ class SimpleTool(Tool, metaclass=ABCMeta): default_out_ptype: str """ Default value for out_ptype """ + mirror_bend: bool = True + """ Whether a clockwise bend should be mirrored (vs rotated) to get a ccw bend """ + @dataclass(frozen=True, slots=True) class LData: """ Data for planL """ @@ -378,11 +381,13 @@ class SimpleTool(Tool, metaclass=ABCMeta): else: straight_tree = straight_pat_or_tree top = straight_tree.top() - straight_tree.flatten(top) + straight_tree.flatten(top, dangling_ok=True) pat.plug(straight_tree[top], pmap, append=True) if data.ccw is not None: bend, bport_in, bport_out = self.bend - pat.plug(bend, {port_names[1]: bport_in}, mirrored=bool(data.ccw)) + mirrored = self.mirror_bend and bool(data.ccw) + inport = bport_in if (self.mirror_bend or not data.ccw) else bport_out + pat.plug(bend, {port_names[1]: inport}, mirrored=mirrored) return tree def path( @@ -463,7 +468,8 @@ class AutoTool(Tool, metaclass=ABCMeta): abstract: Abstract in_port_name: str out_port_name: str - clockwise: bool = True + clockwise: bool = True # Is in-to-out clockwise? + mirror: bool = True # Should we mirror to get the other rotation? @property def in_port(self) -> Port: @@ -653,13 +659,16 @@ class AutoTool(Tool, metaclass=ABCMeta): else: straight_tree = straight_pat_or_tree top = straight_tree.top() - straight_tree.flatten(top) + straight_tree.flatten(top, dangling_ok=True) pat.plug(straight_tree[top], pmap, append=True) if data.b_transition: pat.plug(data.b_transition.abstract, {port_names[1]: data.b_transition.our_port_name}) if data.ccw is not None: - assert data.bend is not None - pat.plug(data.bend.abstract, {port_names[1]: data.bend.in_port_name}, mirrored=bool(data.ccw) == data.bend.clockwise) + bend = data.bend + assert bend is not None + mirrored = bend.mirror and (bool(data.ccw) == bend.clockwise) + inport = bend.in_port_name if (bend.mirror or bool(data.ccw) != bend.clockwise) else bend.out_port_name + pat.plug(bend.abstract, {port_names[1]: inport}, mirrored=mirrored) if data.out_transition: pat.plug(data.out_transition.abstract, {port_names[1]: data.out_transition.our_port_name}) return tree @@ -801,7 +810,7 @@ class AutoTool(Tool, metaclass=ABCMeta): else: straight_tree = straight_pat_or_tree top = straight_tree.top() - straight_tree.flatten(top) + straight_tree.flatten(top, dangling_ok=True) pat.plug(straight_tree[top], pmap, append=True) if data.b_transition: pat.plug(data.b_transition.abstract, {port_names[1]: data.b_transition.our_port_name}) @@ -813,7 +822,7 @@ class AutoTool(Tool, metaclass=ABCMeta): else: sbend_tree = sbend_pat_or_tree top = sbend_tree.top() - sbend_tree.flatten(top) + sbend_tree.flatten(top, dangling_ok=True) pat.plug(sbend_tree[top], pmap, append=True, mirrored=data.jog_remaining < 0) if data.out_transition: pat.plug(data.out_transition.abstract, {port_names[1]: data.out_transition.our_port_name}) diff --git a/masque/library.py b/masque/library.py index b52da74..9e7c133 100644 --- a/masque/library.py +++ b/masque/library.py @@ -264,6 +264,7 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta): self, tops: str | Sequence[str], flatten_ports: bool = False, + dangling_ok: bool = False, ) -> dict[str, 'Pattern']: """ Returns copies of all `tops` patterns with all refs @@ -276,6 +277,9 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta): tops: The pattern(s) to flattern. flatten_ports: If `True`, keep ports from any referenced patterns; otherwise discard them. + dangling_ok: If `True`, no error will be thrown if any + ref points to a name which is not present in the library. + Default False. Returns: {name: flat_pattern} mapping for all flattened patterns. @@ -292,6 +296,8 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta): for target in pat.refs: if target is None: continue + if dangling_ok and target not in self: + continue if target not in flattened: flatten_single(target) @@ -307,7 +313,9 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta): p.ports.clear() pat.append(p) - pat.refs.clear() + for target in set(pat.refs.keys()) & set(self.keys()): + del pat.refs[target] + flattened[name] = pat for top in tops: