60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
import { readFileSync } from 'node:fs';
|
|
const { compData, wireData, pinData } = JSON.parse(readFileSync('H:/caiic_workspace/mothercup/hardware_design/_dump.json', 'utf8'));
|
|
|
|
function connPoint(p) {
|
|
const len = p.len || 20;
|
|
switch (p.rot) {
|
|
case 0: return { x: p.x - len, y: p.y }; // pin points left
|
|
case 90: return { x: p.x, y: p.y - len }; // pin points up
|
|
case 180: return { x: p.x + len, y: p.y }; // pin points right
|
|
case 270: return { x: p.x, y: p.y + len }; // pin points down
|
|
default: return { x: p.x, y: p.y };
|
|
}
|
|
}
|
|
|
|
function onSeg(px, py, ax, ay, bx, by, tol = 4) {
|
|
const cr = (bx - ax) * (py - ay) - (by - ay) * (px - ax);
|
|
if (Math.abs(cr) > tol * 20) return false;
|
|
return px >= Math.min(ax, bx) - tol && px <= Math.max(ax, bx) + tol &&
|
|
py >= Math.min(ay, by) - tol && py <= Math.max(ay, by) + tol;
|
|
}
|
|
function wpts(line) {
|
|
if (!Array.isArray(line)) return [];
|
|
if (Array.isArray(line[0])) return line.flat();
|
|
return line;
|
|
}
|
|
|
|
// net flags (components with no designator but a name)
|
|
const netflags = compData.filter(c => (!c.d || c.d.trim()==='') && c.n);
|
|
|
|
function netsAt(pt) {
|
|
const s = new Set();
|
|
for (const w of wireData) {
|
|
const p = wpts(w.line);
|
|
for (let i = 0; i + 3 < p.length; i += 2) {
|
|
if (onSeg(pt.x, pt.y, p[i], p[i+1], p[i+2], p[i+3])) if (w.net) s.add(w.net);
|
|
}
|
|
}
|
|
for (const f of netflags) {
|
|
if (Math.abs(f.x - pt.x) <= 8 && Math.abs(f.y - pt.y) <= 8 && f.n) s.add(f.n);
|
|
}
|
|
return [...s];
|
|
}
|
|
|
|
console.log('=== key component pin -> net ===');
|
|
for (const k of Object.keys(pinData)) {
|
|
const pd = pinData[k];
|
|
if (!pd) { console.log(`${k}: (not found)`); continue; }
|
|
console.log(`${k} (${pd.name}) @ (${pd.pos.x},${pd.pos.y})`);
|
|
for (const p of pd.pins) {
|
|
const pt = connPoint(p);
|
|
const nets = netsAt(pt);
|
|
console.log(` pin ${p.num} ${p.name||''} rot=${p.rot} len=${p.len} -> ${JSON.stringify(nets)}`);
|
|
}
|
|
}
|
|
|
|
// all components with positions (sorted by x then y) to see layout
|
|
console.log('\n=== all components (d:name @x,y) ===');
|
|
const sorted = [...compData].filter(c=>c.d && c.d.trim()).sort((a,b)=>a.x-b.x);
|
|
for (const c of sorted) console.log(` ${c.d}:${c.n||''} @ (${c.x},${c.y})`);
|