mohan-bee/tscircuit-robot-vacuum
A tscircuit package created by mohan-bee.
- Version
- 1.0.0
- License
- unset
- Stars
- 0
scripts/export-views.ts
import fs from "node:fs/promises"
import { convertCircuitJsonToGltf } from "circuit-json-to-gltf"
import { convertCircuitJsonToPcbSvg, convertCircuitJsonToStackedSchematicSheetsSvg } from "circuit-to-svg"
import { renderGLTFToPNGFromGLB } from "poppygl"
import { Resvg } from "@resvg/resvg-js"
import type { AnyCircuitElement } from "circuit-json"
const assembled: AnyCircuitElement[] = JSON.parse(await fs.readFile("dist/circuit.json", "utf8"))
const pcbSvg = convertCircuitJsonToPcbSvg(assembled, { width: 1600, height: 1300 })
await fs.writeFile("dist/pcb.svg", pcbSvg)
await fs.writeFile("dist/pcb.png", new Resvg(pcbSvg).render().asPng())
await fs.writeFile("dist/schematic.svg", convertCircuitJsonToStackedSchematicSheetsSvg(assembled))
const hiddenSources = new Set(assembled.filter((e: any) => e.type === "source_component" && /TOP_COVER|BIN_LID|RPLIDAR/.test(e.name ?? "")).map((e: any) => e.source_component_id))
for (const [name, records] of [
["assembled", assembled],
["exploded", JSON.parse(await fs.readFile("dist/exploded.circuit.json", "utf8"))],
["internals", assembled.filter((e: any) => !(e.type === "cad_component" && hiddenSources.has(e.source_component_id)))],
] as const) {
console.log(`Exporting ${name} native geometry`)
const model = await convertCircuitJsonToGltf(records, { format: "glb", includeModels: true, boardTextureResolution: 1024 })
if (!(model instanceof ArrayBuffer)) throw new Error("Expected binary GLB")
await fs.writeFile(`dist/${name}.glb`, new Uint8Array(model))
// Camera is authored for this complete assembly, rather than PCB-only bounds.
const png = await renderGLTFToPNGFromGLB(model, { width: 1400, height: 1000, supersampling: 1, backgroundColor: "#e7ebf0", camPos: [-450, 360, 450], lookAt: [0, name === "exploded" ? 50 : 0, 0], fov: 40, projection: "orthographic", orthographicHeight: name === "exploded" ? 440 : 340, showEdges: false })
await fs.writeFile(`dist/${name}.png`, png)
}