MustafaMulla29/rp2040-bldc-motor-controller-new

A USB‑C PD–powered RP2040 BLDC motor controller PCB with 3.3 V logic, MCT8329A sensorless commutation, three MOSFET half-bridges, protected power switching, USB data, controls, and motor connectors.

Version
0.3.82
License
unset
Stars
0

scripts/check-schematic-wires.mjs

/** Check real schematic wires against IC/connector symbol interiors.
 * This checks the compiled artifact, not the authored placement coordinates.
 * Pin stems, symbol graphics and annotation boxes are not electrical wires.
 */
import { readFileSync, writeFileSync, mkdirSync } from "node:fs"

const input = process.argv[2] ?? "dist/index/circuit.json"
const circuit = JSON.parse(readFileSync(input, "utf8"))
const names = new Map(circuit.filter(x => x.type === "source_component").map(x => [x.source_component_id, x.name]))
const sheets = new Map(circuit.filter(x => x.type === "schematic_sheet").map(x => [x.schematic_sheet_id, x.name]))
const boxes = circuit.filter(x => x.type === "schematic_component" && !x.symbol_name && x.is_box_with_pins !== false && x.size)
const traces = circuit.filter(x => x.type === "schematic_trace")

function crossesInterior(a, b, box) {
  const inset = 0.005
  const min = { x: box.center.x - box.size.width / 2 + inset, y: box.center.y - box.size.height / 2 + inset }
  const max = { x: box.center.x + box.size.width / 2 - inset, y: box.center.y + box.size.height / 2 - inset }
  let enter = 0, leave = 1
  for (const axis of ["x", "y"]) {
    const delta = b[axis] - a[axis]
    if (Math.abs(delta) < 1e-10) {
      if (a[axis] < min[axis] || a[axis] > max[axis]) return false
    } else {
      const t1 = (min[axis] - a[axis]) / delta, t2 = (max[axis] - a[axis]) / delta
      enter = Math.max(enter, Math.min(t1, t2))
      leave = Math.min(leave, Math.max(t1, t2))
      if (enter >= leave) return false
    }
  }
  return leave - enter > 1e-8 && Math.hypot(b.x - a.x, b.y - a.y) > 1e-8
}

const crossings = []
for (const box of boxes) for (const trace of traces) {
  if (box.schematic_sheet_id !== trace.schematic_sheet_id) continue
  const edges = trace.edges?.filter(e => crossesInterior(e.from, e.to, box)) ?? []
  if (edges.length) crossings.push({ component: names.get(box.source_component_id), sheet: sheets.get(box.schematic_sheet_id), trace: trace.schematic_trace_id, sourceTrace: trace.source_trace_id, edges })
}
const report = { status: crossings.length ? "FAIL" : "PASS", input, checkedBoxes: boxes.length, crossings }
mkdirSync(".tscircuit/checks", { recursive: true })
writeFileSync(".tscircuit/checks/schematic-wire-interiors.json", JSON.stringify(report, null, 2) + "\n")
console.log(JSON.stringify({ status: report.status, checkedBoxes: boxes.length, crossingCount: crossings.length,
  components: [...new Set(crossings.map(x => x.component))] }))
process.exitCode = crossings.length ? 1 : 0