export type Point3d = { x: number y: number z: number } export type BodyMeasurement = { id: string name: string center: Point3d dimensions: Point3d volume?: number surfaceArea?: number } export type ExplodeOffset = { id: string offset: Point3d } export type BomRow = { key: string itemNumber: number name: string quantity: number dimensions: Point3d volume?: number surfaceArea?: number material: string process: string bodyIds: string[] } export type EngineeringSheetInput = { projectName: string generatedAt: string overallDimensions: Point3d brandName?: string brandLogoUrl?: string snapshotDataUrl?: string bomRows: BomRow[] notes?: string } const EPSILON = 1e-6 function roundDimension(value: number) { return Math.round(value * 100) / 100 } function vectorLength(point: Point3d) { return Math.hypot(point.x, point.y, point.z) } function normalize(point: Point3d): Point3d { const length = vectorLength(point) if (length < EPSILON) { return { x: 1, y: 0, z: 0 } } return { x: point.x / length, y: point.y / length, z: point.z / length, } } function fallbackDirection(index: number, count: number): Point3d { const goldenAngle = Math.PI * (3 - Math.sqrt(5)) const y = count <= 1 ? 0 : 1 - (index / (count - 1)) * 2 const radius = Math.sqrt(Math.max(0, 1 - y * y)) const angle = index * goldenAngle return normalize({ x: Math.cos(angle) * radius, y, z: Math.sin(angle) * radius, }) } export function getOverallBounds(bodies: BodyMeasurement[]) { if (bodies.length === 0) { return { center: { x: 0, y: 0, z: 0 }, dimensions: { x: 0, y: 0, z: 0 }, } } const min = { x: Infinity, y: Infinity, z: Infinity } const max = { x: -Infinity, y: -Infinity, z: -Infinity } for (const body of bodies) { for (const axis of ['x', 'y', 'z'] as const) { const half = body.dimensions[axis] / 2 min[axis] = Math.min(min[axis], body.center[axis] - half) max[axis] = Math.max(max[axis], body.center[axis] + half) } } return { center: { x: (min.x + max.x) / 2, y: (min.y + max.y) / 2, z: (min.z + max.z) / 2, }, dimensions: { x: max.x - min.x, y: max.y - min.y, z: max.z - min.z, }, } } export function buildExplodeOffsets( bodies: BodyMeasurement[], strength = 0.7 ): ExplodeOffset[] { if (bodies.length < 2) return [] const bounds = getOverallBounds(bodies) const diagonal = Math.max(vectorLength(bounds.dimensions), 25) return bodies.map((body, index) => { const fromCenter = { x: body.center.x - bounds.center.x, y: body.center.y - bounds.center.y, z: body.center.z - bounds.center.z, } const direction = vectorLength(fromCenter) < diagonal * 0.025 ? fallbackDirection(index, bodies.length) : normalize(fromCenter) const bodyDiagonal = vectorLength(body.dimensions) const distance = diagonal * strength * (0.7 + index / Math.max(bodies.length * 5, 1)) + bodyDiagonal * 0.18 return { id: body.id, offset: { x: roundDimension(direction.x * distance), y: roundDimension(direction.y * distance), z: roundDimension(direction.z * distance), }, } }) } function normalizePartName(name: string) { const cleaned = name.trim() || '未命名零件' return cleaned.replace(/(?:[_-]?(?:copy|part))?[_-]?\d+$/i, '').trim() || cleaned } function dimensionsKey(dimensions: Point3d) { return [dimensions.x, dimensions.y, dimensions.z] .map((value) => roundDimension(value).toFixed(2)) .join('x') } export function inferProcess(dimensions: Point3d) { const values = [dimensions.x, dimensions.y, dimensions.z] .map((value) => Math.abs(value)) .sort((a, b) => a - b) const [smallest, middle, largest] = values if (largest <= 0) return '待确认' if (smallest / largest < 0.08 && middle / largest > 0.25) { return '板材下料 / 折弯 / 表面处理' } if (middle / largest < 0.2) { return '棒料下料 / 车削或磨削' } return '机加工或增材制造(待工艺确认)' } export function groupBodiesIntoBom(bodies: BodyMeasurement[]): BomRow[] { const groups = new Map() for (const body of bodies) { const name = normalizePartName(body.name) const key = `${name.toLowerCase()}::${dimensionsKey(body.dimensions)}` const existing = groups.get(key) if (existing) { existing.quantity += 1 existing.bodyIds.push(body.id) continue } groups.set(key, { key, itemNumber: groups.size + 1, name, quantity: 1, dimensions: { x: roundDimension(body.dimensions.x), y: roundDimension(body.dimensions.y), z: roundDimension(body.dimensions.z), }, volume: body.volume, surfaceArea: body.surfaceArea, material: '待确认', process: inferProcess(body.dimensions), bodyIds: [body.id], }) } return [...groups.values()] } function csvCell(value: string | number | undefined) { const text = value === undefined ? '' : String(value) return `"${text.replaceAll('"', '""')}"` } export function createBomCsv(projectName: string, rows: BomRow[]) { const header = [ '项目', '序号', '零件名称', '数量', '长(mm)', '宽(mm)', '高(mm)', '材料', '生产工艺', '体积(mm³)', '表面积(mm²)', ] const dataRows = rows.map((row) => [ projectName, row.itemNumber, row.name, row.quantity, row.dimensions.x, row.dimensions.y, row.dimensions.z, row.material, row.process, row.volume === undefined ? '' : roundDimension(row.volume), row.surfaceArea === undefined ? '' : roundDimension(row.surfaceArea), ]) return `\uFEFF${[header, ...dataRows] .map((row) => row.map(csvCell).join(',')) .join('\r\n')}` } function escapeHtml(value: string) { return value .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('"', '"') .replaceAll("'", ''') } function formatDimension(value: number) { return `${roundDimension(value)} mm` } function dimensionViewSvg( label: string, horizontal: number, vertical: number, width = 310, height = 210 ) { const objectX = 72 const objectY = 40 const objectWidth = 180 const objectHeight = 112 const horizontalText = escapeHtml(formatDimension(horizontal)) const verticalText = escapeHtml(formatDimension(vertical)) return `
${horizontalText} ${verticalText} ${escapeHtml(label)}
` } export function createEngineeringSheetHtml(input: EngineeringSheetInput) { const { overallDimensions: dimensions } = input const rows = input.bomRows .map( (row) => ` ${row.itemNumber} ${escapeHtml(row.name)} ${row.quantity} ${formatDimension(row.dimensions.x)} × ${formatDimension(row.dimensions.y)} × ${formatDimension(row.dimensions.z)} ${escapeHtml(row.material)} ${escapeHtml(row.process)} ` ) .join('') const snapshot = input.snapshotDataUrl ? `爆炸图` : '
暂无爆炸图快照
' const brandName = escapeHtml(input.brandName || '由壹点AI') const brandLogo = input.brandLogoUrl ? `` : '' return ` ${escapeHtml(input.projectName)} - 生产工艺图
${brandLogo} ${brandName}

${escapeHtml(input.projectName)} · 生产工艺图

总体尺寸:${formatDimension(dimensions.x)} × ${formatDimension(dimensions.y)} × ${formatDimension(dimensions.z)}

单位:mm
生成时间:${escapeHtml(input.generatedAt)}

装配爆炸图

${brandLogo}${brandName}
${snapshot}

总体三视图与尺寸

${dimensionViewSvg('正视图', dimensions.x, dimensions.z)} ${dimensionViewSvg('俯视图', dimensions.x, dimensions.y)} ${dimensionViewSvg('右视图', dimensions.y, dimensions.z)}

配件明细表(BOM)

${rows}
序号零件名称数量外形尺寸材料生产工艺
生产说明:
${escapeHtml(input.notes || '材料、表面处理、公差、连接方式和检验标准须在投产前由工程人员复核。')}
` }