hrithik18k/punck-tscircuit-recreation
A four-layer Eurorack drum/sampler controller PCB built around an STM32H743, dual QSPI flash, PCM5100 stereo DAC, TL074 analog audio circuitry, input clamps, 12V-to-3.3V power regulation, MIDI/USB/SWD connectors, controls, LEDs, and trigger/audio I/O.
- Version
- 1.0.3
- License
- unset
- Stars
- 0
scripts/repair-native-vias.ts
import fs from 'node:fs'
import {runAllChecks} from '@tscircuit/checks'
import {convertCircuitJsonToPcbSvg} from 'circuit-to-svg'
import sharp from 'sharp'
const path='dist/native/components/circuit.json'
const raw=JSON.parse(fs.readFileSync(path,'utf8'))
const oldErrors=raw.filter((e:any)=>e.type.endsWith('_error'))
if(oldErrors.some((e:any)=>e.type!=='pcb_placement_error'||!/^Via hole at .* overlaps SMD pad (U2\.PA12|C41\.pin2) /.test(e.message)))throw Error('Unexpected build errors; refusing automatic repair')
fs.writeFileSync('evidence/native-autorouter-raw.circuit.json',JSON.stringify(raw,null,2))
const j=raw.filter((e:any)=>!e.type.endsWith('_error'))
const repairs=[]
// These moves were individually checked against this board geometry, including inner layers.
// Refuse unexpected geometry instead of applying guesses after future layout/router changes.
const known=[{x:6.717216735697619,y:-3.5702938335914167,toX:6.75,toY:-2.61},
{x:-28.465895499340757,y:40.79999854749832,toX:-28.45,toY:42.30}]
for(const m of known){
const v=j.find((e:any)=>e.type==='pcb_via'&&Math.hypot(e.x-m.x,e.y-m.y)<1e-6)
if(!v)continue
for(const t of j.filter((e:any)=>e.type==='pcb_trace'))for(const p of t.route)if(Math.hypot(p.x-v.x,p.y-v.y)<1e-6){p.x=m.toX;p.y=m.toY}
repairs.push({via:v.pcb_via_id,from:{x:v.x,y:v.y},to:{x:m.toX,y:m.toY}})
v.x=m.toX;v.y=m.toY
}
const checks=await runAllChecks(j)
fs.writeFileSync('evidence/native-repair-checks.json',JSON.stringify(checks,null,2))
fs.writeFileSync('evidence/native-via-repairs.json',JSON.stringify(repairs,null,2))
const errors=checks.filter((e:any)=>e.type.endsWith('_error'))
if(errors.length){console.error(errors);throw Error('Post-route repair failed validation; original output retained')}
fs.writeFileSync(path,JSON.stringify(j,null,2))
const svg=convertCircuitJsonToPcbSvg(j)
fs.writeFileSync('dist/native/components/pcb.svg',svg)
await sharp(Buffer.from(svg)).resize({width:1400}).png().toFile('dist/native/components/pcb.png')
console.log(`Repaired ${repairs.length} autorouted vias; all checks: 0 errors, ${checks.length} warnings. See evidence/native-repair-checks.json.`)