am-freescan/am-web/src/views/lab/inspection/featurePresentation.ts

207 lines
7.0 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.

const derivedFields = new Set([
'measurement_state',
'assessment_state',
'reason_code',
'next_action',
'workflow_note',
'workflow_warnings',
'extraction_audit',
'measured',
'evaluation',
'evaluation_state',
'result',
'result_snapshot',
'diagnostic',
'status',
'patch_mesh',
'support_points',
'preview_points',
'scan_region',
'scan_region_diagnostic'
])
export function toleranceLabel(t: any): string {
if (!t) return '—'
if (t.mode === 'limits')
return t.lower != null && t.upper != null
? `${t.lower}~${t.upper}`
: t.lower != null
? `≥${t.lower}`
: `≤${t.upper}`
if (t.mode === 'upper') return `≤${t.upper}`
const signed = (x: number) => (x === 0 ? '0' : x > 0 ? `+${x}` : `${x}`)
return `${signed(t.upper)}/${signed(t.lower)}`
}
// Editable metadata defines dirtiness; multi-megabyte immutable geometry is not a form field.
export function definitionSignature(doc: any): string {
return JSON.stringify(doc, (key, value) => (derivedFields.has(key) ? undefined : value))
}
export function definitionForExport(doc: any): any {
const outputs = new Set([
'extraction_audit',
'measured',
'result',
'evaluation',
'evaluation_state',
'result_snapshot',
'scan_region',
'scan_region_diagnostic'
])
const copy = JSON.parse(
JSON.stringify(doc, (key, value) => (outputs.has(key) ? undefined : value))
)
invalidateFeatureResults(copy)
return copy
}
export function invalidateFeatureResults(doc: any): void {
for (const feature of doc.features) {
delete feature.extraction_audit
delete feature.measured
delete feature.status
delete feature.diagnostic
delete feature.scan_region
delete feature.scan_region_diagnostic
}
for (const control of doc.controls) {
delete control.result
for (const key of [
'measurement_state',
'assessment_state',
'reason_code',
'next_action',
'workflow_note',
'workflow_warnings'
])
delete control[key]
}
delete doc.result_snapshot
delete doc.evaluation_state
delete doc.evaluation
}
export function useFeatureResults(graph: any): boolean {
if (graph.evaluation_state === 'stale')
return (
graph.controls.length > 0 &&
(graph.report_scope === 'feature_plan' ||
!!graph.template_selection ||
graph.controls.some((c: any) => c.required))
)
if (!graph.evaluation) return false
if (graph.report_scope === 'feature_plan' && graph.controls.length) return true
if (graph.controls.some((c: any) => c.result?.value != null)) return true
if (graph.legacy_migration) return false
return (
!!graph.template_selection ||
graph.controls.some(
(c: any) => c.required || !['cad_geometry', 'drawing_recommendation'].includes(c.origin)
)
)
}
export function combinedResultRows(plan: any[], features: any[]): any[] {
return [
...plan.map((row, plan_index) => ({
...row,
plan_index,
...(row.measured_mm == null && row.trial_measured_mm != null
? {
measured_mm: row.trial_measured_mm,
error_mm: row.nominal_mm == null ? null : row.trial_measured_mm - row.nominal_mm,
inspection_result: '待判定'
}
: {}),
result_source: '检测计划'
})),
...features.map((row) => ({ ...row, result_source: 'CAD 特征' }))
]
}
export type ResultLoadState = 'idle' | 'loading' | 'ready' | 'error'
export function snapshotResultRows(
plan: any[],
features: any[],
primary: boolean,
state: ResultLoadState
): any[] {
return state === 'ready' ? combinedResultRows(primary ? [] : plan, features) : []
}
export function featureDisplayControls(graph: any, hasPlan: boolean): any[] {
if (!useFeatureResults(graph)) return []
const controls =
!hasPlan || graph.template_selection
? graph.controls
: graph.controls.filter(
(c: any) =>
c.required ||
!['cad_geometry', 'drawing_recommendation'].includes(c.origin) ||
c.result?.value != null
)
return graph.evaluation_state === 'stale'
? controls.map((c: any) => ({
...c,
result: { status: 'stale' },
measurement_state: 'pending',
assessment_state: 'pending',
reason_code: 'result_stale',
next_action: 'recalculate',
workflow_note: '检测条件已更新,请重新计算;历史报告仍可查看。'
}))
: controls
}
export function resultNote(
row: any,
sanitize: (value: any, fallback?: string) => string = (
_value,
fallback = '请核对测量位置与提取参数'
) => fallback
): string {
const diagnostic = row.workflow_note || row.diagnostic || row.measurement_reason || ''
if (
row.reason_code === 'nominal_mismatch' ||
/与计划名义.*不一致|CAD 尺寸.*不一致/.test(diagnostic)
)
return 'CAD 尺寸与计划不一致,请核对位置或模型版本'
if (
row.measurement_state === 'computed_unverified' ||
(row.trial_measured_mm != null &&
row.measurement_status !== '自动测量' &&
/门槛|候选试测/.test(diagnostic))
)
return '拟合质量未通过,请核对扫描覆盖与提取范围'
const labels: any = {
result_stale: '检测条件已更新,请重新计算;历史报告仍可查看。',
binding_required: '请选择测量位置',
ambiguous_binding: '存在多个匹配位置,请选择对应要素',
no_points_in_region: '当前选区未提取到扫描点,请先核对位置与对齐',
insufficient_support: '当前选区支持点不足,请核对提取范围与局部扫描',
insufficient_coverage: '当前特征覆盖不足,请核对选区与扫描覆盖',
reference_extraction_failed: row.workflow_note || '参考要素提取失败,请核对对应测量位置',
insufficient_scan: '当前特征支持不足,请核对选区与局部扫描',
datum_required: '请设置基准或检查基准扫描数据',
unsupported: '当前测量方式不支持此项配置',
cad_unavailable: '设计模型读取失败,请检查模型文件',
alignment_check: '请核对单位与对齐结果',
tolerance_unset: '未配置公差,保留测量值',
capability_unverified: '已有测量值,判定依据待完善'
}
if (labels[row.reason_code]) return labels[row.reason_code]
if (row.measured_mm == null) {
if (diagnostic) return sanitize(diagnostic, '请核对测量位置与提取参数')
return '请选择测量位置并计算'
}
if (!row.tol_text && !row.tolerance) return labels.tolerance_unset
if (row.status === 'capability_unverified') return labels.capability_unverified
return ''
}
export function resultAction(row: any): string {
const labels: Record<string, string> = {
recalculate: '重新计算',
tolerance: '设置公差',
datum: '设置基准',
capability: '完善判定依据',
scan: '检查扫描',
parameters: '调整参数',
model: '检查模型',
alignment: '检查对齐'
}
return labels[row.next_action] || (row.measured_mm == null ? '选择测量位置' : '定位复核')
}