From d9fe5ed9866bf03be4e39a419821433be6307d98 Mon Sep 17 00:00:00 2001 From: Rishabh Gupta Date: Tue, 26 Nov 2024 01:17:26 +0530 Subject: [PATCH 1/2] feat: convert simple_chip to dsn --- .../convert-circuit-json-to-dsn-json.ts | 4 +- .../circuit-json-to-dsn-json/process-chips.ts | 89 +++++++++++++++++++ .../process-components-and-pads.ts | 3 + 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts diff --git a/lib/dsn-pcb/circuit-json-to-dsn-json/convert-circuit-json-to-dsn-json.ts b/lib/dsn-pcb/circuit-json-to-dsn-json/convert-circuit-json-to-dsn-json.ts index 431a96d..5f3007d 100644 --- a/lib/dsn-pcb/circuit-json-to-dsn-json/convert-circuit-json-to-dsn-json.ts +++ b/lib/dsn-pcb/circuit-json-to-dsn-json/convert-circuit-json-to-dsn-json.ts @@ -4,7 +4,7 @@ import { processComponentsAndPads } from "./process-components-and-pads" import { processNets } from "./process-nets" import { processPcbTraces } from "./process-pcb-traces" import { processPlatedHoles } from "./process-plated-holes" - +import { processChips } from "./process-chips" export function convertCircuitJsonToDsnJson( circuitElements: AnyCircuitElement[], ): DsnPcb { @@ -129,7 +129,7 @@ export function convertCircuitJsonToDsnJson( processPlatedHoles(circuitElements, pcb) processNets(circuitElements, pcb) processPcbTraces(circuitElements, pcb) - + processChips(circuitElements, pcb) return pcb } diff --git a/lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts b/lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts new file mode 100644 index 0000000..e66de6f --- /dev/null +++ b/lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts @@ -0,0 +1,89 @@ +import type { AnyCircuitElement, PcbComponent, PcbPort, SourceComponentBase, SourcePort } from "circuit-json" +import type { DsnPcb } from "../types" + +export function processChips(circuitElements: AnyCircuitElement[], pcb: DsnPcb) { + // Group pcb_port by component + const portsByComponent = new Map() + + // Find all source components that are simple_chips + const chipComponents = circuitElements.filter((e) => + e.type === "source_component" && e.ftype === "simple_chip" + ) as SourceComponentBase[] + + // Find all pcb_ports for each chip component + chipComponents.forEach(chip => { + const componentPorts = circuitElements.filter((e): e is PcbPort => { + if (e.type !== "pcb_port") return false + // Find the source port this pcb_port is connected to + const sourcePort = circuitElements.find( + (sp): sp is SourcePort => sp.type === "source_port" && sp.source_port_id === e.source_port_id + ) as SourcePort | undefined + // Check if the source port belongs to this chip + return sourcePort?.source_component_id === chip.source_component_id + }) + + if (componentPorts.length > 0) { + portsByComponent.set(chip.source_component_id, componentPorts) + } + }) + + // Process each chip component + chipComponents.forEach(chip => { + const ports = portsByComponent.get(chip.source_component_id) + if (!ports) return + + // Create or update image for this chip + const imageName = `${chip.name}_footprint` + const existingImage = pcb.library.images.find(img => img.name === imageName) + + if (!existingImage) { + // Create new image with pins from ports + pcb.library.images.push({ + name: imageName, + outlines: [], + pins: ports.map((port, index) => ({ + padstack_name: `default_pad_${imageName}`, + pin_number: index + 1, + x: port.x * 1000, // Convert mm to μm + y: port.y * 1000 + })) + }) + + // Add padstack if not exists + if (!pcb.library.padstacks.find(p => p.name === `default_pad_${imageName}`)) { + pcb.library.padstacks.push({ + name: `default_pad_${imageName}`, + shapes: [ + { + shapeType: "polygon", + layer: "F.Cu", + width: 0, + coordinates: [-300, 300, 300, 300, 300, -300, -300, -300, -300, 300] + } + ], + attach: "off" + }) + } + + // Find corresponding pcb_component + const pcbComponent = circuitElements.find( + (e): e is PcbComponent => e.type === "pcb_component" && e.source_component_id === chip.source_component_id + ) as PcbComponent | undefined + + if (pcbComponent) { + // Add component placement using pcb_component coordinates + pcb.placement.components.push({ + name: imageName, + places: [{ + refdes: chip.name, + x: pcbComponent.center.x * 1000, // Convert mm to μm + y: pcbComponent.center.y * 1000, + side: "front", + rotation: 0, + PN: chip.manufacturer_part_number || "" + }] + }) + } + } + }) +} diff --git a/lib/dsn-pcb/circuit-json-to-dsn-json/process-components-and-pads.ts b/lib/dsn-pcb/circuit-json-to-dsn-json/process-components-and-pads.ts index 4e0dfe1..f74fe09 100644 --- a/lib/dsn-pcb/circuit-json-to-dsn-json/process-components-and-pads.ts +++ b/lib/dsn-pcb/circuit-json-to-dsn-json/process-components-and-pads.ts @@ -69,6 +69,9 @@ export function processComponentsAndPads( e.source_component_id === pcbComponent.source_component_id, ) as SourceComponentBase) + // Skip components with ftype "simple_chip" + if (sourceComponent?.ftype === "simple_chip") continue + const footprintName = getFootprintName(sourceComponent?.ftype) const componentName = sourceComponent?.name || "Unknown" const circuitSpaceCoordinates = applyToPoint( From fcedd06d28def4f392ac85c3d7356d1c2c3bdd78 Mon Sep 17 00:00:00 2001 From: Rishabh Gupta Date: Tue, 26 Nov 2024 01:18:55 +0530 Subject: [PATCH 2/2] format --- .../circuit-json-to-dsn-json/process-chips.ts | 178 ++++++++++-------- 1 file changed, 100 insertions(+), 78 deletions(-) diff --git a/lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts b/lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts index e66de6f..0e6fdf3 100644 --- a/lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts +++ b/lib/dsn-pcb/circuit-json-to-dsn-json/process-chips.ts @@ -1,89 +1,111 @@ -import type { AnyCircuitElement, PcbComponent, PcbPort, SourceComponentBase, SourcePort } from "circuit-json" +import type { + AnyCircuitElement, + PcbComponent, + PcbPort, + SourceComponentBase, + SourcePort, +} from "circuit-json" import type { DsnPcb } from "../types" -export function processChips(circuitElements: AnyCircuitElement[], pcb: DsnPcb) { - // Group pcb_port by component - const portsByComponent = new Map() +export function processChips( + circuitElements: AnyCircuitElement[], + pcb: DsnPcb, +) { + // Group pcb_port by component + const portsByComponent = new Map() - // Find all source components that are simple_chips - const chipComponents = circuitElements.filter((e) => - e.type === "source_component" && e.ftype === "simple_chip" - ) as SourceComponentBase[] + // Find all source components that are simple_chips + const chipComponents = circuitElements.filter( + (e) => e.type === "source_component" && e.ftype === "simple_chip", + ) as SourceComponentBase[] - // Find all pcb_ports for each chip component - chipComponents.forEach(chip => { - const componentPorts = circuitElements.filter((e): e is PcbPort => { - if (e.type !== "pcb_port") return false - // Find the source port this pcb_port is connected to - const sourcePort = circuitElements.find( - (sp): sp is SourcePort => sp.type === "source_port" && sp.source_port_id === e.source_port_id - ) as SourcePort | undefined - // Check if the source port belongs to this chip - return sourcePort?.source_component_id === chip.source_component_id - }) - - if (componentPorts.length > 0) { - portsByComponent.set(chip.source_component_id, componentPorts) - } + // Find all pcb_ports for each chip component + chipComponents.forEach((chip) => { + const componentPorts = circuitElements.filter((e): e is PcbPort => { + if (e.type !== "pcb_port") return false + // Find the source port this pcb_port is connected to + const sourcePort = circuitElements.find( + (sp): sp is SourcePort => + sp.type === "source_port" && sp.source_port_id === e.source_port_id, + ) as SourcePort | undefined + // Check if the source port belongs to this chip + return sourcePort?.source_component_id === chip.source_component_id }) - // Process each chip component - chipComponents.forEach(chip => { - const ports = portsByComponent.get(chip.source_component_id) - if (!ports) return + if (componentPorts.length > 0) { + portsByComponent.set(chip.source_component_id, componentPorts) + } + }) - // Create or update image for this chip - const imageName = `${chip.name}_footprint` - const existingImage = pcb.library.images.find(img => img.name === imageName) - - if (!existingImage) { - // Create new image with pins from ports - pcb.library.images.push({ - name: imageName, - outlines: [], - pins: ports.map((port, index) => ({ - padstack_name: `default_pad_${imageName}`, - pin_number: index + 1, - x: port.x * 1000, // Convert mm to μm - y: port.y * 1000 - })) - }) + // Process each chip component + chipComponents.forEach((chip) => { + const ports = portsByComponent.get(chip.source_component_id) + if (!ports) return - // Add padstack if not exists - if (!pcb.library.padstacks.find(p => p.name === `default_pad_${imageName}`)) { - pcb.library.padstacks.push({ - name: `default_pad_${imageName}`, - shapes: [ - { - shapeType: "polygon", - layer: "F.Cu", - width: 0, - coordinates: [-300, 300, 300, 300, 300, -300, -300, -300, -300, 300] - } - ], - attach: "off" - }) - } + // Create or update image for this chip + const imageName = `${chip.name}_footprint` + const existingImage = pcb.library.images.find( + (img) => img.name === imageName, + ) - // Find corresponding pcb_component - const pcbComponent = circuitElements.find( - (e): e is PcbComponent => e.type === "pcb_component" && e.source_component_id === chip.source_component_id - ) as PcbComponent | undefined + if (!existingImage) { + // Create new image with pins from ports + pcb.library.images.push({ + name: imageName, + outlines: [], + pins: ports.map((port, index) => ({ + padstack_name: `default_pad_${imageName}`, + pin_number: index + 1, + x: port.x * 1000, // Convert mm to μm + y: port.y * 1000, + })), + }) - if (pcbComponent) { - // Add component placement using pcb_component coordinates - pcb.placement.components.push({ - name: imageName, - places: [{ - refdes: chip.name, - x: pcbComponent.center.x * 1000, // Convert mm to μm - y: pcbComponent.center.y * 1000, - side: "front", - rotation: 0, - PN: chip.manufacturer_part_number || "" - }] - }) - } - } - }) + // Add padstack if not exists + if ( + !pcb.library.padstacks.find( + (p) => p.name === `default_pad_${imageName}`, + ) + ) { + pcb.library.padstacks.push({ + name: `default_pad_${imageName}`, + shapes: [ + { + shapeType: "polygon", + layer: "F.Cu", + width: 0, + coordinates: [ + -300, 300, 300, 300, 300, -300, -300, -300, -300, 300, + ], + }, + ], + attach: "off", + }) + } + + // Find corresponding pcb_component + const pcbComponent = circuitElements.find( + (e): e is PcbComponent => + e.type === "pcb_component" && + e.source_component_id === chip.source_component_id, + ) as PcbComponent | undefined + + if (pcbComponent) { + // Add component placement using pcb_component coordinates + pcb.placement.components.push({ + name: imageName, + places: [ + { + refdes: chip.name, + x: pcbComponent.center.x * 1000, // Convert mm to μm + y: pcbComponent.center.y * 1000, + side: "front", + rotation: 0, + PN: chip.manufacturer_part_number || "", + }, + ], + }) + } + } + }) }