fix(pcb): 회로도 자동 배선이 선·스텁이 겹쳐 넷을 합치고 넷 목록을 덮어쓰던 문제 — 스텁은 다른 선·핀·이름표에 닿지 않는 길이로, 완료 후 원래 넷과 전체 대조(불일치 시 짧은 스텁으로 재시도, 그래도 안 되면 원래대로 복원)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-10-04 15:28:55 +09:00
1 parent 63ad050970
commit 9b3418f21b
1 file changed
+30 -5
+30 -5
View File
@@ -1121,7 +1121,8 @@ function makeRouter(){
// · GND·전원(VIN/VOUT/VCC…) 넷 → 핀마다 짧은 선 + 전원 기호
// · 그 밖의 넷 → 핀들을 최소 신장 트리로 이은 직각 배선(부품 몸체를 피해 꺾음) + 넷 이름표 1개
// · 배선이 다른 넷을 잘못 건드려 연결이 달라지면 그 넷만 짧은 선 + 이름표 방식으로 되돌린다
function schFromNets(){
function schFromNetsOnce(stubMax){
const expect=window.schExpect||P.nets.map(n=>({name:n.name,pins:[...n.pins]}));
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);
@@ -1133,7 +1134,15 @@ function schFromNets(){
}
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)];
// 스터브는 이미 그려진 다른 선·핀·이름표에 닿지 않는 가장 긴 길이를 쓴다(세로로 쌓인 부품의 스터브가 겹쳐 넷이 합쳐지던 문제)
const segDist=(a,b,c,d)=>{const ccw=(p,q,r)=>(r[1]-p[1])*(q[0]-p[0])-(q[1]-p[1])*(r[0]-p[0]);if(ccw(a,c,d)*ccw(b,c,d)<0&&ccw(a,b,c)*ccw(a,b,d)<0)return 0;return Math.min(distSeg(a[0],a[1],c,d),distSeg(b[0],b[1],c,d),distSeg(c[0],c[1],a,b),distSeg(d[0],d[1],a,b));};
const stubOk=(id,g,e)=>{const a=[g.x,g.y];
for(const w of S.wires) for(let k=1;k<w.pts.length;k++){const c=w.pts[k-1],d=w.pts[k]; const own=(Math.abs(c[0]-g.x)<.02&&Math.abs(c[1]-g.y)<.02)||(Math.abs(d[0]-g.x)<.02&&Math.abs(d[1]-g.y)<.02); if(own) continue; if(segDist(a,e,c,d)<0.7) return false;}
for(const l of [...S.labels,...S.power]) if(distSeg(l.x,l.y,a,e)<0.7) return false;
for(const q of Object.values(pinGeo)) if(q.id!==id&&distSeg(q.x,q.y,a,e)<0.7) return false;
return true;};
const stubEnd=(id,g)=>{for(const Ls of [5.08,3.81,2.54,1.27]){if(Ls>stubMax)continue;const e=[r2(g.x+g.dx*Ls),r2(g.y+g.dy*Ls)];if(stubOk(id,g,e))return e;}return [r2(g.x+g.dx*1.27),r2(g.y+g.dy*1.27)];};
const stubs=(net,ids)=>{ for(const id of ids){const g=pinGeo[id]; if(!g) continue; const e=stubEnd(id,g);
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();
// 전원 레일: 핀이 3개 이상인 전원 넷(VIN/VOUT/VCC…)은 시트 맨 위에 가로 모선 하나를 두고 핀들을 거기에 잇는다(기호는 모선 왼쪽 끝 한 곳)
@@ -1186,9 +1195,24 @@ function schFromNets(){
(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};
// 최종 검증: 회로도에서 읽은 연결이 원래 넷과 정확히 같아야 한다(선이 서로 닿아 넷이 합쳐지면 실패)
const fin=schDerive(), key=n=>n.pins.slice().sort().join(','), want=new Map(expect.filter(n=>n.pins.length>=2).map(n=>[key(n),n.name])), got=new Set(fin.nets.filter(n=>n.pins.length>=2).map(key));
const bad=[...want.entries()].filter(([k])=>!got.has(k)).map(([,nm])=>nm).concat([...got].filter(k=>!want.has(k)).map(k=>'(합쳐짐: '+k.split(',').length+'핀)'));
if(bad.length) return {ok:false,bad,wire:nWire,stub:nStub,fallback:nFall};
schCommit(); return {ok:true,wire:nWire,stub:nStub,fallback:nFall};
}
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 schFromNets(){
const snapSch=JSON.stringify(P.sch), snapNets=JSON.stringify(P.nets); window.schReverted=null;
for(const mx of [5.08,2.54]){
window.schExpect=JSON.parse(snapNets);
const r=schFromNetsOnce(mx);
if(r.ok) return r;
P.sch=JSON.parse(snapSch); P.nets=JSON.parse(snapNets); window.schReverted={bad:r.bad};
}
schDerive(); changed(); return {wire:0,stub:0,fallback:0,reverted:true,bad:window.schReverted.bad};
}
function schAutoWires(){ if(!P.nets.length){$('hint').innerHTML='<span class="warn">연결(넷)이 없습니다.</span>';return;} const r=schFromNets(); if(r.reverted){$('hint').innerHTML='<span class="warn">⚠ 자동 배선이 선을 서로 닿게 해 넷이 합쳐져서 취소했습니다(원래 회로도·연결 그대로): '+esc(r.bad.slice(0,4).join(', '))+'. 부품을 좀 더 벌려 놓고 다시 시도하세요.</span>';return;} $('hint').textContent=`배선 ${r.wire}개, 전원·이름표 연결 ${r.stub}개를 만들었습니다${r.fallback?` (겹침 때문에 ${r.fallback}개 넷은 이름표 방식)`:''}. 부품을 끌어 배치를 정리하세요.`; schFit(); }
function setTab(t){
if(tab==='cons'&&t!=='cons'&&typeof closeConsoleTab==='function') closeConsoleTab();
tab=t; $('t-pcb').classList.toggle('on',t==='pcb'); $('t-sch').classList.toggle('on',t==='sch'); $('t-cons').classList.toggle('on',t==='cons');
@@ -1361,9 +1385,10 @@ window.addEventListener('keydown',e=>{
function schRewire(){
ensureSch(); if(!P.nets.length){$('hint').innerHTML='<span class="warn">연결(넷)이 없습니다.</span>';return;}
if(schDriven()&&!confirm('회로도의 선·이름표·전원 기호를 모두 지우고 다시 배선합니다. (부품 위치와 연결은 그대로, 실행 취소로 되돌릴 수 있습니다)')) return;
const keep=P.nets.map(n=>({name:n.name,pins:[...n.pins]}));
const keep=P.nets.map(n=>({name:n.name,pins:[...n.pins]})), before=JSON.stringify(P.sch);
P.sch.wires=[];P.sch.labels=[];P.sch.power=[];ssel=null;P.nets=keep;
schAutoWires();
if(window.schReverted){ P.sch=JSON.parse(before); P.nets=keep; schDerive(); changed(); drawSch(); } // 실패하면 다시 배선 전 모습 그대로
}
// ───────────── 프로젝트: 사용자 작업 폴더에 서버 저장 ─────────────