/** 치수·지시선이 함께 쓰는 조각 만들기 (화살표·치수 문자·직전 치수 기억) */ import { Point } from '@flatten-js/core'; import { getDimArrowSize, getDimDecimals, getDimTextHeight } from '../../commands/dim-settings'; import { ArrowHeadEntity } from '../../entities/ArrowHeadEntity'; import type { Entity } from '../../entities/Entity'; import type { MeasurementEntity } from '../../entities/MeasurementEntity'; import { getActiveLayerId, getActiveLineColor, getScreenCanvasDrawController } from '../../state'; import { textEntity } from '../factories/entity-factory'; /** 화면 px 기준 상수를 도면 단위로 바꾼다 (줌이 달라도 크기가 일정하게 보인다) */ export function worldFactor(): number { try { return getScreenCanvasDrawController().getScreenScale() || 1; } catch { return 1; } } /** tip을 꼭짓점으로 하고 from 방향에서 들어오는 화살표 */ export function arrowHead(tip: Point, from: Point, size = getDimArrowSize()): ArrowHeadEntity { const length = size / worldFactor(); const dx = tip.x - from.x; const dy = tip.y - from.y; const distance = Math.hypot(dx, dy) || 1; const ux = dx / distance; const uy = dy / distance; const baseX = tip.x - ux * length; const baseY = tip.y - uy * length; const halfWidth = length * 0.35; const head = new ArrowHeadEntity( getActiveLayerId(), tip, new Point(baseX - uy * halfWidth, baseY + ux * halfWidth), new Point(baseX + uy * halfWidth, baseY - ux * halfWidth) ); head.lineColor = getActiveLineColor(); head.fillColor = getActiveLineColor(); return head; } /** 치수 문자 — 설정한 소수 자릿수·문자 높이를 따른다 */ export function dimensionText(value: number, position: Point, prefix = '', suffix = ''): Entity { const label = `${prefix}${value.toFixed(getDimDecimals())}${suffix}`; return textEntity(label, position, { fontSize: getDimTextHeight() / worldFactor(), textAlign: 'center', }); } /** 문자 그대로 찍는 주석 (좌표·각도처럼 단위가 다른 값) */ export function annotationText(label: string, position: Point): Entity { return textEntity(label, position, { fontSize: getDimTextHeight() / worldFactor(), textAlign: 'center', }); } /** DIMBASELINE·DIMCONTINUE가 이어 그릴 직전 치수 */ let lastDimension: MeasurementEntity | null = null; export const rememberDimension = (dimension: MeasurementEntity): void => { lastDimension = dimension; }; export const getLastDimension = (): MeasurementEntity | null => lastDimension;