Compare commits

...

5 Commits

4 changed files with 42 additions and 10 deletions

View File

@ -1,4 +1,4 @@
from typing import Self, TYPE_CHECKING from typing import Self
from collections.abc import Sequence, Iterator, Iterable from collections.abc import Sequence, Iterator, Iterable
import logging import logging
from contextlib import contextmanager from contextlib import contextmanager
@ -537,6 +537,10 @@ class PortPather:
self.pather[self.port].set_ptype(ptype) self.pather[self.port].set_ptype(ptype)
return self return self
def translate(self, *args, **kwargs) -> Self:
self.pather[self.port].translate(*args, **kwargs)
return self
def mirror(self, *args, **kwargs) -> Self: def mirror(self, *args, **kwargs) -> Self:
self.pather[self.port].mirror(*args, **kwargs) self.pather[self.port].mirror(*args, **kwargs)
return self return self
@ -556,3 +560,13 @@ class PortPather:
def rename_from(self, old_name: str) -> Self: def rename_from(self, old_name: str) -> Self:
self.pather.rename_ports({old_name: self.port}) self.pather.rename_ports({old_name: self.port})
return self 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

View File

@ -279,6 +279,7 @@ class RenderPather(PatherMixin):
thru = thru, thru = thru,
set_rotation = set_rotation, set_rotation = set_rotation,
append = append, append = append,
ok_connections = ok_connections,
) )
return self return self

View File

@ -302,6 +302,9 @@ class SimpleTool(Tool, metaclass=ABCMeta):
default_out_ptype: str default_out_ptype: str
""" Default value for out_ptype """ """ 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) @dataclass(frozen=True, slots=True)
class LData: class LData:
""" Data for planL """ """ Data for planL """
@ -378,11 +381,13 @@ class SimpleTool(Tool, metaclass=ABCMeta):
else: else:
straight_tree = straight_pat_or_tree straight_tree = straight_pat_or_tree
top = straight_tree.top() top = straight_tree.top()
straight_tree.flatten(top) straight_tree.flatten(top, dangling_ok=True)
pat.plug(straight_tree[top], pmap, append=True) pat.plug(straight_tree[top], pmap, append=True)
if data.ccw is not None: if data.ccw is not None:
bend, bport_in, bport_out = self.bend 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 return tree
def path( def path(
@ -463,7 +468,8 @@ class AutoTool(Tool, metaclass=ABCMeta):
abstract: Abstract abstract: Abstract
in_port_name: str in_port_name: str
out_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 @property
def in_port(self) -> Port: def in_port(self) -> Port:
@ -653,13 +659,16 @@ class AutoTool(Tool, metaclass=ABCMeta):
else: else:
straight_tree = straight_pat_or_tree straight_tree = straight_pat_or_tree
top = straight_tree.top() top = straight_tree.top()
straight_tree.flatten(top) straight_tree.flatten(top, dangling_ok=True)
pat.plug(straight_tree[top], pmap, append=True) pat.plug(straight_tree[top], pmap, append=True)
if data.b_transition: if data.b_transition:
pat.plug(data.b_transition.abstract, {port_names[1]: data.b_transition.our_port_name}) pat.plug(data.b_transition.abstract, {port_names[1]: data.b_transition.our_port_name})
if data.ccw is not None: if data.ccw is not None:
assert data.bend is not None bend = data.bend
pat.plug(data.bend.abstract, {port_names[1]: data.bend.in_port_name}, mirrored=bool(data.ccw) == data.bend.clockwise) 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: if data.out_transition:
pat.plug(data.out_transition.abstract, {port_names[1]: data.out_transition.our_port_name}) pat.plug(data.out_transition.abstract, {port_names[1]: data.out_transition.our_port_name})
return tree return tree
@ -801,7 +810,7 @@ class AutoTool(Tool, metaclass=ABCMeta):
else: else:
straight_tree = straight_pat_or_tree straight_tree = straight_pat_or_tree
top = straight_tree.top() top = straight_tree.top()
straight_tree.flatten(top) straight_tree.flatten(top, dangling_ok=True)
pat.plug(straight_tree[top], pmap, append=True) pat.plug(straight_tree[top], pmap, append=True)
if data.b_transition: if data.b_transition:
pat.plug(data.b_transition.abstract, {port_names[1]: data.b_transition.our_port_name}) 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: else:
sbend_tree = sbend_pat_or_tree sbend_tree = sbend_pat_or_tree
top = sbend_tree.top() 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) pat.plug(sbend_tree[top], pmap, append=True, mirrored=data.jog_remaining < 0)
if data.out_transition: if data.out_transition:
pat.plug(data.out_transition.abstract, {port_names[1]: data.out_transition.our_port_name}) pat.plug(data.out_transition.abstract, {port_names[1]: data.out_transition.our_port_name})

View File

@ -264,6 +264,7 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
self, self,
tops: str | Sequence[str], tops: str | Sequence[str],
flatten_ports: bool = False, flatten_ports: bool = False,
dangling_ok: bool = False,
) -> dict[str, 'Pattern']: ) -> dict[str, 'Pattern']:
""" """
Returns copies of all `tops` patterns with all refs 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. tops: The pattern(s) to flattern.
flatten_ports: If `True`, keep ports from any referenced flatten_ports: If `True`, keep ports from any referenced
patterns; otherwise discard them. 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: Returns:
{name: flat_pattern} mapping for all flattened patterns. {name: flat_pattern} mapping for all flattened patterns.
@ -292,6 +296,8 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
for target in pat.refs: for target in pat.refs:
if target is None: if target is None:
continue continue
if dangling_ok and target not in self:
continue
if target not in flattened: if target not in flattened:
flatten_single(target) flatten_single(target)
@ -307,7 +313,9 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
p.ports.clear() p.ports.clear()
pat.append(p) pat.append(p)
pat.refs.clear() for target in set(pat.refs.keys()) & set(self.keys()):
del pat.refs[target]
flattened[name] = pat flattened[name] = pat
for top in tops: for top in tops: