diff --git a/lib/components/primitive-components/Group/Group_doInitialSchematicTraceRender/insertNetLabelsForPortsMissingTrace.ts b/lib/components/primitive-components/Group/Group_doInitialSchematicTraceRender/insertNetLabelsForPortsMissingTrace.ts index 9a5044d00..f2cd95501 100644 --- a/lib/components/primitive-components/Group/Group_doInitialSchematicTraceRender/insertNetLabelsForPortsMissingTrace.ts +++ b/lib/components/primitive-components/Group/Group_doInitialSchematicTraceRender/insertNetLabelsForPortsMissingTrace.ts @@ -1,10 +1,15 @@ import type { Group } from "lib/components" import { computeSchematicNetLabelCenter } from "lib/utils/schematic/computeSchematicNetLabelCenter" import { getEnteringEdgeFromDirection } from "lib/utils/schematic/getEnteringEdgeFromDirection" +import { getNetNameFromPorts } from "./getNetNameFromPorts" +import type { Port } from "../../Port" import type { SourceNet } from "circuit-json" const NEAR_EXISTING_NET_LABEL_DISTANCE = 0.5 const SAME_ANCHOR_POSITION_DISTANCE = 0.1 +// Slightly larger than SAME_ANCHOR_POSITION_DISTANCE to catch labels placed +// slightly offset from port centers (e.g. trace-anchored labels) +const PORT_LABEL_PROXIMITY_DISTANCE = 0.25 export const insertNetLabelsForPortsMissingTrace = ({ allSourceAndSchematicPortIdsInScope, @@ -34,6 +39,61 @@ export const insertNetLabelsForPortsMissingTrace = ({ if (!connKey) continue const sourceNet = connKeyToSourceNet.get(connKey) if (!sourceNet) { + // No explicit source_net (e.g. connections made via the `connections` + // prop without a named ). Derive label text from connected ports. + const portsOnSameNet = group + .selectAll("port") + .filter((p: Port) => p._getSubcircuitConnectivityKey() === connKey) + if (portsOnSameNet.length === 0) continue + + const { name: text } = getNetNameFromPorts(portsOnSameNet) + const side = + getEnteringEdgeFromDirection( + (schPort.facing_direction as any) || "right", + ) || "right" + const center = computeSchematicNetLabelCenter({ + anchor_position: schPort.center, + anchor_side: side, + text, + }) + + // If solver placed a same-net label near this port (possibly with wrong + // anchor due to trace routing), fix it in place rather than duplicating + const sameNetLabelNearPort = db.schematic_net_label.list().find((nl) => { + if (nl.source_net_id !== connKey) return false + const dx = (nl.anchor_position?.x ?? 0) - schPort.center.x + const dy = (nl.anchor_position?.y ?? 0) - schPort.center.y + return ( + dx * dx + dy * dy < + PORT_LABEL_PROXIMITY_DISTANCE * PORT_LABEL_PROXIMITY_DISTANCE + ) + }) + if (sameNetLabelNearPort) { + db.schematic_net_label.update( + sameNetLabelNearPort.schematic_net_label_id, + { anchor_position: schPort.center, center, anchor_side: side }, + ) + continue + } + + // Skip if any label already sits exactly at this port position + const hasLabelAtPort = db.schematic_net_label.list().some((nl) => { + const dx = (nl.anchor_position?.x ?? 0) - schPort.center.x + const dy = (nl.anchor_position?.y ?? 0) - schPort.center.y + return ( + dx * dx + dy * dy < + SAME_ANCHOR_POSITION_DISTANCE * SAME_ANCHOR_POSITION_DISTANCE + ) + }) + if (hasLabelAtPort) continue + + db.schematic_net_label.insert({ + text, + source_net_id: connKey, + anchor_position: schPort.center, + center, + anchor_side: side, + }) continue } diff --git a/tests/components/primitive-components/__snapshots__/schematic-section-2-schematic.snap.svg b/tests/components/primitive-components/__snapshots__/schematic-section-2-schematic.snap.svg index f748979f2..4de28db34 100644 --- a/tests/components/primitive-components/__snapshots__/schematic-section-2-schematic.snap.svg +++ b/tests/components/primitive-components/__snapshots__/schematic-section-2-schematic.snap.svg @@ -1,4 +1,4 @@ -