|
|
@@ -1,6 +1,9 @@
|
|
|
/**
|
|
|
- * page/lab.js —— 实验室:单屏不滚。
|
|
|
- * 左(手机为上)Babylon 画布 + 底部可收起坞(形体参数 / BOM / 导出)。
|
|
|
+ * page/lab.js —— 实验室 · 可编辑工程台(单屏不滚)。
|
|
|
+ * 3D 视口占满屏幕,底部浮起三档伸缩坞(peek 20% / half 46% / full 72%):
|
|
|
+ * 形体参数 / 结构件 / BOM / 导出。
|
|
|
+ * 结构件:14 类形体库实例化 → 列表增删 → 单件编辑(高亮 + 相机聚焦 + 数值面板 + 吸附 + 阵列 / 贴壁)。
|
|
|
+ * 视口:点选 / 拖动落位(拖动锁相机,靠内壁 1.5mm 自动贴边)/ 双击复位;撤销重做 40 步。
|
|
|
* Babylon 只在本页初始化;离开页面(自定义元素 disconnected)时 dispose 引擎与监听。
|
|
|
*/
|
|
|
import { html } from 'lit';
|
|
|
@@ -11,6 +14,14 @@ import { KEY_LABEL, rangeFor, stepFor } from '../utils/format.js';
|
|
|
import { linkHref, currentPath } from '../utils/router.js';
|
|
|
import { labStyle } from './styles/lab-style.js';
|
|
|
import { applyCss } from '../lib/css.js';
|
|
|
+import { lsGet, lsSet } from '../utils/storage.js';
|
|
|
+import {
|
|
|
+ PRIMS, PRIM_KEYS, cavity, primRequirement, primDimText, ruleKey, procOptions, derived,
|
|
|
+} from '../core/geometry/solid.js';
|
|
|
+import {
|
|
|
+ createPart, clonePart, arrayRing, mirrorPart, snapToInnerWall, validate, paramSchema, dimText,
|
|
|
+ PROC_RULES, PROC_KEYS, resolveProc, reserveUids, footprint, fromFeatures,
|
|
|
+} from '../core/geometry/parts.js';
|
|
|
|
|
|
let babylonPromise = null;
|
|
|
function ensureBabylon() {
|
|
|
@@ -28,6 +39,32 @@ function ensureBabylon() {
|
|
|
}
|
|
|
|
|
|
const HIDE_KEYS = new Set(['dBottom']); // 底部外径由壁厚/拔模推导,面板不单独暴露
|
|
|
+const DOCK = { peek: 0.2, half: 0.46, full: 0.72 };
|
|
|
+const SNAP_STEPS = [0.5, 1, 5];
|
|
|
+const UNDO_MAX = 40;
|
|
|
+const buzz = (ms = 8) => { try { navigator.vibrate?.(ms); } catch { /* 不支持则忽略 */ } };
|
|
|
+const fmt = (v, d = 1) => (Math.round((Number(v) || 0) * 10 ** d) / 10 ** d).toFixed(d);
|
|
|
+const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
|
+
|
|
|
+/** 14 类形体的简图(24×24 viewBox 的 SVG 片段,只用当前文字色描边) */
|
|
|
+export function primIcon(type) {
|
|
|
+ const S = 'fill="none" stroke="currentColor" stroke-width="1.3" stroke-linejoin="round"';
|
|
|
+ switch (type) {
|
|
|
+ case 'box': return `<rect x="4" y="7" width="13" height="10" ${S}/><path d="M4 7l4-3h13v10l-4 3M17 7l4-3M17 17l4-3" ${S}/>`;
|
|
|
+ case 'cyl': return `<ellipse cx="12" cy="6" rx="6" ry="2.6" ${S}/><path d="M6 6v11c0 1.4 2.7 2.6 6 2.6s6-1.2 6-2.6V6" ${S}/>`;
|
|
|
+ case 'rib': return `<path d="M5 19V8h3v11M11 19V8h3v11M17 19V8h2" ${S}/>`;
|
|
|
+ case 'pocket': return `<rect x="3" y="4" width="18" height="16" rx="2" ${S}/><rect x="7" y="8" width="10" height="8" rx="1" ${S} stroke-dasharray="2 1.5"/>`;
|
|
|
+ case 'hole': return `<circle cx="12" cy="12" r="7" ${S} stroke-dasharray="2.5 1.8"/><circle cx="12" cy="12" r="3" ${S}/>`;
|
|
|
+ case 'boss2': case 'boss25': case 'boss3': return `<ellipse cx="12" cy="5" rx="5" ry="2" ${S}/><path d="M7 5v13c0 1.1 2.2 2 5 2s5-.9 5-2V5" ${S}/><ellipse cx="12" cy="5" rx="2" ry="1" ${S}/><path d="M10 9v11M14 9v11" ${S} stroke-dasharray="1.5 1.5"/>`;
|
|
|
+ case 'thru25': return `<rect x="3" y="3" width="18" height="18" rx="2" ${S}/><circle cx="12" cy="12" r="3.2" ${S}/><path d="M12 4v4M12 16v4M4 12h4M16 12h4" ${S}/>`;
|
|
|
+ case 'pin3': return `<path d="M12 2v3" ${S}/><ellipse cx="12" cy="7" rx="3" ry="1.4" ${S}/><path d="M9 7v11c0 .9 1.3 1.6 3 1.6s3-.7 3-1.6V7" ${S}/>`;
|
|
|
+ case 'pad4': return `<rect x="4" y="5" width="16" height="14" rx="2" ${S}/>${[[7, 8], [17, 8], [7, 16], [17, 16]].map(([x, y]) => `<circle cx="${x}" cy="${y}" r="1.9" ${S}/>`).join('')}`;
|
|
|
+ case 'wall': return `<rect x="3" y="5" width="18" height="14" rx="2" ${S}/><path d="M12 5v14M3 12h6M15 12h6" ${S}/>`;
|
|
|
+ case 'tray': return `<rect x="4" y="4" width="16" height="16" rx="2" ${S}/><rect x="7" y="7" width="10" height="10" rx="1" ${S}/><path d="M12 7v-3M9 4h6" ${S}/>`;
|
|
|
+ case 'clip': return `<path d="M6 20V8c0-2 .8-3 2-3s2 1 2 3v8" ${S}/><path d="M10 10h5V5" ${S}/>`;
|
|
|
+ default: return `<rect x="5" y="5" width="14" height="14" rx="3" ${S}/>`;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
class PhLab extends PhPage {
|
|
|
constructor() {
|
|
|
@@ -36,11 +73,17 @@ class PhLab extends PhPage {
|
|
|
this.p = null;
|
|
|
this.error = null;
|
|
|
this.tab = 'shape';
|
|
|
- this.dockOpen = true;
|
|
|
+ this.dock = 'half';
|
|
|
this.params = {};
|
|
|
- this.touched = {};
|
|
|
+ this.parts = [];
|
|
|
+ this.selUid = null;
|
|
|
+ this.picker = false;
|
|
|
+ this.snapStep = 1;
|
|
|
+ this.undoStack = [];
|
|
|
+ this.redoStack = [];
|
|
|
this.viewer = null;
|
|
|
this.status = '';
|
|
|
+ this.rk = 'injection';
|
|
|
this.title = '实验室 · Pocket Hardware Lab · FmodeCode';
|
|
|
}
|
|
|
|
|
|
@@ -58,14 +101,25 @@ class PhLab extends PhPage {
|
|
|
|
|
|
async _load() {
|
|
|
this.p = await loadProduct(this.id);
|
|
|
- this.params = { ...(this.p.shape?.defaults || {}) };
|
|
|
+ const base = this.p.shape?.defaults || {};
|
|
|
+ const saved = lsGet('lab:' + this.p.id);
|
|
|
+ this.params = saved?.params ? { ...base, ...saved.params } : { ...base };
|
|
|
+ this.parts = (saved?.parts || []).filter((x) => x && PRIMS[x.type]);
|
|
|
+ this.rk = saved?.rk || this._defaultRk();
|
|
|
+ this.dock = saved?.dock || 'half';
|
|
|
+ reserveUids(this.parts);
|
|
|
this.title = `${this.p.name} · 实验室 · Pocket Hardware Lab`;
|
|
|
+ this._bindKeys();
|
|
|
this.requestUpdate();
|
|
|
await this.updateComplete;
|
|
|
if (!this.isConnected) return;
|
|
|
await this._mount3d();
|
|
|
}
|
|
|
|
|
|
+ _defaultRk() {
|
|
|
+ return resolveProc((this.p.processes || [])[0]?.id || 'injection');
|
|
|
+ }
|
|
|
+
|
|
|
async _mount3d() {
|
|
|
const canvas = this.querySelector('canvas');
|
|
|
if (!canvas) return;
|
|
|
@@ -75,15 +129,131 @@ class PhLab extends PhPage {
|
|
|
if (!this.isConnected) return;
|
|
|
const { ShellViewer } = await import('../services/viewer3d.js');
|
|
|
this.viewer = new ShellViewer(canvas, this.p);
|
|
|
+ this.viewer._snapStep = this.snapStep;
|
|
|
this.viewer.setParams(this.params);
|
|
|
+ this.viewer.setParts(this.parts);
|
|
|
+ this._wireViewer();
|
|
|
this.viewer._onChange = () => this.requestUpdate();
|
|
|
+ this.viewer.bgPointer = () => { if (this.selUid) this._select(null); };
|
|
|
this._statsLoop();
|
|
|
+ this._setDockPx(this.dock, 0);
|
|
|
+ this._watchViewport();
|
|
|
} catch (e) {
|
|
|
console.warn(e);
|
|
|
this.error = e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ _wireViewer() {
|
|
|
+ const v = this.viewer;
|
|
|
+ v.onSelect = (uid, mesh) => {
|
|
|
+ if (uid && uid !== this.selUid) {
|
|
|
+ this._select(uid);
|
|
|
+ } else if (uid === null && this.selUid) {
|
|
|
+ this.selUid = null;
|
|
|
+ this.requestUpdate();
|
|
|
+ }
|
|
|
+ void mesh;
|
|
|
+ };
|
|
|
+ v.onDragEnd = (uid, at) => {
|
|
|
+ const part = this.parts.find((x) => x.uid === uid);
|
|
|
+ if (!part) return;
|
|
|
+ this._pushUndo();
|
|
|
+ part.at = { ...part.at, x: at.x, z: at.z };
|
|
|
+ v.syncPart(part);
|
|
|
+ this._afterChange(0);
|
|
|
+ buzz(8);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ---------------- 状态:撤销 / 重做 / 持久化 ---------------- */
|
|
|
+ _snapshot() {
|
|
|
+ return JSON.stringify({ params: this.params, parts: this.parts, selUid: this.selUid });
|
|
|
+ }
|
|
|
+
|
|
|
+ _pushUndo() {
|
|
|
+ const snap = this._snapshot();
|
|
|
+ if (this.undoStack[this.undoStack.length - 1] === snap) return;
|
|
|
+ this.undoStack.push(snap);
|
|
|
+ if (this.undoStack.length > UNDO_MAX) this.undoStack.shift();
|
|
|
+ this.redoStack = [];
|
|
|
+ this._syncHistoryBtns();
|
|
|
+ }
|
|
|
+
|
|
|
+ _restore(snap) {
|
|
|
+ const s = JSON.parse(snap);
|
|
|
+ this.params = s.params;
|
|
|
+ this.parts = s.parts;
|
|
|
+ this.selUid = s.selUid && this.parts.some((p) => p.uid === s.selUid) ? s.selUid : null;
|
|
|
+ this.viewer?.setParams(this.params);
|
|
|
+ this.viewer?.setParts(this.parts);
|
|
|
+ if (this.selUid) this._enterEdit(this.selUid, false);
|
|
|
+ this.requestUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ undo() {
|
|
|
+ const snap = this.undoStack.pop();
|
|
|
+ if (!snap) return;
|
|
|
+ this.redoStack.push(this._snapshot());
|
|
|
+ this._restore(snap);
|
|
|
+ this._syncHistoryBtns();
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ redo() {
|
|
|
+ const snap = this.redoStack.pop();
|
|
|
+ if (!snap) return;
|
|
|
+ this.undoStack.push(this._snapshot());
|
|
|
+ this._restore(snap);
|
|
|
+ this._syncHistoryBtns();
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ _syncHistoryBtns() {
|
|
|
+ const u = this.querySelector('.hist .undo'), r = this.querySelector('.hist .redo');
|
|
|
+ if (u) u.disabled = !this.undoStack.length;
|
|
|
+ if (r) r.disabled = !this.redoStack.length;
|
|
|
+ }
|
|
|
+
|
|
|
+ _saveSoon() {
|
|
|
+ clearTimeout(this._saveT);
|
|
|
+ this._saveT = setTimeout(() => {
|
|
|
+ lsSet('lab:' + this.p.id, { params: this.params, parts: this.parts, rk: this.rk, dock: this.dock, v: 3 });
|
|
|
+ }, 260);
|
|
|
+ }
|
|
|
+
|
|
|
+ _bindKeys() {
|
|
|
+ if (this._keysBound) return;
|
|
|
+ this._keysBound = true;
|
|
|
+ this._onKey = (e) => {
|
|
|
+ if (!this.isConnected) return;
|
|
|
+ const tag = (e.target?.tagName || '').toLowerCase();
|
|
|
+ const typing = tag === 'input' || tag === 'textarea';
|
|
|
+ if ((e.ctrlKey || e.metaKey) && (e.key === 'z' || e.key === 'Z')) {
|
|
|
+ if (typing) return;
|
|
|
+ e.preventDefault();
|
|
|
+ if (e.shiftKey) this.redo(); else this.undo();
|
|
|
+ } else if ((e.ctrlKey || e.metaKey) && (e.key === 'y' || e.key === 'Y')) {
|
|
|
+ e.preventDefault();
|
|
|
+ this.redo();
|
|
|
+ } else if (e.key === 'Escape' && this.selUid && !this.picker) {
|
|
|
+ this._select(null);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ addEventListener('keydown', this._onKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ disconnectedCallback() {
|
|
|
+ super.disconnectedCallback();
|
|
|
+ if (this._statRaf) cancelAnimationFrame(this._statRaf);
|
|
|
+ if (this._keysBound) removeEventListener('keydown', this._onKey);
|
|
|
+ this._keysBound = false;
|
|
|
+ this._unwatchViewport?.();
|
|
|
+ clearTimeout(this._saveT);
|
|
|
+ this.viewer?.dispose();
|
|
|
+ this.viewer = null;
|
|
|
+ }
|
|
|
+
|
|
|
_statsLoop() {
|
|
|
const tick = () => {
|
|
|
if (!this.isConnected || !this.viewer) return;
|
|
|
@@ -91,86 +261,483 @@ class PhLab extends PhPage {
|
|
|
if (s) {
|
|
|
this.stats = s;
|
|
|
const el = this.querySelector('.live');
|
|
|
- if (el) el.textContent = `${s.positions} 顶点 · ${s.indices} 三角面`;
|
|
|
+ if (el) el.textContent = `${s.positions + (s.partVertices || 0)} 顶点 · ${s.parts} 结构件`;
|
|
|
}
|
|
|
this._statRaf = requestAnimationFrame(tick);
|
|
|
};
|
|
|
this._statRaf = requestAnimationFrame(tick);
|
|
|
}
|
|
|
|
|
|
- disconnectedCallback() {
|
|
|
- super.disconnectedCallback();
|
|
|
- if (this._statRaf) cancelAnimationFrame(this._statRaf);
|
|
|
- this.viewer?.dispose();
|
|
|
- this.viewer = null;
|
|
|
- }
|
|
|
-
|
|
|
- _set(key, value) {
|
|
|
+ /* ---------------- 形体参数 ---------------- */
|
|
|
+ _set(key, value, push = true) {
|
|
|
const v = Number(value);
|
|
|
if (!Number.isFinite(v)) return;
|
|
|
+ if (push && v !== this.params[key]) this._pushUndo();
|
|
|
this.params = { ...this.params, [key]: v };
|
|
|
this.viewer?.setParams({ [key]: v });
|
|
|
this.requestUpdate();
|
|
|
+ this._saveSoon();
|
|
|
}
|
|
|
|
|
|
_resetAll() {
|
|
|
+ this._pushUndo();
|
|
|
this.params = { ...(this.p.shape?.defaults || {}) };
|
|
|
this.viewer?.setParams(this.params);
|
|
|
this.requestUpdate();
|
|
|
+ this._saveSoon();
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ---------------- 结构件:选中 / 编辑 ---------------- */
|
|
|
+ get sel() { return this.parts.find((p) => p.uid === this.selUid) || null; }
|
|
|
+
|
|
|
+ get checks() { return validate(this.parts, this._shellDim(), this.rk); }
|
|
|
+
|
|
|
+ /** 落位包络:内腔 XZ + 壳高(工艺校验与贴壁共用) */
|
|
|
+ _shellDim() {
|
|
|
+ const c = cavity(this.p.shape?.type, this.params);
|
|
|
+ const h = Number(this.params.height) || 8;
|
|
|
+ if (c.kind === 'cyl') return { kind: 'cyl', r: c.r, h, wall: Number(this.params.wall) };
|
|
|
+ return { kind: 'rect', w: c.halfX * 2, d: c.halfZ * 2, h, wall: Number(this.params.wall) };
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 新建件的落位:内腔底部、离壁 6mm 处,按已有件数错开 */
|
|
|
+ _spawnAt() {
|
|
|
+ const sd = this._shellDim();
|
|
|
+ const n = this.parts.length;
|
|
|
+ if (sd.kind === 'cyl') {
|
|
|
+ const r = Math.max(0, sd.r * 0.55);
|
|
|
+ const a = n * 0.7;
|
|
|
+ return { x: Math.round(r * Math.cos(a) * 2) / 2, y: 0, z: Math.round(-r * Math.sin(a) * 2) / 2 };
|
|
|
+ }
|
|
|
+ return { x: 0, y: 0, z: Math.max(0, Math.round((sd.d / 2 - 8) * 2) / 2) };
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 选中 / 取消选中;列表里点的是「进入编辑」,回到列表走 exitEdit() */
|
|
|
+ _select(uid) {
|
|
|
+ if (uid === this.selUid) return;
|
|
|
+ this.selUid = uid;
|
|
|
+ if (uid) this._enterEdit(uid, true);
|
|
|
+ else { this.viewer?.applySelection(null); this.viewer?.homeView(false); }
|
|
|
+ this.requestUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 单件编辑 → 结构件列表(退出选中态、恢复全局视图) */
|
|
|
+ exitEdit() {
|
|
|
+ this._select(null);
|
|
|
+ if (this.tab === 'parts') this.requestUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ _enterEdit(uid, focus = true) {
|
|
|
+ const part = this.parts.find((p) => p.uid === uid);
|
|
|
+ if (!part) return;
|
|
|
+ this.viewer?.applySelection(uid);
|
|
|
+ if (focus) this.viewer?.focusPart(part);
|
|
|
+ }
|
|
|
+
|
|
|
+ _addPart(type) {
|
|
|
+ if (!PRIMS[type]) return;
|
|
|
+ this._pushUndo();
|
|
|
+ const part = createPart(type, this._spawnAt());
|
|
|
+ this.parts = [...this.parts, part];
|
|
|
+ this.picker = false;
|
|
|
+ this.tab = 'parts';
|
|
|
+ this.dock = this.dock === 'peek' ? 'half' : this.dock;
|
|
|
+ this.viewer?.setParts(this.parts);
|
|
|
+ this._setDockPx(this.dock, 0);
|
|
|
+ this.selUid = part.uid;
|
|
|
+ this._enterEdit(part.uid);
|
|
|
+ this.requestUpdate();
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ _applyFeatures() {
|
|
|
+ const feats = this.p.features || [];
|
|
|
+ if (!feats.length) return;
|
|
|
+ this._pushUndo();
|
|
|
+ const fresh = fromFeatures(feats);
|
|
|
+ reserveUids(fresh);
|
|
|
+ this.parts = [...this.parts, ...fresh];
|
|
|
+ for (const part of this.parts) {
|
|
|
+ if (['boss2', 'boss25', 'boss3', 'rib', 'wall', 'clip'].includes(part.type)) {
|
|
|
+ if (Math.abs(Number(part.at?.y) || 0) < 1e-6) part.at = { ...part.at, y: 0 };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.viewer?.setParts(this.parts);
|
|
|
+ this.requestUpdate();
|
|
|
+ this._saveSoon();
|
|
|
+ this.status = `已应用 ${fresh.length} 个推荐结构件`;
|
|
|
+ }
|
|
|
+
|
|
|
+ _updatePart(uid, fn) {
|
|
|
+ const i = this.parts.findIndex((p) => p.uid === uid);
|
|
|
+ if (i < 0) return;
|
|
|
+ const next = fn({ ...this.parts[i], params: { ...this.parts[i].params }, at: { ...this.parts[i].at } });
|
|
|
+ if (!next) return;
|
|
|
+ this.parts = this.parts.map((p, k) => (k === i ? next : p));
|
|
|
+ this.viewer?.updatePart(next);
|
|
|
+ this.requestUpdate();
|
|
|
+ this._saveSoon();
|
|
|
+ }
|
|
|
+
|
|
|
+ _setPartParam(uid, key, value, push = true) {
|
|
|
+ const v = Number(value);
|
|
|
+ if (!Number.isFinite(v)) return;
|
|
|
+ const cur = this.parts.find((p) => p.uid === uid);
|
|
|
+ if (!cur || cur.params[key] === v) return;
|
|
|
+ if (push) this._pushUndo();
|
|
|
+ this._updatePart(uid, (p) => { p.params[key] = v; return p; });
|
|
|
+ }
|
|
|
+
|
|
|
+ _setPartAt(uid, axis, value, push = true) {
|
|
|
+ const v = Number(value);
|
|
|
+ if (!Number.isFinite(v)) return;
|
|
|
+ const cur = this.parts.find((p) => p.uid === uid);
|
|
|
+ if (!cur || cur.at[axis] === v) return;
|
|
|
+ if (push) this._pushUndo();
|
|
|
+ this._updatePart(uid, (p) => { p.at[axis] = v; return p; });
|
|
|
+ }
|
|
|
+
|
|
|
+ _setRot(uid, deg) {
|
|
|
+ const v = Number(deg);
|
|
|
+ if (!Number.isFinite(v)) return;
|
|
|
+ this._pushUndo();
|
|
|
+ this._updatePart(uid, (p) => { p.rotY = Math.round(v * 10) / 10; return p; });
|
|
|
+ }
|
|
|
+
|
|
|
+ _toggleVisible(uid) {
|
|
|
+ this._pushUndo();
|
|
|
+ this._updatePart(uid, (p) => { p.visible = !p.visible; return p; });
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ _deletePart(uid) {
|
|
|
+ this._pushUndo();
|
|
|
+ this.parts = this.parts.filter((p) => p.uid !== uid);
|
|
|
+ if (this.selUid === uid) { this.selUid = null; this.viewer?.applySelection(null); this.viewer?.homeView(false); }
|
|
|
+ this.viewer?.setParts(this.parts);
|
|
|
+ this.requestUpdate();
|
|
|
+ this._saveSoon();
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ _clone(uid) {
|
|
|
+ const part = this.parts.find((p) => p.uid === uid);
|
|
|
+ if (!part) return;
|
|
|
+ const fp = footprint(part) * 2 + 2;
|
|
|
+ this._pushUndo();
|
|
|
+ const c = clonePart(part, fp);
|
|
|
+ this.parts = [...this.parts, c];
|
|
|
+ this.viewer?.setParts(this.parts);
|
|
|
+ this.selUid = c.uid;
|
|
|
+ this._enterEdit(c.uid, false);
|
|
|
+ this.requestUpdate();
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ _mirror(uid) {
|
|
|
+ const part = this.parts.find((p) => p.uid === uid);
|
|
|
+ if (!part) return;
|
|
|
+ this._pushUndo();
|
|
|
+ const c = mirrorPart(part);
|
|
|
+ this.parts = [...this.parts, c];
|
|
|
+ this.viewer?.setParts(this.parts);
|
|
|
+ this.requestUpdate();
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ _ring(uid, n) {
|
|
|
+ const part = this.parts.find((p) => p.uid === uid);
|
|
|
+ if (!part) return;
|
|
|
+ this._pushUndo();
|
|
|
+ const r = Math.hypot(Number(part.at?.x) || 0, Number(part.at?.z) || 0) || Math.max(8, this._shellDim().r ? this._shellDim().r * 0.55 : 20);
|
|
|
+ const ring = arrayRing(part, n, r);
|
|
|
+ this.parts = [...this.parts, ...ring.slice(1)];
|
|
|
+ this.viewer?.setParts(this.parts);
|
|
|
+ this.requestUpdate();
|
|
|
+ this._saveSoon();
|
|
|
+ buzz(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ _tool(uid, kind) {
|
|
|
+ const part = this.parts.find((p) => p.uid === uid);
|
|
|
+ if (!part) return;
|
|
|
+ const sd = this._shellDim();
|
|
|
+ const h = Number(part.params.h) || 1;
|
|
|
+ if (kind === 'center') {
|
|
|
+ this._pushUndo();
|
|
|
+ this._updatePart(uid, (p) => { p.at.x = 0; p.at.z = 0; return p; });
|
|
|
+ } else if (kind === 'wall') {
|
|
|
+ this._pushUndo();
|
|
|
+ const at = snapToInnerWall(part, sd, 0.5);
|
|
|
+ this._updatePart(uid, (p) => { p.at = { x: at.x, y: p.at.y, z: at.z }; return p; });
|
|
|
+ } else if (kind === 'top') {
|
|
|
+ this._pushUndo();
|
|
|
+ this._updatePart(uid, (p) => { p.at.y = Math.round((sd.h - h) * 10) / 10; return p; });
|
|
|
+ } else if (kind === 'bottom') {
|
|
|
+ this._pushUndo();
|
|
|
+ this._updatePart(uid, (p) => { p.at.y = 0; return p; });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _snapStepChange(v) {
|
|
|
+ this.snapStep = Number(v);
|
|
|
+ if (this.viewer) this.viewer._snapStep = this.snapStep;
|
|
|
+ this.requestUpdate();
|
|
|
}
|
|
|
|
|
|
- _export() {
|
|
|
- const payload = { id: this.p.id, name: this.p.name, shapeType: this.p.shape?.type, exportedAt: new Date().toISOString(), params: this.params };
|
|
|
- const blob = new Blob([JSON.stringify(payload, null, 2)], { type: 'application/json' });
|
|
|
+ _afterChange() { this.requestUpdate(); }
|
|
|
+
|
|
|
+ /* ---------------- 坞:三档吸附 ---------------- */
|
|
|
+ _setDockPx(name, animateMs = 300) {
|
|
|
+ this.dock = name;
|
|
|
+ const el = this.querySelector('.dock');
|
|
|
+ if (!el) return;
|
|
|
+ const h = Math.round(innerHeight * DOCK[name]);
|
|
|
+ el.style.transitionDuration = `${animateMs}ms`;
|
|
|
+ const px = name === 'peek' ? Math.max(56, h) : h;
|
|
|
+ el.style.height = `${px}px`;
|
|
|
+ // 3D 视口随坞收缩:gl-stage 的 bottom 用这个变量,避免模型被坞压住
|
|
|
+ this.style.setProperty('--dock-h', `${px}px`);
|
|
|
+ if (name === 'peek') el.classList.add('min');
|
|
|
+ else el.classList.remove('min');
|
|
|
+ this._saveSoon();
|
|
|
+ }
|
|
|
+
|
|
|
+ _nearestDock(px) {
|
|
|
+ let best = 'half', bd = Infinity;
|
|
|
+ for (const [k, r] of Object.entries(DOCK)) {
|
|
|
+ const d = Math.abs(px - innerHeight * r);
|
|
|
+ if (d < bd) { bd = d; best = k; }
|
|
|
+ }
|
|
|
+ return best;
|
|
|
+ }
|
|
|
+
|
|
|
+ _bindHead() {
|
|
|
+ const head = this.querySelector('.dock-head');
|
|
|
+ if (!head || head._bound) return;
|
|
|
+ head._bound = true;
|
|
|
+ let dragging = false, startY = 0, h0 = 0, moved = false;
|
|
|
+ head.addEventListener('pointerdown', (e) => {
|
|
|
+ if (e.target.closest('button, input')) return;
|
|
|
+ dragging = true; moved = false;
|
|
|
+ startY = e.clientY;
|
|
|
+ const el = this.querySelector('.dock');
|
|
|
+ h0 = el.getBoundingClientRect().height;
|
|
|
+ el.style.transitionDuration = '0ms';
|
|
|
+ head.setPointerCapture?.(e.pointerId);
|
|
|
+ });
|
|
|
+ head.addEventListener('pointermove', (e) => {
|
|
|
+ if (!dragging) return;
|
|
|
+ const dy = startY - e.clientY;
|
|
|
+ if (Math.abs(dy) > 4) moved = true;
|
|
|
+ const h = Math.max(56, Math.min(innerHeight * 0.86, h0 + dy));
|
|
|
+ this.querySelector('.dock').style.height = `${h}px`;
|
|
|
+ this.style.setProperty('--dock-h', `${Math.round(h)}px`);
|
|
|
+ });
|
|
|
+ const end = () => {
|
|
|
+ if (!dragging) return;
|
|
|
+ dragging = false;
|
|
|
+ const el = this.querySelector('.dock');
|
|
|
+ const h = el.getBoundingClientRect().height;
|
|
|
+ if (!moved) {
|
|
|
+ this._setDockPx(this.dock === 'peek' ? 'half' : 'peek', 300);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this._setDockPx(this._nearestDock(h), 300);
|
|
|
+ };
|
|
|
+ head.addEventListener('pointerup', end);
|
|
|
+ head.addEventListener('pointercancel', end);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 键盘弹出:visualViewport 变矮时把坞顶到键盘上方,并为坞加内边距 */
|
|
|
+ _watchViewport() {
|
|
|
+ const vv = window.visualViewport;
|
|
|
+ if (!vv) return;
|
|
|
+ const onVv = () => {
|
|
|
+ const kb = Math.max(0, innerHeight - vv.height - vv.offsetTop);
|
|
|
+ this.style.setProperty('--kb', `${Math.round(kb)}px`);
|
|
|
+ this.style.setProperty('--dock-max', `${Math.round(vv.height)}px`);
|
|
|
+ };
|
|
|
+ vv.addEventListener('resize', onVv);
|
|
|
+ vv.addEventListener('scroll', onVv);
|
|
|
+ onVv();
|
|
|
+ this._unwatchViewport = () => { vv.removeEventListener('resize', onVv); vv.removeEventListener('scroll', onVv); };
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ---------------- 导出 ---------------- */
|
|
|
+ _download(name, data, type) {
|
|
|
+ const blob = data instanceof Blob ? data : new Blob([data], { type });
|
|
|
+ const url = URL.createObjectURL(blob);
|
|
|
const a = document.createElement('a');
|
|
|
- a.href = URL.createObjectURL(blob);
|
|
|
- a.download = `${this.p.id}-shape.json`;
|
|
|
+ a.href = url;
|
|
|
+ a.download = name;
|
|
|
document.body.appendChild(a);
|
|
|
a.click();
|
|
|
a.remove();
|
|
|
- setTimeout(() => URL.revokeObjectURL(a.href), 1000);
|
|
|
+ setTimeout(() => URL.revokeObjectURL(url), 1500);
|
|
|
+ this.status = '已导出 ' + name;
|
|
|
+ this.requestUpdate();
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ _exportJson() {
|
|
|
+ const payload = {
|
|
|
+ app: 'Pocket Hardware Lab', version: 'v3', generator: 'FmodeCode', exportedAt: new Date().toISOString(), unit: 'mm',
|
|
|
+ id: this.p.id, name: this.p.name, shapeType: this.p.shape?.type, process: this.rk,
|
|
|
+ params: this.params, parts: this.parts, cav: this._shellDim(),
|
|
|
+ };
|
|
|
+ this._download(`${this.p.id}-engineering.json`, JSON.stringify(payload, null, 2), 'application/json');
|
|
|
+ }
|
|
|
+
|
|
|
+ _exportStl(onlyUid = null) {
|
|
|
+ if (!this.viewer) return;
|
|
|
+ const { buffer, triangles } = this.viewer.exportSTL(onlyUid);
|
|
|
+ if (!triangles) { this.status = '没有可导出的网格'; this.requestUpdate(); return; }
|
|
|
+ const name = onlyUid ? `${this.p.id}-${onlyUid}.stl` : `${this.p.id}-assembly.stl`;
|
|
|
+ this._download(name, buffer, 'model/stl');
|
|
|
+ this.status = `已导出 ${name}(${triangles} 三角面)`;
|
|
|
+ this.requestUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ _exportAllStl() {
|
|
|
+ for (const part of this.parts) {
|
|
|
+ if (!part.visible) continue;
|
|
|
+ const { buffer, triangles } = this.viewer.exportSTL(part.uid);
|
|
|
+ if (triangles) this._download(`${this.p.id}-${part.uid}.stl`, buffer, 'model/stl');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _exportBomCsv() {
|
|
|
+ const rows = [['序号', '分组', '名称', '规格', '供应商', '单价', '数量', '小计']];
|
|
|
+ for (const r of this.p.bom?.rows || []) rows.push([r.seq, r.group || '', r.name, r.spec || '', r.supplier || '', r.price, r.qty, r.subtotal]);
|
|
|
+ rows.push(['', '', '合计', '', '', '', '', this.p.bom?.bomTotal ?? '']);
|
|
|
+ const csv = '' + rows.map((r) => r.map((v) => `"${String(v ?? '').replace(/"/g, '""')}"`).join(',')).join('\n');
|
|
|
+ this._download(`${this.p.id}-bom.csv`, csv, 'text/csv;charset=utf-8');
|
|
|
+ }
|
|
|
+
|
|
|
+ async _exportPng() {
|
|
|
+ if (!this.viewer) return;
|
|
|
+ const data = await this.viewer.screenshot(2);
|
|
|
+ if (!data) { this.status = '截图失败(当前渲染后端不支持)'; this.requestUpdate(); return; }
|
|
|
+ const a = document.createElement('a');
|
|
|
+ a.href = data;
|
|
|
+ a.download = `${this.p.id}-${this.selUid || 'view'}.png`;
|
|
|
+ document.body.appendChild(a); a.click(); a.remove();
|
|
|
this.status = '已导出 ' + a.download;
|
|
|
this.requestUpdate();
|
|
|
}
|
|
|
|
|
|
+ /** 生产需求单(Markdown):参数快照 + 公差 + 装配 + 结构件坐标 + CNC 弧面 + 报价回填 */
|
|
|
+ specMarkdown() {
|
|
|
+ const p = this.p, params = this.params, rk = this.rk, rule = PROC_RULES[rk];
|
|
|
+ const L = [];
|
|
|
+ const date = new Date().toISOString().slice(0, 10);
|
|
|
+ L.push(`# 生产需求单 · ${p.name}`, '');
|
|
|
+ L.push(`- 型号:${p.latin || p.name}(${p.id})`);
|
|
|
+ L.push(`- 日期:${date};单位 mm / °;工艺:${rule.name}`);
|
|
|
+ L.push('- 本单由 Pocket Hardware Lab 实验室按当前 3D 参数快照自动生成(FmodeCode)', '');
|
|
|
+ L.push('## 1. 参数快照', '', '| 参数 | 键 | 数值 |', '| --- | --- | --- |');
|
|
|
+ for (const [k, v] of Object.entries(params)) if (typeof v === 'number') L.push(`| ${KEY_LABEL[k] || k} | ${k} | ${fmt(v, 2)} |`);
|
|
|
+ L.push('', '派生尺寸:', ...derived(p.shape?.type, params, {}).map(([k, v]) => `- ${k}:${v}`), '');
|
|
|
+ const arcs = ['domeR', 'recessR', 'recessDepth', 'chamferC', 'platform'].filter((k) => params[k] !== undefined);
|
|
|
+ L.push('**CNC 弧面编程参数**:' + arcs.map((k) => `${KEY_LABEL[k] || k} ${fmt(params[k], 2)}`).join(';') + '。');
|
|
|
+ L.push('> 弧面用 R≥1 球头 / 圆鼻刀连续走刀,过渡区禁止接刀台阶;关键尺寸 ±0.05 出首件报告。', '');
|
|
|
+ L.push('## 2. 公差与验收', '', '| 部位 | 公差 |', '| --- | --- |');
|
|
|
+ for (const pt of p.parts || []) if (pt.tolerance) L.push(`| ${pt.name} | ${pt.tolerance} |`);
|
|
|
+ L.push('| 整机外形 | ±0.10 |', '| 孔位 / 柱位 | ±0.05 |', '| 首件 | 三坐标 / 影像仪报告 + 实配照片 |', '| 抽检 | AQL 1.0 外观 / 0.65 关键尺寸 |', '');
|
|
|
+ L.push('## 3. 装配顺序', '');
|
|
|
+ (p.processes || []).forEach((pr, i) => L.push(`${i + 1}. **${pr.name}**:${pr.desc || pr.equipment || '—'}${pr.bulkDays ? `(批量 ${pr.bulkDays})` : ''}`));
|
|
|
+ L.push('', '## 4. 结构件坐标表(壳底中心为原点;X 右 / Y 上 / Z 前)', '');
|
|
|
+ if (!this.parts.length) L.push('- 暂无自定义结构件(按主体参数推定)。');
|
|
|
+ else {
|
|
|
+ L.push('| uid | 类型 | X | Y | Z | 绕 Y | 尺寸 | 工艺要求 |', '| --- | --- | --- | --- | --- | --- | --- | --- |');
|
|
|
+ for (const s of this.parts) {
|
|
|
+ L.push(`| ${s.uid} | ${esc(s.label || PRIMS[s.type]?.n)} | ${fmt(s.at?.x, 2)} | ${fmt(s.at?.y, 2)} | ${fmt(s.at?.z, 2)} | ${fmt(s.rotY, 1)}° | ${primDimText({ ...s.params, type: s.type })} | ${primRequirement({ ...s.params, type: s.type, rot: s.rotY, x: s.at?.x, y: s.at?.y, z: s.at?.z }, rk)} |`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ L.push('', `## 5. 工艺校验(${rule.name})`, '');
|
|
|
+ const checks = this.checks;
|
|
|
+ if (!checks.length) L.push('- 全部通过。');
|
|
|
+ for (const c of checks) L.push(`- [${c.level === 'error' ? ' ' : 'x'}] ${c.level === 'error' ? '待改' : '提醒'}:${c.msg}`);
|
|
|
+ L.push('', '## 6. 供应商报价回填表', '', '| 项目 | 供应商填写 |', '| --- | --- |');
|
|
|
+ for (const x of ['模具费(含试模 / 改模)', '单品单价(含税 / 不含税)', '最小起订量 MOQ', '打样费及抵扣规则', '交期(打样 / 大货)', '模具归属', '包装方式与费用', '有效期']) L.push(`| ${x} | |`);
|
|
|
+ L.push('', '---', '生产需求单由 Pocket Hardware Lab · FmodeCode 生成;价格口径见产品页,不构成报价承诺。');
|
|
|
+ return L.join('\n');
|
|
|
+ }
|
|
|
+
|
|
|
+ _exportSpec() {
|
|
|
+ this._download(`${this.p.id}-生产需求单.md`, this.specMarkdown(), 'text/markdown;charset=utf-8');
|
|
|
+ }
|
|
|
|
|
|
+ /* ---------------- 渲染 ---------------- */
|
|
|
render() {
|
|
|
if (this.error) return html`<ph-error .error=${this.error}></ph-error>`;
|
|
|
if (!this.p) return html`<div style="padding:96px;text-align:center;color:var(--ink-3)">载入产品…</div>`;
|
|
|
const p = this.p;
|
|
|
const keys = Object.keys(p.shape?.defaults || {}).filter((k) => !HIDE_KEYS.has(k));
|
|
|
+ const errs = this.checks.filter((c) => c.level === 'error').length;
|
|
|
+ const warns = this.checks.length - errs;
|
|
|
return html`
|
|
|
<div class="lab">
|
|
|
<div class="bar">
|
|
|
<span><a data-link href=${linkHref(`/p/${p.id}`)}>← 详情</a></span>
|
|
|
<span class="t">${p.name} · 实验室</span>
|
|
|
<span class="live"></span>
|
|
|
+ <button class="reset" @click=${() => this.viewer?.reset()}>复位</button>
|
|
|
</div>
|
|
|
- <div class="stage">
|
|
|
- <canvas aria-label="3D 外壳预览:单指旋转,双指缩放,双击复位"></canvas>
|
|
|
- <button class="reset" @click=${() => this.viewer?.reset()}>复位视角</button>
|
|
|
- <div class="hint">单指旋转 · 双指缩放 · 双击复位</div>
|
|
|
+ <div class="stage" style=${`bottom:${this.dock === 'peek' ? 56 : `calc(${DOCK[this.dock] * 100}dvh)`}`}>
|
|
|
+ <canvas aria-label="3D 外壳与结构件:单指旋转,双指缩放,双击复位,点选结构件可拖动"></canvas>
|
|
|
+ <div class="hint">点选结构件拖动 · 单指旋转 · 双指缩放 · 双击复位</div>
|
|
|
+ ${this.sel ? html`<div class="sel-tip">编辑:${this.sel.label} · ${esc(dimText(this.sel))}</div>` : ''}
|
|
|
</div>
|
|
|
- <section class="dock ${this.dockOpen ? '' : 'closed'}">
|
|
|
+ <section class="dock ${this.dock}" data-dock=${this.dock}>
|
|
|
+ <div class="dock-head" title="拖动调高度 · 点击档位切换">
|
|
|
+ <div class="grab" aria-hidden="true"></div>
|
|
|
+ <div class="head-row">
|
|
|
+ <button class="tab" @click=${() => this._setDockPx(this.dock === 'peek' ? 'half' : 'peek', 300)} aria-label="切换面板高度">${this.tab === 'shape' ? '形体参数' : this.tab === 'parts' ? '结构件' : this.tab === 'bom' ? 'BOM' : '导出'}</button>
|
|
|
+ <span class="count">${this.parts.length ? `${this.parts.length} 件` : ''}</span>
|
|
|
+ ${errs ? html`<span class="badge err">${errs} 错误</span>` : ''}
|
|
|
+ ${warns ? html`<span class="badge warn">${warns} 提醒</span>` : ''}
|
|
|
+ <span class="grow"></span>
|
|
|
+ <span class="hist">
|
|
|
+ <button class="undo" title="撤销 ⌘Z" ?disabled=${!this.undoStack.length} @click=${() => this.undo()}>↺</button>
|
|
|
+ <button class="redo" title="重做 ⇧⌘Z" ?disabled=${!this.redoStack.length} @click=${() => this.redo()}>↻</button>
|
|
|
+ </span>
|
|
|
+ <button class="fold" aria-label=${this.dock === 'peek' ? '展开面板' : '收起面板'} @click=${() => this._setDockPx(this.dock === 'peek' ? 'half' : 'peek', 300)}>${this.dock === 'peek' ? '⌃' : '⌄'}</button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
<div class="tabs">
|
|
|
- ${[['shape', '形体参数'], ['bom', 'BOM'], ['export', '导出']].map(([k, label]) => html`
|
|
|
- <button class=${this.tab === k ? 'on' : ''} @click=${() => { this.tab = k; this.dockOpen = true; this.requestUpdate(); }}>${label}</button>`)}
|
|
|
- <span class="grow"></span>
|
|
|
- <button class="fold" aria-label=${this.dockOpen ? '收起面板' : '展开面板'} @click=${() => { this.dockOpen = !this.dockOpen; this.requestUpdate(); }}>${this.dockOpen ? '⌄' : '⌃'}</button>
|
|
|
+ ${[['shape', '形体'], ['parts', '结构件'], ['bom', 'BOM'], ['export', '导出']].map(([k, label]) => html`
|
|
|
+ <button class=${this.tab === k ? 'on' : ''} @click=${() => { this.tab = k; if (this.dock === 'peek') this._setDockPx('half', 300); this.requestUpdate(); }}>${label}</button>`)}
|
|
|
</div>
|
|
|
<div class="body">
|
|
|
${this.tab === 'shape' ? this._shapeTab(keys) : ''}
|
|
|
+ ${this.tab === 'parts' ? (this.sel ? this._editTab() : this._partsTab()) : ''}
|
|
|
${this.tab === 'bom' ? html`<ph-bom .bom=${p.bom}></ph-bom>` : ''}
|
|
|
${this.tab === 'export' ? this._exportTab() : ''}
|
|
|
</div>
|
|
|
+ ${this.picker ? this._pickerOverlay() : ''}
|
|
|
</section>
|
|
|
</div>`;
|
|
|
}
|
|
|
|
|
|
+ updated() {
|
|
|
+ this._bindHead();
|
|
|
+ this._syncHistoryBtns();
|
|
|
+ }
|
|
|
+
|
|
|
_shapeTab(keys) {
|
|
|
+ const opts = procOptions(this.p);
|
|
|
return html`
|
|
|
+ <div class="proc-row">
|
|
|
+ <span>工艺规则</span>
|
|
|
+ <select @change=${(e) => { this.rk = resolveProc(e.target.value); this.requestUpdate(); this._saveSoon(); }} aria-label="工艺规则">
|
|
|
+ ${PROC_KEYS.map((k) => html`<option value=${k} ?selected=${this.rk === k}>${PROC_RULES[k].name}</option>`)}
|
|
|
+ </select>
|
|
|
+ <small>${opts.length} 种产品工艺可选</small>
|
|
|
+ </div>
|
|
|
${this.viewer?.profile?.warnings?.length ? html`<p class="warn">${this.viewer.profile.warnings.join(';')}</p>` : ''}
|
|
|
- <p style="margin:0 0 14px;font-size:12px;color:var(--ink-3)">参数按 shape.defaults 自动生成 · 单位 mm / °</p>
|
|
|
+ <p class="muted">参数按 shape.defaults 自动生成 · 单位 mm / ° · 对称结构保证剖面 G1 连续</p>
|
|
|
<div class="grid">
|
|
|
${keys.map((k) => {
|
|
|
const v = this.params[k];
|
|
|
@@ -179,20 +746,146 @@ class PhLab extends PhPage {
|
|
|
return html`<div class="ctl">
|
|
|
<label for="s-${k}"><span>${KEY_LABEL[k] || k}</span><small>${k}</small></label>
|
|
|
<input type="range" id="s-${k}" min=${lo} max=${hi} step=${step} .value=${String(v)}
|
|
|
- @input=${(e) => this._set(k, e.target.value)} aria-label=${KEY_LABEL[k] || k} />
|
|
|
+ @input=${(e) => this._set(k, e.target.value, false)} @change=${() => this._pushUndo()}
|
|
|
+ aria-label=${KEY_LABEL[k] || k} />
|
|
|
<input type="number" min=${lo} max=${hi} step=${step} .value=${String(v)}
|
|
|
@change=${(e) => this._set(k, e.target.value)} aria-label=${(KEY_LABEL[k] || k) + ' 数值'} />
|
|
|
</div>`;
|
|
|
})}
|
|
|
</div>
|
|
|
- <div style="margin-top:18px"><button class="btn ghost" style="height:38px;font-size:14px" @click=${() => this._resetAll()}>恢复默认参数</button></div>`;
|
|
|
+ <div style="margin-top:18px"><button class="btn ghost sm" @click=${() => this._resetAll()}>恢复默认参数</button></div>`;
|
|
|
+ }
|
|
|
+
|
|
|
+ _partsTab() {
|
|
|
+ return html`
|
|
|
+ <div class="list">
|
|
|
+ ${this.parts.length ? this.parts.map((s) => html`
|
|
|
+ <div class="row ${s.uid === this.selUid ? 'on' : ''} ${s.visible ? '' : 'hide'}" data-uid=${s.uid}
|
|
|
+ @click=${() => this._select(s.uid)} role="button" tabindex="0"
|
|
|
+ @keydown=${(e) => { if (e.key === 'Enter') this._select(s.uid); }}>
|
|
|
+ <span class="ic" aria-hidden="true" .innerHTML=${primIcon(s.type)}></span>
|
|
|
+ <span class="meta">
|
|
|
+ <b>${s.label}</b>
|
|
|
+ <small>${esc(dimText(s))} · (${fmt(s.at.x, 1)}, ${fmt(s.at.y, 1)}, ${fmt(s.at.z, 1)})</small>
|
|
|
+ </span>
|
|
|
+ <button class="eye" aria-label=${s.visible ? '隐藏' : '显示'} @click=${(e) => { e.stopPropagation(); this._toggleVisible(s.uid); }}>${s.visible ? '◉' : '○'}</button>
|
|
|
+ <button class="del" aria-label="删除" @click=${(e) => { e.stopPropagation(); this._deletePart(s.uid); }}>×</button>
|
|
|
+ </div>`)
|
|
|
+ : html`<div class="empty">还没有结构件 ${this.p.features?.length ? '· 下方可一键应用推荐结构' : ''}</div>`}
|
|
|
+ </div>
|
|
|
+ <div class="add-row">
|
|
|
+ <button class="btn sm" @click=${() => { this.picker = true; this.requestUpdate(); }}>+ 添加结构件</button>
|
|
|
+ ${this.p.features?.length ? html`<button class="btn ghost sm" @click=${() => this._applyFeatures()}>应用推荐结构(${this.p.features.length})</button>` : ''}
|
|
|
+ </div>
|
|
|
+ <p class="muted">点一行进入单件编辑:相机聚焦、其余件半透明;也可直接在视口里点选拖动。</p>`;
|
|
|
+ }
|
|
|
+
|
|
|
+ _editTab() {
|
|
|
+ const s = this.sel;
|
|
|
+ if (!s) return '';
|
|
|
+ const schema = paramSchema(s.type);
|
|
|
+ const issues = this.checks.filter((c) => c.partUid === s.uid);
|
|
|
+ const sd = this._shellDim();
|
|
|
+ const maxY = Math.max(0, sd.h - (Number(s.params.h) || 0));
|
|
|
+ const xzMax = Math.max(10, Math.round((sd.kind === 'cyl' ? sd.r : Math.max(sd.w, sd.d) / 2) + 5));
|
|
|
+ return html`
|
|
|
+ <div class="edit-head">
|
|
|
+ <button class="back" @click=${() => this.exitEdit()}>← 全部</button>
|
|
|
+ <span class="ic" .innerHTML=${primIcon(s.type)}></span>
|
|
|
+ <b>${s.label}</b>
|
|
|
+ <small>${esc(PRIMS[s.type].short)} · ${esc(dimText(s))}</small>
|
|
|
+ <span class="grow"></span>
|
|
|
+ <span class="snap">吸附
|
|
|
+ ${SNAP_STEPS.map((v) => html`<button class=${this.snapStep === v ? 'on' : ''} @click=${() => this._snapStepChange(v)}>${v}</button>`)}
|
|
|
+ <i>mm</i>
|
|
|
+ </span>
|
|
|
+ </div>
|
|
|
+ <div class="grid dense">
|
|
|
+ ${[['x', 'X', -xzMax, xzMax], ['y', 'Y(底面高)', 0, Math.max(0.1, maxY)], ['z', 'Z', -xzMax, xzMax]].map(([k, lb, lo, hi]) => html`
|
|
|
+ <div class="ctl">
|
|
|
+ <label><span>${lb}</span><small>${k}</small></label>
|
|
|
+ <input type="range" min=${lo} max=${hi} step=${this.snapStep} .value=${String(s.at[k])}
|
|
|
+ @input=${(e) => this._setPartAt(s.uid, k, e.target.value, false)} @change=${() => this._pushUndo()} />
|
|
|
+ <input type="number" min=${lo} max=${hi} step=${this.snapStep} .value=${String(s.at[k])}
|
|
|
+ @change=${(e) => { this._setPartAt(s.uid, k, e.target.value, false); this._pushUndo(); }} />
|
|
|
+ </div>`)}
|
|
|
+ ${schema.map((f) => html`
|
|
|
+ <div class="ctl">
|
|
|
+ <label><span>${f.label}${f.unit || ''}</span><small>${f.key}</small></label>
|
|
|
+ <input type="range" min=${f.min} max=${f.max} step=${f.step} .value=${String(s.params[f.key] ?? f.def)}
|
|
|
+ @input=${(e) => this._setPartParam(s.uid, f.key, e.target.value, false)} @change=${() => this._pushUndo()} />
|
|
|
+ <input type="number" min=${f.min} max=${f.max} step=${f.step} .value=${String(s.params[f.key] ?? f.def)}
|
|
|
+ @change=${(e) => { this._setPartParam(s.uid, f.key, e.target.value, false); this._pushUndo(); }} />
|
|
|
+ </div>`)}
|
|
|
+ <div class="ctl">
|
|
|
+ <label><span>绕 Y 旋转</span><small>rotY</small></label>
|
|
|
+ <input type="range" min="0" max="360" step=${this.snapStep === 0.5 ? 1 : this.snapStep} .value=${String(s.rotY)}
|
|
|
+ @input=${(e) => { const cur = this.parts.find((p) => p.uid === s.uid); if (cur) cur.rotY = Number(e.target.value); this.viewer?.updatePart(cur); this.requestUpdate(); }}
|
|
|
+ @change=${(e) => this._setRot(s.uid, e.target.value)} />
|
|
|
+ <input type="number" min="0" max="360" step="1" .value=${String(s.rotY)} @change=${(e) => this._setRot(s.uid, e.target.value)} />
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <div class="tools">
|
|
|
+ <button @click=${() => this._tool(s.uid, 'center')}>居中</button>
|
|
|
+ <button @click=${() => this._tool(s.uid, 'wall')}>贴内壁</button>
|
|
|
+ <button @click=${() => this._tool(s.uid, 'top')}>贴顶</button>
|
|
|
+ <button @click=${() => this._tool(s.uid, 'bottom')}>贴底</button>
|
|
|
+ <span class="sep"></span>
|
|
|
+ ${[4, 6, 8].map((n) => html`<button @click=${() => this._ring(s.uid, n)}>环形 ×${n}</button>`)}
|
|
|
+ <button @click=${() => this._clone(s.uid)}>复制</button>
|
|
|
+ <button @click=${() => this._mirror(s.uid)}>镜像</button>
|
|
|
+ <button class="danger" @click=${() => this._deletePart(s.uid)}>删除</button>
|
|
|
+ </div>
|
|
|
+ ${issues.length ? html`<ul class="issues">${issues.map((c) => html`<li class=${c.level}>${c.msg}</li>`)}</ul>` : ''}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ _pickerOverlay() {
|
|
|
+ const groups = {};
|
|
|
+ for (const k of PRIM_KEYS) (groups[PRIMS[k].short] = groups[PRIMS[k].short] || []).push(k);
|
|
|
+ return html`
|
|
|
+ <div class="picker" role="dialog" aria-label="选择结构件类型">
|
|
|
+ <div class="picker-head"><b>添加结构件</b><span class="muted">14 类标准形体</span><span class="grow"></span><button class="x" aria-label="关闭" @click=${() => { this.picker = false; this.requestUpdate(); }}>×</button></div>
|
|
|
+ ${Object.entries(groups).map(([g, list]) => html`
|
|
|
+ <div class="pgroup"><div class="glabel">${g}</div>
|
|
|
+ <div class="pgrid">
|
|
|
+ ${list.map((k) => html`<button class="pcell" data-type=${k} @click=${() => this._addPart(k)}>
|
|
|
+ <span class="ic big" .innerHTML=${primIcon(k)}></span><b>${PRIMS[k].n}</b>
|
|
|
+ </button>`)}
|
|
|
+ </div>
|
|
|
+ </div>`)}
|
|
|
+ </div>`;
|
|
|
}
|
|
|
|
|
|
_exportTab() {
|
|
|
+ const sel = this.sel;
|
|
|
return html`<div class="export">
|
|
|
- <p>把当前形体参数导出为 JSON(含产品 id、shape 类型与全部参数),可直接交给结构工程师或复现到 CAD。</p>
|
|
|
- <pre>${JSON.stringify({ id: this.p.id, shapeType: this.p.shape?.type, params: this.params }, null, 2)}</pre>
|
|
|
- <button class="btn" @click=${() => this._export()}>下载 JSON</button>
|
|
|
+ <div class="exgrid">
|
|
|
+ <div class="ex">
|
|
|
+ <b>整件 STL</b><small>壳体 + 全部可见结构件,二进制 STL</small>
|
|
|
+ <button class="btn sm" @click=${() => this._exportStl(null)}>下载整件</button>
|
|
|
+ </div>
|
|
|
+ <div class="ex">
|
|
|
+ <b>按零件 STL</b><small>${sel ? `当前编辑:${sel.label}` : '未选中 → 每个结构件一个文件'}</small>
|
|
|
+ <button class="btn ghost sm" @click=${() => (sel ? this._exportStl(sel.uid) : this._exportAllStl())}>${sel ? '下载该件' : '下载全部'}</button>
|
|
|
+ </div>
|
|
|
+ <div class="ex">
|
|
|
+ <b>截图 PNG</b><small>2× 分辨率,当前视角</small>
|
|
|
+ <button class="btn ghost sm" @click=${() => this._exportPng()}>保存截图</button>
|
|
|
+ </div>
|
|
|
+ <div class="ex">
|
|
|
+ <b>生产需求单</b><small>参数 / 公差 / 装配 / 坐标表 / 报价回填</small>
|
|
|
+ <button class="btn ghost sm" @click=${() => this._exportSpec()}>下载 .md</button>
|
|
|
+ </div>
|
|
|
+ <div class="ex">
|
|
|
+ <b>BOM CSV</b><small>物料清单,Excel 可直接打开</small>
|
|
|
+ <button class="btn ghost sm" @click=${() => this._exportBomCsv()}>下载 CSV</button>
|
|
|
+ </div>
|
|
|
+ <div class="ex">
|
|
|
+ <b>工程 JSON</b><small>壳体参数 + 结构件 + 工艺,可复现</small>
|
|
|
+ <button class="btn ghost sm" @click=${() => this._exportJson()}>下载 JSON</button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <p class="muted">文件名前缀 ${this.p.id};STL 单位为 mm,Z 轴向上,壳底中心为原点。</p>
|
|
|
${this.status ? html`<span class="status">${this.status}</span>` : ''}
|
|
|
</div>`;
|
|
|
}
|