rushabhcodes/desk-fidget-counter

A PLA desk fidget counter assembly houses an F1 PCB, 1.28-inch LCD with harness, rotary knob/bearing mechanism, button cap, and screw-secured base/display mount.

Version
6.0.16
License
MIT
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone rushabhcodes/desk-fidget-counter  # download the package files into ./desk-fidget-counter
tsci add rushabhcodes/desk-fidget-counter    # add it to your project as @tsci/rushabhcodes.desk-fidget-counter

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

scripts/export-assembly-drawing.ts

// SPDX-License-Identifier: MIT
import assert from "node:assert/strict"
import { mkdir } from "node:fs/promises"
import { pathToFileURL } from "node:url"
import { convertCircuitJsonToDrawingSvg } from "@tscircuit/circuit-json-to-drawing"
import { convertCircuitJsonTo3D, convertSceneToGLTF } from "circuit-json-to-gltf"
import { mat4 } from "gl-matrix"
import { bufferFromDataURI, createSceneFromGLTF } from "poppygl"
import type { CircuitJson } from "tscircuit"

const dist = "dist/assembly"
const circuit: CircuitJson = await Bun.file(`${dist}/circuit.json`).json()
const board = circuit.find((part) => part.type === "pcb_board")
assert(board, "The assembly must contain the F1 PCB")

const scene = await convertCircuitJsonTo3D(circuit, {
  projectBaseUrl: pathToFileURL(`${process.cwd()}/`).href,
  boardThickness: board.thickness,
  renderBoardTextures: false,
  showBoundingBoxes: false,
})
const gltf = await convertSceneToGLTF(scene, { binary: false }) as {
  scene: number
  scenes: { nodes: number[] }[]
  nodes: { name: string; extras?: { tscircuit?: { source_component_id?: string } } }[]
  buffers: { uri: string }[]
}
const resources = {
  buffers: gltf.buffers.map(({ uri }) => bufferFromDataURI(uri)),
  images: [],
}
// glTF exports (-X, Z, Y); the drawing renderer uses (X, Z, -Y).
const canonical = mat4.fromYRotation(mat4.create(), Math.PI)
const parts = gltf.scenes[gltf.scene].nodes.map((index) => {
  const node = gltf.nodes[index]
  const source = node.extras?.tscircuit?.source_component_id
  assert(source || ["Box0", "Via Plating"].includes(node.name), `Unmapped model: ${node.name}`)
  const { drawCalls } = createSceneFromGLTF({ ...gltf, scene: 0, scenes: [{ nodes: [index] }] }, resources)
  return {
    ...(source ? { source_component_id: source } : { pcb_board_id: board.pcb_board_id }),
    drawCalls: drawCalls.map((call) => ({
      ...call,
      model: mat4.multiply(mat4.create(), canonical, call.model),
    })),
  }
})

const svg = await convertCircuitJsonToDrawingSvg(circuit, { resolveSceneParts: async () => parts })
const output = `${dist}/drawings/classic-f1-service.svg`
await mkdir(`${dist}/drawings`, { recursive: true })
await Bun.write(output, svg)
console.log(`Wrote ${output}`)