1026 lines
33 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export type Point3d = {
x: number
y: number
z: number
}
export type HoleFeature = {
type: 'through-hole' | 'blind-hole'
center: Point3d
diameter: number
axis: keyof Point3d
depth?: number
}
export type CircularEdgeSample = {
controlPoints: Point3d[]
length?: number
}
export type BodyMeasurement = {
id: string
name: string
center: Point3d
dimensions: Point3d
volume?: number
surfaceArea?: number
holes?: HoleFeature[]
}
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[]
center: Point3d
holes: HoleFeature[]
}
export type EngineeringSheetInput = {
projectName: string
generatedAt: string
overallDimensions: Point3d
bodies?: BodyMeasurement[]
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 maximumDimension(body: BodyMeasurement) {
return Math.max(
Math.abs(body.dimensions.x),
Math.abs(body.dimensions.y),
Math.abs(body.dimensions.z)
)
}
function compactPartName(name: string) {
return name.trim().replaceAll(/[\s_-]+/g, '').toLowerCase()
}
function isKnownTemplateBody(body: BodyMeasurement) {
return /^unit(?:box|cube|cylinder[xyz]?|righttriangle[xyz]?)$/.test(
compactPartName(body.name)
)
}
export function filterManufacturingBodies(bodies: BodyMeasurement[]) {
if (bodies.length <= 1) return bodies
const hasProductionScaleBody = bodies.some(
(body) => maximumDimension(body) >= 20
)
const filtered = bodies.filter((body) => {
if (isKnownTemplateBody(body)) return false
return !(hasProductionScaleBody && maximumDimension(body) <= 2)
})
return filtered.length > 0 ? filtered : bodies
}
function normalizeBase64Payload(value: string) {
const normalized = value
.trim()
.replaceAll(/\s+/g, '')
.replaceAll('-', '+')
.replaceAll('_', '/')
return normalized.padEnd(Math.ceil(normalized.length / 4) * 4, '=')
}
export function createSnapshotDataUrl(contents: string) {
const trimmed = contents.trim()
if (trimmed.startsWith('data:')) {
const commaIndex = trimmed.indexOf(',')
if (commaIndex < 0 || !trimmed.slice(0, commaIndex).includes(';base64')) {
return trimmed
}
return `${trimmed.slice(0, commaIndex + 1)}${normalizeBase64Payload(
trimmed.slice(commaIndex + 1)
)}`
}
return `data:image/png;base64,${normalizeBase64Payload(trimmed)}`
}
function vectorLength(point: Point3d) {
return Math.hypot(point.x, point.y, point.z)
}
function panelAxes(dimensions: Point3d) {
const axes = (['x', 'y', 'z'] as Array<keyof Point3d>).sort(
(left, right) =>
Math.abs(dimensions[left]) - Math.abs(dimensions[right])
)
return {
depth: axes[0],
vertical: axes[1],
horizontal: axes[2],
}
}
function cross(left: Point3d, right: Point3d): Point3d {
return {
x: left.y * right.z - left.z * right.y,
y: left.z * right.x - left.x * right.z,
z: left.x * right.y - left.y * right.x,
}
}
function subtract(left: Point3d, right: Point3d): Point3d {
return {
x: left.x - right.x,
y: left.y - right.y,
z: left.z - right.z,
}
}
function projectedCircumcircle(
points: Point3d[],
horizontal: keyof Point3d,
vertical: keyof Point3d
) {
let best:
| {
first: Point3d
second: Point3d
third: Point3d
area: number
}
| undefined
const candidates = points.slice(0, 16)
for (let firstIndex = 0; firstIndex < candidates.length - 2; firstIndex += 1) {
for (
let secondIndex = firstIndex + 1;
secondIndex < candidates.length - 1;
secondIndex += 1
) {
for (
let thirdIndex = secondIndex + 1;
thirdIndex < candidates.length;
thirdIndex += 1
) {
const first = candidates[firstIndex]
const second = candidates[secondIndex]
const third = candidates[thirdIndex]
const area = Math.abs(
(second[horizontal] - first[horizontal]) *
(third[vertical] - first[vertical]) -
(third[horizontal] - first[horizontal]) *
(second[vertical] - first[vertical])
)
if (!best || area > best.area) {
best = { first, second, third, area }
}
}
}
}
if (!best || best.area < EPSILON) return undefined
const firstHorizontal = best.first[horizontal]
const firstVertical = best.first[vertical]
const secondHorizontal = best.second[horizontal]
const secondVertical = best.second[vertical]
const thirdHorizontal = best.third[horizontal]
const thirdVertical = best.third[vertical]
const denominator =
2 *
(firstHorizontal * (secondVertical - thirdVertical) +
secondHorizontal * (thirdVertical - firstVertical) +
thirdHorizontal * (firstVertical - secondVertical))
if (Math.abs(denominator) < EPSILON) return undefined
const firstSquared =
firstHorizontal * firstHorizontal + firstVertical * firstVertical
const secondSquared =
secondHorizontal * secondHorizontal + secondVertical * secondVertical
const thirdSquared =
thirdHorizontal * thirdHorizontal + thirdVertical * thirdVertical
const centerHorizontal =
(firstSquared * (secondVertical - thirdVertical) +
secondSquared * (thirdVertical - firstVertical) +
thirdSquared * (firstVertical - secondVertical)) /
denominator
const centerVertical =
(firstSquared * (thirdHorizontal - secondHorizontal) +
secondSquared * (firstHorizontal - thirdHorizontal) +
thirdSquared * (secondHorizontal - firstHorizontal)) /
denominator
const radius = Math.hypot(
centerHorizontal - firstHorizontal,
centerVertical - firstVertical
)
const normal = normalize(
cross(
subtract(best.second, best.first),
subtract(best.third, best.first)
)
)
return {
centerHorizontal,
centerVertical,
radius,
normal,
}
}
export function detectPanelHoles(
body: BodyMeasurement,
circularEdges: CircularEdgeSample[]
) {
const axes = panelAxes(body.dimensions)
const thickness = Math.abs(body.dimensions[axes.depth])
const faceSize = Math.min(
Math.abs(body.dimensions[axes.horizontal]),
Math.abs(body.dimensions[axes.vertical])
)
if (
thickness <= EPSILON ||
faceSize <= EPSILON ||
thickness / faceSize > 0.35
) {
return []
}
const candidates = circularEdges.flatMap((edge): HoleFeature[] => {
if (edge.controlPoints.length < 3) return []
const circle = projectedCircumcircle(
edge.controlPoints,
axes.horizontal,
axes.vertical
)
if (!circle || circle.radius < 0.5) return []
if (Math.abs(circle.normal[axes.depth]) < 0.82) return []
const diameter = circle.radius * 2
if (diameter > faceSize * 0.45) return []
const circumference = Math.PI * diameter
if (
edge.length !== undefined &&
edge.length < circumference * 0.82
) {
return []
}
const center = { ...body.center }
center[axes.horizontal] = circle.centerHorizontal
center[axes.vertical] = circle.centerVertical
center[axes.depth] = body.center[axes.depth]
return [
{
type: 'through-hole',
center,
diameter: roundDimension(diameter),
axis: axes.depth,
depth: roundDimension(thickness),
},
]
})
const deduplicated: HoleFeature[] = []
for (const candidate of candidates) {
const duplicate = deduplicated.some(
(existing) =>
existing.axis === candidate.axis &&
Math.abs(existing.diameter - candidate.diameter) < 0.2 &&
vectorLength(subtract(existing.center, candidate.center)) < 0.35
)
if (!duplicate) deduplicated.push(candidate)
}
return deduplicated.sort((left, right) => {
const horizontalDifference =
left.center[axes.horizontal] - right.center[axes.horizontal]
if (Math.abs(horizontalDifference) > EPSILON) return horizontalDifference
return left.center[axes.vertical] - right.center[axes.vertical]
})
}
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() || '未命名零件'
const withoutCopySuffix =
cleaned.replace(/(?:[_-]?(?:copy|part))?[_-]?\d+$/i, '').trim() || cleaned
const locationLegMatch = withoutCopySuffix.match(
/^(?:front|back|rear)(?:left|right)(leg|foot)$/i
)
return locationLegMatch?.[1] ?? withoutCopySuffix
}
const CHINESE_PART_NAMES: Record<string, string> = {
backpanel: '背板',
base: '底座',
baseboard: '底板',
bottompanel: '底板',
bracket: '支架',
camconnector: '三合一偏心连接件',
camlock: '三合一偏心轮',
connectorbolt: '连接螺栓',
connectorplate: '连接片',
dividerpanel: '隔板',
doorpanel: '门板',
dowel: '木榫',
drawer: '抽屉',
drawerfront: '抽屉面板',
foot: '支腿',
handle: '拉手',
hinge: '铰链',
metalbracket: '金属连接角码',
leftsidepanel: '左侧板',
leg: '支腿',
rightsidepanel: '右侧板',
screw: '螺钉',
shelfpin: '层板托',
seat: '座板',
seatboard: '座板',
seatbody: '座板',
shelf: '层板',
shelfpanel: '层板',
sidepanel: '侧板',
support: '支撑件',
tablebody: '桌体',
tabletop: '台面板',
topboard: '顶板',
topbottompanel: '顶底板',
toppanel: '顶板',
washer: '垫圈',
}
export function localizePartName(name: string) {
const cleaned = name.trim() || '未命名零件'
if (/[\u3400-\u9fff]/.test(cleaned)) return cleaned
return CHINESE_PART_NAMES[compactPartName(cleaned)] ?? cleaned
}
function dimensionsKey(dimensions: Point3d) {
return [dimensions.x, dimensions.y, dimensions.z]
.map((value) => roundDimension(value).toFixed(2))
.join('x')
}
function holePatternKey(body: BodyMeasurement) {
return (body.holes ?? [])
.map((hole) => {
const local = {
x: hole.center.x - body.center.x,
y: hole.center.y - body.center.y,
z: hole.center.z - body.center.z,
}
return `${hole.axis}:${roundDimension(hole.diameter).toFixed(2)}:${[
local.x,
local.y,
local.z,
]
.map((value) => roundDimension(value).toFixed(2))
.join(',')}`
})
.sort()
.join('|')
}
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<string, BomRow>()
for (const body of bodies) {
const normalizedName = normalizePartName(body.name)
const name = localizePartName(normalizedName)
const key = `${normalizedName.toLowerCase()}::${dimensionsKey(
body.dimensions
)}::${holePatternKey(body)}`
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],
center: body.center,
holes: body.holes ?? [],
})
}
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('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#039;')
}
function formatDimension(value: number) {
return `${roundDimension(value)} mm`
}
function projectedBodyRect(
body: BodyMeasurement,
horizontalAxis: keyof Point3d,
verticalAxis: keyof Point3d,
originX: number,
originY: number,
scale: number,
minHorizontal: number,
minVertical: number,
projectedHeight: number
) {
const horizontalStart =
body.center[horizontalAxis] - body.dimensions[horizontalAxis] / 2
const verticalStart =
body.center[verticalAxis] - body.dimensions[verticalAxis] / 2
const rectX = originX + (horizontalStart - minHorizontal) * scale
const rectWidth = Math.max(body.dimensions[horizontalAxis] * scale, 1)
const rectHeight = Math.max(body.dimensions[verticalAxis] * scale, 1)
const rectY =
originY +
projectedHeight -
(verticalStart - minVertical) * scale -
rectHeight
return `<rect class="projection-part"
data-part-name="${escapeHtml(localizePartName(normalizePartName(body.name)))}"
x="${roundDimension(rectX)}" y="${roundDimension(rectY)}"
width="${roundDimension(rectWidth)}" height="${roundDimension(rectHeight)}"
fill="#e5e7eb" fill-opacity="0.64" stroke="#111827" stroke-width="1.4"/>`
}
function orthographicViewSvg(
label: string,
bodies: BodyMeasurement[],
horizontalAxis: keyof Point3d,
verticalAxis: keyof Point3d,
width = 310,
height = 210
) {
const overall = getOverallBounds(bodies)
const horizontal = overall.dimensions[horizontalAxis]
const vertical = overall.dimensions[verticalAxis]
const maxObjectWidth = 180
const maxObjectHeight = 112
const scale = Math.min(
maxObjectWidth / Math.max(horizontal, EPSILON),
maxObjectHeight / Math.max(vertical, EPSILON)
)
const objectWidth = Math.max(horizontal * scale, 1)
const objectHeight = Math.max(vertical * scale, 1)
const objectX = 72 + (maxObjectWidth - objectWidth) / 2
const objectY = 40 + (maxObjectHeight - objectHeight) / 2
const minHorizontal =
overall.center[horizontalAxis] - overall.dimensions[horizontalAxis] / 2
const minVertical =
overall.center[verticalAxis] - overall.dimensions[verticalAxis] / 2
const horizontalText = escapeHtml(formatDimension(horizontal))
const verticalText = escapeHtml(formatDimension(vertical))
const projectedBodies = bodies
.map((body) =>
projectedBodyRect(
body,
horizontalAxis,
verticalAxis,
objectX,
objectY,
scale,
minHorizontal,
minVertical,
objectHeight
)
)
.join('')
return `
<figure class="dimension-view">
<svg viewBox="0 0 ${width} ${height}" role="img" aria-label="${escapeHtml(label)}">
<defs>
<marker id="arrow-${escapeHtml(label)}" viewBox="0 0 10 10" refX="5" refY="5"
markerWidth="5" markerHeight="5" orient="auto-start-reverse">
<path d="M 0 0 L 10 5 L 0 10 z" fill="#111827"/>
</marker>
</defs>
${projectedBodies}
<line x1="${objectX}" y1="${objectY + objectHeight + 25}"
x2="${objectX + objectWidth}" y2="${objectY + objectHeight + 25}"
stroke="#111827" marker-start="url(#arrow-${escapeHtml(label)})"
marker-end="url(#arrow-${escapeHtml(label)})"/>
<line x1="${objectX}" y1="${objectY + objectHeight}" x2="${objectX}"
y2="${objectY + objectHeight + 35}" stroke="#6b7280"/>
<line x1="${objectX + objectWidth}" y1="${objectY + objectHeight}"
x2="${objectX + objectWidth}" y2="${objectY + objectHeight + 35}" stroke="#6b7280"/>
<text x="${objectX + objectWidth / 2}" y="${objectY + objectHeight + 48}"
text-anchor="middle">${horizontalText}</text>
<line x1="${objectX - 25}" y1="${objectY}" x2="${objectX - 25}"
y2="${objectY + objectHeight}" stroke="#111827"
marker-start="url(#arrow-${escapeHtml(label)})"
marker-end="url(#arrow-${escapeHtml(label)})"/>
<line x1="${objectX - 35}" y1="${objectY}" x2="${objectX}"
y2="${objectY}" stroke="#6b7280"/>
<line x1="${objectX - 35}" y1="${objectY + objectHeight}" x2="${objectX}"
y2="${objectY + objectHeight}" stroke="#6b7280"/>
<text x="${objectX - 42}" y="${objectY + objectHeight / 2}"
text-anchor="middle" transform="rotate(-90 ${objectX - 42} ${objectY + objectHeight / 2})">
${verticalText}
</text>
<text x="${width / 2}" y="20" text-anchor="middle" font-weight="700">
${escapeHtml(label)}
</text>
</svg>
</figure>`
}
function uniqueRounded(values: number[]) {
return [
...new Set(
values.map((value) => roundDimension(value).toFixed(2))
),
]
.map(Number)
.sort((left, right) => left - right)
}
function machiningDetailSvg(row: BomRow) {
const axes = panelAxes(row.dimensions)
const horizontal = Math.abs(row.dimensions[axes.horizontal])
const vertical = Math.abs(row.dimensions[axes.vertical])
const depth = Math.abs(row.dimensions[axes.depth])
const objectMaxWidth = 230
const objectMaxHeight = 145
const scale = Math.min(
objectMaxWidth / Math.max(horizontal, EPSILON),
objectMaxHeight / Math.max(vertical, EPSILON)
)
const objectWidth = Math.max(horizontal * scale, 1)
const objectHeight = Math.max(vertical * scale, 1)
const objectX = 75 + (objectMaxWidth - objectWidth) / 2
const objectY = 42 + (objectMaxHeight - objectHeight) / 2
const holes = row.holes.filter((hole) => hole.axis === axes.depth)
const holePositions = holes.map((hole, index) => {
const fromLeft =
hole.center[axes.horizontal] -
row.center[axes.horizontal] +
horizontal / 2
const fromBottom =
hole.center[axes.vertical] -
row.center[axes.vertical] +
vertical / 2
return {
...hole,
index: index + 1,
fromLeft: roundDimension(fromLeft),
fromBottom: roundDimension(fromBottom),
x: objectX + fromLeft * scale,
y: objectY + objectHeight - fromBottom * scale,
radius: Math.max((hole.diameter / 2) * scale, 2.5),
}
})
const horizontalHolePositions = uniqueRounded(
holePositions.map((hole) => hole.fromLeft)
)
const verticalHolePositions = uniqueRounded(
holePositions.map((hole) => hole.fromBottom)
)
const horizontalPitch =
horizontalHolePositions.length > 1
? roundDimension(
horizontalHolePositions.at(-1)! - horizontalHolePositions[0]
)
: undefined
const verticalPitch =
verticalHolePositions.length > 1
? roundDimension(
verticalHolePositions.at(-1)! - verticalHolePositions[0]
)
: undefined
const holeMarkup = holePositions
.map(
(hole) => `
<circle class="detail-hole" cx="${roundDimension(hole.x)}" cy="${roundDimension(
hole.y
)}" r="${roundDimension(hole.radius)}"/>
<line class="center-line" x1="${roundDimension(hole.x - hole.radius - 5)}"
y1="${roundDimension(hole.y)}" x2="${roundDimension(
hole.x + hole.radius + 5
)}" y2="${roundDimension(hole.y)}"/>
<line class="center-line" x1="${roundDimension(hole.x)}"
y1="${roundDimension(hole.y - hole.radius - 5)}" x2="${roundDimension(
hole.x
)}" y2="${roundDimension(hole.y + hole.radius + 5)}"/>
<text x="${roundDimension(hole.x + hole.radius + 4)}"
y="${roundDimension(hole.y - hole.radius - 3)}">H${hole.index}</text>`
)
.join('')
const holeNotes =
holePositions.length > 0
? holePositions
.map(
(hole) =>
`<li>H${hole.index}:Ø${roundDimension(
hole.diameter
)} ${hole.type === 'through-hole' ? '通孔' : `深 ${roundDimension(
hole.depth ?? 0
)}`},距左 ${hole.fromLeft},距下 ${hole.fromBottom}</li>`
)
.join('')
: '<li>该零件未识别到圆孔特征</li>'
const pitchNotes = [
horizontalPitch === undefined ? '' : `横向中心孔距 ${horizontalPitch} mm`,
verticalPitch === undefined ? '' : `纵向中心孔距 ${verticalPitch} mm`,
]
.filter(Boolean)
.join(';')
return `
<article class="part-detail">
<div class="part-detail-heading">
<strong>${row.itemNumber}. ${escapeHtml(row.name)}</strong>
<span>数量 ${row.quantity} · 厚度 ${formatDimension(depth)}</span>
</div>
<div class="part-detail-layout">
<svg viewBox="0 0 390 245" role="img" aria-label="${escapeHtml(
row.name
)} 加工尺寸图">
<defs>
<marker id="part-arrow-${row.itemNumber}" viewBox="0 0 10 10" refX="5" refY="5"
markerWidth="5" markerHeight="5" orient="auto-start-reverse">
<path d="M 0 0 L 10 5 L 0 10 z" fill="#111827"/>
</marker>
</defs>
<rect x="${roundDimension(objectX)}" y="${roundDimension(objectY)}"
width="${roundDimension(objectWidth)}" height="${roundDimension(
objectHeight
)}" fill="#f9fafb" stroke="#111827" stroke-width="1.6"/>
${holeMarkup}
<line x1="${roundDimension(objectX)}" y1="${roundDimension(
objectY + objectHeight + 22
)}" x2="${roundDimension(objectX + objectWidth)}"
y2="${roundDimension(objectY + objectHeight + 22)}" stroke="#111827"
marker-start="url(#part-arrow-${row.itemNumber})"
marker-end="url(#part-arrow-${row.itemNumber})"/>
<line x1="${roundDimension(objectX)}" y1="${roundDimension(
objectY + objectHeight
)}" x2="${roundDimension(objectX)}" y2="${roundDimension(
objectY + objectHeight + 30
)}" stroke="#6b7280"/>
<line x1="${roundDimension(objectX + objectWidth)}" y1="${roundDimension(
objectY + objectHeight
)}" x2="${roundDimension(objectX + objectWidth)}" y2="${roundDimension(
objectY + objectHeight + 30
)}" stroke="#6b7280"/>
<text x="${roundDimension(objectX + objectWidth / 2)}"
y="${roundDimension(objectY + objectHeight + 39)}"
text-anchor="middle">${formatDimension(horizontal)}</text>
<line x1="${roundDimension(objectX - 22)}" y1="${roundDimension(objectY)}"
x2="${roundDimension(objectX - 22)}" y2="${roundDimension(
objectY + objectHeight
)}" stroke="#111827" marker-start="url(#part-arrow-${row.itemNumber})"
marker-end="url(#part-arrow-${row.itemNumber})"/>
<line x1="${roundDimension(objectX - 30)}" y1="${roundDimension(objectY)}"
x2="${roundDimension(objectX)}" y2="${roundDimension(objectY)}"
stroke="#6b7280"/>
<line x1="${roundDimension(objectX - 30)}" y1="${roundDimension(
objectY + objectHeight
)}" x2="${roundDimension(objectX)}" y2="${roundDimension(
objectY + objectHeight
)}" stroke="#6b7280"/>
<text x="${roundDimension(objectX - 37)}"
y="${roundDimension(objectY + objectHeight / 2)}" text-anchor="middle"
transform="rotate(-90 ${roundDimension(
objectX - 37
)} ${roundDimension(objectY + objectHeight / 2)})">${formatDimension(
vertical
)}</text>
</svg>
<div class="hole-schedule">
<strong>孔位与加工基准</strong>
<ul>${holeNotes}</ul>
${
pitchNotes
? `<p><strong>孔距:</strong>${escapeHtml(pitchNotes)}</p>`
: ''
}
<p><strong>基准:</strong>左下角为孔位坐标基准,尺寸单位 mm。</p>
</div>
</div>
</article>`
}
export function createEngineeringSheetHtml(input: EngineeringSheetInput) {
const { overallDimensions: dimensions } = input
const projectionBodies =
input.bodies && input.bodies.length > 0
? input.bodies
: [
{
id: 'overall',
name: '总体',
center: { x: 0, y: 0, z: 0 },
dimensions,
},
]
const rows = input.bomRows
.map(
(row) => `
<tr>
<td>${row.itemNumber}</td>
<td>${escapeHtml(row.name)}</td>
<td>${row.quantity}</td>
<td>${formatDimension(row.dimensions.x)} × ${formatDimension(row.dimensions.y)} × ${formatDimension(row.dimensions.z)}</td>
<td>${escapeHtml(row.material)}</td>
<td>${escapeHtml(row.process)}</td>
<td>${row.holes.length > 0 ? `${row.holes.length} × Ø${escapeHtml(
uniqueRounded(row.holes.map((hole) => hole.diameter)).join(' / Ø')
)}` : '-'}</td>
</tr>`
)
.join('')
const partDetails = input.bomRows.map(machiningDetailSvg).join('')
const snapshot = input.snapshotDataUrl
? `<img class="snapshot" src="${escapeHtml(input.snapshotDataUrl)}" alt="爆炸图"/>`
: '<div class="snapshot-empty">暂无爆炸图快照</div>'
const brandName = escapeHtml(input.brandName || '由壹点AI')
const brandLogo = input.brandLogoUrl
? `<img class="brand-logo" src="${escapeHtml(input.brandLogoUrl)}" alt="${brandName}"/>`
: ''
return `<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<title>${escapeHtml(input.projectName)} - 生产工艺图</title>
<style>
@page { size: A3 landscape; margin: 12mm; }
* { box-sizing: border-box; }
body { margin: 0; color: #111827; font-family: Arial, "Microsoft YaHei", sans-serif; }
header { display: flex; align-items: end; justify-content: space-between; gap: 24px; border-bottom: 2px solid #111827; padding-bottom: 8px; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 170px; }
.brand-logo { width: 38px; height: 38px; object-fit: contain; }
.brand-name { font-size: 18px; font-weight: 700; white-space: nowrap; }
.title { flex: 1; }
h1 { margin: 0; font-size: 24px; }
h2 { margin: 0 0 8px; font-size: 16px; }
p { margin: 4px 0; }
.meta { color: #4b5563; font-size: 12px; text-align: right; }
.sheet-grid { display: grid; grid-template-columns: 1.05fr 1fr; gap: 16px; margin-top: 14px; }
section { break-inside: avoid; }
.snapshot-wrap { position: relative; }
.snapshot-brand { position: absolute; left: 10px; top: 10px; z-index: 1; display: flex; align-items: center; gap: 6px; padding: 5px 8px; background: rgba(255,255,255,.9); border: 1px solid #d1d5db; font-size: 11px; font-weight: 700; }
.snapshot-brand img { width: 20px; height: 20px; object-fit: contain; }
.snapshot { width: 100%; height: 310px; object-fit: contain; border: 1px solid #9ca3af; }
.snapshot-empty { height: 310px; display: grid; place-items: center; border: 1px dashed #9ca3af; color: #6b7280; }
.views { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.dimension-view { margin: 0; border: 1px solid #d1d5db; }
.dimension-view svg { width: 100%; display: block; font-size: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 11px; }
th, td { border: 1px solid #6b7280; padding: 5px 6px; text-align: left; vertical-align: top; }
th { background: #f3f4f6; }
.bom { grid-column: 1 / -1; }
.part-details { grid-column: 1 / -1; break-before: page; }
.part-detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.part-detail { border: 1px solid #6b7280; break-inside: avoid; }
.part-detail-heading { display: flex; justify-content: space-between; gap: 12px; padding: 7px 9px; border-bottom: 1px solid #9ca3af; background: #f3f4f6; font-size: 12px; }
.part-detail-layout { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(190px, .8fr); gap: 8px; align-items: start; padding: 6px; }
.part-detail svg { width: 100%; display: block; font-size: 10px; }
.detail-hole { fill: white; stroke: #111827; stroke-width: 1.5; }
.center-line { stroke: #6b7280; stroke-width: .7; stroke-dasharray: 4 3; }
.hole-schedule { padding: 8px 6px 6px 0; font-size: 10px; line-height: 1.5; }
.hole-schedule ul { margin: 5px 0 8px; padding-left: 18px; }
.hole-schedule p { margin: 5px 0; }
.notes { grid-column: 1 / -1; border: 1px solid #9ca3af; padding: 10px; min-height: 68px; white-space: pre-wrap; }
footer { margin-top: 12px; display: grid; grid-template-columns: 1fr 1fr 1fr; border: 1px solid #6b7280; font-size: 11px; }
footer div { min-height: 34px; padding: 8px; border-right: 1px solid #6b7280; }
footer div:last-child { border-right: 0; }
</style>
</head>
<body>
<header>
<div class="brand">
${brandLogo}
<span class="brand-name">${brandName}</span>
</div>
<div class="title">
<h1>${escapeHtml(input.projectName)} · 生产工艺图</h1>
<p>总体尺寸:${formatDimension(dimensions.x)} × ${formatDimension(dimensions.y)} × ${formatDimension(dimensions.z)}</p>
</div>
<div class="meta">单位:mm<br/>生成时间:${escapeHtml(input.generatedAt)}</div>
</header>
<main class="sheet-grid">
<section>
<h2>装配爆炸图</h2>
<div class="snapshot-wrap">
<div class="snapshot-brand">${brandLogo}<span>${brandName}</span></div>
${snapshot}
</div>
</section>
<section>
<h2>实体正投影三视图与尺寸</h2>
<div class="views">
${orthographicViewSvg('正视图', projectionBodies, 'x', 'z')}
${orthographicViewSvg('俯视图', projectionBodies, 'x', 'y')}
${orthographicViewSvg('右视图', projectionBodies, 'y', 'z')}
</div>
</section>
<section class="bom">
<h2>配件明细表(BOM)</h2>
<table>
<thead>
<tr><th>序号</th><th>零件名称</th><th>数量</th><th>外形尺寸</th><th>材料</th><th>生产工艺</th><th>孔位</th></tr>
</thead>
<tbody>${rows}</tbody>
</table>
</section>
<section class="part-details">
<h2>零件加工详图、孔径与孔位尺寸</h2>
<div class="part-detail-grid">${partDetails}</div>
</section>
<section class="notes"><strong>生产说明:</strong><br/>${escapeHtml(input.notes || '材料、表面处理、公差、连接方式和检验标准须在投产前由工程人员复核。')}</section>
</main>
<footer>
<div>制图:</div><div>审核:</div><div>批准:</div>
</footer>
<script>window.addEventListener('load', () => setTimeout(() => window.print(), 250));</script>
</body>
</html>`
}