Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 15 additions & 26 deletions cli/build/generate-kicad-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "node:path"
import {
CircuitJsonToKicadPcbConverter,
CircuitJsonToKicadSchConverter,
resolveAndLoadKicad3dModelFiles,
} from "circuit-json-to-kicad"
import type { AnyCircuitElement } from "circuit-json"

Expand Down Expand Up @@ -85,32 +86,20 @@ export const generateKicadProject = async ({
fs.writeFileSync(path.join(outputDir, boardFileName), pcbContent)
fs.writeFileSync(path.join(outputDir, projectFileName), proContent)

// Download and write 3D model files
const platformFetch = globalThis.fetch
for (const modelPath of pcbConverter.getModel3dSourcePaths()) {
const fileName = path.basename(modelPath)
const isRemote =
modelPath.startsWith("http://") || modelPath.startsWith("https://")
const shapesDir = isRemote
? "tscircuit_builtin.3dshapes"
: `${sanitizedProjectName}.3dshapes`
const destDir = path.join(outputDir, `3dmodels/${shapesDir}`)
fs.mkdirSync(destDir, { recursive: true })

if (isRemote) {
try {
const response = await platformFetch(modelPath)
if (response.ok) {
const buffer = Buffer.from(await response.arrayBuffer())
fs.writeFileSync(path.join(destDir, fileName), buffer)
}
} catch (error) {
console.warn(`Failed to fetch 3D model from ${modelPath}`)
}
} else if (fs.existsSync(modelPath)) {
fs.copyFileSync(modelPath, path.join(destDir, fileName))
}
}
await resolveAndLoadKicad3dModelFiles({
model3dSourcePaths: pcbConverter.getModel3dSourcePaths(),
projectName: sanitizedProjectName,
fetch: (modelPath) => globalThis.fetch(modelPath),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fetch: (modelPath) => globalThis.fetch(modelPath),
fetch: platformConfig?.platformFetch ?? globalThis.fetch,

readFile: (modelPath) => fs.promises.readFile(modelPath),
onModelFile: ({ outputPath, content }) => {
const outputFilePath = path.join(outputDir, outputPath)
fs.mkdirSync(path.dirname(outputFilePath), { recursive: true })
fs.writeFileSync(outputFilePath, content)
},
onError: ({ sourcePath }) => {
console.warn(`Failed to load 3D model from ${sourcePath}`)
},
})
}

return {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"circuit-json": "^0.0.425",
"circuit-json-to-bom-csv": "^0.0.7",
"circuit-json-to-gerber": "^0.0.51",
"circuit-json-to-kicad": "^0.0.125",
"circuit-json-to-kicad": "0.0.137",
"circuit-json-to-pnp-csv": "^0.0.7",
"circuit-json-to-readable-netlist": "^0.0.15",
"circuit-json-to-spice": "^0.0.10",
Expand All @@ -58,7 +58,7 @@
"jwt-decode": "^4.0.0",
"kicad-component-converter": "^0.1.40",
"kicad-to-circuit-json": "^0.0.59",
"kicadts": "^0.0.24",
"kicadts": "^0.0.35",
"kleur": "^4.1.5",
"ky": "^1.7.4",
"make-vfs": "^1.0.15",
Expand Down
Loading