Files
vibemake/scripts/assembly-tool.html

124 lines
6.0 KiB
HTML

<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>装配 JSON 生成器</title>
<style>
:root { color-scheme: dark; font: 16px/1.6 system-ui, sans-serif; background: #101214; color: #eef2f4; }
* { box-sizing: border-box; }
main { max-width: 700px; margin: 7vh auto; padding: 24px; }
h1 { font-size: clamp(26px, 5vw, 40px); margin: 0; }
p { margin: 8px 0 16px; }
.muted { color: #b2bcc3; }
form, #result { background: #191d21; border: 1px solid #343c43; border-radius: 14px; padding: 24px; }
label { display: grid; gap: 6px; margin-bottom: 18px; font-weight: 600; }
label small { font-weight: 400; }
input, select, button, a { font: inherit; }
input, select { width: 100%; padding: 9px; border: 1px solid #46515b; border-radius: 6px; background: #101214; }
button, a { display: inline-block; padding: 8px 14px; border: 1px solid #9fe262; border-radius: 6px; background: #b8f278; color: #15220f; font-weight: 650; text-decoration: none; cursor: pointer; }
button:disabled { opacity: .55; cursor: wait; }
#status { white-space: pre-wrap; overflow-wrap: anywhere; }
#status[data-error] { color: #ffad9e; }
#result { margin-top: 14px; }
#report { background: transparent; border-color: #64707a; color: #eef2f4; }
.downloads { display: flex; flex-wrap: wrap; gap: 10px; }
</style>
</head>
<body>
<main>
<h1>装配 JSON 生成器</h1>
<p class="muted">选择整机和编号零件,在本机计算装配步骤。</p>
<form id="assembly">
<label>整机 GLB
<input id="whole" type="file" accept=".glb" required>
</label>
<label>零件文件夹
<input id="parts" type="file" webkitdirectory multiple required>
<small class="muted">选择包含编号部位目录或平铺分件的文件夹。支持小组件目录和 +、-、!、# 标记。</small>
</label>
<label>装配方式
<select id="mode">
<option value="sequential">按目录和标记装配</option>
<option value="grouped">部件预装后总装(最后一组为总装散件)</option>
</select>
</label>
<label>内六角扳手 GLB(选填)
<input id="hex-key" type="file" accept=".glb">
<small class="muted">工作端朝模型局部 -Z。六角孔尺寸和安装方向匹配后生成拧入动画。</small>
</label>
<label>附加件位置(选填)
<input id="extra" type="text" placeholder="填写插入前的步号,或 end 放在组末">
<small class="muted">仅用于文件名带「附加」的零件。</small>
</label>
<button id="submit" type="submit">生成装配 JSON</button>
<p id="status" role="status" aria-live="polite"></p>
</form>
<section id="result" hidden>
<p id="summary"></p>
<div class="downloads">
<a id="manifest">下载装配 JSON</a>
<a id="report" download="轴向核对.json">下载核对报告</a>
</div>
</section>
</main>
<script type="module">
const $ = id => document.getElementById(id);
const status = $('status');
const download = (id, filename, data) => {
const link = $(id);
if (link.href.startsWith('blob:')) URL.revokeObjectURL(link.href);
const content = typeof data === 'string' ? data : JSON.stringify(data, null, 2) + '\n';
link.href = URL.createObjectURL(new Blob([content], { type: 'application/json' }));
link.download = filename;
};
$('assembly').addEventListener('submit', async event => {
event.preventDefault();
$('result').hidden = true;
$('submit').disabled = true;
delete status.dataset.error;
status.textContent = '正在读取模型、匹配零件并检查安装方向,请保持页面打开。';
try {
const whole = $('whole').files[0];
const key = $('hex-key').files[0];
const parts = [...$('parts').files]
.map(file => ({ file, path: file.webkitRelativePath.split('/').slice(1).join('/') }))
.filter(part => /\.glb$/i.test(part.path) && !part.path.split('/').some(segment => segment.startsWith('.')))
.filter(part => ![whole, key].some(file => file && part.path === file.name
&& part.file.size === file.size && part.file.lastModified === file.lastModified));
if (!parts.length) throw new Error('所选文件夹内没有分件 GLB。');
const totalBytes = whole.size + (key?.size ?? 0) + parts.reduce((total, part) => total + part.file.size, 0);
if (totalBytes >= 256 * 1024 * 1024) throw new Error('模型总大小须小于 256 MiB。');
const form = new FormData();
form.append('whole', whole, 'whole.glb');
form.append('wholeName', whole.name);
if (key) {
form.append('hexKey', key, 'tool.glb');
form.append('hexKeyName', key.name);
}
form.append('mode', $('mode').value);
form.append('extraBefore', $('extra').value.trim());
form.append('paths', JSON.stringify(parts.map(part => part.path)));
for (const part of parts) form.append('parts', part.file, 'part.glb');
const response = await fetch('/generate', { method: 'POST', body: form });
const result = await response.json();
if (!response.ok) throw new Error(result.error || '生成失败,请检查模型文件。');
download('manifest', result.filename, result.manifest);
download('report', '轴向核对.json', result.report);
const { partCount, steps, needReview, tool } = result.report;
$('summary').textContent = `已生成 ${partCount} 个零件、${steps} 个装配步骤。${needReview ? `有 ${needReview} 步的直线插入方向需复核,详见核对报告。` : '没有待复核步骤。'}`
+ (tool ? ` 匹配 ${tool.matched} 个拧入步骤。${tool.matched ? `上传零件时请把 ${tool.file} 放在零件目录根层。` : ''}` : '');
$('result').hidden = false;
status.textContent = '';
} catch (error) {
status.dataset.error = '';
status.textContent = error.message;
} finally {
$('submit').disabled = false;
}
});
</script>
</body>
</html>