Skip to content

Commit

Permalink
Merge pull request #15 from tscircuit/fix/parse-back-to-circuitjson
Browse files Browse the repository at this point in the history
fix: Add support for the polygon shape
  • Loading branch information
imrishabh18 authored Nov 8, 2024
2 parents 99e1b9d + d0cf49c commit 8886dae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,49 @@ export function convertPadstacksToSmtPads(
return
}

// Get the rect shape from the padstack
// Find shape in padstack - try rectangle first, then polygon
const rectShape = padstack.shapes.find(
(shape) => shape.shapeType === "rect",
)

if (!rectShape) {
console.warn(`No rect shape found for padstack: ${padstack.name}`)
const polygonShape = padstack.shapes.find(
(shape) => shape.shapeType === "polygon",
)

let width: number
let height: number

if (rectShape) {
// Handle rectangle shape
const [x1, y1, x2, y2] = rectShape.coordinates
width = Math.abs(x2 - x1) / 1000 // Convert μm to mm
height = Math.abs(y2 - y1) / 1000 // Convert μm to mm
} else if (polygonShape) {
// Handle polygon shape
const coordinates = polygonShape.coordinates
let minX = Infinity
let maxX = -Infinity
let minY = Infinity
let maxY = -Infinity

// Coordinates are in pairs (x,y), so iterate by 2
for (let i = 0; i < coordinates.length; i += 2) {
const x = coordinates[i]
const y = coordinates[i + 1]

minX = Math.min(minX, x)
maxX = Math.max(maxX, x)
minY = Math.min(minY, y)
maxY = Math.max(maxY, y)
}

width = Math.abs(maxX - minX) / 1000
height = Math.abs(maxY - minY) / 1000
} else {
console.warn(`No valid shape found for padstack: ${padstack.name}`)
return
}

// Extract the width and height from the rect shape coordinates
const [x1, y1, x2, y2] = rectShape.coordinates
const width = Math.abs(x2 - x1) / 1000 // Convert μm to mm
const height = Math.abs(y2 - y1) / 1000 // Convert μm to mm

// Calculate position in circuit space using the transformation matrix
const { x: circuitX, y: circuitY } = applyToPoint(transform, {
x: compX + pin.x,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions tests/dsn-pcb/convert-dsn-file-to-circuit-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ import dsnFileWithFreeroutingTrace from "../assets/testkicadproject/freeroutingT
}

test("parse dsn to circuit json", async () => {
const fs = require("fs")
const dsnJson = parseDsnToDsnJson(dsnFileWithFreeroutingTrace)
const circuitJson = convertDsnJsonToCircuitJson(dsnJson)

fs.writeFileSync(
"circuitJsonConverted.json",
JSON.stringify(circuitJson, null, 2),
)

expect(convertCircuitJsonToPcbSvg(circuitJson)).toMatchSvgSnapshot(
import.meta.path,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/dsn-pcb/parse-dsn-json-to-circuit-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { parseDsnToDsnJson } from "lib"

// @ts-ignore

test.skip("parse s-expr to json", async () => {
test("parse s-expr to json", async () => {
const pcbJson = parseDsnToDsnJson(testDsnFile)
expect(pcbJson).toBeTruthy()
})

test.skip("parse json to circuit json", async () => {
test("parse json to circuit json", async () => {
const pcb = parseDsnToDsnJson(testDsnFile)
const circuitJson = convertDsnJsonToCircuitJson(pcb)

Expand Down

0 comments on commit 8886dae

Please sign in to comment.