-
Notifications
You must be signed in to change notification settings - Fork 176
Disambiguate disconnected same-name nets with schematic superscripts #3660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ded1a3
Assign schematic net superscripts for disconnected same-name networks
seveibar 6099231
Assign net superscripts through a dedicated schematic render phase
seveibar 56d1c58
Update schematic snapshots for disambiguated local net names
seveibar 00f8649
Rename phase to SchematicLabelNetsWithConflictingNames
seveibar 2111db1
Simplify net label assignment with circuit-json connectivity map
seveibar 396144a
Only disambiguate names displayed on multiple networks
seveibar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
lib/utils/schematic/assign-schematic-net-label-superscripts.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import type { CircuitJsonUtilObjects } from "@tscircuit/circuit-json-util" | ||
| import type { SourceNet } from "circuit-json" | ||
| import { | ||
| type ConnectivityMap, | ||
| getFullConnectivityMapFromCircuitJson, | ||
| } from "circuit-json-to-connectivity-map" | ||
|
|
||
| type NetName = SourceNet["name"] | ||
| type SourceNetworkId = keyof ConnectivityMap["netMap"] | ||
|
|
||
| /** Assign display suffixes during SchematicLabelNetsWithConflictingNames. */ | ||
| export function assignSchematicNetLabelSuperscripts( | ||
| db: CircuitJsonUtilObjects, | ||
| ) { | ||
| const labels = [ | ||
| ...db.schematic_net_label.list(), | ||
| ...db.schematic_text.list().filter((text) => text.source_trace_id), | ||
| ] | ||
| if (labels.length === 0) return | ||
|
|
||
| // Source connectivity is authoritative; PCB routing must not join source nets. | ||
| const connMap = getFullConnectivityMapFromCircuitJson( | ||
| db.toArray().filter((element) => element.type.startsWith("source_")), | ||
| ) | ||
| // The full-map API omits standalone nets and standalone internal connections. | ||
| connMap.addConnections([ | ||
| ...db.source_net.list().map((net) => [net.source_net_id]), | ||
| ...db.source_component_internal_connection | ||
| .list() | ||
| .map((connection) => connection.source_port_ids), | ||
| ]) | ||
|
|
||
| const labelsWithNetworks = labels.map((label) => { | ||
| const isNetLabel = label.type === "schematic_net_label" | ||
| const sourceTraceId = | ||
| label.source_trace_id ?? | ||
| (isNetLabel && label.schematic_trace_id | ||
| ? db.schematic_trace.get(label.schematic_trace_id)?.source_trace_id | ||
| : undefined) | ||
| const network = | ||
| (isNetLabel | ||
| ? connMap.getNetConnectedToId(label.source_net_id) | ||
| : undefined) ?? | ||
| (sourceTraceId ? connMap.getNetConnectedToId(sourceTraceId) : undefined) | ||
| return { label, network } | ||
| }) | ||
|
|
||
| const networksByName = new Map<NetName, SourceNetworkId[]>() | ||
| const addName = (name: NetName, network: SourceNetworkId | undefined) => { | ||
| if (!name.trim() || network === undefined) return | ||
| const networks = networksByName.get(name) ?? [] | ||
| if (!networks.includes(network)) networks.push(network) | ||
| networksByName.set(name, networks) | ||
| } | ||
| // Only names displayed on multiple networks need disambiguation. | ||
| // Unused source-net declarations must not create a visible conflict. | ||
| for (const { label, network } of labelsWithNetworks) { | ||
| addName(label.text, network) | ||
| } | ||
|
|
||
| // Member IDs keep numbering stable when Circuit JSON is reordered. | ||
| const firstSourceId = (network: SourceNetworkId) => | ||
| [...connMap.getIdsConnectedToNet(network)].sort()[0]! | ||
| for (const networks of networksByName.values()) { | ||
| networks.sort((a, b) => | ||
| firstSourceId(a).localeCompare(firstSourceId(b), "en", { numeric: true }), | ||
| ) | ||
| } | ||
|
|
||
| for (const { label, network } of labelsWithNetworks) { | ||
| const networks = networksByName.get(label.text) ?? [] | ||
| const display_superscript = | ||
| network !== undefined && networks.length > 1 | ||
| ? String(networks.indexOf(network) + 1) | ||
| : undefined | ||
| if (label.display_superscript === display_superscript) continue | ||
| if (label.type === "schematic_net_label") { | ||
| db.schematic_net_label.update(label.schematic_net_label_id, { | ||
| display_superscript, | ||
| }) | ||
| } else { | ||
| db.schematic_text.update(label.schematic_text_id, { display_superscript }) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/components/__snapshots__/normal-components-two-chips-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.
55 changes: 55 additions & 0 deletions
55
tests/components/base-components/schematic-label-nets-with-conflicting-names-phase.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { | ||
| Renderable, | ||
| orderedRenderPhases, | ||
| } from "lib/components/base-components/Renderable" | ||
|
|
||
| test("superscript phase waits for descendant label work and supports dirty updates", async () => { | ||
| let finishTraceRender!: () => void | ||
| const traceRenderFinished = new Promise<void>((resolve) => { | ||
| finishTraceRender = resolve | ||
| }) | ||
| class AsyncLabels extends Renderable { | ||
| doInitialSchematicTraceRender() { | ||
| this._queueAsyncEffect("labels", () => traceRenderFinished) | ||
| } | ||
| } | ||
| class SuperscriptRoot extends Renderable { | ||
| initialCalls = 0 | ||
| updateCalls = 0 | ||
| doInitialSchematicLabelNetsWithConflictingNames() { | ||
| this.initialCalls++ | ||
| } | ||
| updateSchematicLabelNetsWithConflictingNames() { | ||
| this.updateCalls++ | ||
| } | ||
| } | ||
| const root = new SuperscriptRoot({}) | ||
| const child = new AsyncLabels({}) | ||
| root.children.push(child) | ||
| child.parent = root | ||
| root.runRenderCycle() | ||
| expect(root.initialCalls).toBe(0) | ||
| expect( | ||
| root.renderPhaseStates.SchematicLabelNetsWithConflictingNames.initialized, | ||
| ).toBe(false) | ||
| finishTraceRender() | ||
| await traceRenderFinished | ||
| await Promise.resolve() | ||
| root.runRenderCycle() | ||
| expect(root.initialCalls).toBe(1) | ||
| root.runRenderCycle() | ||
| expect(root.updateCalls).toBe(0) | ||
| child._markDirty("SourceTraceRender") | ||
| expect( | ||
| root.renderPhaseStates.SchematicLabelNetsWithConflictingNames.dirty, | ||
| ).toBe(true) | ||
| root.runRenderCycle() | ||
| expect(root.initialCalls).toBe(1) | ||
| expect(root.updateCalls).toBe(1) | ||
| expect( | ||
| orderedRenderPhases.indexOf("SchematicLabelNetsWithConflictingNames"), | ||
| ).toBeGreaterThan( | ||
| orderedRenderPhases.indexOf("SchematicReplaceNetLabelsWithSymbols"), | ||
| ) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...hematic-net-superscripts/__snapshots__/connected-subcircuits-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.
26 changes: 26 additions & 0 deletions
26
...atic-net-superscripts/__snapshots__/disconnected-subcircuits-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.
12 changes: 12 additions & 0 deletions
12
...atures/schematic-net-superscripts/__snapshots__/named-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.
33 changes: 33 additions & 0 deletions
33
.../features/schematic-net-superscripts/__snapshots__/source-connectivity.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions
48
tests/features/schematic-net-superscripts/connected-subcircuits.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { SourceNet } from "circuit-json" | ||
| import { expect, test } from "bun:test" | ||
| import { getTestFixture } from "tests/fixtures/get-test-fixture" | ||
|
|
||
| test("connected same-name nets share one superscript and lose it when all networks join", async () => { | ||
| const { circuit } = getTestFixture({ platform: { pcbDisabled: true } }) | ||
| circuit.add( | ||
| <board width={30} height={20}> | ||
| {["A", "B", "C"].map((name, index) => ( | ||
| <subcircuit key={name} name={name} schX={index * 4}> | ||
| <resistor name={`R${index + 1}`} resistance="1k" /> | ||
| <netlabel | ||
| net="GND" | ||
| connectsTo={`R${index + 1}.pin1`} | ||
| schY={-2} | ||
| anchorSide="top" | ||
| /> | ||
| </subcircuit> | ||
| ))} | ||
| <trace from=".A .R1 .pin1" to=".B .R2 .pin1" /> | ||
| </board>, | ||
| ) | ||
| await circuit.renderUntilSettled() | ||
| const nets = circuit.db.source_net.list().filter((net) => net.name === "GND") | ||
| expect(nets).toHaveLength(3) | ||
| const suffixForNet = (netId: SourceNet["source_net_id"]) => | ||
| circuit.db.schematic_net_label | ||
| .list() | ||
| .find((label) => label.source_net_id === netId)?.display_superscript | ||
| expect(suffixForNet(nets[0]!.source_net_id)).toBe("1") | ||
| expect(suffixForNet(nets[1]!.source_net_id)).toBe("1") | ||
| expect(suffixForNet(nets[2]!.source_net_id)).toBe("2") | ||
| expect(circuit).toMatchSchematicSnapshot(import.meta.path) | ||
|
|
||
| // A direct database edit must dirty the phase that consumes the new data. | ||
| circuit.db.source_trace.insert({ | ||
| connected_source_net_ids: nets.map((net) => net.source_net_id), | ||
| connected_source_port_ids: [], | ||
| }) | ||
| circuit.firstChild!._markDirty("SchematicLabelNetsWithConflictingNames") | ||
| circuit.render() | ||
| expect( | ||
| circuit.db.schematic_net_label | ||
| .list() | ||
| .filter((label) => label.text === "GND") | ||
| .every((label) => label.display_superscript === undefined), | ||
| ).toBe(true) | ||
| }) | ||
73 changes: 73 additions & 0 deletions
73
tests/features/schematic-net-superscripts/disconnected-subcircuits.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { getTestFixture } from "tests/fixtures/get-test-fixture" | ||
|
|
||
| test("disconnected same-name nets get stable superscripts on regular and inline labels", async () => { | ||
| const { circuit } = getTestFixture({ platform: { pcbDisabled: true } }) | ||
| circuit.add( | ||
| <board width={30} height={20} schMaxTraceDistance={10}> | ||
| {["A", "B"].map((name, index) => ( | ||
| <subcircuit key={name} name={name} schY={index * -4}> | ||
| <resistor name={`R${index * 2 + 1}`} resistance="1k" schX={-3} /> | ||
| <resistor name={`R${index * 2 + 2}`} resistance="2k" schX={3} /> | ||
| <netlabel net="GND" connectsTo={`R${index * 2 + 1}.pin1`} schX={-5} /> | ||
| <netlabel net="SIGNAL" connectsTo={`R${index * 2 + 1}.pin2`} inline /> | ||
| <trace from={`R${index * 2 + 2}.pin1`} to="net.SIGNAL" /> | ||
| <netlabel | ||
| net={`UNIQUE_${name}`} | ||
| connectsTo={`R${index * 2 + 2}.pin2`} | ||
| schX={5} | ||
| /> | ||
| </subcircuit> | ||
| ))} | ||
| </board>, | ||
| ) | ||
| let suffixesAtPhaseEnd: Array<string | undefined> = [] | ||
| circuit.on("renderable:renderLifecycle:anyEvent", (event) => { | ||
| if ( | ||
| event.type === | ||
| "renderable:renderLifecycle:SchematicLabelNetsWithConflictingNames:end" && | ||
| event.renderId === circuit.firstChild!._renderId | ||
| ) { | ||
| suffixesAtPhaseEnd = circuit.db.schematic_net_label | ||
| .list() | ||
| .filter((label) => label.text === "GND") | ||
| .map((label) => label.display_superscript) | ||
| } | ||
| }) | ||
| await circuit.renderUntilSettled() | ||
| expect(new Set(suffixesAtPhaseEnd)).toEqual(new Set(["1", "2"])) | ||
|
|
||
| const groundLabels = circuit.db.schematic_net_label | ||
| .list() | ||
| .filter((label) => label.text === "GND") | ||
| const signalLabels = circuit.db.schematic_text | ||
| .list() | ||
| .filter((label) => label.text === "SIGNAL") | ||
| expect( | ||
| new Set(groundLabels.map((label) => label.display_superscript)), | ||
| ).toEqual(new Set(["1", "2"])) | ||
| expect( | ||
| new Set(signalLabels.map((label) => label.display_superscript)), | ||
| ).toEqual(new Set(["1", "2"])) | ||
| expect( | ||
| circuit.db.schematic_net_label | ||
| .list() | ||
| .filter((label) => label.text.startsWith("UNIQUE_")) | ||
| .every((label) => label.display_superscript === undefined), | ||
| ).toBe(true) | ||
| const before = [...groundLabels, ...signalLabels].map( | ||
| (label) => label.display_superscript, | ||
| ) | ||
| circuit.render() | ||
| expect( | ||
| [ | ||
| ...circuit.db.schematic_net_label | ||
| .list() | ||
| .filter((label) => label.text === "GND"), | ||
| ...circuit.db.schematic_text | ||
| .list() | ||
| .filter((label) => label.text === "SIGNAL"), | ||
| ].map((label) => label.display_superscript), | ||
| ).toEqual(before) | ||
| expect(circuit).toMatchSchematicSnapshot(import.meta.path) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The folder name
schematic-net-superscriptsis inconsistent with the naming used throughout the rest of the project. The utility file is namedassign-schematic-net-label-superscripts.ts, the render phase is calledSchematicNetLabelSuperscripts, and the base-components test file is namedschematic-net-label-superscripts-phase.test.ts. The folder should be renamed toschematic-net-label-superscriptsto match the established naming convention (kebab-case, consistent with other file/export names in the project).Spotted by Graphite (based on custom rule: Custom rule)

Is this helpful? React 👍 or 👎 to let us know.