Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 <net>). Derive label text from connected ports.
const portsOnSameNet = group
.selectAll<Port>("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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(schPort.facing_direction as any) || "right",
schPort.facing_direction || "right",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you need to cast schPort, i think it should be done somewhere else

) || "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) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this fix be in the solver?

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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a connKey is not a source_net_id, this is a bug

anchor_position: schPort.center,
center,
anchor_side: side,
})
continue
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 72 additions & 23 deletions tests/repros/__snapshots__/repro119-no-traces-schematic.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions tests/repros/repro119-no-traces.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,20 @@ test("repro119", async () => {
}
})

const json = circuit.getCircuitJson()
const schNetLabels = json.filter(
(el: any) => el.type === "schematic_net_label",
)
const distinctNets = new Set(schNetLabels.map((l: any) => l.source_net_id))

// All 5 connectivity nets should have at least one label
expect(distinctNets.size).toBeGreaterThanOrEqual(5)

// U2's connections to U1 (chip-to-chip) were previously missing labels
const u2Labels = schNetLabels.filter(
(l: any) => l.text && l.text.includes("U2"),
)
expect(u2Labels.length).toBeGreaterThan(0)

expect(circuit).toMatchSchematicSnapshot(import.meta.path)
})
Loading