From 69e577f119fffddfe3999cb2a997d121147035bd Mon Sep 17 00:00:00 2001 From: Rishabh Gupta Date: Wed, 27 Nov 2024 01:49:04 +0530 Subject: [PATCH] fix: trace on hover shows correct connection --- .../convert-nets-to-source-nets-and-traces.ts | 4 +- .../convert-padstacks-to-smtpads.ts | 4 +- .../convert-polyline-path-to-pcb-traces.ts | 2 +- .../convert-wiring-path-to-pcb-traces.ts | 2 +- tests/assets/repro/trace-id-fix-circuit.json | 589 ++++++++++++++++++ tests/repros/repro3-fix-hover-trace.test.ts | 34 + 6 files changed, 630 insertions(+), 5 deletions(-) create mode 100644 tests/assets/repro/trace-id-fix-circuit.json create mode 100644 tests/repros/repro3-fix-hover-trace.test.ts diff --git a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-nets-to-source-nets-and-traces.ts b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-nets-to-source-nets-and-traces.ts index 11edde9..e392d7f 100644 --- a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-nets-to-source-nets-and-traces.ts +++ b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-nets-to-source-nets-and-traces.ts @@ -8,6 +8,7 @@ export const convertNetsToSourceNetsAndTraces = ({ const result: Array = [] const { nets } = dsnPcb.network + let source_trace_id = 0 for (const { name, pins } of nets) { if (name.startsWith("unconnected-")) continue const source_net: SourceNet = { @@ -28,9 +29,10 @@ export const convertNetsToSourceNetsAndTraces = ({ type: "source_trace", connected_source_net_ids: [source_net.source_net_id], connected_source_port_ids, - source_trace_id: `source_trace_${name}`, + source_trace_id: `source_trace_${source_trace_id}`, } result.push(source_net, source_trace) + source_trace_id++ } return result diff --git a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-padstacks-to-smtpads.ts b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-padstacks-to-smtpads.ts index 44f3c56..2d18c2a 100644 --- a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-padstacks-to-smtpads.ts +++ b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-padstacks-to-smtpads.ts @@ -84,9 +84,9 @@ export function convertPadstacksToSmtPads( const pcbPad: AnyCircuitElement = { type: "pcb_smtpad", - pcb_smtpad_id: `${pin.padstack_name}_${pin.pin_number}`, + pcb_smtpad_id: `pcb_smtpad_${pin.pin_number - 1}`, pcb_component_id: componentId, - pcb_port_id: `${pin.padstack_name}_${pin.pin_number}`, + pcb_port_id: `pcb_port_${componentId}-Pad${pin.pin_number}`, shape: "rect", x: circuitX, y: circuitY, diff --git a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-polyline-path-to-pcb-traces.ts b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-polyline-path-to-pcb-traces.ts index a0966fd..6d114b5 100644 --- a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-polyline-path-to-pcb-traces.ts +++ b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-polyline-path-to-pcb-traces.ts @@ -28,7 +28,7 @@ export const convertPolylinePathToPcbTraces = ({ traces.push({ type: "pcb_trace", - pcb_trace_id: `trace_${netName}_${Math.random().toString(36).substr(2, 9)}`, + pcb_trace_id: `pcb_trace_${netName}`, source_trace_id: netName, route_thickness_mode: "constant", should_round_corners: false, diff --git a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-wiring-path-to-pcb-traces.ts b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-wiring-path-to-pcb-traces.ts index c8544a3..cd837a3 100644 --- a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-wiring-path-to-pcb-traces.ts +++ b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-wiring-path-to-pcb-traces.ts @@ -41,7 +41,7 @@ export const convertWiringPathToPcbTraces = ({ const pcbTrace: PcbTrace = { type: "pcb_trace", - pcb_trace_id: `trace_${netName}_${Math.random().toString(36).substr(2, 9)}`, + pcb_trace_id: `pcb_trace_${netName}`, source_trace_id: netName, route_thickness_mode: "constant", should_round_corners: false, diff --git a/tests/assets/repro/trace-id-fix-circuit.json b/tests/assets/repro/trace-id-fix-circuit.json new file mode 100644 index 0000000..4055672 --- /dev/null +++ b/tests/assets/repro/trace-id-fix-circuit.json @@ -0,0 +1,589 @@ +[ + { + "type": "source_port", + "source_port_id": "source_port_0", + "name": "pin1", + "pin_number": 1, + "port_hints": [ + "anode", + "pos", + "left", + "pin1", + "1" + ], + "source_component_id": "source_component_0" + }, + { + "type": "source_port", + "source_port_id": "source_port_1", + "name": "pin2", + "pin_number": 2, + "port_hints": [ + "cathode", + "neg", + "right", + "pin2", + "2" + ], + "source_component_id": "source_component_0" + }, + { + "type": "source_component", + "source_component_id": "source_component_0", + "ftype": "simple_resistor", + "name": "R1", + "supplier_part_numbers": { + "jlcpcb": [ + "C11702", + "C106235", + "C226166" + ] + }, + "resistance": 1000 + }, + { + "type": "source_port", + "source_port_id": "source_port_2", + "name": "pin1", + "pin_number": 1, + "port_hints": [ + "anode", + "pos", + "left", + "pin1", + "1" + ], + "source_component_id": "source_component_1" + }, + { + "type": "source_port", + "source_port_id": "source_port_3", + "name": "pin2", + "pin_number": 2, + "port_hints": [ + "cathode", + "neg", + "right", + "pin2", + "2" + ], + "source_component_id": "source_component_1" + }, + { + "type": "source_component", + "source_component_id": "source_component_1", + "ftype": "simple_capacitor", + "name": "C1", + "supplier_part_numbers": { + "jlcpcb": [ + "C77018", + "C1523", + "C106205" + ] + }, + "capacitance": 1e-9 + }, + { + "type": "source_trace", + "source_trace_id": "source_trace_0", + "connected_source_port_ids": [ + "source_port_0", + "source_port_2" + ], + "connected_source_net_ids": [], + "subcircuit_connectivity_map_key": "unnamedsubcircuit211_connectivity_net0" + }, + { + "type": "schematic_component", + "schematic_component_id": "schematic_component_0", + "center": { + "x": 3, + "y": 0 + }, + "rotation": 0, + "size": { + "width": 1.1238982820000005, + "height": 0.24999600000000122 + }, + "source_component_id": "source_component_0", + "symbol_name": "boxresistor_horz", + "symbol_display_value": "1kΩ" + }, + { + "type": "schematic_component", + "schematic_component_id": "schematic_component_1", + "center": { + "x": -3, + "y": 0 + }, + "rotation": 0, + "size": { + "width": 1.1587354820000002, + "height": 0.5900173999999989 + }, + "source_component_id": "source_component_1", + "symbol_name": "capacitor_right", + "symbol_display_value": "1nF" + }, + { + "type": "schematic_port", + "schematic_port_id": "schematic_port_0", + "schematic_component_id": "schematic_component_0", + "center": { + "x": 2.4662093, + "y": 0.004194800000000665 + }, + "source_port_id": "source_port_0", + "facing_direction": "left", + "distance_from_component_edge": 0.4, + "pin_number": 1, + "display_pin_label": "left" + }, + { + "type": "schematic_port", + "schematic_port_id": "schematic_port_1", + "schematic_component_id": "schematic_component_0", + "center": { + "x": 3.5337907000000004, + "y": 0.004741299999999338 + }, + "source_port_id": "source_port_1", + "facing_direction": "right", + "distance_from_component_edge": 0.4, + "pin_number": 2, + "display_pin_label": "right" + }, + { + "type": "schematic_port", + "schematic_port_id": "schematic_port_2", + "schematic_component_id": "schematic_component_1", + "center": { + "x": -3.5512093, + "y": 0.033619599999999916 + }, + "source_port_id": "source_port_2", + "facing_direction": "left", + "distance_from_component_edge": 0.4, + "pin_number": 1, + "display_pin_label": "left" + }, + { + "type": "schematic_port", + "schematic_port_id": "schematic_port_3", + "schematic_component_id": "schematic_component_1", + "center": { + "x": -2.4487907, + "y": 0.03307310000000124 + }, + "source_port_id": "source_port_3", + "facing_direction": "right", + "distance_from_component_edge": 0.4, + "pin_number": 2, + "display_pin_label": "right" + }, + { + "type": "schematic_trace", + "schematic_trace_id": "schematic_trace_0", + "source_trace_id": "source_trace_0", + "edges": [ + { + "from": { + "route_type": "wire", + "x": 2.4662093, + "y": 0.004194800000000665, + "width": 0.1, + "layer": "top" + }, + "to": { + "route_type": "wire", + "x": -1.3987907000000002, + "y": 0.004194800000000665, + "width": 0.1, + "layer": "top" + } + }, + { + "from": { + "route_type": "wire", + "x": -1.3987907000000002, + "y": 0.004194800000000665, + "width": 0.1, + "layer": "top" + }, + "to": { + "route_type": "wire", + "x": -1.3987907000000002, + "y": -1.0169268999999987, + "width": 0.1, + "layer": "top" + } + }, + { + "from": { + "route_type": "wire", + "x": -1.3987907000000002, + "y": -1.0169268999999987, + "width": 0.1, + "layer": "top" + }, + "to": { + "route_type": "wire", + "x": -3.5512093, + "y": -1.0169268999999987, + "width": 0.1, + "layer": "top" + } + }, + { + "from": { + "route_type": "wire", + "x": -3.5512093, + "y": -1.0169268999999987, + "width": 0.1, + "layer": "top" + }, + "to": { + "route_type": "wire", + "x": -3.5512093, + "y": -0.39500869999999944, + "width": 0.1, + "layer": "top" + } + }, + { + "from": { + "route_type": "wire", + "x": -3.5512093, + "y": -0.39500869999999944, + "width": 0.1, + "layer": "top" + }, + "to": { + "route_type": "wire", + "x": -4.579367741, + "y": -0.39500869999999944, + "width": 0.1, + "layer": "top" + } + }, + { + "from": { + "route_type": "wire", + "x": -4.579367741, + "y": -0.39500869999999944, + "width": 0.1, + "layer": "top" + }, + "to": { + "route_type": "wire", + "x": -4.579367741, + "y": 0.033619599999999916, + "width": 0.1, + "layer": "top" + } + }, + { + "from": { + "route_type": "wire", + "x": -4.579367741, + "y": 0.033619599999999916, + "width": 0.1, + "layer": "top" + }, + "to": { + "route_type": "wire", + "x": -3.7012093, + "y": 0.033619599999999916, + "width": 0.1, + "layer": "top" + } + }, + { + "from": { + "route_type": "wire", + "x": -3.7012093, + "y": 0.033619599999999916, + "width": 0.1, + "layer": "top" + }, + "to": { + "x": -3.5512093, + "y": 0.033619599999999916 + } + } + ], + "junctions": [] + }, + { + "type": "pcb_component", + "pcb_component_id": "pcb_component_0", + "center": { + "x": 3, + "y": 0 + }, + "width": 1.5999999999999996, + "height": 0.6000000000000001, + "layer": "top", + "rotation": 0, + "source_component_id": "source_component_0" + }, + { + "type": "pcb_component", + "pcb_component_id": "pcb_component_1", + "center": { + "x": -3, + "y": 0 + }, + "width": 1.5999999999999996, + "height": 0.6000000000000001, + "layer": "top", + "rotation": 0, + "source_component_id": "source_component_1" + }, + { + "type": "pcb_board", + "pcb_board_id": "pcb_board_0", + "center": { + "x": 0, + "y": 0 + }, + "thickness": 1.4, + "num_layers": 4, + "width": 10, + "height": 10 + }, + { + "type": "pcb_smtpad", + "pcb_smtpad_id": "pcb_smtpad_0", + "pcb_component_id": "pcb_component_0", + "pcb_port_id": "pcb_port_0", + "layer": "top", + "shape": "rect", + "width": 0.6000000000000001, + "height": 0.6000000000000001, + "port_hints": [ + "1", + "left" + ], + "x": 2.5, + "y": 0 + }, + { + "type": "pcb_solder_paste", + "pcb_solder_paste_id": "pcb_solder_paste_0", + "layer": "top", + "shape": "rect", + "width": 0.42000000000000004, + "height": 0.42000000000000004, + "x": 2.5, + "y": 0, + "pcb_component_id": "pcb_component_0", + "pcb_smtpad_id": "pcb_smtpad_0" + }, + { + "type": "pcb_smtpad", + "pcb_smtpad_id": "pcb_smtpad_1", + "pcb_component_id": "pcb_component_0", + "pcb_port_id": "pcb_port_1", + "layer": "top", + "shape": "rect", + "width": 0.6000000000000001, + "height": 0.6000000000000001, + "port_hints": [ + "2", + "right" + ], + "x": 3.5, + "y": 0 + }, + { + "type": "pcb_solder_paste", + "pcb_solder_paste_id": "pcb_solder_paste_1", + "layer": "top", + "shape": "rect", + "width": 0.42000000000000004, + "height": 0.42000000000000004, + "x": 3.5, + "y": 0, + "pcb_component_id": "pcb_component_0", + "pcb_smtpad_id": "pcb_smtpad_1" + }, + { + "type": "pcb_smtpad", + "pcb_smtpad_id": "pcb_smtpad_2", + "pcb_component_id": "pcb_component_1", + "pcb_port_id": "pcb_port_2", + "layer": "top", + "shape": "rect", + "width": 0.6000000000000001, + "height": 0.6000000000000001, + "port_hints": [ + "1", + "left" + ], + "x": -3.5, + "y": 0 + }, + { + "type": "pcb_solder_paste", + "pcb_solder_paste_id": "pcb_solder_paste_2", + "layer": "top", + "shape": "rect", + "width": 0.42000000000000004, + "height": 0.42000000000000004, + "x": -3.5, + "y": 0, + "pcb_component_id": "pcb_component_1", + "pcb_smtpad_id": "pcb_smtpad_2" + }, + { + "type": "pcb_smtpad", + "pcb_smtpad_id": "pcb_smtpad_3", + "pcb_component_id": "pcb_component_1", + "pcb_port_id": "pcb_port_3", + "layer": "top", + "shape": "rect", + "width": 0.6000000000000001, + "height": 0.6000000000000001, + "port_hints": [ + "2", + "right" + ], + "x": -2.5, + "y": 0 + }, + { + "type": "pcb_solder_paste", + "pcb_solder_paste_id": "pcb_solder_paste_3", + "layer": "top", + "shape": "rect", + "width": 0.42000000000000004, + "height": 0.42000000000000004, + "x": -2.5, + "y": 0, + "pcb_component_id": "pcb_component_1", + "pcb_smtpad_id": "pcb_smtpad_3" + }, + { + "type": "pcb_port", + "pcb_port_id": "pcb_port_0", + "pcb_component_id": "pcb_component_0", + "layers": [ + "top" + ], + "x": 2.5, + "y": 0, + "source_port_id": "source_port_0" + }, + { + "type": "pcb_port", + "pcb_port_id": "pcb_port_1", + "pcb_component_id": "pcb_component_0", + "layers": [ + "top" + ], + "x": 3.5, + "y": 0, + "source_port_id": "source_port_1" + }, + { + "type": "pcb_port", + "pcb_port_id": "pcb_port_2", + "pcb_component_id": "pcb_component_1", + "layers": [ + "top" + ], + "x": -3.5, + "y": 0, + "source_port_id": "source_port_2" + }, + { + "type": "pcb_port", + "pcb_port_id": "pcb_port_3", + "pcb_component_id": "pcb_component_1", + "layers": [ + "top" + ], + "x": -2.5, + "y": 0, + "source_port_id": "source_port_3" + }, + { + "type": "pcb_trace", + "pcb_trace_id": "pcb_trace_0", + "route": [ + { + "route_type": "wire", + "x": 2.5, + "y": 0, + "width": 0.16, + "layer": "top", + "start_pcb_port_id": "pcb_port_0" + }, + { + "route_type": "wire", + "x": -1.2000000000000002, + "y": 0, + "width": 0.16, + "layer": "top" + }, + { + "route_type": "wire", + "x": -1.2000000000000002, + "y": 1.3, + "width": 0.16, + "layer": "top" + }, + { + "route_type": "wire", + "x": -3.5, + "y": 1.3, + "width": 0.16, + "layer": "top" + }, + { + "route_type": "wire", + "x": -3.5, + "y": 0, + "width": 0.16, + "layer": "top", + "end_pcb_port_id": "pcb_port_2" + } + ], + "source_trace_id": "source_trace_0" + }, + { + "type": "cad_component", + "cad_component_id": "cad_component_0", + "position": { + "x": 3, + "y": 0, + "z": 0.7 + }, + "rotation": { + "x": 0, + "y": 0, + "z": 0 + }, + "pcb_component_id": "pcb_component_0", + "source_component_id": "source_component_0", + "footprinter_string": "0402" + }, + { + "type": "cad_component", + "cad_component_id": "cad_component_1", + "position": { + "x": -3, + "y": 0, + "z": 0.7 + }, + "rotation": { + "x": 0, + "y": 0, + "z": 0 + }, + "pcb_component_id": "pcb_component_1", + "source_component_id": "source_component_1", + "footprinter_string": "0402" + } + ] \ No newline at end of file diff --git a/tests/repros/repro3-fix-hover-trace.test.ts b/tests/repros/repro3-fix-hover-trace.test.ts new file mode 100644 index 0000000..208c0f7 --- /dev/null +++ b/tests/repros/repro3-fix-hover-trace.test.ts @@ -0,0 +1,34 @@ +import { expect, test } from "bun:test" +import { convertCircuitJsonToDsnString, convertDsnJsonToCircuitJson, parseDsnToDsnJson } from "lib" + +import circuitJson from "../assets/repro/trace-id-fix-circuit.json" +import type { AnyCircuitElement } from "circuit-json" +import type { DsnPcb } from "lib" + +test("check that on hover all the trace and connected pads are correctly linked", async () => { + // Getting the dsn file from the circuit json + const dsnFile = convertCircuitJsonToDsnString( + circuitJson as AnyCircuitElement[], + ) + + const dsnJson = parseDsnToDsnJson(dsnFile) as DsnPcb + + const circuitJson2 = convertDsnJsonToCircuitJson(dsnJson) + + // pcb_trace source_trace_id should match the source_trace_id of the source_trace + const pcb_trace = circuitJson2.find((e) => e.type === "pcb_trace") + const source_trace = circuitJson2.find((e) => e.type === "source_trace") + + expect(pcb_trace?.source_trace_id && source_trace?.source_trace_id).toBeTruthy() + expect(pcb_trace!.source_trace_id).toBe(source_trace!.source_trace_id) + + // Check that pcb_smtpad's pcb_port_id matches the pattern of corresponding source_port's source_port_id + const pcb_smtpad = circuitJson2.find((e) => e.type === "pcb_smtpad") + const source_port = circuitJson2.find((e) => e.type === "source_port") + + expect(pcb_smtpad?.pcb_port_id && source_port?.source_port_id).toBeTruthy() + + // Type assertion to tell TypeScript that pcb_smtpad has pcb_port_id + const expected_source_port_id = (pcb_smtpad as { pcb_port_id: string }).pcb_port_id.replace("pcb_port_", "source_port_") + expect(expected_source_port_id).toBe(source_port!.source_port_id) +})