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 12:40:50 +09:00
1 parent bc946e8ecd
commit 6633d85bba
1 file changed
+15 -5
+15 -5
View File
@@ -231,7 +231,13 @@ function loadSample(skipAsk){
rules:{...DEF_RULES},mill:{...DEF_MILL}};
routed=null;gres=null;sel=null;pending=null;histReset();fitView();syncUI();changed(true);
}
function save(){try{localStorage.setItem('pcb-project',JSON.stringify(P));}catch(e){}}
// 배선 결과가 지금의 배치·연결·규칙에 대한 것인지 판별하는 키(djb2)
function pcbKey(){
if(!P) return '';
const t=JSON.stringify({b:P.board,p:P.parts.map(x=>[x.ref,x.fp,x.x,x.y,x.rot]),n:P.nets.map(n=>[n.name,n.pins]),r:[P.rules.trace,P.rules.clearance,P.rules.layers,P.rules.copper,P.rules.viaDia,P.rules.viaDrill]});
let h=5381; for(let i=0;i<t.length;i++) h=((h<<5)+h+t.charCodeAt(i))|0; return String(h);
}
function save(){try{localStorage.setItem('pcb-project',JSON.stringify({...P,routed:routed||null,routedKey:routed?pcbKey():null}));}catch(e){}}
function changed(keepRoute){
if(!keepRoute){ routed=null; gres=null; $('rres').textContent=''; $('res').innerHTML=''; $('dl').innerHTML=''; }
save(); renderNets(); updSel(); draw(); if(tab==='sch') drawSch(); if(typeof renderSim==='function') renderSim();
@@ -486,8 +492,10 @@ function applyMill(){
async function doRoute(){
const b=$('rbtn');b.disabled=true;$('rres').innerHTML='Freerouting 실행 중… (보드에 따라 수 초~수 분)';
try{
const key0=pcbKey();
const d=await api('/api/pcb/route',{project:P,maxPasses:+$('passes').value||30,timeoutSec:+$('tmo').value||120,jumpers:$('jmp').checked});
routed={traces:d.traces,vias:d.vias,jumpers:d.jumpers||[]};gres=null;$('res').innerHTML='';$('dl').innerHTML='';
if(pcbKey()!==key0){$('rres').innerHTML='<span class="warn">배선 도중 배치·연결이 바뀌어 결과를 버렸습니다. 다시 눌러 주세요.</span>';return;}
routed={traces:d.traces,vias:d.vias,jumpers:d.jumpers||[]};routedCache={key:pcbKey(),routed};gres=null;$('res').innerHTML='';$('dl').innerHTML='';
const un=d.stats.unrouted, jn=routed.jumpers.length;
const jl=jn?`<div class="note" style="margin-top:4px">점퍼선(윗면, 부품 구멍 사이 전선):<br>`+routed.jumpers.map((j,i)=>`J${i+1}: ${esc(j.a)} ↔ ${esc(j.b)} · ${j.len}mm${j.smd?' ⚠SMD':''}`).join('<br>')+'</div>':'';
$('rres').innerHTML=(un===0?`<span class="ok">✔ 구리 배선 완료</span> · ${d.traces.length}구간 · 비아 ${d.vias.length}개 · ${d.stats.seconds}초`
@@ -1133,7 +1141,7 @@ function setTab(t){
$('bar').style.display=t==='pcb'?'flex':'none'; $('sbar').style.display=t==='sch'?'flex':'none';
$('cv').style.display=t==='pcb'?'block':'none'; $('scv').style.display=t==='sch'?'block':'none';
if(t==='sch'){ensurePlaced();schDerive();resizeSch();if(!sfit){sfit=true;schFit();}setSMode(smode);}
else{resize();$('hint').textContent='부품을 왼쪽에서 눌러 추가 → 끌어서 배치 → 🔗 핀 연결 모드에서 핀을 차례로 클릭해 연결 → 오른쪽에서 자동 배선';}
else{resize();if(!routed&&P.nets.length&&!$('rbtn').disabled)$('rres').innerHTML='<span class="warn">아직 배선이 없습니다. 위의 "🧭 자동 배선"을 누르세요.</span>';$('hint').textContent='부품을 왼쪽에서 눌러 추가 → 끌어서 배치 → 🔗 핀 연결 모드에서 핀을 차례로 클릭해 연결 → 오른쪽에서 자동 배선';}
}
@@ -1213,11 +1221,12 @@ async function loadExample(id){
await registerFps(); syncUI(); fitView();
setTab('sch'); await new Promise(r=>setTimeout(r,600)); schFromNets(); schFit();
changed(); $('hint').innerHTML='<span class="ok">✔ 예제를 불러왔습니다.</span> '+esc(E.note);
$('rres').innerHTML='자동 배선 준비 중…'; doRoute();
}
// ───────────── 실행 취소 / 다시 실행 ─────────────
// 기판·회로도 편집 상태(보드·부품·넷·규칙·회로도)만 스냅샷으로 기록한다. 시뮬레이션/밀링 설정과 풋프린트 캐시는 되돌리지 않는다.
let hist=[], hidx=-1, histBusy=false;
let hist=[], hidx=-1, histBusy=false, routedCache=null; // routedCache: 마지막 배선 결과와 그 배치 키(실행 취소로 같은 배치가 되면 복원)
const histSnap=()=>JSON.stringify({board:P.board,parts:P.parts,nets:P.nets,rules:P.rules,sch:P.sch});
function histPush(){
if(histBusy||!P) return; const sn=histSnap(); if(hist[hidx]===sn) return;
@@ -1229,6 +1238,7 @@ function histRestore(){
const o=JSON.parse(hist[hidx]); histBusy=true;
P.board=o.board;P.parts=o.parts;P.nets=o.nets;P.rules=o.rules;P.sch=o.sch;
routed=null;gres=null;sel=null;ssel=null;wiring=null;pending=null;
if(routedCache&&routedCache.key===pcbKey()) routed=routedCache.routed; // 배치가 배선했을 때와 같아지면 배선도 복원
syncUI();renderNets();updSel();draw();if(tab==='sch'){schDerive();drawSch();}renderSim();save();
histBusy=false;updUndo();
}
@@ -1253,7 +1263,7 @@ function schRewire(){
try{FP=(await api('/api/pcb/footprints')).footprints;FPM=Object.fromEntries(FP.map(f=>[f.id,f]));}catch(e){$('lib').innerHTML=`<div class="note bad">${esc(e.message)}</div>`;}
try{const s=await api('/api/pcb/status');$('fr').innerHTML=s.installed&&s.java?`<span class="ok">● Freerouting 준비됨</span> (Java ${esc(s.java)})`:`<span class="bad">● Freerouting 미설치</span> — scripts/install-freerouting.sh`;}catch(e){$('fr').textContent='';}
let saved=null;try{saved=JSON.parse(localStorage.getItem('pcb-project')||'null');}catch(e){}
if(saved&&saved.board&&saved.parts){P={board:saved.board,parts:saved.parts,nets:saved.nets||[],fps:saved.fps||{},sch:saved.sch,sim:saved.sim,rules:{...DEF_RULES,...(saved.rules||{})},mill:{...DEF_MILL,...(saved.mill||{})}};syncUI();renderNets();updSel();registerFps();}
if(saved&&saved.board&&saved.parts){P={board:saved.board,parts:saved.parts,nets:saved.nets||[],fps:saved.fps||{},sch:saved.sch,sim:saved.sim,rules:{...DEF_RULES,...(saved.rules||{})},mill:{...DEF_MILL,...(saved.mill||{})}};syncUI();renderNets();updSel();registerFps();if(saved.routed&&saved.routedKey&&saved.routedKey===pcbKey()){routed=saved.routed;routedCache={key:saved.routedKey,routed};}}
else newProject(false);
try{const k=await api('/api/pcb/kicad/status');$('kst').innerHTML=k.installed?`<span class="ok">● ${k.footprints.toLocaleString()} 풋프린트 · ${k.symbols.toLocaleString()} 심볼</span>`:'<span class="bad">미설치</span>';}catch(e){}
renderLib();setMode('select');renderSim();resize();fitView();millRefresh();ensureSch();ensurePlaced();histReset();histPush();