feat(pcb): 회로도 자동 배선을 실제 연결선으로 — 신호 넷은 1.27mm 격자 A* 로 부품 몸체를 피해 직각 배선(다른 넷과는 직각 교차만), GND·전원은 기호, 배선이 다른 넷과 합쳐지면 그 넷만 이름표 방식으로 되돌림(연결 검증), 핀 시작선 자리 선예약
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
11a8536d9d
commit
848979b57b
1 file changed
+81
-12
+81
-12
@@ -997,27 +997,96 @@ function schDelete(){
|
||||
ssel=null; if(schDriven()) schCommit(); else {schDerive();changed();}
|
||||
}
|
||||
|
||||
// 기판/예제의 연결(넷)을 회로도에 짧은 선 + 넷 이름(전원은 전원 기호)으로 자동 생성한다.
|
||||
|
||||
// ── 회로도 자동 배선기: 1.27mm 격자 A* (부품 몸체 회피, 꺾임 최소화, 다른 넷과는 직각 교차만 허용) ──
|
||||
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;
|
||||
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 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)]];
|
||||
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]);}
|
||||
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();
|
||||
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;
|
||||
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]);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return {route,mark};
|
||||
}
|
||||
|
||||
// 기판/예제의 연결(넷)을 회로도에 자동으로 그린다.
|
||||
// · GND·전원(VIN/VOUT/VCC…) 넷 → 핀마다 짧은 선 + 전원 기호
|
||||
// · 그 밖의 넷 → 핀들을 최소 신장 트리로 이은 직각 배선(부품 몸체를 피해 꺾음) + 넷 이름표 1개
|
||||
// · 배선이 다른 넷을 잘못 건드려 연결이 달라지면 그 넷만 짧은 선 + 이름표 방식으로 되돌린다
|
||||
function schFromNets(){
|
||||
ensureSch(); ensurePlaced(); schDerive(); const S=P.sch, pinGeo={}; let n=0;
|
||||
window.schFails=[]; ensureSch(); ensurePlaced(); schDerive(); const S=P.sch, pinGeo={}; let nWire=0, nStub=0, nFall=0;
|
||||
for(const part of P.parts){
|
||||
const pos=sPos(part.ref), g=partGfx(part); if(!pos||!g||g.loading) continue; const lay=layoutOf(g);
|
||||
g.units.forEach((u,i)=>u.pins.forEach(p=>{
|
||||
const id=part.ref+'-'+p.n; if(pinGeo[id]) return; const a=(p.angle+180)*Math.PI/180;
|
||||
const [x,y]=sxf(pos,p.x,p.y-lay.offs[i]), [x2,y2]=sxf(pos,p.x+Math.cos(a),p.y+Math.sin(a)-lay.offs[i]);
|
||||
pinGeo[id]={x:r2(x),y:r2(y),dx:Math.round(x2-x),dy:Math.round(y2-y)};
|
||||
pinGeo[id]={id,x:r2(x),y:r2(y),dx:Math.round(x2-x),dy:Math.round(y2-y)};
|
||||
}));
|
||||
}
|
||||
const done=new Set(sres.conn);
|
||||
for(const net of P.nets) for(const id of net.pins){
|
||||
const g=pinGeo[id]; if(!g||done.has(id)) continue; const L=5.08, e=[r2(g.x+g.dx*L),r2(g.y+g.dy*L)];
|
||||
S.wires.push({pts:[[g.x,g.y],e]});
|
||||
const gnd=/^(gnd|vss|0v|agnd|dgnd)$/i.test(net.name), sup=/^(vin|vout|vcc|vdd|[+-]\d)/i.test(net.name), dir=g.dx<0?'l':g.dx>0?'r':g.dy<0?'u':'d';
|
||||
(gnd||sup?S.power:S.labels).push({x:e[0],y:e[1],name:net.name,dir}); n++;
|
||||
const done=new Set(sres.conn), dirOf=g=>g.dx<0?'l':g.dx>0?'r':g.dy<0?'u':'d';
|
||||
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);
|
||||
// 모든 핀의 시작 선(스터브) 자리를 먼저 예약해, 먼저 그린 다른 넷의 선이 그 자리를 지나가지 못하게 한다
|
||||
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;
|
||||
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);}
|
||||
}
|
||||
schCommit(); return n;
|
||||
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; }
|
||||
// 최소 신장 트리(스터브 끝 사이 맨해튼 거리)
|
||||
const L=2.54, end=id=>{const g=pinGeo[id];return [g.x+g.dx*L,g.y+g.dy*L];}, used=[ids[0]], rest=ids.slice(1), edges=[];
|
||||
while(rest.length){let bi=null,bj=null,bd=1e9;for(const i of used)for(const j of rest){const a=end(i),b=end(j),d=Math.abs(a[0]-b[0])+Math.abs(a[1]-b[1]);if(d<bd){bd=d;bi=i;bj=j;}}edges.push([bi,bj]);used.push(bj);rest.splice(rest.indexOf(bj),1);}
|
||||
const mark={w:S.wires.length,l:S.labels.length,p:S.power.length}, added=[];
|
||||
for(const [i,j] of edges){
|
||||
const A=pinGeo[i],B=pinGeo[j],A2=[r2(A.x+A.dx*L),r2(A.y+A.dy*L)],B2=[r2(B.x+B.dx*L),r2(B.y+B.dy*L)];
|
||||
let mid=(onGrid(A2[0])&&onGrid(A2[1])&&onGrid(B2[0])&&onGrid(B2[1]))?router.route(A2,B2,net.name):null;
|
||||
if(!mid) mid=ortho([A2,B2]);
|
||||
const pts=dedupe([[A.x,A.y],...mid,[B.x,B.y]]); S.wires.push({pts}); added.push(pts); router.mark(pts,net.name);
|
||||
}
|
||||
const g0=pinGeo[ids[0]]; S.labels.push({x:r2(g0.x+g0.dx*L),y:r2(g0.y+g0.dy*L),name:net.name,dir:dirOf(g0)});
|
||||
// 검증: 이 넷의 핀들이 정확히 하나의 연결로 묶이고 다른 핀이 섞이지 않았는가
|
||||
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+=added.length; continue; }
|
||||
(window.schFails=window.schFails||[]).push({net:net.name,ids,got:hit?hit.pins:null});
|
||||
S.wires.length=mark.w; S.labels.length=mark.l; S.power.length=mark.p; nFall++; stubs(net,ids);
|
||||
}
|
||||
schCommit(); return {wire:nWire,stub:nStub,fallback:nFall};
|
||||
}
|
||||
function schAutoWires(){ if(!P.nets.length){$('hint').innerHTML='<span class="warn">연결(넷)이 없습니다.</span>';return;} const n=schFromNets(); $('hint').textContent=`연결선 ${n}개를 만들었습니다. 부품 배치는 끌어서 정리하세요.`; schFit(); }
|
||||
function schAutoWires(){ if(!P.nets.length){$('hint').innerHTML='<span class="warn">연결(넷)이 없습니다.</span>';return;} const r=schFromNets(); $('hint').textContent=`배선 ${r.wire}개, 전원·이름표 연결 ${r.stub}개를 만들었습니다${r.fallback?` (겹침 때문에 ${r.fallback}개 넷은 이름표 방식)`:''}. 부품을 끌어 배치를 정리하세요.`; schFit(); }
|
||||
function setTab(t){
|
||||
tab=t; $('t-pcb').classList.toggle('on',t==='pcb'); $('t-sch').classList.toggle('on',t==='sch');
|
||||
$('bar').style.display=t==='pcb'?'flex':'none'; $('sbar').style.display=t==='sch'?'flex':'none';
|
||||
@@ -1094,7 +1163,7 @@ async function loadExample(id){
|
||||
if(id==='basic'){ loadSample(true); return; }
|
||||
const E=id==='smps_fb'?EX_SMPS_FB:EX_SMPS;
|
||||
P={board:{...E.board},parts:E.parts.map(x=>({ref:x.ref,fp:x.fp,sym:x.sym,value:x.value,x:x.pcb[0],y:x.pcb[1],rot:x.pcb[2]})),nets:E.nets.map(n=>({name:n.name,pins:[...n.pins]})),fps:{},
|
||||
sch:{pos:Object.fromEntries(E.parts.map(x=>[x.ref,{x:x.sch[0],y:x.sch[1],rot:x.sch[2]||0,mir:!!x.sch[3]}])),wires:[],labels:[],power:[]},
|
||||
sch:{pos:Object.fromEntries(E.parts.map(x=>[x.ref,{x:r2(Math.round(x.sch[0]/2.54)*2.54),y:r2(Math.round(x.sch[1]/2.54)*2.54),rot:x.sch[2]||0,mir:!!x.sch[3]}])),wires:[],labels:[],power:[]},
|
||||
sim:JSON.parse(JSON.stringify(E.sim)),rules:{...DEF_RULES,trace:0.6,clearance:0.4},mill:{...DEF_MILL}};
|
||||
for(const k of Object.keys(P.parts[0])) if(P.parts[0][k]===undefined) delete P.parts[0][k];
|
||||
routed=null;gres=null;sel=null;pending=null;simRes=null;$('simpane').style.display='none';
|
||||
|
||||
Reference in new issue
Block a user