[svg] add annotate_ports arg

This commit is contained in:
jan 2026-03-07 10:32:22 -08:00
commit ad4e9af59d

View file

@ -20,6 +20,7 @@ def writefile(
top: str, top: str,
filename: str, filename: str,
custom_attributes: bool = False, custom_attributes: bool = False,
annotate_ports: bool = False,
) -> None: ) -> None:
""" """
Write a Pattern to an SVG file, by first calling .polygonize() on it Write a Pattern to an SVG file, by first calling .polygonize() on it
@ -44,6 +45,8 @@ def writefile(
filename: Filename to write to. filename: Filename to write to.
custom_attributes: Whether to write non-standard `pattern_layer` attribute to the custom_attributes: Whether to write non-standard `pattern_layer` attribute to the
SVG elements. SVG elements.
annotate_ports: If True, draw an arrow for each port (similar to
`Pattern.visualize(..., ports=True)`).
""" """
pattern = library[top] pattern = library[top]
@ -79,6 +82,27 @@ def writefile(
svg_group.add(path) svg_group.add(path)
if annotate_ports:
# Draw arrows for the ports, pointing into the device (per port definition)
for port_name, port in pat.ports.items():
if port.rotation is not None:
p1 = port.offset
angle = port.rotation
size = 1.0 # arrow size
p2 = p1 + size * numpy.array([numpy.cos(angle), numpy.sin(angle)])
# head
head_angle = 0.5
h1 = p1 + 0.7 * size * numpy.array([numpy.cos(angle + head_angle), numpy.sin(angle + head_angle)])
h2 = p1 + 0.7 * size * numpy.array([numpy.cos(angle - head_angle), numpy.sin(angle - head_angle)])
line = svg.line(start=p1, end=p2, stroke='green', stroke_width=0.2)
head = svg.polyline(points=[h1, p1, h2], fill='none', stroke='green', stroke_width=0.2)
svg_group.add(line)
svg_group.add(head)
svg_group.add(svg.text(port_name, insert=p2, font_size=0.5, fill='green'))
for target, refs in pat.refs.items(): for target, refs in pat.refs.items():
if target is None: if target is None:
continue continue