import { Shape, textBox, textMeasurer } from './dxf-geometry'; import { Scene } from './scene'; /** * Find text: every TEXT, MTEXT, attribute, table cell and leader label in the * model and on each layout, matched without regard to case or spacing. */ export interface TextHit { /** The tab it is on. */ sheet: number; sheetName: string; shape: Shape; /** The whole text, its lines joined. */ text: string; /** What it is, for the list: "Table cell", "Attribute", "MTEXT"... */ kind: string; } export const MAX_HITS = 500; export function findText(scene: Scene, query: string): { hits: TextHit[]; more: boolean } { const q = norm(query); const hits: TextHit[] = []; if (!q) return { hits, more: false }; for (let i = 0; i < scene.sheets.length; i++) { const sheet = scene.sheets[i]; for (const s of sheet.space.shapes) { if (s.kind !== 'text' || !s.text) continue; const text = s.text.lines.join(' '); if (!norm(text).includes(q)) continue; if (hits.length >= MAX_HITS) return { hits, more: true }; hits.push({ sheet: i, sheetName: sheet.name, shape: s, text, kind: kindOf(s) }); } } return { hits, more: false }; } function norm(s: string): string { return s.toLowerCase().replace(/\s+/g, ' ').trim(); } function kindOf(s: Shape): string { const i = s.info; if (i?.owner?.type === 'ACAD_TABLE') return 'Table cell'; if (i?.owner?.type === 'DIMENSION') return 'Dimension'; if (i?.type === 'ATTRIB') return 'Attribute'; if (i?.type === 'MULTILEADER' || i?.type === 'MLEADER') return 'Leader'; if (i?.type === 'MTEXT') return 'MTEXT'; return 'TEXT'; } let measure: ((s: string, h: number) => number) | null = null; /** A text's outline, in its tab's units: to zoom to it and draw the highlight round it. */ export function hitOutline(hit: TextHit): number[] { measure ??= textMeasurer(); return textBox(hit.shape.text!, measure, hit.shape.text!.height * 0.3); }