Skip to content

Commit

Permalink
new test showing broken pad positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Jul 8, 2024
1 parent 25f2b03 commit 3157a5f
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
node_modules
.aider*
dist
*.raweasy.json
/*.raweasy.json
*.bettereasy.json
.vscode
Binary file modified bun.lockb
Binary file not shown.
22 changes: 22 additions & 0 deletions lib/compute-center-offset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { z } from "zod"
import type { EasyEdaJson } from "./schemas/easy-eda-json-schema"
import type { PadSchema } from "./schemas/package-detail-shape-schema"

export const computeCenterOffset = (easyeda: EasyEdaJson) => {
const pads = easyeda.packageDetail.dataStr.shape.filter(
(shape): shape is z.infer<typeof PadSchema> => shape.type === "PAD"
)

const minX = Math.min(...pads.map((pad) => pad.center.x))
const maxX = Math.max(...pads.map((pad) => pad.center.x))
const minY = Math.min(...pads.map((pad) => pad.center.y))
const maxY = Math.max(...pads.map((pad) => pad.center.y))

const centerX = (minX + maxX) / 2
const centerY = (minY + maxY) / 2

return {
x: centerX,
y: centerY,
}
}
16 changes: 13 additions & 3 deletions lib/convert-easyeda-json-to-tscircuit-soup-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
import * as Soup from "@tscircuit/soup"
import { generateArcFromSweep, generateArcPathWithMid } from "./math/arc-utils"
import { transformPCBElements } from "@tscircuit/builder"
import { scale } from "transformation-matrix"
import { scale, translate } from "transformation-matrix"
import { computeCenterOffset } from "./compute-center-offset"

const handleSilkscreenPath = (
track: z.infer<typeof TrackSchema>,
Expand Down Expand Up @@ -61,13 +62,15 @@ const handleSilkscreenArc = (arc: z.infer<typeof ArcSchema>, index: number) => {

interface Options {
useModelCdn?: boolean
shouldRecenter?: boolean
}

export const convertEasyEdaJsonToTscircuitSoupJson = (
easyEdaJson: EasyEdaJson,
{ useModelCdn }: Options = {}
{ useModelCdn, shouldRecenter = true }: Options = {}
): AnySoupElement[] => {
const soupElements: AnySoupElement[] = []
const centerOffset = computeCenterOffset(easyEdaJson)

// Add source component
const source_component = any_source_component.parse({
Expand All @@ -86,7 +89,7 @@ export const convertEasyEdaJsonToTscircuitSoupJson = (
width: 0, // TODO compute width
height: 0, // TODO compute height
rotation: 0,
center: { x: 0, y: 0 },
center: { x: centerOffset.x, y: centerOffset.y },
layer: "top",
} as Soup.PCBComponentInput)

Expand Down Expand Up @@ -200,5 +203,12 @@ export const convertEasyEdaJsonToTscircuitSoupJson = (
)
}

if (shouldRecenter) {
transformPCBElements(
soupElements,
translate(-centerOffset.x, centerOffset.y)
)
}

return soupElements
}
12 changes: 4 additions & 8 deletions lib/generate-footprint-tsx.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { z } from "zod"
import type { EasyEdaJson } from "./schemas/easy-eda-json-schema"
import { PadSchema } from "./schemas/package-detail-shape-schema"
import { computeCenterOffset } from "./compute-center-offset"

export const generateFootprintTsx = (easyEdaJson: EasyEdaJson): string => {
const pads = easyEdaJson.packageDetail.dataStr.shape.filter(
(shape): shape is z.infer<typeof PadSchema> => shape.type === "PAD"
)

// Calculate the center point of all pads
const minX = Math.min(...pads.map((pad) => pad.center.x))
const maxX = Math.max(...pads.map((pad) => pad.center.x))
const minY = Math.min(...pads.map((pad) => pad.center.y))
const maxY = Math.max(...pads.map((pad) => pad.center.y))

const centerX = (minX + maxX) / 2
const centerY = (minY + maxY) / 2
const centerOffset = computeCenterOffset(easyEdaJson)
const centerX = centerOffset.x
const centerY = centerOffset.y

const footprintElements = pads.map((pad) => {
const { center, width, height, holeRadius, number } = pad
Expand Down
3 changes: 3 additions & 0 deletions lib/schemas/package-detail-shape-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { z } from "zod"
import { mm } from "@tscircuit/mm"

const mil = z.number().transform((n) => mm(`${n}mil`))

export const PointSchema = z
.any()
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"typescript": "^5.5.2"
},
"dependencies": {
"@tscircuit/mm": "^0.0.7",
"commander": "^12.1.0",
"transformation-matrix": "^2.16.1",
"zod": "^3.23.8"
Expand Down
299 changes: 299 additions & 0 deletions tests/assets/C88224.raweasy.json

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions tests/convert-to-soup-tests/c88224.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { it, expect } from "bun:test"
import { logSoup } from "@tscircuit/log-soup"
import c88224RawEasy from "../assets/C88224.raweasy.json"
import { EasyEdaJsonSchema } from "lib/schemas/easy-eda-json-schema"
import { convertEasyEdaJsonToTscircuitSoupJson } from "lib/convert-easyeda-json-to-tscircuit-soup-json"
import { su } from "@tscircuit/soup-util"
import type { AnySoupElement } from "@tscircuit/soup"

it("should parse easyeda json for a c88224 and convert to tscircuit soup", async () => {
const parsedJson = EasyEdaJsonSchema.parse(c88224RawEasy)
const soupElements = convertEasyEdaJsonToTscircuitSoupJson(parsedJson).concat(
[
{
type: "pcb_board",
center: { x: 0, y: 0 },
width: 20,
height: 20,
},
]
)

await logSoup("easyeda c88224 to soup", soupElements as AnySoupElement[])
})

0 comments on commit 3157a5f

Please sign in to comment.