import './style.css'; import * as THREE from 'three'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; import * as XLSX from 'xlsx'; import { parseWorkbook, type SheetReport } from './core/parseWorkbook'; import { buildPier, type PierModel } from './core/geometry'; import { NOT_IMPLEMENTED_RULES, validateInput, validatePier } from './core/validate'; import { buildTemplateWorkbook } from './core/template'; import { EDITABLE_FIELDS, FIELD_LABELS, overrideKeys, pendingKeys, revertDraft, type EditableKey, type PierValues } from './core/changes'; import { adjustRow, layoutText, resolveRowCounts, rowLayout, totalPiles } from './core/piles'; import { applyCopy, DEFAULT_COPY_ITEMS, planCopy, type CopyItem } from './core/copy'; import { pileGeometry } from './core/geometry'; import { rebarSummary, type RebarRow } from './core/rebar/summary'; import { shearSegments } from './core/rebar/coping'; import { buildRebar3D, type RebarColorMode, type RebarScene } from './view/rebar3d'; import { DEFAULT_MATERIAL, FCK_MEMBERS, fyOf, GRADE_CHOICES, isAssumedMaterial, materialFrom, type BendFactors, type FckByMember, type FckMember, type GradeKey, type Material } from './core/rebar/common'; import { buildQuantity, quantityRows, type Quantity, type QuantityLine } from './core/rebar/quantity'; import { allVisibleChecked, setVisibleChecked, type TypeFilter } from './core/selection'; import { choose, defaultSummary, isSelectable, SHAPE_GROUPS, UNDEFINED_REASON, type MemberId } from './core/shape-options'; import { parseProject, SCHEMA_VERSION, serializeProject, type ProjectFile, type ProjectInfo } from './core/project'; import { clearBackup, readBackup, writeBackup, type Backup } from './view/backup'; import type { Issue, PierInput, PierType } from './core/types'; import { buildPierScene, setRebarView, viewPosition, type PartName, type PierScene, type ViewKind } from './view/scene'; /** One pier as shown in the list. `checked` is the multi-select state (시안 ①). */ interface Pier extends PierValues { model: PierModel | null; issues: Issue[]; checked: boolean; } /** Part toggles in the order of 시안 ① "부재 표시". Each toggle controls one or more scene parts. */ /** The four stages: ① 형상 생성, ② 형상계획, ③ 철근배근, ④ 재료표 (결과 화면). */ type Stage = 'shape' | 'plan' | 'rebar' | 'quantity'; const PART_GROUPS: { label: string; parts: PartName[] }[] = [ { label: '받침·받침블럭', parts: ['blocks', 'pads'] }, { label: '코핑', parts: ['coping'] }, { label: '기둥', parts: ['columns'] }, { label: '확대기초', parts: ['footings'] }, { label: '말뚝', parts: ['piles'] }, { label: '버림', parts: ['blinding'] }, { label: '지반면', parts: ['ground'] }, ]; const VIEWS: { kind: ViewKind; label: string }[] = [ { kind: 'iso', label: '아이소' }, { kind: 'front', label: '정면' }, { kind: 'side', label: '측면' }, { kind: 'top', label: '평면' }, ]; const TYPES: PierType[] = ['T형', '2주형', '3주형']; const CRS = '국토좌표계 (x = 동, y = 북)'; /** 시안 ② 부재 목록 (spec §5.2). `keys` are the inputs that exist today; the rest show values only. */ const MEMBERS: { id: MemberId; label: string; keys: EditableKey[]; note: string }[] = [ { id: 'bearing', label: '교량받침·받침블럭·솔플레이트', keys: [], note: '받침 상세(C3)는 Q16 결정 후 추가합니다. 지금은 표시만 합니다.' }, { id: 'coping', label: '코핑', keys: ['copingWidthMm', 'copingLengthMm'], note: '폭과 길이를 입력합니다. 계단 위치·단차선 입력은 다음 단계에서 추가합니다.' }, { id: 'column', label: '기둥', keys: [], note: '기둥 지름 입력은 다음 단계에서 추가합니다. 높이와 위치는 계산값입니다.' }, { id: 'footing', label: '확대기초 · 말뚝', keys: ['pileLengthMm'], note: '말뚝 길이를 입력합니다. 열별 본수는 다음 단계에서 추가합니다.' }, { id: 'ground', label: '지반·지층', keys: [], note: '지반고는 입력값을 표시만 합니다. 지층 입력은 다음 단계에서 추가합니다.' }, ]; interface Pending { fileName: string; entries: Pier[]; sheets: SheetReport[]; preview: string | null; } const state = { piers: [] as Pier[], active: null as string | null, view: 'iso' as ViewKind, visible: Object.fromEntries(PART_GROUPS.flatMap((g) => g.parts).map((p) => [p, true])) as Record, scene: null as PierScene | null, project: { name: '', bridge: '', client: '', author: '', crs: CRS, memo: '' } as ProjectInfo, /** Which stage screen is shown: ① 형상 생성 or ② 형상계획 (plan.md C1). */ stage: 'shape' as Stage, /** ③ 공통 설정 (fck · fy). */ material: { ...DEFAULT_MATERIAL } as Material, /** ② 부재별 fck. A member without an entry uses the 공통 fck (③). */ fckByMember: {} as FckByMember, /** Bar highlighted on ③ after [보기] (시안 ③ 알림). */ /** Bars selected on ③ (철근표 checkboxes, 3D click, [보기]). */ rebarSelection: new Set(), /** 선택만 보기: the bars not selected are hidden in 3D. */ rebarIsolate: false, /** Colour basis of the 3D rebar on ③: role (default), diameter, or member. */ rebarColor: 'role' as RebarColorMode, /** Member tab on ③ (철근표). */ rebarMember: 'coping' as RebarRow['member'], /** Member card selected on ② (시안 ② 부재 목록). */ member: 'coping' as MemberId, /** True once the project has a file on disk (first save via 시안 ⑤, or an opened file). Later saves go straight to it. */ savedOnce: false, filter: 'all' as 'all' | PierType, pending: null as Pending | null, source: { fileName: '', sheets: [] as string[] }, history: [] as ProjectFile['history'], /** Snapshot of the inputs at the last save or open. null until the project is first saved. */ savedSnapshot: null as string | null, }; let pendingBackup: Backup | null = null; /** 3D rebar of the current pier (③); its bars are named as in the 철근표. */ let rebar3d: RebarScene | null = null; let backupTimer: number | undefined; const $ = (id: string): T => document.getElementById(id) as T; function el(tag: K, text?: string, cls?: string): HTMLElementTagNameMap[K] { const node = document.createElement(tag); if (text !== undefined) node.textContent = text; if (cls) node.className = cls; return node; } /** Results and short notices: a toast that goes away after 4 s. */ function setStatus(msg: string): void { showToast(msg, false); } /** Problems: a toast that stays until the user closes it. */ function setError(msg: string): void { showToast(msg, true); } function showToast(msg: string, sticky: boolean): void { const host = $('toast-host'); const t = el('div', undefined, sticky ? 'toast error' : 'toast'); t.appendChild(el('span', msg)); if (sticky) { const close = el('button', '닫기', 'btn small'); close.type = 'button'; close.addEventListener('click', () => t.remove()); t.appendChild(close); } else { window.setTimeout(() => t.remove(), 4000); } host.appendChild(t); while (host.childElementCount > 3) host.firstElementChild?.remove(); } // --- 3D renderer ------------------------------------------------------------- const container = $('canvas'); const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.localClippingEnabled = true; // mitre planes on the bent coping renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); container.appendChild(renderer.domElement); const world = new THREE.Scene(); world.background = new THREE.Color(0xe9eef4); const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 5000); const controls = new OrbitControls(camera, renderer.domElement); controls.enableDamping = true; world.add(new THREE.HemisphereLight(0xffffff, 0x8899aa, 1.6)); const sun = new THREE.DirectionalLight(0xffffff, 1.2); sun.position.set(30, 60, 40); world.add(sun); function resize(): void { const w = container.clientWidth; const h = container.clientHeight; if (w === 0 || h === 0) return; renderer.setSize(w, h); camera.aspect = w / h; camera.updateProjectionMatrix(); } window.addEventListener('resize', resize); resize(); function animate(): void { controls.update(); renderer.render(world, camera); requestAnimationFrame(animate); } animate(); // --- pier model ---------------------------------------------------------------- function buildEntry(input: PierInput, checked: boolean): Pier { const issues: Issue[] = []; let model: PierModel | null = null; if (validateInput(input, issues)) { try { model = buildPier(input); validatePier(input, model, issues); } catch (err) { issues.push({ code: '미실시', level: 'E', pier: input.name, message: (err as Error).message }); } } return { input, model, issues, checked, defaults: input, draft: input }; } /** Builds the model from the draft (the cards' values) and keeps the imported defaults. */ function regenerateEntry(p: Pier): Pier { return { ...buildEntry(p.draft, p.checked), defaults: p.defaults, draft: p.draft }; } function sheetOk(sheets: SheetReport[], name: string): boolean { return sheets.find((s) => s.name === name)?.ok === true; } function activePier(): Pier | undefined { return state.piers.find((p) => p.input.name === state.active); } // --- parts and views ------------------------------------------------------------- function buildPartToggles(): void { const box = $('parts'); const row = el('div', undefined, 'parts-row'); for (const group of PART_GROUPS) { const label = el('label'); const input = el('input'); input.type = 'checkbox'; input.checked = group.parts.every((p) => state.visible[p]); input.addEventListener('change', () => { for (const p of group.parts) { state.visible[p] = input.checked; if (state.scene) state.scene.parts[p].visible = input.checked; } }); label.append(input, document.createTextNode(group.label)); row.appendChild(label); } box.appendChild(row); } function buildViewButtons(): void { const box = $('views'); for (const v of VIEWS) { const btn = el('button', v.label, 'btn'); btn.type = 'button'; btn.setAttribute('aria-pressed', String(v.kind === state.view)); btn.addEventListener('click', () => setView(v.kind)); box.appendChild(btn); } } function setView(kind: ViewKind): void { state.view = kind; document.querySelectorAll('#views button').forEach((b, i) => { b.setAttribute('aria-pressed', String(VIEWS[i].kind === kind)); }); if (!state.scene) return; const pier = activePier(); if (!pier?.model) return; const { position, target } = viewPosition(state.scene, kind, pier.model); camera.position.copy(position); controls.target.copy(target); controls.update(); } function setupScene(): void { if (state.scene) world.remove(state.scene.root); state.scene = null; if (rebar3d) world.remove(rebar3d.group); rebar3d = null; const pier = activePier(); if (pier?.model) { rebar3d = buildRebar3D(pier.model, state.material, state.fckByMember); rebar3d.group.visible = state.stage === 'rebar'; rebar3d.setColorMode(state.rebarColor); world.add(rebar3d.group); const scene = buildPierScene(pier.model); for (const key of Object.keys(state.visible) as PartName[]) { scene.parts[key].visible = state.visible[key]; } world.add(scene.root); setRebarView(scene, state.stage === 'rebar'); state.scene = scene; setView(state.view); } } // --- main list (시안 ① 교각 목록) ---------------------------------------------------- function renderPierList(): void { const select = $('type-filter'); if (select.options.length === 0) { select.appendChild(new Option('전체', 'all')); for (const t of TYPES) select.appendChild(new Option(t, t)); } select.value = state.filter; for (const opt of Array.from(select.options)) { const count = opt.value === 'all' ? state.piers.length : state.piers.filter((p) => p.input.type === opt.value).length; opt.textContent = `${opt.value === 'all' ? '전체' : opt.value} (${count})`; } renderPierRows($('pier-items'), 'shape'); renderPierRows($('plan-pier-items'), 'plan'); const visible = state.piers.filter((p) => state.filter === 'all' || p.input.type === state.filter); $('pier-empty').hidden = state.piers.length > 0; $('select-all').textContent = allVisibleChecked(state.piers, state.filter) ? '선택 해제' : '전체 선택'; } /** Rows of the pier list. ① shows the type and warnings; ② shows the count of changes from the defaults. */ function renderPierRows(list: HTMLUListElement, mode: Stage): void { list.replaceChildren(); const visible = state.piers.filter((p) => state.filter === 'all' || p.input.type === state.filter); for (const pier of visible) { const li = el('li'); const check = el('input'); check.type = 'checkbox'; check.checked = pier.checked; check.disabled = !pier.model; check.setAttribute('aria-label', `${pier.input.name} 선택`); check.addEventListener('change', () => { pier.checked = check.checked; renderAll(); }); const name = el('button', pier.input.name, 'pier-name'); name.type = 'button'; name.setAttribute('aria-pressed', String(pier.input.name === state.active)); name.addEventListener('click', () => { state.active = pier.input.name; state.rebarSelection = new Set(); setupScene(); renderAll(); }); const badge = mode === 'plan' ? el('span', `변경 ${overrideKeys(pier).length}`, 'badge') : mode === 'rebar' || mode === 'quantity' ? (pier.model ? el('span', '계산됨', 'badge') : el('span', '오류', 'badge error')) : pier.model ? el('span', `${pier.input.type}${pier.issues.some((i) => i.level !== 'I') ? ' · 경고' : ''}`, 'badge') : el('span', '오류', 'badge error'); li.append(check, name, badge); list.appendChild(li); } } function renderHeader(): void { const pier = activePier(); const parts = [state.project.name || '프로젝트 미지정']; if (state.project.bridge) parts.push(state.project.bridge); parts.push(`교각 ${state.piers.length}기`); $('project-line').textContent = parts.join(' · '); $('step1-status').textContent = pier?.model ? '생성됨' : '미생성'; renderSaveStatus(); const total = pendingTotal(); const checked = state.piers.filter((p) => p.checked).length; $('step2-status').textContent = total ? `변경 ${total}건 · 재생성 필요` : '예정'; const regen = $('regen'); regen.disabled = checked === 0; regen.textContent = total ? `선택 교각 다시 생성 · 변경 ${total}` : '선택 교각 형상 생성'; $('bulk-count').textContent = `선택 ${checked}기`; $('bulk-regen').disabled = checked === 0; $('bulk-copy').disabled = !activePier(); } // --- results panel (시안 ① 입력값 · 계산 결과) ---------------------------------------- type Pair = [string, string]; function fillKv(dl: HTMLElement, pairs: Pair[]): void { dl.replaceChildren(); for (const [k, v] of pairs) { const row = el('div'); row.append(el('dt', k), el('dd', v)); dl.appendChild(row); } } const f3 = (v: number) => v.toFixed(3); const pt = (p: { e: number; n: number }) => `E ${p.e.toFixed(3)}, N ${p.n.toFixed(3)}`; function renderPanels(): void { const pier = activePier(); const m = pier?.model; $('viewer-title').textContent = pier ? `${pier.input.name} · ${pier.input.type} 교각 형상` : '교각 3D 뷰'; $('origin').textContent = m ? `원점 = 교각 중심 (${pt(m.pivot)})` : ''; if (!pier) { fillKv($('inputs'), []); fillKv($('res-axis'), []); fillKv($('res-column'), []); fillKv($('res-found'), []); $('issues').replaceChildren(el('li', '교각을 불러오면 알림이 표시됩니다.')); $('issue-count').textContent = ''; return; } const inp = pier.input; const front = inp.bearings.filter((b) => b.row === '전열').length; const back = inp.bearings.filter((b) => b.row === '후열').length; const ip2 = inp.ips.find((p) => p.name === 'IP4'); fillKv($('inputs'), [ ['교각 / 기초 형식', `${inp.type} · ${inp.foundationType}`], ['코핑 상면 EL', `${f3(inp.copingTopEl)} m`], ['기초 하단 EL', `${f3(inp.footingBottomEl)} m`], ['지반고 EL', `${f3(inp.groundEl)} m`], ['코핑 폭 × 길이', `${inp.copingWidthMm} × ${inp.copingLengthMm} mm`], ['말뚝 길이', `${inp.pileLengthMm} mm`], ['IP', ip2 && ip2.angleDeg === 0 ? 'IP1–IP3 직선 (꺾임 0°)' : '꺾임 코핑'], ['받침', `전열 ${front} · 후열 ${back}`], ]); if (!m) { fillKv($('res-axis'), [['상태', '생성 불가 (알림 참조)']]); fillKv($('res-column'), []); fillKv($('res-found'), []); } else { fillKv($('res-axis'), [ ['IP 길이 / 축 방위각', `${f3(m.lengthIp)} m / ${m.azimuthDeg.toFixed(2)}°`], ['받침블럭 높이 전열 / 후열', `${Math.round(m.derived.blockHeightFront * 1000)} / ${Math.round(m.derived.blockHeightBack * 1000)} mm`], ['계단 경계 (BP1 기준)', m.segments.slice(1).map((s) => f3(s.s0)).join(' / ') + ' m'], ['횡방향 단차', '없음'], ['코핑 하면 EL 끝단 / Level', `${f3(m.endBottom)} / ${f3(m.levelBottom)} m`], ]); fillKv($('res-column'), [ ['기둥 높이', `${f3(m.derived.columnHeight)} m`], ['기둥 중심', pt(m.pivot)], ]); fillKv($('res-found'), [ ['확대기초 상면 EL', m.footings.length ? `${f3(m.footings[0].top)} m` : '없음'], ['말뚝 배치', m.derived.pileLayout ?? '없음'], ['말뚝 수 / 선단 EL', `${m.derived.pileCount} 본 / ${m.derived.pileTipEl === null ? '없음' : f3(m.derived.pileTipEl) + ' m'}`], ['흙덮개', `${f3(m.derived.soilCover)} m`], ]); } const list = $('issues'); list.replaceChildren(); if (pier.issues.length === 0) list.appendChild(el('li', '알림 없음')); for (const issue of pier.issues) { const li = el('li'); li.append(el('span', issue.code, `code ${issue.level}`), document.createTextNode(` ${issue.message}`)); list.appendChild(li); } $('issue-count').textContent = `(${pier.issues.length}건)`; } function renderDefaults(): void { const box = $('defaults'); box.replaceChildren(); const items = [ '[미확정] Q16 받침 EL·E 식: 현재 모델(블럭 높이 = EL 차) 적용', '[미확정] Q5 말뚝 지름 600 mm (강관) 기본값', '[미확정] Q4 직접기초 버림콘크리트 적용', ...NOT_IMPLEMENTED_RULES.map((r) => `미구현 규칙: ${r}`), ]; for (const text of items) box.appendChild(el('li', text)); } function renderAll(): void { $('status').textContent = state.piers.length ? '' : '엑셀을 불러오면 교각 형상을 생성합니다.'; renderHeader(); renderPierList(); renderPanels(); renderEditCard(); renderPlan(); renderRebar(); renderRebarColor(); if (state.stage === 'quantity') renderQuantityScreen(); scheduleBackup(); } // --- 속성 복사 (시안 ⑥, plan C8) --------------------------------------------------------- /** Names of the pier targets ticked in the copy dialog. */ let copySelection = new Set(); function copyTargets(): Pier[] { const src = activePier(); const sameType = $('cp-same-type').checked; return state.piers.filter((p) => p !== src && (!sameType || p.input.type === src?.input.type)); } /** Targets that are both visible in the dialog and ticked. Hidden targets are never changed. */ function selectedCopyTargets(): Pier[] { return copyTargets().filter((t) => copySelection.has(t.input.name)); } function chosenCopyItems(): CopyItem[] { return Array.from(document.querySelectorAll('#copy-dialog input[data-item]')) .filter((i) => i.checked) .map((i) => i.dataset.item as CopyItem); } function renderCopyTargets(): void { const list = $('cp-targets'); list.replaceChildren(); for (const t of copyTargets()) { const li = el('li'); const check = el('input'); check.type = 'checkbox'; check.checked = copySelection.has(t.input.name); check.setAttribute('aria-label', `${t.input.name} 대상`); check.addEventListener('change', () => { if (check.checked) copySelection.add(t.input.name); else copySelection.delete(t.input.name); renderCopyPreview(); }); li.append(check, el('span', `${t.input.name} · ${t.input.type} · ${t.input.foundationType}`)); list.appendChild(li); } } function renderCopyPreview(): void { const src = activePier(); const items = chosenCopyItems(); const body = $('cp-preview').tBodies[0]; body.replaceChildren(); let applied = 0; let skippedTotal = 0; const selected = selectedCopyTargets(); for (const t of selected) { if (!src) break; const plan = planCopy(src.draft, t.draft, items); const labels = plan.changes.map((c) => `${FIELD_LABELS[c.key]} ${c.from} → ${c.to}`); const skipped = plan.skipped.map((k) => k.reason); skippedTotal += skipped.length; if (plan.changes.length) applied++; const row = el('tr'); row.append( el('td', t.input.name), el('td', t.input.type), el('td', String(plan.changes.length)), el('td', String(skipped.length)), el('td', [...labels, ...skipped].join(' · ') || '바뀌는 값 없음'), ); body.appendChild(row); } const n = selected.length; $('cp-summary').textContent = `${src?.input.name ?? ''} → ${n}기 · 항목 ${items.length}개 · 건너뜀 ${skippedTotal}건`; const apply = $('cp-apply'); apply.textContent = `대상 ${applied}기에 적용`; apply.disabled = applied === 0; } function openCopyDialog(): void { const src = activePier(); if (!src) return; $('cp-source').textContent = `${src.input.name} · ${src.input.type} · ${src.input.foundationType}`; for (const i of document.querySelectorAll('#copy-dialog input[data-item]')) { i.checked = (DEFAULT_COPY_ITEMS as string[]).includes(i.dataset.item ?? ''); } copySelection = new Set(state.piers.filter((p) => p.checked && p !== src).map((p) => p.input.name)); renderCopyTargets(); renderCopyPreview(); $('copy-dialog').showModal(); } function applyCopyDialog(): void { const src = activePier(); if (!src) return; const items = chosenCopyItems(); const regen = $('cp-regen').checked; let applied = 0; let changes = 0; const targets = new Set(selectedCopyTargets().map((t) => t.input.name)); state.piers = state.piers.map((t) => { if (t === src || !targets.has(t.input.name)) return t; const plan = planCopy(src.draft, t.draft, items); if (plan.changes.length === 0) return t; applied++; changes += plan.changes.length; const next = { ...t, draft: applyCopy(t.draft, plan) }; return regen ? regenerateEntry(next) : next; }); $('copy-dialog').close(); setupScene(); renderAll(); setStatus(`${applied}기에 속성을 복사했습니다. 변경 ${changes}건${regen ? ' (형상 다시 생성)' : ' · 재생성 필요'}.`); } // --- input cards and change tracking (plan B1 · C2) ------------------------------------- function pendingTotal(): number { return state.piers.filter((p) => p.checked).reduce((n, p) => n + pendingKeys(p).length, 0); } function fieldLabel(key: EditableKey): string { return FIELD_LABELS[key]; } function editField(name: string, key: EditableKey, raw: string): void { const p = state.piers.find((q) => q.input.name === name); if (!p) return; const value = Number(raw); if (!Number.isFinite(value) || value <= 0) { setError(`${fieldLabel(key)}은(는) 0보다 큰 숫자여야 합니다. 값을 되돌렸습니다.`); renderAll(); return; } p.draft = { ...p.draft, [key]: value }; renderAll(); } function revertField(name: string, key: EditableKey): void { const p = state.piers.find((q) => q.input.name === name); if (!p) return; p.draft = revertDraft(p, key); renderAll(); } function regenerateOne(name: string): void { const i = state.piers.findIndex((q) => q.input.name === name); if (i < 0) return; state.piers[i] = regenerateEntry(state.piers[i]); setupScene(); renderAll(); const msg = state.piers[i].model ? '형상을 다시 생성했습니다.' : '형상을 생성하지 못했습니다. 알림을 확인하세요.'; setStatus(msg); } function renderEditCard(): void { const pier = activePier(); const btn = $('edit-regen'); if (!pier) { $('edit-fields').replaceChildren(); $('edit-summary').textContent = '교각을 선택하면 입력을 고칠 수 있습니다.'; btn.textContent = '다시 생성 · 변경 0'; btn.disabled = true; return; } const status = fieldRows($('edit-fields'), pier, EDITABLE_FIELDS.map((f) => f.key)); $('edit-summary').textContent = status.pending.length ? `미반영 변경 ${status.pending.length}건. 3D 그림은 생성본을 유지합니다.` : status.overrides.length ? `기본값과 ${status.overrides.length}건 다릅니다. 생성본이 최신입니다.` : '기본값과 같습니다.'; btn.textContent = `다시 생성 · 변경 ${status.pending.length}`; btn.disabled = status.pending.length === 0; } /** One row per input key: value in the draft, the imported default beside it, and a revert button when overridden. */ function fieldRows(box: HTMLDivElement, pier: Pier, keys: EditableKey[]): { overrides: EditableKey[]; pending: EditableKey[] } { box.replaceChildren(); const overrides = overrideKeys(pier); const pending = pendingKeys(pier); for (const f of EDITABLE_FIELDS.filter((x) => keys.includes(x.key))) { const changed = overrides.includes(f.key); const row = el('div', undefined, changed ? 'edit-row changed' : 'edit-row'); const id = `edit-${f.key}`; const label = el('label', `${f.label} (${f.unit})`); label.htmlFor = id; const input = el('input'); input.id = id; input.type = 'number'; input.step = 'any'; input.value = String(pier.draft[f.key]); input.addEventListener('change', () => editField(pier.input.name, f.key, input.value)); row.append(label, input, el('span', `기본 ${pier.defaults[f.key]}`, 'muted small')); if (changed) { const revert = el('button', '되돌리기', 'btn small'); revert.type = 'button'; revert.addEventListener('click', () => revertField(pier.input.name, f.key)); row.appendChild(revert); } box.appendChild(row); } return { overrides, pending }; } /** Footing length of the generated model, which the 열별 본수 limits depend on. */ function footingLengthOf(p: Pier): number | undefined { return p.model?.footings[0]?.length; } /** 열별 본수 −/+ per 열 (spec §5.4). Shows the spacing, the total, and a warning below the minimum spacing. */ function renderPileRows(box: HTMLDivElement, pier: Pier): void { const footingLength = footingLengthOf(pier); if (pier.input.foundationType !== '말뚝기초' || footingLength === undefined) return; const g = pileGeometry(footingLength); let counts: number[]; try { counts = resolveRowCounts(pier.draft.pileRows, g); } catch (err) { box.appendChild(el('p', (err as Error).message, 'warn-text')); return; } const layout = rowLayout(counts, g); const wrap = el('div', undefined, 'pile-rows'); wrap.appendChild(el('p', `열별 본수 · 합계 ${totalPiles(counts)}본`, 'lbl')); counts.forEach((count, j) => { const row = el('div', undefined, layout[j].belowMinimum ? 'pile-row warn' : 'pile-row'); const minus = el('button', '−', 'btn small'); minus.type = 'button'; minus.setAttribute('aria-label', `열 ${j + 1} 본수 줄이기`); minus.addEventListener('click', () => setPileRow(pier.input.name, j, -1)); const plus = el('button', '+', 'btn small'); plus.type = 'button'; plus.setAttribute('aria-label', `열 ${j + 1} 본수 늘리기`); plus.addEventListener('click', () => setPileRow(pier.input.name, j, 1)); row.append(el('span', `열 ${j + 1}`), minus, el('span', `${count}본`, 'num'), plus, el('span', `간격 ${Math.round(layout[j].spacing * 1000)} mm`, 'num')); if (layout[j].belowMinimum) row.append(el('span', `최소 ${Math.round(g.minSpacing * 1000)} mm보다 좁습니다`, 'warn-text')); wrap.appendChild(row); }); wrap.appendChild(el('p', layoutText(counts, g), 'num')); box.appendChild(wrap); } function setPileRow(name: string, row: number, delta: number): void { const p = state.piers.find((q) => q.input.name === name); const length = p && footingLengthOf(p); if (!p || length === undefined) return; const counts = resolveRowCounts(p.draft.pileRows, pileGeometry(length)); p.draft = { ...p.draft, pileRows: adjustRow(counts, row, delta) }; renderAll(); } /** 형상 선택칩 (plan.md C5): the default is pressed; every other option is shown and disabled with the reason. */ function renderShapeOptions(member: MemberId): void { const box = $('member-options'); box.replaceChildren(); SHAPE_GROUPS[member].forEach((group, gi) => { const row = el('div', undefined, 'shape-row'); row.appendChild(el('span', group.title, 'lbl')); for (const option of group.options) { const chip = el('button', option.label, 'chip-opt'); chip.type = 'button'; chip.setAttribute('aria-pressed', String(option.isDefault)); chip.disabled = !isSelectable(option); if (chip.disabled) chip.title = UNDEFINED_REASON; chip.addEventListener('click', () => { try { choose(member, gi, option.label); } catch (err) { setError((err as Error).message); } }); row.appendChild(chip); } box.appendChild(row); }); } // --- ② 형상계획 (plan.md C1) ------------------------------------------------------------- /** * The 3D viewer block (views, canvas, part toggles) is shared: it sits in ① and moves into the top slot of * ② or ③ while that screen is shown. Moving the element keeps the WebGL context. */ const viewerHome: { parent: Node | null; next: Node | null } = { parent: null, next: null }; function captureViewerHome(): void { const block = $('viewer-block'); viewerHome.parent = block.parentElement; viewerHome.next = block.nextElementSibling; } function placeViewer(stage: Stage): void { const block = $('viewer-block'); const slot = stage === 'plan' ? $('slot-plan') : stage === 'rebar' ? $('slot-rebar') : null; if (slot) slot.appendChild(block); else if (viewerHome.parent) viewerHome.parent.insertBefore(block, viewerHome.next); renderRebarColor(); resize(); } /** The 철근표 group that a bar name belongs to. */ function memberOfBar(name: string): RebarRow['member'] { if (name === 'S1' || name === 'S2') return 'bearing'; if (name.startsWith('B')) return 'footing'; if (name.startsWith('P') || name.includes('커플러')) return 'column'; return 'coping'; } /** Click on a 3D bar selects its 철근표 row (on ③ only). */ function wireRebarPicking(): void { const dom = renderer.domElement; let down: { x: number; y: number } | null = null; dom.addEventListener('pointerdown', (e) => { down = { x: e.clientX, y: e.clientY }; }); dom.addEventListener('pointerup', (e) => { if (!down || state.stage !== 'rebar' || !rebar3d) return; if (Math.hypot(e.clientX - down.x, e.clientY - down.y) > 5) return; // a drag rotates the view const rect = dom.getBoundingClientRect(); const ndc = new THREE.Vector2(((e.clientX - rect.left) / rect.width) * 2 - 1, -((e.clientY - rect.top) / rect.height) * 2 + 1); const ray = new THREE.Raycaster(); ray.setFromCamera(ndc, camera); const hit = ray.intersectObjects(rebar3d.group.children, false).find((h) => h.object.visible); if (!hit) return; const name = hit.object.userData.name as string; state.rebarSelection = new Set([name]); state.rebarMember = memberOfBar(name); renderAll(); }); } function setStage(stage: Stage): void { state.stage = stage; if (state.scene) setRebarView(state.scene, stage === 'rebar'); $('view-shape').hidden = stage !== 'shape'; $('view-plan').hidden = stage !== 'plan'; $('view-rebar').hidden = stage !== 'rebar'; $('view-quantity').hidden = stage !== 'quantity'; if (stage === 'quantity') renderQuantityScreen(); // Move the viewer after its new parent is visible, so that resize() measures the real size. placeViewer(stage); if (rebar3d) rebar3d.group.visible = stage === 'rebar'; // The highlight is the 'current' class (style.css); aria-current is set alongside it for assistive tech. const nav: [string, Stage][] = [['nav-shape', 'shape'], ['nav-plan', 'plan'], ['nav-rebar', 'rebar'], ['nav-quantity', 'quantity']]; for (const [id, key] of nav) { const btn = $(id); btn.classList.toggle('current', stage === key); btn.setAttribute('aria-current', String(stage === key)); } renderAll(); } // --- ③ 철근배근 (plan C5 · ③ screen v1) -------------------------------------------------- const REBAR_MEMBERS: { id: RebarRow['member']; label: string }[] = [ { id: 'bearing', label: '받침블럭' }, { id: 'coping', label: '코핑' }, { id: 'column', label: '기둥' }, { id: 'footing', label: '확대기초' }, ]; /** 3D colour basis buttons on ③ and their legend (spec §6.7). */ function renderRebarColor(): void { // The colour control lives in the 3D window and is shown only on ③. $('rebar-color').hidden = state.stage !== 'rebar'; for (const b of document.querySelectorAll('#rebar-color button')) { b.setAttribute('aria-pressed', String(b.dataset.color === state.rebarColor)); } const legend = $('rebar-legend'); legend.replaceChildren(); const items = rebar3d?.legend(state.rebarColor) ?? []; if (state.rebarColor === 'role') { legend.appendChild(el('li', '역할별 색 (명세 §6.7)', 'muted small')); } for (const item of items) { const li = el('li', undefined, 'rebar-legend-item'); const swatch = el('span', undefined, 'swatch'); swatch.style.background = item.color; li.append(swatch, el('span', item.label)); legend.appendChild(li); } } for (const b of document.querySelectorAll('#rebar-color button')) { b.addEventListener('click', () => { state.rebarColor = b.dataset.color as RebarColorMode; rebar3d?.setColorMode(state.rebarColor); renderRebarColor(); }); } function renderRebar(): void { const pier = activePier(); // 교각 목록 with the 배근 status (시안 ③). The checks are the same as in ① and ②. renderPierRows($('rebar-pier-items'), 'rebar'); const checked = state.piers.filter((p) => p.checked).length; $('rebar-bulk-count').textContent = `선택 ${checked}기`; $('rebar-bulk-copy').disabled = !pier; const tabs = $('rebar-tabs'); tabs.replaceChildren(); for (const m of REBAR_MEMBERS) { const li = el('li'); const btn = el('button', m.label, 'member'); btn.type = 'button'; btn.setAttribute('aria-pressed', String(m.id === state.rebarMember)); btn.addEventListener('click', () => { state.rebarMember = m.id; state.rebarSelection = new Set(); renderAll(); }); li.appendChild(btn); tabs.appendChild(li); } const box = $('rebar-table'); box.replaceChildren(); const issues = $('rebar-issues'); issues.replaceChildren(); if (!pier?.model) { $('rebar-note').textContent = '교각을 생성하면 철근표가 나옵니다.'; return; } const { rows, warnings } = rebarSummary(pier.model, state.material, state.fckByMember); $('rebar-note').textContent = `${pier.input.name} · 형상 기준 계산 · 값은 기본값 기준 문서 규칙`; const shown = rows.filter((r) => r.member === state.rebarMember); box.appendChild(rebarSelectBar(shown.map((r) => r.name))); const table = el('table', undefined, 'grid-table rebar-table'); const head = el('thead'); const hr = el('tr'); for (const h of ['선택', '구분', '번호', '규격', '강도', '간격', '실간격', '위치', '방향', '형상', '길이 (mm)', '개수', '근거', '비고']) hr.appendChild(el('th', h)); head.appendChild(hr); table.appendChild(head); const body = el('tbody'); for (const r of shown) { const tr = el('tr'); const nameCell = el('td', r.name, 'bar-no'); // Clicking a row selects that bar alone; clicking it again clears it (시안: 번호를 누르면 그 철근만 강조). tr.addEventListener('click', () => { state.rebarSelection = state.rebarSelection.size === 1 && state.rebarSelection.has(r.name) ? new Set() : new Set([r.name]); renderAll(); }); if (state.rebarSelection.has(r.name)) tr.classList.add('hl'); const check = el('input'); check.type = 'checkbox'; check.checked = state.rebarSelection.has(r.name); check.setAttribute('aria-label', `${r.name} 선택`); // The checkbox changes the selection on its own; its click must not reach the row handler. check.addEventListener('click', (ev) => ev.stopPropagation()); check.addEventListener('change', () => { const next = new Set(state.rebarSelection); if (check.checked) next.add(r.name); else next.delete(r.name); state.rebarSelection = next; renderAll(); }); const pick = el('td'); pick.appendChild(check); tr.append( pick, el('td', ROLE[r.name] ?? '—'), nameCell, el('td', r.size), el('td', /^[DHS]\d/.test(r.size) ? `SD${fyOf(state.material, r.size)}` : '—'), el('td', r.spacing), el('td', r.actual), el('td', r.position), el('td', r.direction), el('td', r.shape), el('td', r.length === null ? (r.pieces ? '여러 길이' : '—') : String(Math.round(r.length * 10) / 10), undefined), el('td', String(r.count)), basisCell(r.basis), el('td', r.note), ); body.appendChild(tr); } table.appendChild(body); box.appendChild(table); // Alerts: [보기] goes to the member, [입력] goes to the 공통 설정 input (시안 ③ 알림). for (const w of warnings) { const li = el('li', `${w.code} ${w.message} `); const btn = el('button', w.target === 'common' ? '입력' : '보기', 'btn small'); btn.type = 'button'; btn.addEventListener('click', () => { if (w.target === 'common') { $('mat-fck').focus(); $('mat-fck').scrollIntoView({ block: 'center' }); } else { // [보기]: go to the member and highlight the bar the alert is about. state.rebarMember = w.target; state.rebarSelection = new Set(w.bar ? [w.bar] : []); renderAll(); } }); li.appendChild(btn); issues.appendChild(li); } if (issues.childElementCount === 0) issues.appendChild(el('li', '알림이 없습니다.')); // Warning count on each member tab (시안: 부재 탭 배지). const counts = (member: RebarRow['member']) => warnings.filter((w) => w.target === member).length; for (const [i, m] of REBAR_MEMBERS.entries()) { const badge = tabs.children[i]?.querySelector('button'); const n = counts(m.id); if (badge && n > 0) badge.appendChild(el('span', String(n), 'badge warn')); } $('step3-status').textContent = `계산됨 · 경고 ${warnings.length}`; renderMaterial(); // 전단철근 구간 표 (시안 ③ 코핑 탭): segments of C9 / C10 / C11 along the coping, in metres. if (state.rebarMember === 'coping') { const seg = shearSegments(pier.model.copingL * 1000, pier.model.columns.map((c) => c.s * 1000)); const st = el('table', undefined, 'grid-table rebar-table'); const sh = el('thead'); const shr = el('tr'); for (const h of ['구간', '시작 (m)', '끝 (m)', '간격 (mm)', '개수', '번호']) shr.appendChild(el('th', h)); sh.appendChild(shr); st.appendChild(sh); const sb = el('tbody'); for (const g of seg) { const tr = el('tr'); tr.append( el('td', g.segment), el('td', (g.from / 1000).toFixed(3)), el('td', (g.to / 1000).toFixed(3)), el('td', String(g.spacing)), el('td', String(g.count)), el('td', g.bar), ); sb.appendChild(tr); } st.appendChild(sb); box.appendChild(el('p', '전단철근 구간 (끝면 기준, 철근 위치의 처음과 끝)', 'lbl')); box.appendChild(st); } // Table and 3D model point at each other: the focused bar is highlighted in both. renderQuantityLink(box, rows); rebar3d?.show(state.rebarSelection.size ? state.rebarSelection : null, state.rebarIsolate); const focused = box.querySelector('tr.hl'); if (focused && state.rebarSelection.size) focused.scrollIntoView({ block: 'center' }); } /** ③ keeps only a link to the 철근재료표 screen ④ (승인 2026-10-23). */ function renderQuantityLink(box: HTMLElement, rows: RebarRow[]): void { const q = buildQuantity(rows, state.material); const line = el('p', `철근재료표 · 합계 ${q.kg.toFixed(1)} kg · 할증 포함 ${q.kgWithWaste.toFixed(1)} kg`, 'pnote'); const link = el('button', '④ 재료표에서 보기', 'btn small'); link.type = 'button'; link.addEventListener('click', () => setStage('quantity')); box.append(line, link); } /** ④ 재료표: the 철근재료표 of each target pier, bars by 강종 → 직경 → 번호, with subtotals and the totals. */ function quantityTargets(): Pier[] { const checked = state.piers.filter((p) => p.checked && p.model); if (checked.length) return checked; const active = activePier(); return active?.model ? [active] : []; } /** Length cell of a 재료표 line: the length, or 여러 길이 with the pieces on hover. */ function lengthCell(l: QuantityLine): HTMLTableCellElement { if (l.length !== null) return el('td', String(l.length)); const cell = el('td', `여러 길이 (${l.pieces.length}종)`); cell.title = l.pieces.map((p) => `${Math.round(p.length)} × ${p.count}`).join(' · '); return cell; } function quantityTable(q: Quantity): HTMLTableElement { const table = el('table', undefined, 'grid-table rebar-table'); const head = el('thead'); const hr = el('tr'); for (const h of ['강종', '번호', '형상', '규격', '개수', '길이 (mm)', '총길이 (m)', '단위중량 (kg/m)', '총중량 (kg)']) hr.appendChild(el('th', h)); head.appendChild(hr); table.appendChild(head); const body = el('tbody'); for (const g of q.groups) { for (const d of g.diameters) { for (const l of d.lines) { body.appendChild(el('tr')).append( el('td', `SD${g.grade}`), el('td', l.name, 'bar-no'), el('td', l.shape), el('td', l.size), el('td', String(l.count)), lengthCell(l), el('td', l.totalM.toFixed(1)), el('td', l.kgPerM.toFixed(3)), el('td', l.kg.toFixed(1)), ); } body.appendChild(el('tr', undefined, 'subtotal')).append(el('td', ''), el('td', `${d.size} 소계`), el('td', ''), el('td', d.size), el('td', ''), el('td', ''), el('td', d.totalM.toFixed(1)), el('td', ''), el('td', d.kg.toFixed(1))); } body.appendChild(el('tr', undefined, 'subtotal')).append(el('td', `SD${g.grade} 소계`), el('td', ''), el('td', ''), el('td', ''), el('td', ''), el('td', ''), el('td', ''), el('td', ''), el('td', g.kg.toFixed(1))); } table.appendChild(body); return table; } function renderQuantityScreen(): void { const body = $('quantity-body'); body.replaceChildren(); renderPierRows($('quantity-pier-items'), 'quantity'); const targets = quantityTargets(); $('step4-status').textContent = targets.length ? `${targets.length}기` : '예정'; $('quantity-note').textContent = targets.length ? `${targets.length}기 · 할증 ${state.material.waste}% (기본 0%, 가정). 삿갓 길이는 h − 100으로 가정(Q-K5). 체크한 교각 기준이고, 체크가 없으면 현재 교각입니다.` : '계산된 교각이 없습니다. 교각을 생성하세요.'; let kg = 0; let kgWithWaste = 0; for (const pier of targets) { if (!pier.model) continue; const q = buildQuantity(rebarSummary(pier.model, state.material, state.fckByMember).rows, state.material); kg += q.kg; kgWithWaste += q.kgWithWaste; body.appendChild(el('h3', `${pier.input.name} · ${pier.input.type}`)); if (q.missing.length) body.appendChild(el('p', `길이 미산정 ${q.missing.length}개는 합계에서 뺐습니다: ${q.missing.join(', ')}`, 'muted small')); body.appendChild(quantityTable(q)); body.appendChild(el('p', `합계 ${q.kg.toFixed(1)} kg · 할증 포함 ${q.kgWithWaste.toFixed(1)} kg (${q.ton.toFixed(3)} ton) · 커플러 ${q.couplers.map((c) => `${c.name} ${c.count}개`).join(', ') || '없음'}`, 'pnote')); } if (targets.length > 1) { body.appendChild(el('p', `전체 ${targets.length}기 합계 ${kg.toFixed(1)} kg · 할증 포함 ${kgWithWaste.toFixed(1)} kg (${(kgWithWaste / 1000).toFixed(3)} ton)`, 'lbl')); } } /** Excel: one sheet per pier and a 합계 sheet (승인 2026-10-23). */ function exportQuantityWorkbook(): void { const targets = quantityTargets(); if (!targets.length) return; const wb = XLSX.utils.book_new(); const summary: (string | number)[][] = [['교각', '합계 (kg)', '할증 포함 (kg)', 'ton']]; for (const pier of targets) { if (!pier.model) continue; const q = buildQuantity(rebarSummary(pier.model, state.material, state.fckByMember).rows, state.material); const sheet = pier.input.name.replace(/[[\]:*?/\\]/g, '_').slice(0, 31); XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet(quantityRows(q)), sheet); summary.push([pier.input.name, Math.round(q.kg * 10) / 10, Math.round(q.kgWithWaste * 10) / 10, Math.round(q.ton * 1000) / 1000]); } XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet(summary), '합계'); XLSX.writeFile(wb, `${state.project.bridge || '교량'}_철근재료표.xlsx`); } $('quantity-export').addEventListener('click', exportQuantityWorkbook); $('quantity-print').addEventListener('click', () => window.print()); /** Above the 철근표: selection count, select-all / clear for the shown member, and 선택만 보기 (승인 2026-10-23). */ function rebarSelectBar(names: string[]): HTMLDivElement { const bar = el('div', undefined, 'rebar-select'); const picked = names.filter((n) => state.rebarSelection.has(n)).length; bar.appendChild(el('span', `선택 ${picked} / ${names.length}`, 'muted small')); const all = el('button', picked === names.length && names.length > 0 ? '전체 해제' : '전체 선택', 'btn small'); all.type = 'button'; all.addEventListener('click', () => { state.rebarSelection = picked === names.length ? new Set() : new Set(names); renderAll(); }); const iso = el('button', '선택만 보기', 'btn small'); iso.type = 'button'; iso.setAttribute('aria-pressed', String(state.rebarIsolate)); iso.addEventListener('click', () => { state.rebarIsolate = !state.rebarIsolate; renderAll(); }); bar.append(all, iso); return bar; } /** 구분 (role) of each bar, as in the 시안 ③ table. */ const ROLE: Record = { S1: '받침블럭', S2: '받침블럭', C1: '주철근', C2: '주철근', C3: '주철근', C4: '주철근', C5: '주철근', C6: '주철근', C7: '주철근', C8: '주철근', C9: '전단·띠', C10: '전단·띠', C11: '전단·띠', C12: '측면 수평', C13: '측면 수평', C14: '직각 수평', C15: '계단부', C16: '계단부', P1: '주철근', P2: '주철근', '주철근 커플러': '조립', '띠철근 커플러': '조립', 커플러: '조립', P3: '띠철근', P5: '띠철근', P4: '띠철근', P6: '크로스타이', B1: '주철근', B2: '주철근', B3: '주철근', B4: '주철근', B5: '주철근', B6: '주철근', B7: '주철근', B8: '주철근', 'B10-1': '측면 수평', 'B10-2': '측면 수평', B11: '조립 수평', B12: '조립 수평', B9: '전단·띠', }; /** 근거 badge (시안 ③): 입력 / 기본값 / 계산 / 가정. */ function basisCell(b: RebarRow['basis']): HTMLElement { const label = { input: '입력', default: '기본값', calc: '계산', assumed: '가정' }[b]; return el('td', label, `basis basis-${b}`); } /** ③ 공통 설정 inputs. Changes apply on 'change' and regenerate the rebar screen. */ function renderMaterial(): void { const m = state.material; const assumed = isAssumedMaterial(m); $('mat-fck').value = String(m.fck); for (const k of ['d', 'h', 's'] as const) { $(`mat-grade-${k}`).value = String(m.grade[k.toUpperCase() as GradeKey]); } for (const k of ['small16', 'main25', 'main35', 'main51'] as const) { $(`bend-${k}`).value = String(m.bend[k]); } $('mat-waste').value = String(m.waste); $('mat-note').textContent = assumed ? 'fck 가정값 (I02): 입력을 바꾸면 정착·이음 길이가 달라집니다.' : '입력값을 사용합니다.'; } /** Sets or clears one member's fck. An empty value clears it, so the member uses the 공통 fck again. */ function setMemberFck(member: FckMember, raw: string): void { const next = { ...state.fckByMember }; if (raw.trim() === '') { delete next[member]; } else { const value = Number(raw); if (!Number.isFinite(value) || value <= 0) { setError('부재별 fck는 0보다 큰 숫자여야 합니다.'); renderPlan(); return; } next[member] = value; } state.fckByMember = next; setupScene(); renderAll(); } /** fck row on a ② card for a member that has its own fck: value, the 공통 value beside it, and a revert when set. */ function renderMemberFck(box: HTMLDivElement, member: FckMember): void { const own = state.fckByMember[member]; // Not the orange "changed" style: fck changes only the rebar, so it is not a pending shape change (변경 n건). const row = el('div', undefined, 'edit-row'); const id = `member-fck-${member}`; const label = el('label', 'fck (MPa)'); label.htmlFor = id; const input = el('input'); input.id = id; input.type = 'number'; input.step = 'any'; input.value = String(own ?? state.material.fck); input.addEventListener('change', () => setMemberFck(member, input.value)); const note = own === undefined ? `공통 ${state.material.fck}` : `공통 ${state.material.fck} · 배근에만 반영`; row.append(label, input, el('span', note, 'muted small')); if (own !== undefined) { const revert = el('button', '되돌리기', 'btn small'); revert.type = 'button'; revert.addEventListener('click', () => setMemberFck(member, '')); row.appendChild(revert); } box.appendChild(row); } /** 재료표 할증 (%) on 공통 설정. Zero or more. */ function setWaste(raw: string): void { const value = Number(raw); if (!Number.isFinite(value) || value < 0) { setError('재료표 할증은 0 이상의 숫자여야 합니다.'); renderMaterial(); return; } state.material = { ...state.material, waste: value }; renderAll(); } /** Bend factor (× diameter) input on 공통 설정. Must be a positive number. */ function setBend(key: keyof BendFactors, raw: string): void { const value = Number(raw); if (!Number.isFinite(value) || value <= 0) { setError('굽힘 내면반경 배수는 0보다 큰 숫자여야 합니다.'); renderMaterial(); return; } state.material = { ...state.material, bend: { ...state.material.bend, [key]: value } }; setupScene(); renderAll(); } function setMaterial(key: 'fck' | GradeKey, raw: string): void { const value = Number(raw); const valid = key === 'fck' ? Number.isFinite(value) && value > 0 : (GRADE_CHOICES as readonly number[]).includes(value); if (!valid) { setError(key === 'fck' ? '공통 설정 fck는 0보다 큰 숫자여야 합니다.' : '철근 강도는 SD300 · SD400 · SD500 중 하나여야 합니다.'); renderMaterial(); return; } state.material = key === 'fck' ? { ...state.material, fck: value } : { ...state.material, grade: { ...state.material.grade, [key]: value } }; // The 3D bars depend on the material too (their lengths), so the scene is rebuilt with it. setupScene(); renderAll(); } function renderPlan(): void { const pier = activePier(); const members = $('members'); members.replaceChildren(); for (const m of MEMBERS) { const li = el('li'); const btn = el('button', m.label, 'member'); btn.type = 'button'; btn.setAttribute('aria-pressed', String(m.id === state.member)); const changed = pier ? overrideKeys(pier).filter((k) => m.keys.includes(k)).length : 0; btn.append(el('span', changed ? `변경 ${changed}` : '변경 0', 'badge')); btn.addEventListener('click', () => { state.member = m.id; renderAll(); }); li.appendChild(btn); members.appendChild(li); } const member = MEMBERS.find((m) => m.id === state.member)!; $('member-title').textContent = member.label; // Default and current selection are shown separately (spec §5.1). Only defaults are selectable today. $('member-summary').textContent = `기본: ${defaultSummary(member.id)} · 선택: ${defaultSummary(member.id)}`; renderShapeOptions(member.id); $('member-note').textContent = member.note; if (member.id === 'bearing' && pier?.model) { // Row skew of the bearing blocks (계산값): the block sides follow the bearing row (승인 2026-10-23). const front = pier.model.blocks.find((b) => b.row === '전열'); const back = pier.model.blocks.find((b) => b.row === '후열'); $('member-note').textContent += ` 받침 열 기울기(교축 직각 기준, 계산값): 전열 ${front?.rowSkewDeg.toFixed(2) ?? '—'}° · 후열 ${back?.rowSkewDeg.toFixed(2) ?? '—'}°.`; } const regen = $('member-regen'); const fields = $('member-fields'); if (!pier) { fields.replaceChildren(); regen.textContent = '다시 생성 · 변경 0'; regen.disabled = true; } else { fieldRows(fields, pier, member.keys); if (member.id === 'footing') renderPileRows(fields, pier); if (FCK_MEMBERS.includes(member.id as FckMember)) renderMemberFck(fields, member.id as FckMember); regen.textContent = `다시 생성 · 변경 ${pendingKeys(pier).length}`; regen.disabled = pendingKeys(pier).length === 0; } const changes = $('changes'); changes.replaceChildren(); const pending = pier ? pendingKeys(pier) : []; for (const key of pier ? overrideKeys(pier) : []) { const li = el('li', `${FIELD_LABELS[key]}: 기본 ${pier!.defaults[key] ?? '없음'} → 현재 ${pier!.draft[key] ?? '없음'}${pending.includes(key) ? ' (미반영)' : ''}`); const revert = el('button', '되돌리기', 'btn small'); revert.type = 'button'; revert.addEventListener('click', () => revertField(pier!.input.name, key)); li.appendChild(revert); changes.appendChild(li); } if (changes.childElementCount === 0) changes.appendChild(el('li', '기본값과 같습니다.')); const issues = $('plan-issues'); issues.replaceChildren(); for (const i of pier?.issues ?? []) issues.appendChild(el('li', `${i.code} ${i.message}`)); if (issues.childElementCount === 0) issues.appendChild(el('li', '알림이 없습니다.')); } // --- project file (plan B3·B4·B5) -------------------------------------------------------- /** Everything that a save writes except the history, so that a save itself does not look like a change. */ function snapshot(): string { return JSON.stringify({ project: state.project, source: state.source, piers: state.piers.map((p) => ({ input: p.input, defaults: p.defaults, draft: p.draft })), material: state.material, fckByMember: state.fckByMember, }); } function isDirty(): boolean { return state.piers.length > 0 && snapshot() !== state.savedSnapshot; } function currentProjectText(): string { return serializeProject({ schemaVersion: SCHEMA_VERSION, project: { ...state.project }, source: state.source, piers: state.piers.map((p) => ({ id: p.input.name, input: p.input, defaults: p.defaults, draft: p.draft })), history: state.history, common: state.material, fckByMember: state.fckByMember, }); } function renderSaveStatus(): void { const saveBtn = $('save-project'); saveBtn.disabled = state.piers.length === 0; const status = $('save-status'); if (state.piers.length === 0) { status.textContent = '저장 전'; } else if (state.savedSnapshot === null || isDirty()) { status.textContent = state.savedSnapshot === null ? '저장 전 · 변경 있음' : '변경 있음 · 미저장'; } else { status.textContent = '저장됨'; } } /** 저장 button and Ctrl+S: the first save opens the project dialog (시안 ⑤); later saves write the same file. */ function saveProject(): void { if (state.piers.length === 0) return; if (!state.savedOnce) { openSaveDialog(); return; } writeProjectFile(); } function projectFileName(): string { return `${state.project.bridge || state.project.name || '교각설계'}_교각설계.pzr.json`; } function openSaveDialog(): void { $('sv-name').value = state.project.name; $('sv-bridge').value = state.project.bridge; $('sv-client').value = state.project.client; $('sv-author').value = state.project.author; $('sv-memo').value = state.project.memo; $('sv-file').textContent = projectFileName(); $('save-dialog').showModal(); $('sv-name').focus(); } function confirmSaveDialog(): void { const name = $('sv-name').value.trim(); if (!name) { $('sv-name').focus(); setError('프로젝트명을 입력하세요.'); return; } state.project = { name, bridge: $('sv-bridge').value.trim(), client: $('sv-client').value.trim(), author: $('sv-author').value.trim(), crs: CRS, memo: $('sv-memo').value.trim(), }; $('save-dialog').close(); writeProjectFile(); } function writeProjectFile(): void { state.savedOnce = true; state.history = [...state.history, { version: String(state.history.length + 1), note: '저장', at: new Date().toISOString() }]; const text = currentProjectText(); const name = projectFileName(); const url = URL.createObjectURL(new Blob([text], { type: 'application/json' })); const link = document.createElement('a'); link.href = url; link.download = name; link.click(); window.setTimeout(() => URL.revokeObjectURL(url), 1000); state.savedSnapshot = snapshot(); clearBackup().catch(() => undefined); renderHeader(); setStatus(`${name} 파일을 저장했습니다.`); } /** Opens a project from its text. A backup stays unsaved until the user saves it. */ function openProjectText(text: string, saved: boolean): void { const parsed = parseProject(text); state.project = { ...parsed.project }; state.source = parsed.source; state.history = parsed.history; state.material = materialFrom(parsed.common); state.fckByMember = { ...parsed.fckByMember }; state.piers = parsed.piers.map((p) => ({ ...buildEntry(p.input, false), defaults: p.defaults ?? p.input, draft: p.draft ?? p.input, })); state.active = state.piers[0]?.input.name ?? null; state.filter = 'all'; state.pending = null; state.savedSnapshot = saved ? snapshot() : null; setupScene(); renderAll(); } async function openProjectFile(file: File): Promise { try { openProjectText(await file.text(), true); state.savedOnce = true; clearBackup().catch(() => undefined); setStatus(`${file.name}을 열었습니다. ${state.piers.length}기를 원본 엑셀 없이 다시 생성했습니다.`); } catch (err) { setError(`프로젝트 파일을 열지 못했습니다: ${(err as Error).message}`); } } function scheduleBackup(): void { window.clearTimeout(backupTimer); backupTimer = window.setTimeout(() => { if (!isDirty()) return; writeBackup(currentProjectText()).catch(() => setError('자동 백업을 저장하지 못했습니다. 브라우저 저장소 설정을 확인하세요.')); }, 800); } async function checkBackup(): Promise { try { const backup = await readBackup(); if (!backup) return; pendingBackup = backup; $('backup-text').textContent = `저장되지 않은 작업이 있습니다 (${new Date(backup.at).toLocaleString('ko-KR')}).`; $('backup-banner').hidden = false; } catch { // Storage unavailable (private window, blocked): no recovery is offered, and the app works without it. } } // --- import dialog (시안 ④) ------------------------------------------------------------ function selectable(p: Pending, e: Pier): boolean { return !!e.model && sheetOk(p.sheets, e.input.sheet); } function renderDialog(): void { const p = state.pending; const sheetsBox = $('sheets'); sheetsBox.replaceChildren(); $('file-name').textContent = p ? `선택된 파일: ${p.fileName}` : ''; if (!p) { sheetsBox.appendChild(el('li', '파일을 선택하면 시트별 인식 결과가 표시됩니다.')); renderPreview(null); updateApply(); return; } for (const sheet of p.sheets) { const li = el('li'); const head = el('div', undefined); const label = sheet.piers === 0 ? (sheet.ok ? '보조 표 시트 (IP·받침)' : '오류') : sheet.ok ? '선택 가능' : '선택 불가 (오류)'; head.append( el('span', `${sheet.name} · ${label}`, sheet.ok ? 'ok' : 'bad'), document.createTextNode(` · 교각 ${sheet.tables.pier}표 · IP ${sheet.tables.ip}표 · 받침 ${sheet.tables.bearing}표`), ); li.appendChild(head); for (const issue of sheet.issues) { const line = el('div', `${issue.code} ${issue.pier ? `[${issue.pier}] ` : ''}${issue.message}`, 'small'); line.style.color = issue.level === 'E' ? 'var(--error)' : 'var(--muted)'; li.appendChild(line); } for (const entry of p.entries.filter((e) => e.input.sheet === sheet.name)) { const row = el('div', undefined, 'sheet-pier'); const check = el('input'); check.type = 'checkbox'; check.checked = entry.checked; check.disabled = !selectable(p, entry); check.setAttribute('aria-label', `${entry.input.name} 불러오기`); check.addEventListener('change', () => { entry.checked = check.checked; updateApply(); }); const name = el('button', `${entry.input.name} · ${entry.input.type}`, 'pier-name'); name.type = 'button'; name.setAttribute('aria-pressed', String(p.preview === entry.input.name)); name.addEventListener('click', () => { p.preview = entry.input.name; renderDialog(); }); row.append(check, name); li.appendChild(row); } sheetsBox.appendChild(li); } const preview = p.entries.find((e) => e.input.name === p.preview) ?? p.entries.find((e) => selectable(p, e)) ?? null; renderPreview(preview ? preview.input : null); updateApply(); } function renderPreview(input: PierInput | null): void { const title = $('preview-title'); const pierTable = $('pv-pier').tBodies[0]; const bearingBody = $('pv-bearings').tBodies[0]; const ipBody = $('pv-ips').tBodies[0]; pierTable.replaceChildren(); bearingBody.replaceChildren(); ipBody.replaceChildren(); if (!input) { title.textContent = '미리볼 교각이 없습니다.'; return; } title.textContent = `${input.name} · 시트 ${input.sheet}`; const pairs: Pair[] = [ ['교각명', input.name], ['교각형식', input.type], ['두부보 EL (코핑 상면)', f3(input.copingTopEl)], ['기초 하단 EL', f3(input.footingBottomEl)], ['지반고 EL', f3(input.groundEl)], ['기초 형식', input.foundationType], ['말뚝길이 (mm)', String(input.pileLengthMm)], ['두부보 폭 × 길이 (mm)', `${input.copingWidthMm} × ${input.copingLengthMm}`], ['기둥갯수', String(input.columnCount)], ]; for (const [k, v] of pairs) { const tr = el('tr'); tr.append(el('th', k), el('td', v)); pierTable.appendChild(tr); } for (const b of input.bearings) { const tr = el('tr'); const azimuth = Number.isNaN(b.azimuthDeg) ? '오류' : `${b.azimuthDeg.toFixed(4)}°`; for (const v of [String(b.no), b.x.toFixed(3), b.y.toFixed(3), f3(b.z), azimuth, b.row]) tr.appendChild(el('td', v)); bearingBody.appendChild(tr); } for (const ip of input.ips) { const tr = el('tr'); for (const v of [ip.name, ip.x.toFixed(3), ip.y.toFixed(3), `${ip.angleDeg}°`]) tr.appendChild(el('td', v)); ipBody.appendChild(tr); } } function updateApply(): void { const p = state.pending; const count = p ? p.entries.filter((e) => e.checked && selectable(p, e)).length : 0; const total = p ? p.entries.filter((e) => selectable(p, e)).length : 0; $('dlg-selected').textContent = `선택 ${count} / ${total}기`; const named = $('project-name').value.trim().length > 0; const apply = $('dlg-apply'); apply.textContent = `선택한 교각 ${count}기 형상 생성`; apply.disabled = count === 0 || !named; } function loadBytes(bytes: Uint8Array, fileName: string): void { const { piers, sheets } = parseWorkbook(XLSX.read(bytes, { type: 'array' })); const entries = piers.map((input) => buildEntry(input, false)); // A sheet is selectable only if every pier in it builds. Show the reason next to the sheet. for (const sheet of sheets) { const own = entries.filter((e) => e.input.sheet === sheet.name); for (const e of own) sheet.issues.push(...e.issues.filter((i) => i.level === 'E')); sheet.ok = sheet.ok && own.every((e) => e.model !== null); } const pending: Pending = { fileName, entries, sheets, preview: null }; for (const e of entries) e.checked = selectable(pending, e); pending.preview = entries.find((e) => selectable(pending, e))?.input.name ?? entries[0]?.input.name ?? null; state.pending = pending; renderDialog(); } function applyImport(): void { const p = state.pending; const name = $('project-name').value.trim(); if (!p || !name) return; state.project = { ...state.project, name, bridge: $('bridge-name').value.trim() }; state.piers = p.entries.filter((e) => e.checked && selectable(p, e)); state.active = state.piers[0]?.input.name ?? null; state.filter = 'all'; state.pending = null; state.source = { fileName: p.fileName, sheets: p.sheets.map((sheet) => sheet.name) }; state.history = []; state.savedSnapshot = null; state.savedOnce = false; $('import-dialog').close(); setupScene(); renderAll(); setStatus(`${state.piers.length}기 형상을 생성했습니다. 정보·경고는 오른쪽 알림에서 확인하세요.`); } function openImport(): void { const dlg = $('import-dialog'); if (!dlg.open) dlg.showModal(); $('project-name').focus(); } async function fetchSample(): Promise { const res = await fetch(new URL('samples/운곡교P1입력.xlsx', document.baseURI)); if (!res.ok) throw new Error(`샘플 응답 ${res.status}`); return new Uint8Array(await res.arrayBuffer()); } function regenerateChecked(): void { let errors = 0; let count = 0; state.piers = state.piers.map((p) => { if (!p.checked) return p; count++; const next = regenerateEntry(p); if (!next.model) errors++; return next; }); setupScene(); renderAll(); setStatus(`${count}기 형상을 생성했습니다.${errors ? ` 오류 ${errors}기는 생성되지 않았습니다.` : ''}`); } // --- events ----------------------------------------------------------------------------- $('open-import').addEventListener('click', openImport); $('dlg-close').addEventListener('click', () => $('import-dialog').close()); $('dlg-cancel').addEventListener('click', () => $('import-dialog').close()); $('dlg-apply').addEventListener('click', applyImport); $('regen').addEventListener('click', regenerateChecked); $('bulk-regen').addEventListener('click', regenerateChecked); $('bulk-copy').addEventListener('click', openCopyDialog); $('rebar-bulk-copy').addEventListener('click', openCopyDialog); $('nav-shape').addEventListener('click', () => setStage('shape')); $('nav-plan').addEventListener('click', () => setStage('plan')); $('nav-rebar').addEventListener('click', () => setStage('rebar')); $('nav-quantity').addEventListener('click', () => setStage('quantity')); $('mat-fck').addEventListener('change', (ev) => setMaterial('fck', (ev.target as HTMLInputElement).value)); $('mat-waste').addEventListener('change', (ev) => setWaste((ev.target as HTMLInputElement).value)); for (const k of ['small16', 'main25', 'main35', 'main51'] as const) { $(`bend-${k}`).addEventListener('change', (ev) => setBend(k, (ev.target as HTMLInputElement).value)); } for (const k of ['D', 'H', 'S'] as const) { $(`mat-grade-${k.toLowerCase()}`).addEventListener('change', (ev) => setMaterial(k, (ev.target as HTMLSelectElement).value)); } $('member-regen').addEventListener('click', () => { const pier = activePier(); if (pier) regenerateOne(pier.input.name); }); $('cp-same-type').addEventListener('change', () => { renderCopyTargets(); renderCopyPreview(); }); for (const i of document.querySelectorAll('#copy-dialog input[data-item]')) { i.addEventListener('change', renderCopyPreview); } $('cp-regen').addEventListener('change', renderCopyPreview); $('cp-apply').addEventListener('click', applyCopyDialog); $('cp-cancel').addEventListener('click', () => $('copy-dialog').close()); $('cp-close').addEventListener('click', () => $('copy-dialog').close()); $('sv-save').addEventListener('click', confirmSaveDialog); $('sv-cancel').addEventListener('click', () => $('save-dialog').close()); $('sv-close').addEventListener('click', () => $('save-dialog').close()); $('edit-regen').addEventListener('click', () => { const pier = activePier(); if (pier) regenerateOne(pier.input.name); }); $('project-name').addEventListener('input', updateApply); $('pick-file').addEventListener('click', () => $('file').click()); $('download-template').addEventListener('click', () => { XLSX.writeFile(buildTemplateWorkbook(), '교각입력템플릿.xlsx'); }); $('file').addEventListener('change', async (ev) => { const file = (ev.target as HTMLInputElement).files?.[0]; if (!file) return; try { loadBytes(new Uint8Array(await file.arrayBuffer()), file.name); } catch (err) { setError(`엑셀을 읽지 못했습니다: ${(err as Error).message}`); } }); /** The project's standing input and names (RAG/운곡교P1입력.xlsx → 프로젝트 운곡교P1, 교량 운곡교). */ const SAMPLE_PROJECT = { name: '운곡교P1', bridge: '운곡교' } as const; function fillSampleNames(): void { $('project-name').value = SAMPLE_PROJECT.name; $('bridge-name').value = SAMPLE_PROJECT.bridge; // Setting .value does not fire 'input', so the apply button must be refreshed here. updateApply(); } $('pick-sample').addEventListener('click', async () => { try { loadBytes(await fetchSample(), '샘플 P1 (운곡교P1입력.xlsx)'); fillSampleNames(); } catch (err) { setError(`샘플을 불러오지 못했습니다: ${(err as Error).message}`); } }); const drop = $('drop'); drop.addEventListener('dragover', (ev) => { ev.preventDefault(); drop.classList.add('over'); }); drop.addEventListener('dragleave', () => drop.classList.remove('over')); drop.addEventListener('drop', async (ev) => { ev.preventDefault(); drop.classList.remove('over'); const file = ev.dataTransfer?.files?.[0]; if (!file) return; try { loadBytes(new Uint8Array(await file.arrayBuffer()), file.name); } catch (err) { setError(`엑셀을 읽지 못했습니다: ${(err as Error).message}`); } }); $('type-filter').addEventListener('change', (ev) => { state.filter = (ev.target as HTMLSelectElement).value as 'all' | PierType; renderPierList(); }); $('select-all').addEventListener('click', () => { state.piers = setVisibleChecked(state.piers, state.filter, !allVisibleChecked(state.piers, state.filter)); renderAll(); }); $('save-project').addEventListener('click', saveProject); $('open-project').addEventListener('click', () => $('project-file').click()); $('project-file').addEventListener('change', (ev) => { const file = (ev.target as HTMLInputElement).files?.[0]; if (file) void openProjectFile(file); (ev.target as HTMLInputElement).value = ''; }); window.addEventListener('keydown', (ev) => { if ((ev.ctrlKey || ev.metaKey) && ev.key.toLowerCase() === 's') { ev.preventDefault(); saveProject(); } }); window.addEventListener('beforeunload', (ev) => { if (isDirty()) ev.preventDefault(); }); $('restore-backup').addEventListener('click', () => { if (!pendingBackup) return; try { openProjectText(pendingBackup.text, false); setStatus('자동 백업을 복구했습니다. 저장하지 않은 변경이 있습니다.'); } catch (err) { setError(`백업을 복구하지 못했습니다: ${(err as Error).message}`); } pendingBackup = null; $('backup-banner').hidden = true; }); $('discard-backup').addEventListener('click', () => { pendingBackup = null; $('backup-banner').hidden = true; clearBackup().catch(() => undefined); }); // --- start ------------------------------------------------------------------------------ captureViewerHome(); wireRebarPicking(); buildPartToggles(); buildViewButtons(); renderDefaults(); renderDialog(); renderAll(); void checkBackup(); // Deep link for the bundled sample: ?sample=1 loads P1 directly and skips the dialog. if (new URLSearchParams(window.location.search).get('sample') === '1') { fetchSample() .then((bytes) => { loadBytes(bytes, '샘플 P1'); fillSampleNames(); applyImport(); }) .catch((err: Error) => setError(`샘플을 불러오지 못했습니다: ${err.message}`)); }