mohan-bee/tscircuit-robot-vacuum

A tscircuit package created by mohan-bee.

Version
1.0.0
License
unset
Stars
0

capability-audit/routing-probe.tsx

PCBPCB preview for capability-audit/routing-probe.tsx
SchematicSchematic preview for capability-audit/routing-probe.tsx
import fs from "node:fs"
import React from "react"
import { Circuit } from "tscircuit"
import { MainBoard } from "../robot-electronics"

const circuit = new Circuit()
circuit.schematicDisabled = true
const started = Date.now()
const forced = process.env.ROUTE_REMAINING === "1"
const outputPrefix = forced ? "capability-audit/routing-forced" : "capability-audit/routing-basic"
let saved = false
const events: any[] = []
const stats = () => {
  const json = circuit.getCircuitJson()
  const counts: Record<string, number> = {}
  for (const el of json) counts[el.type] = (counts[el.type] ?? 0) + 1
  return { elapsedMs: Date.now() - started, counts }
}
const save = (status: string, reason?: string) => {
  if (saved) return
  saved = true
  const json = circuit.getCircuitJson()
  const counts = stats().counts
  const summary = { status, reason, forced, routingSucceeded: !(counts.pcb_autorouting_error ?? 0) && !(counts.pcb_port_not_connected_error ?? 0), ...stats(), events }
  fs.writeFileSync(`${outputPrefix}.circuit.json`, JSON.stringify(json, null, 2))
  fs.writeFileSync(`${outputPrefix}-summary.json`, JSON.stringify(summary, null, 2))
  console.log(JSON.stringify({ summary }))
}
for (const eventName of ["board:renderPhaseStarted", "autorouting:start", "autorouting:progress", "autorouting:end", "autorouting:error"] as const) {
  circuit.on(eventName, (event: any) => {
    const record = { name: eventName, elapsedMs: Date.now()-started, keys: Object.keys(event), message:event.message, error:event.error, progress:event.progress, phase:event.phase, solverName:event.solverName, autorouterName:event.autorouterName, phaseName:event.phaseName, connectionCount:event.connectionCount, layerCount:event.layerCount }
    events.push(record)
    console.log(JSON.stringify(record))
    if (eventName === "autorouting:start") fs.writeFileSync(`${outputPrefix}-start-event.json`, JSON.stringify(event, null, 2))
  })
}
process.on("SIGINT", () => { save("timeout", "External 60-second routing experiment limit reached"); process.exit(124) })
const board = MainBoard({ routingDisabled: false })
circuit.add(forced ? React.cloneElement(board, { routeRemaining: true }) : board)
try {
  await circuit.renderUntilSettled()
  save("completed")
} catch (err: any) {
  save("failed", String(err?.stack ?? err))
  process.exitCode = 1
}