ShiboSoftwareDev/nema8-20mm-dual-usbc-stepper-controller

A compact PCB component set comprising a pushbutton, logic buffer, Schottky diodes, USB connector, resettable fuse, microcontroller, voltage regulator, and polarized capacitors.

Version
1.0.35
License
unset
Stars
0

scripts/find-local-placement.mjs

import fs from "node:fs"
import { runAllPlacementChecks } from "@tscircuit/checks"

const [targetName, xMinText, xMaxText, yMinText, yMaxText, stepText = "0.25"] = process.argv.slice(2)
if (!targetName) throw new Error("usage: node scripts/find-local-placement.mjs COMPONENT X_MIN X_MAX Y_MIN Y_MAX [STEP]")

const [xMin, xMax, yMin, yMax, step] = [xMinText, xMaxText, yMinText, yMaxText, stepText].map(Number)
const circuit = JSON.parse(fs.readFileSync("dist/index/circuit.json", "utf8"))
  .filter((element) => !String(element.type).endsWith("error"))
const sourceById = new Map(circuit
  .filter((element) => element.type === "source_component")
  .map((element) => [element.source_component_id, element]))
const componentById = new Map(circuit
  .filter((element) => element.type === "pcb_component")
  .map((element) => [element.pcb_component_id, element]))
const nameForPcbId = (id) => sourceById.get(componentById.get(id)?.source_component_id)?.name
const target = [...componentById.values()].find((component) => nameForPcbId(component.pcb_component_id) === targetName)
if (!target) throw new Error(`missing ${targetName}`)

const moved = (x, y) => circuit.map((element) => {
  if (element.pcb_component_id !== target.pcb_component_id) return element
  const dx = x - target.center.x
  const dy = y - target.center.y
  if (element.type === "pcb_component") return { ...element, center: { x, y } }
  if (typeof element.x === "number" && typeof element.y === "number") {
    return { ...element, x: element.x + dx, y: element.y + dy }
  }
  if (element.center) return { ...element, center: { x: element.center.x + dx, y: element.center.y + dy } }
  if (Array.isArray(element.outline)) return {
    ...element,
    outline: element.outline.map((point) => ({ ...point, x: point.x + dx, y: point.y + dy })),
  }
  return element
})

const legal = []
for (let y = yMin; y <= yMax + 1e-9; y += step) {
  for (let x = xMin; x <= xMax + 1e-9; x += step) {
    const cx = Number(x.toFixed(3))
    const cy = Number(y.toFixed(3))
    const errors = await runAllPlacementChecks(moved(cx, cy))
    const targetErrors = errors.filter((error) =>
      error.pcb_component_id === target.pcb_component_id ||
      error.pcb_component_ids?.includes(target.pcb_component_id) ||
      JSON.stringify(error).includes(targetName))
    if (targetErrors.length === 0) legal.push([cx, cy])
  }
}

console.log(JSON.stringify({ targetName, layer: target.layer, rotation: target.rotation, legal }))