diff --git a/src/stores/useModelStore.ts b/src/stores/useModelStore.ts index 7f76df5..b598904 100644 --- a/src/stores/useModelStore.ts +++ b/src/stores/useModelStore.ts @@ -562,11 +562,16 @@ export const useModelStore = create((set, get) => ({ const distanceWorldMM = Math.sqrt(dx * dx + dy * dy + dz * dz); const factor = state.scaleRatio?.factor ?? 1; const distanceMM = distanceWorldMM / factor; + // Convert world points to active model's local frame so they follow the model. + const modelId = state.activeModelId ?? undefined; + const aLocal = (modelId && worldToModelLocal(modelId, a)) || a; + const bLocal = (modelId && worldToModelLocal(modelId, b)) || b; const measurement: Measurement = { id: `m_${Date.now()}`, - pointA: a, - pointB: b, + pointA: aLocal, + pointB: bLocal, distanceMM, + modelId: worldToModelLocal(modelId, a) ? modelId : undefined, }; return { measurePoints: [], measurements: [...state.measurements, measurement] }; } @@ -587,27 +592,40 @@ export const useModelStore = create((set, get) => ({ return { measurements: state.measurements.slice(0, -1) }; }), registerHoverMeasurement: (info) => set((state) => { + const modelId = state.activeModelId ?? undefined; + const toLocal = (p: MeasurePoint): { point: MeasurePoint; attached: boolean } => { + const conv = modelId ? worldToModelLocal(modelId, p) : null; + return conv ? { point: conv, attached: true } : { point: p, attached: false }; + }; let measurement: Measurement; + let attached = false; if (info.type === 'hole') { - const c: MeasurePoint = { x: info.position[0], y: info.position[1], z: info.position[2] }; + const cw: MeasurePoint = { x: info.position[0], y: info.position[1], z: info.position[2] }; + const c = toLocal(cw); + attached = c.attached; measurement = { id: `m_${Date.now()}`, - pointA: c, - pointB: c, + pointA: c.point, + pointB: c.point, distanceMM: info.value, kind: 'hole', label: `Ø ${info.value.toFixed(1)}`, + modelId: attached ? modelId : undefined, }; } else { - const a = info.endpoints?.a ?? { x: info.position[0], y: info.position[1], z: info.position[2] }; - const b = info.endpoints?.b ?? a; + const aw = info.endpoints?.a ?? { x: info.position[0], y: info.position[1], z: info.position[2] }; + const bw = info.endpoints?.b ?? aw; + const a = toLocal(aw); + const b = toLocal(bw); + attached = a.attached && b.attached; measurement = { id: `m_${Date.now()}`, - pointA: a, - pointB: b, + pointA: attached ? a.point : aw, + pointB: attached ? b.point : bw, distanceMM: info.value, kind: 'edge', label: `${info.value.toFixed(1)} mm`, + modelId: attached ? modelId : undefined, }; } return { measurements: [...state.measurements, measurement], hoverInfo: null };