def cross_connect(plant: Plant, racks: list[Rack], face: str = "front",
title: str = "") -> Drawing: # fmt: skip
"""The racks' ``face`` elevations side by side with every cable whose two ends are both
drawn on that face routed between them. Cables with an end not shown (a rear port, a rack
not in the row) are listed in the drawing's description instead."""
pitch = RACK_WIDTH_MM + GAP_MM
elevations = [elevation(r, faces=(face,), ports=True) for r in racks]
styles: dict[str, Style] = {}
for e in elevations:
styles.update(e.styles)
styles["rack-name"] = Style(color=INK)
heights = [e.view[3] + e.view[1] - 6 for e in elevations] # the rack's own height
top = max(heights)
# The rows, and where each port is.
items: list = []
spots: dict[Termination, PortSpot] = {}
for n, (rack, e) in enumerate(zip(racks, elevations, strict=True)):
dx = GAP_MM + n * pitch
items.append(Group(tuple(e.items), translate(dx, 0), title=rack.label,
meta=Meta.of(id=f"rack-{n}", kind="rack", name=rack.label))) # fmt: skip
items.append(Text(dx + RACK_WIDTH_MM / 2, -TITLE_MM + 4, rack.label, 14,
style="rack-name")) # fmt: skip
for it in e.items:
if isinstance(it, Use) and it.meta.kind == "port":
data = dict(it.meta.data)
t = Termination(data["device"], data["side"], data["port"])
spots[t] = PortSpot(it.transform.e + dx, it.transform.f, n)
# Racks are obstacles from their name down; the channels and the run over the top are free.
boxes = [(GAP_MM + n * pitch, -TITLE_MM + 10, RACK_WIDTH_MM, h + TITLE_MM - 10)
for n, h in enumerate(heights)] # fmt: skip
channels = [GAP_MM / 2 + n * pitch for n in range(len(racks) + 1)]
routes, drawn, missing = [], [], []
for i in range(len(plant.cables)):
for a, b in _pairs(plant, i):
sa, sb = spots.get(a), spots.get(b)
if sa is None or sb is None:
missing.append(f"cable {i + 1} ({a} to {b})")
continue
ca, cb = channels[_channel(sa, sb, pitch)], channels[_channel(sb, sa, pitch)]
# Drop from the port to the unit line below it, run along it to the channel, and
# route from there; the runs along unit lines are spread apart like the rest.
ya, yb = _below(sa.y), _below(sb.y)
middle = orthogonal((ca, ya), (cb, yb), boxes, margin=GAP_MM / 4,
lines=(channels, [-TITLE_MM + 2])) # fmt: skip
routes.append(simplify([(sa.x, sa.y), (sa.x, ya), *middle, (sb.x, yb),
(sb.x, sb.y)])) # fmt: skip
drawn.append((i, a, b))
for points, (i, a, b) in zip(nudge(routes, SPACING_MM), drawn, strict=True):
cable = plant.cables[i]
length = cable.cable_length
what = cable.label or (cable.assembly.name if cable.assembly else cable.cable_type)
bits = [f"Cable {i + 1}", what, f"{a} to {b}"]
if length:
bits.append(f"{length[0]:g} {length[1]}")
items.append(Polyline(tuple(points), stroke=_color(plant, i), width=1.2,
title=" ยท ".join(b for b in bits if b),
meta=Meta.of(id=f"cable-{i}", kind="cable", a=str(a), b=str(b),
status=cable.status))) # fmt: skip
width = GAP_MM + len(racks) * pitch
high = min([-TITLE_MM, *(y for r in routes for _, y in r)]) - 6
desc = f"{len(drawn)} cable runs between {len(racks)} racks, {face} face."
if missing:
more = f" and {len(missing) - 5} more" if len(missing) > 5 else ""
desc += f" Not drawn (an end is not on this face): {', '.join(missing[:5])}{more}."
d = Drawing(view=(0, high, width, top - high + 10), units="mm", stroke=0.35,
font=9.0, title=title or f"Cross-connects, {face}", desc=desc,
styles=styles) # fmt: skip
d.add(*items)
return d