-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from tscircuit/new-fp-gen-mech
New Footprint tsx Generation Mechanism for more stable units, support for <hole />
- Loading branch information
Showing
13 changed files
with
107 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,70 @@ | ||
import { z } from "zod" | ||
import type { BetterEasyEdaJson } from "./schemas/easy-eda-json-schema" | ||
import { PadSchema } from "./schemas/package-detail-shape-schema" | ||
import type { | ||
HoleSchema, | ||
PadSchema, | ||
} from "./schemas/package-detail-shape-schema" | ||
import { computeCenterOffset } from "./compute-center-offset" | ||
import { mm } from "@tscircuit/mm" | ||
import { mm, mmStr } from "@tscircuit/mm" | ||
import { convertEasyEdaJsonToTscircuitSoupJson } from "./convert-easyeda-json-to-tscircuit-soup-json" | ||
import type { AnyCircuitElement } from "circuit-json" | ||
import { su } from "@tscircuit/soup-util" | ||
|
||
export const generateFootprintTsx = ( | ||
easyEdaJson: BetterEasyEdaJson, | ||
circuitJson: AnyCircuitElement[], | ||
): string => { | ||
const pads = easyEdaJson.packageDetail.dataStr.shape.filter( | ||
(shape): shape is z.infer<typeof PadSchema> => shape.type === "PAD", | ||
) | ||
const holes = su(circuitJson).pcb_hole.list() | ||
const platedHoles = su(circuitJson).pcb_plated_hole.list() | ||
const smtPads = su(circuitJson).pcb_smtpad.list() | ||
const silkscreenPaths = su(circuitJson).pcb_silkscreen_path.list() | ||
|
||
const centerOffset = computeCenterOffset(easyEdaJson) | ||
const centerX = centerOffset.x | ||
const centerY = centerOffset.y | ||
const elementStrings: string[] = [] | ||
|
||
const footprintElements = pads.map((pad) => { | ||
const { center, width, height, holeRadius, number } = pad | ||
const isPlatedHole = holeRadius !== undefined && mm(holeRadius) > 0 | ||
for (const hole of holes) { | ||
if (hole.hole_shape === "circle") { | ||
elementStrings.push( | ||
`<hole pcbX="${mmStr(hole.x)}" pcbY="${mmStr(hole.y)}" diameter="${mmStr(hole.hole_diameter)}" />`, | ||
) | ||
} else if (hole.hole_shape === "oval") { | ||
console.warn("Unhandled oval hole in conversion (needs implementation)") | ||
} | ||
} | ||
|
||
// Normalize the position by subtracting the center point | ||
const normalizedX = mm(center.x) - centerX | ||
const normalizedY = mm(center.y) - centerY | ||
for (const platedHole of platedHoles) { | ||
if (platedHole.shape === "oval") { | ||
elementStrings.push( | ||
`<platedhole portHints={${JSON.stringify(platedHole.port_hints)}} pcbX="${mmStr(platedHole.x)}" pcbY="${mmStr(platedHole.y)}" diameter="${mmStr(platedHole.hole_width)}" height="${mmStr(platedHole.hole_height)}" shape="oval" />`, | ||
) | ||
} else if (platedHole.shape === "circle") { | ||
elementStrings.push( | ||
`<platedhole portHints={${JSON.stringify(platedHole.port_hints)}} pcbX="${mmStr(platedHole.x)}" pcbY="${mmStr(platedHole.y)}" diameter="${mmStr(platedHole.hole_diameter)}" shape="circle" />`, | ||
) | ||
} else if (platedHole.shape === "pill") { | ||
console.warn("Unhandled pill hole in conversion (needs implementation)") | ||
} | ||
} | ||
|
||
if (isPlatedHole) { | ||
return ` | ||
<platedhole | ||
pcbX="${normalizedX.toFixed(2)}mm" | ||
pcbY="${normalizedY.toFixed(2)}mm" | ||
hole_diameter="${mm(holeRadius) * 2}mm" | ||
outer_diameter="${mm(width)}mm" | ||
portHints={["${number}"]} | ||
/>`.replace(/\n/, "") | ||
} else { | ||
return ` | ||
<smtpad | ||
pcbX="${normalizedX.toFixed(2)}mm" | ||
pcbY="${normalizedY.toFixed(2)}mm" | ||
width="${mm(width)}mm" | ||
height="${mm(height)}mm" | ||
shape="rect" | ||
portHints={["${number}"]} | ||
/>`.replace(/\n/, "") | ||
for (const smtPad of smtPads) { | ||
if (smtPad.shape === "circle") { | ||
elementStrings.push( | ||
`<smtpad portHints={${JSON.stringify(smtPad.port_hints)}} pcbX="${mmStr(smtPad.x)}" pcbY="${mmStr(smtPad.y)}" radius="${mmStr(smtPad.radius)}" shape="circle" />`, | ||
) | ||
} else if (smtPad.shape === "rect") { | ||
elementStrings.push( | ||
`<smtpad portHints={${JSON.stringify(smtPad.port_hints)}} pcbX="${mmStr(smtPad.x)}" pcbY="${mmStr(smtPad.y)}" width="${mmStr(smtPad.width)}" height="${mmStr(smtPad.height)}" shape="rect" />`, | ||
) | ||
} | ||
}) | ||
} | ||
|
||
for (const silkscreenPath of silkscreenPaths) { | ||
elementStrings.push( | ||
`<silkscreenpath route={${JSON.stringify(silkscreenPath.route)}} />`, | ||
) | ||
} | ||
|
||
return ` | ||
<footprint> | ||
${footprintElements.join("\n")} | ||
${elementStrings.join("\n")} | ||
</footprint> | ||
`.trim() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export * from "./convert-easyeda-json-to-tscircuit-soup-json" | ||
export * from "./fetch-easyeda-json" | ||
export { convertRawEasyEdaToTs } from "./convert-to-typescript-component" | ||
export { convertRawEasyToTsx as convertRawEasyEdaToTs } from "./convert-to-typescript-component" | ||
export { normalizeManufacturerPartNumber } from "./utils/normalize-manufacturer-part-number" | ||
export * from "./schemas/easy-eda-json-schema" | ||
export { convertEasyEdaJsonToVariousFormats } from "./convert-easyeda-json-to-various-formats" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import { it, expect } from "bun:test" | ||
import timerRawEasy from "../assets/C128415.raweasy.json" | ||
import { convertToTypescriptComponent } from "lib/convert-to-typescript-component" | ||
import { convertBetterEasyToTsx } from "lib/convert-to-typescript-component" | ||
import { EasyEdaJsonSchema } from "lib/schemas/easy-eda-json-schema" | ||
import { convertEasyEdaJsonToCircuitJson } from "lib" | ||
|
||
it("should convert 555timer into typescript file", async () => { | ||
const easyeda = EasyEdaJsonSchema.parse(timerRawEasy) | ||
const soup = convertEasyEdaJsonToCircuitJson(easyeda, { | ||
useModelCdn: true, | ||
const betterEasy = EasyEdaJsonSchema.parse(timerRawEasy) | ||
const result = await convertBetterEasyToTsx({ | ||
betterEasy, | ||
}) | ||
const result = await convertToTypescriptComponent({ | ||
easyeda, | ||
soup, | ||
}) | ||
console.log(result) | ||
// TODO snapshot | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters