feat(pcb): 회로도 전원 레일 — 핀 3개 이상인 VIN/VOUT/VCC 넷은 시트 위에 가로 모선 하나로 모으고 기호는 모선 끝 한 곳에. 라우터 장애물을 부품 전체 상자 대신 유닛별 몸체+핀 선으로 정밀화(쌓인 유닛 사이 핀 갇힘 수정)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-10-04 12:37:16 +09:00
1 parent f5fe479202
commit bc946e8ecd
1 file changed
+50 -12
+50 -12
View File
@@ -1006,36 +1006,49 @@ const RG=1.27, onGrid=v=>Math.abs(v/RG-Math.round(v/RG))<0.02;
function makeRouter(){
const key=(x,y)=>x+','+y, blocked=new Set(), occ=new Map();
let bx0=1e9,by0=1e9,bx1=-1e9,by1=-1e9;
for(const part of P.parts){const b=partBoxS(part); if(!b) continue;
// 장애물: 부품 전체 상자가 아니라 유닛별 몸체 그림 + 핀 선만 막는다(쌓인 유닛 사이 빈틈의 핀도 나갈 수 있게)
const blockRect=(x0,y0,x1,y1,m)=>{for(let ix=Math.ceil((x0-m)/RG);ix<=Math.floor((x1+m)/RG);ix++)for(let iy=Math.ceil((y0-m)/RG);iy<=Math.floor((y1+m)/RG);iy++)blocked.add(key(ix,iy));};
for(const part of P.parts){
const b=partBoxS(part); if(!b) continue;
bx0=Math.min(bx0,b.x0);by0=Math.min(by0,b.y0);bx1=Math.max(bx1,b.x1);by1=Math.max(by1,b.y1);
for(let ix=Math.ceil((b.x0-0.8)/RG);ix<=Math.floor((b.x1+0.8)/RG);ix++)for(let iy=Math.ceil((b.y0-0.8)/RG);iy<=Math.floor((b.y1+0.8)/RG);iy++)blocked.add(key(ix,iy));}
const B={x0:Math.floor(bx0/RG)-10,y0:Math.floor(by0/RG)-10,x1:Math.ceil(bx1/RG)+10,y1:Math.ceil(by1/RG)+10};
const pos=sPos(part.ref), g=partGfx(part); if(!g||g.loading||!pos){blockRect(b.x0,b.y0,b.x1,b.y1,0.8);continue;}
const lay=layoutOf(g);
g.units.forEach((u,i)=>{
let x0=1e9,y0=1e9,x1=-1e9,y1=-1e9; const add=(lx,ly)=>{const [a,c]=sxf(pos,lx,ly-lay.offs[i]);x0=Math.min(x0,a);x1=Math.max(x1,a);y0=Math.min(y0,c);y1=Math.max(y1,c);};
u.rects.forEach(r=>{add(r.x0,r.y0);add(r.x1,r.y1);});u.lines.forEach(l=>l.pts.forEach(q=>add(q[0],q[1])));u.circles.forEach(c=>{add(c.x-c.r,c.y-c.r);add(c.x+c.r,c.y+c.r);});u.arcs.forEach(a=>{add(a.s[0],a.s[1]);add(a.m[0],a.m[1]);add(a.e[0],a.e[1]);});
if(x0<=x1) blockRect(x0,y0,x1,y1,0.8);
for(const pn of u.pins){const a=pn.angle*Math.PI/180,[ax,ay]=sxf(pos,pn.x,pn.y-lay.offs[i]),[ex,ey]=sxf(pos,pn.x+pn.length*Math.cos(a),pn.y+pn.length*Math.sin(a)-lay.offs[i]);
const n=Math.max(1,Math.ceil(Math.hypot(ex-ax,ey-ay)/0.4)); for(let t=0;t<=n;t++)blockRect(ax+(ex-ax)*t/n,ay+(ey-ay)*t/n,ax+(ex-ax)*t/n,ay+(ey-ay)*t/n,0.4);}
});
}
const B={x0:Math.floor(bx0/RG)-16,y0:Math.floor(by0/RG)-16,x1:Math.ceil(bx1/RG)+16,y1:Math.ceil(by1/RG)+16};
const mark=(pts,net)=>{for(let i=1;i<pts.length;i++){const a=pts[i-1],b=pts[i],ax=Math.round(a[0]/RG),ay=Math.round(a[1]/RG),bx=Math.round(b[0]/RG),by=Math.round(b[1]/RG);
const horiz=ay===by, n=Math.max(Math.abs(bx-ax),Math.abs(by-ay)), sx=Math.sign(bx-ax), sy=Math.sign(by-ay);
for(let t=0;t<=n;t++){const k=key(ax+sx*t,ay+sy*t),end=t===0||t===n,o=occ.get(k);occ.set(k,{net,o:end||(o&&o.net===net&&o.o!==(horiz?'h':'v'))?'x':(horiz?'h':'v')});}}};
for(const w of P.sch.wires) mark(w.pts,'#ext');
for(const l of [...P.sch.labels,...P.sch.power]) occ.set(key(Math.round(l.x/RG),Math.round(l.y/RG)),{net:'#ext',o:'x'});
const DX=[1,0,-1,0], DY=[0,1,0,-1];
function route(a,b,net){
const sx=Math.round(a[0]/RG),sy=Math.round(a[1]/RG),tx=Math.round(b[0]/RG),ty=Math.round(b[1]/RG);
if(sx===tx&&sy===ty) return [[r2(sx*RG),r2(sy*RG)]];
function route(a,b,net,rail){
const sx=Math.round(a[0]/RG),sy=Math.round(a[1]/RG),tx=b?Math.round(b[0]/RG):0,ty=b?Math.round(b[1]/RG):0;
const atGoal=(x,y)=>rail?(y===rail.iy&&x>=rail.ix0&&x<=rail.ix1):(x===tx&&y===ty), hOf=(x,y)=>rail?Math.abs(y-rail.iy):Math.abs(x-tx)+Math.abs(y-ty);
if(atGoal(sx,sy)) return [[r2(sx*RG),r2(sy*RG)]];
const heap=[], push=(f,v)=>{heap.push([f,v]);let i=heap.length-1;while(i>0){const p=(i-1)>>1;if(heap[p][0]<=heap[i][0])break;[heap[p],heap[i]]=[heap[i],heap[p]];i=p;}};
const pop=()=>{const top=heap[0],last=heap.pop();if(heap.length){heap[0]=last;let i=0;for(;;){let l=2*i+1,r=l+1,m=i;if(l<heap.length&&heap[l][0]<heap[m][0])m=l;if(r<heap.length&&heap[r][0]<heap[m][0])m=r;if(m===i)break;[heap[m],heap[i]]=[heap[i],heap[m]];i=m;}}return top;};
const gs=new Map(), from=new Map(), sk=(x,y,d)=>x+','+y+','+d;
for(let d=0;d<4;d++){gs.set(sk(sx,sy,d),0);push(Math.abs(sx-tx)+Math.abs(sy-ty),[sx,sy,d]);}
for(let d=0;d<4;d++){gs.set(sk(sx,sy,d),0);push(hOf(sx,sy),[sx,sy,d]);}
let guard=0;
while(heap.length&&guard++<250000){
const [,[x,y,d]]=pop(), g=gs.get(sk(x,y,d));
if(x===tx&&y===ty){const pts=[];let k=sk(x,y,d);while(k){const q=k.split(',').map(Number);pts.push([r2(q[0]*RG),r2(q[1]*RG)]);k=from.get(k);}pts.reverse();
if(atGoal(x,y)){const pts=[];let k=sk(x,y,d);while(k){const q=k.split(',').map(Number);pts.push([r2(q[0]*RG),r2(q[1]*RG)]);k=from.get(k);}pts.reverse();
const out=[pts[0]];for(let i=1;i<pts.length-1;i++){const a2=pts[i-1],b2=pts[i],c2=pts[i+1];if((b2[0]-a2[0])*(c2[1]-b2[1])-(b2[1]-a2[1])*(c2[0]-b2[0])!==0)out.push(b2);}out.push(pts[pts.length-1]);return out;}
const here=occ.get(key(x,y)), cross=here&&here.net!==net;
for(let nd=0;nd<4;nd++){
if((nd+2)%4===d) continue; if(cross&&nd!==d) continue;
const nx=x+DX[nd],ny=y+DY[nd]; if(nx<B.x0||nx>B.x1||ny<B.y0||ny>B.y1) continue;
const nk=key(nx,ny), isGoal=nx===tx&&ny===ty; if(!isGoal&&blocked.has(nk)) continue;
const nk=key(nx,ny), isGoal=atGoal(nx,ny); if(!isGoal&&blocked.has(nk)) continue;
let c=1+(nd!==d?4:0); const oc=occ.get(nk);
if(oc&&oc.net!==net){ if(oc.o==='x') continue; const mv=nd%2===0?'h':'v'; if(oc.o===mv) continue; c+=10; }
const ng=g+c, ks=sk(nx,ny,nd); if(gs.has(ks)&&gs.get(ks)<=ng) continue; gs.set(ks,ng); from.set(ks,sk(x,y,d)); push(ng+Math.abs(nx-tx)+Math.abs(ny-ty),[nx,ny,nd]);
const ng=g+c, ks=sk(nx,ny,nd); if(gs.has(ks)&&gs.get(ks)<=ng) continue; gs.set(ks,ng); from.set(ks,sk(x,y,d)); push(ng+hOf(nx,ny),[nx,ny,nd]);
}
}
return null;
@@ -1061,12 +1074,37 @@ function schFromNets(){
const isGnd=n=>/^(gnd|vss|0v|agnd|dgnd)$/i.test(n), isSup=n=>/^(vin|vout|vcc|vdd|[+-]\d)/i.test(n);
const stubs=(net,ids)=>{ for(const id of ids){const g=pinGeo[id]; if(!g) continue; const e=[r2(g.x+g.dx*5.08),r2(g.y+g.dy*5.08)];
S.wires.push({pts:[[g.x,g.y],e]}); (isGnd(net.name)||isSup(net.name)?S.power:S.labels).push({x:e[0],y:e[1],name:net.name,dir:dirOf(g)}); nStub++; if(typeof router!=='undefined') router.mark([[g.x,g.y],e],net.name); } };
const router=makeRouter(), nets=[...P.nets].sort((a,b)=>a.pins.length-b.pins.length);
const router=makeRouter();
// 전원 레일: 핀이 3개 이상인 전원 넷(VIN/VOUT/VCC…)은 시트 맨 위에 가로 모선 하나를 두고 핀들을 거기에 잇는다(기호는 모선 왼쪽 끝 한 곳)
let railIdx=0; const railNets=P.nets.filter(n=>isSup(n.name)&&!isGnd(n.name)&&n.pins.filter(id=>pinGeo[id]&&!done.has(id)).length>=3);
let topY=1e9; for(const part of P.parts){const b=partBoxS(part); if(b) topY=Math.min(topY,b.y0);}
// 모든 핀의 시작 선(스터브) 자리를 먼저 예약해, 먼저 그린 다른 넷의 선이 그 자리를 지나가지 못하게 한다
for(const net of P.nets){
const todo=net.pins.filter(id=>pinGeo[id]&&!done.has(id)), long=isGnd(net.name)||isSup(net.name)||todo.length<2, Ls=long?5.08:2.54;
const todo=net.pins.filter(id=>pinGeo[id]&&!done.has(id)), long=isGnd(net.name)||(isSup(net.name)&&!railNets.includes(net))||todo.length<2, Ls=long?5.08:2.54;
for(const id of todo){const g=pinGeo[id]; router.mark([[g.x,g.y],[r2(g.x+g.dx*Ls),r2(g.y+g.dy*Ls)]],net.name);}
}
const nets=[...P.nets].filter(n=>!railNets.includes(n)).sort((a,b)=>a.pins.length-b.pins.length);
for(const net of railNets){
const ids=net.pins.filter(id=>pinGeo[id]&&!done.has(id)), L=2.54, railY=r2(Math.round((topY-5.08-railIdx*7.62)/RG)*RG), riy=Math.round(railY/RG);
const xs=ids.map(id=>pinGeo[id].x+pinGeo[id].dx*L), ix0=Math.floor((Math.min(...xs)-2.54)/RG), ix1=Math.ceil((Math.max(...xs)+2.54)/RG);
const mark={w:S.wires.length,l:S.labels.length,p:S.power.length}, rails=[]; let okAll=true, minX=1e9, maxX=-1e9;
for(const id of ids){
const g=pinGeo[id], A2=[r2(g.x+g.dx*L),r2(g.y+g.dy*L)];
const mid=(onGrid(A2[0])&&onGrid(A2[1]))?router.route(A2,null,net.name,{iy:riy,ix0,ix1}):null;
if(!mid){okAll=false;window.schRailWhy='no-route '+id;break;}
const pts=dedupe([[g.x,g.y],...mid]); rails.push(pts); S.wires.push({pts}); router.mark(pts,net.name);
const e=pts[pts.length-1]; minX=Math.min(minX,e[0]); maxX=Math.max(maxX,e[0]);
}
if(okAll){
const x0=r2(minX-2.54); S.wires.push({pts:[[x0,railY],[r2(maxX),railY]]}); router.mark([[x0,railY],[r2(maxX),railY]],net.name);
S.power.push({x:x0,y:railY,name:net.name,dir:'u'});
const r=schDerive(), hit=r.nets.find(n=>n.pins.includes(ids[0]));
if(hit&&hit.pins.length===ids.length&&ids.every(id=>hit.pins.includes(id))){ nWire+=rails.length+1; railIdx++; continue; }
window.schRailWhy='verify: got '+(hit?hit.pins.join(','):'none');
}
(window.schFails=window.schFails||[]).push({net:net.name,rail:true});
S.wires.length=mark.w; S.labels.length=mark.l; S.power.length=mark.p; nFall++; stubs(net,ids);
}
for(const net of nets){
const ids=net.pins.filter(id=>pinGeo[id]&&!done.has(id)); if(!ids.length) continue;
if(isGnd(net.name)||isSup(net.name)||ids.length<2){ stubs(net,ids); continue; }