diff --git a/lib/dsn-pcb/dsn-json-to-circuit-json/convert-dsn-pcb-to-circuit-json.ts b/lib/dsn-pcb/dsn-json-to-circuit-json/convert-dsn-pcb-to-circuit-json.ts index b0f51dd..7e9594e 100644 --- a/lib/dsn-pcb/dsn-json-to-circuit-json/convert-dsn-pcb-to-circuit-json.ts +++ b/lib/dsn-pcb/dsn-json-to-circuit-json/convert-dsn-pcb-to-circuit-json.ts @@ -15,7 +15,7 @@ export function convertDsnPcbToCircuitJson( const elements: AnyCircuitElement[] = [] // TODO use pcb.resolution.unit and pcb.resolution.value - const transformUmToMm = scale(1 / 1000) + const transformDsnUnitToMm = scale(1 / 1000) // Add the board // You must use the dsnPcb.boundary to get the center, width and height @@ -34,12 +34,12 @@ export function convertDsnPcbToCircuitJson( const minX = Math.min(...boundaryPath.map(([x]) => x)) const maxY = Math.max(...boundaryPath.map(([, y]) => y)) const minY = Math.min(...boundaryPath.map(([, y]) => y)) - board.center = applyToPoint(transformUmToMm, { + board.center = applyToPoint(transformDsnUnitToMm, { x: (maxX + minX) / 2, y: (maxY + minY) / 2, }) - board.width = (maxX - minX) * transformUmToMm.a - board.height = (maxY - minY) * transformUmToMm.a + board.width = (maxX - minX) * transformDsnUnitToMm.a + board.height = (maxY - minY) * transformDsnUnitToMm.a } else { throw new Error( `Couldn't read DSN boundary, add support for dsnPcb.structure.boundary["${Object.keys(dsnPcb.structure.boundary).join(",")}"]`, @@ -49,7 +49,7 @@ export function convertDsnPcbToCircuitJson( elements.push(board) // Convert padstacks to SMT pads using the transformation matrix - elements.push(...convertPadstacksToSmtPads(dsnPcb, transformUmToMm)) + elements.push(...convertPadstacksToSmtPads(dsnPcb, transformDsnUnitToMm)) // Convert wires to PCB traces using the transformation matrix if (dsnPcb.wiring && dsnPcb.network) { @@ -57,12 +57,17 @@ export function convertDsnPcbToCircuitJson( ...convertWiresToPcbTraces( dsnPcb.wiring, dsnPcb.network, - transformUmToMm, + transformDsnUnitToMm, ), ) } - elements.push(...convertDsnPcbComponentsToSourceComponentsAndPorts(dsnPcb)) + elements.push( + ...convertDsnPcbComponentsToSourceComponentsAndPorts({ + dsnPcb, + transformDsnUnitToMm, + }), + ) elements.push( ...convertNetsToSourceNetsAndTraces({ dsnPcb, diff --git a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-dsn-pcb-components-to-source-components-and-ports.ts b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-dsn-pcb-components-to-source-components-and-ports.ts index 39a23c6..db52012 100644 --- a/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-dsn-pcb-components-to-source-components-and-ports.ts +++ b/lib/dsn-pcb/dsn-json-to-circuit-json/dsn-component-converters/convert-dsn-pcb-components-to-source-components-and-ports.ts @@ -1,9 +1,14 @@ import type { AnySourceComponent, PcbPort, SourcePort } from "circuit-json" +import { applyToPoint, type Matrix } from "transformation-matrix" import type { DsnPcb, Image, Pin } from "lib/dsn-pcb/types" -export const convertDsnPcbComponentsToSourceComponentsAndPorts = ( - dsnPcb: DsnPcb, -): Array => { +export const convertDsnPcbComponentsToSourceComponentsAndPorts = ({ + dsnPcb, + transformDsnUnitToMm, +}: { + dsnPcb: DsnPcb + transformDsnUnitToMm: Matrix +}): Array => { const result: Array = [] // Map to store image definitions for component lookup @@ -35,14 +40,17 @@ export const convertDsnPcbComponentsToSourceComponentsAndPorts = ( pin_number: pin.pin_number, port_hints: [], } + const pcb_port_center = applyToPoint(transformDsnUnitToMm, { + x: component.place.x + pin.x, + y: component.place.y + pin.y, + }) const pcb_port: PcbPort = { pcb_port_id: `pcb_port_${component.name}-Pad${pin.pin_number}`, type: "pcb_port", source_port_id: port.source_port_id, pcb_component_id: component.name, - // TODO get X/Y position of pad from dsnPcb - x: 0, - y: 0, + x: pcb_port_center.x, + y: pcb_port_center.y, layers: [], } result.push(port, pcb_port)