- 엔진: Engine_Cad_Basin 배경선만 꼭짓점 목록 폴리선(id 는 옛 폴리선과 같음) · 노선 · 유역 · 표 · 도각은 옛 꼴 그대로 · B05 · B06 길 손대지 않음 - CAD 앱: PolyLineEntity.fromJson 이 자식 없고 꼭짓점 목록이면 선분으로 펼침(색 · 굵기 · 층 = 부모) · 다시 내보내면 옛 꼴 - DXF 내보내기 · B09 도면 인쇄 PDF: 꼭짓점 목록 폴리선 = 열린 선(해치만 닫음) - 검증_sub2 유역도 5 m 23.17 → 3.22 MB · 10 m 11.91 → 1.77 MB · 좌표 · id · 색 해시 전후 같음 · B05 · B06 등고선 · 배수 응답 해시 같음 - 시험: test_514_3_b07_basin_light 새로 · vitest cad/polyline-points-514-3 새로 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
207 lines
7.0 KiB
TypeScript
207 lines
7.0 KiB
TypeScript
import type * as Flatten from '@flatten-js/core';
|
|
import { Box, Point, type Segment } from '@flatten-js/core';
|
|
import { mapLimit } from 'blend-promise-utils';
|
|
import { compact, maxBy } from 'es-toolkit';
|
|
import { minBy } from 'es-toolkit/compat';
|
|
import type { Shape, SnapPoint } from '../App.types';
|
|
import type { DrawController } from '../drawControllers/DrawController';
|
|
import { getActiveLayerId, isEntityHighlighted, isEntitySelected } from '../state.ts';
|
|
import { ArcEntity, type ArcJsonData } from './ArcEntity.ts';
|
|
import { type Entity, EntityName, type JsonEntity } from './Entity';
|
|
import { LineEntity, type LineJsonData } from './LineEntity.ts';
|
|
|
|
export class PolyLineEntity implements Entity {
|
|
public id: string = crypto.randomUUID();
|
|
public lineColor = '#fff';
|
|
public lineWidth = 1;
|
|
public lineDash: number[] | undefined = undefined;
|
|
public layerId: string;
|
|
/** 0~1 객체 투명도 (Entity 인터페이스의 선택 항목) */
|
|
public opacity?: number;
|
|
/** GROUP으로 묶인 객체가 공유하는 식별자 */
|
|
public groupId?: string;
|
|
|
|
private readonly entities: Entity[];
|
|
|
|
constructor(layerId: string, entities: Entity[]) {
|
|
this.layerId = layerId;
|
|
this.entities = entities.filter((entity) =>
|
|
[EntityName.Line, EntityName.Arc].includes(entity.getType())
|
|
);
|
|
}
|
|
|
|
/** 폴리선을 이루는 선·호 목록 (EXPLODE·샘플링에서 읽는다) */
|
|
public getEntities(): Entity[] {
|
|
return this.entities;
|
|
}
|
|
|
|
public numberOfSegments(): number {
|
|
return this.entities.length;
|
|
}
|
|
|
|
public draw(
|
|
drawController: DrawController,
|
|
parentHighlighted?: boolean,
|
|
parentSelected?: boolean
|
|
): void {
|
|
for (const entity of this.entities) {
|
|
entity.draw(
|
|
drawController,
|
|
parentHighlighted ?? isEntityHighlighted(this),
|
|
parentSelected ?? isEntitySelected(this)
|
|
);
|
|
}
|
|
}
|
|
|
|
public move(x: number, y: number) {
|
|
for (const entity of this.entities) {
|
|
entity.move(x, y);
|
|
}
|
|
}
|
|
|
|
public scale(scaleOrigin: Point, scaleFactor: number) {
|
|
for (const entity of this.entities) {
|
|
entity.scale(scaleOrigin, scaleFactor);
|
|
}
|
|
}
|
|
|
|
public rotate(rotateOrigin: Point, angle: number) {
|
|
for (const entity of this.entities) {
|
|
entity.rotate(rotateOrigin, angle);
|
|
}
|
|
}
|
|
|
|
public mirror(mirrorAxis: LineEntity) {
|
|
for (const entity of this.entities) {
|
|
entity.mirror(mirrorAxis);
|
|
}
|
|
}
|
|
|
|
public clone(): PolyLineEntity {
|
|
const clonedEntities = this.entities.map((entity) => entity.clone());
|
|
return new PolyLineEntity(this.layerId, clonedEntities);
|
|
}
|
|
|
|
public intersectsWithBox(selectionBox: Box): boolean {
|
|
return this.entities.some((entity) => entity.intersectsWithBox(selectionBox));
|
|
}
|
|
|
|
public isContainedInBox(selectionBox: Box): boolean {
|
|
return this.entities.every((entity) => entity.isContainedInBox(selectionBox));
|
|
}
|
|
|
|
public distanceTo(shape: Shape): [number, Segment] | null {
|
|
const distanceInfos = this.entities.map((entity) => entity.distanceTo(shape));
|
|
if (distanceInfos.every((distanceInfo) => distanceInfo === null)) {
|
|
return null;
|
|
}
|
|
return minBy(compact(distanceInfos), (distanceInfo) => distanceInfo?.[0]);
|
|
}
|
|
|
|
public getBoundingBox(): Box {
|
|
const boundingBoxes = this.entities.map((entity) => entity.getBoundingBox());
|
|
const xmin = minBy(boundingBoxes, (boundingBox) => boundingBox.xmin).xmin;
|
|
const ymin = minBy(boundingBoxes, (boundingBox) => boundingBox.ymin).ymin;
|
|
const xmax = maxBy(boundingBoxes, (boundingBox) => boundingBox.xmax).xmax;
|
|
const ymax = maxBy(boundingBoxes, (boundingBox) => boundingBox.ymax).ymax;
|
|
return new Box(xmin, ymin, xmax, ymax);
|
|
}
|
|
|
|
public getShape(): Shape | null {
|
|
return null;
|
|
}
|
|
|
|
public getSnapPoints(): SnapPoint[] {
|
|
return this.entities.flatMap((entity) => entity.getSnapPoints());
|
|
}
|
|
|
|
public getIntersections(entity: Entity): Point[] {
|
|
return this.entities.flatMap((polyLineEntity) => polyLineEntity.getIntersections(entity));
|
|
}
|
|
|
|
public getFirstPoint(): Point | null {
|
|
return this.entities.find((entity) => !!entity.getFirstPoint())?.getFirstPoint() || null;
|
|
}
|
|
|
|
public getSvgString(): string | null {
|
|
const svgTexts = this.entities.map((entity) => entity.getSvgString());
|
|
return compact(svgTexts).join('\n');
|
|
}
|
|
|
|
public getType(): EntityName {
|
|
return EntityName.PolyLine;
|
|
}
|
|
|
|
public containsPointOnShape(point: Flatten.Point): boolean {
|
|
return this.entities.some((entity) => entity.containsPointOnShape(point));
|
|
}
|
|
|
|
public async toJson(): Promise<JsonEntity | null> {
|
|
return {
|
|
id: this.id,
|
|
type: EntityName.PolyLine,
|
|
lineColor: this.lineColor,
|
|
lineWidth: this.lineWidth,
|
|
lineDash: this.lineDash,
|
|
layerId: this.layerId,
|
|
shapeData: null,
|
|
children: compact(await mapLimit(this.entities, 20, (entity) => entity.toJson())),
|
|
};
|
|
}
|
|
|
|
public static async fromJson(jsonEntity: JsonEntity<PolyLineJsonData>): Promise<PolyLineEntity> {
|
|
const entities: (ArcEntity | LineEntity | null)[] = await mapLimit(
|
|
jsonEntity.children || [],
|
|
20,
|
|
async (childEntity: JsonEntity): Promise<LineEntity | ArcEntity | null> => {
|
|
const type = childEntity.type;
|
|
switch (type) {
|
|
case EntityName.Arc:
|
|
return ArcEntity.fromJson(childEntity as JsonEntity<ArcJsonData>);
|
|
case EntityName.Line:
|
|
return LineEntity.fromJson(childEntity as JsonEntity<LineJsonData>);
|
|
// Only arc and lines can be part of a polyline
|
|
// Circle, rectangle can't be used because they are already closed
|
|
// Other entities cannot be used since they are not a valid part of a polyline
|
|
// case EntityName.Rectangle:
|
|
// return RectangleEntity.fromJson(childEntity as JsonEntity<RectangleJsonData>);
|
|
// case EntityName.Point:
|
|
// return PointEntity.fromJson(childEntity as JsonEntity<PointJsonData>);
|
|
// case EntityName.Image:
|
|
// return ImageEntity.fromJson(childEntity as JsonEntity<ImageJsonData>);
|
|
// case EntityName.Measurement:
|
|
// return MeasurementEntity.fromJson(childEntity as JsonEntity<MeasurementJsonData>);
|
|
// case EntityName.ArrowHead:
|
|
// return ArrowHeadEntity.fromJson(childEntity as JsonEntity<ArrowHeadJsonData>);
|
|
// case EntityName.Text:
|
|
// return TextEntity.fromJson(childEntity as JsonEntity<TextJsonData>);
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
);
|
|
|
|
const layerId = jsonEntity.layerId || getActiveLayerId();
|
|
// 514-3 가벼운 꼴 — 자식 대신 꼭짓점 목록(B07 유역도 배경선) · 선분으로 펼침
|
|
const points = jsonEntity.children?.length ? [] : (jsonEntity.shapeData?.points ?? []);
|
|
for (let index = 1; index < points.length; index++) {
|
|
const start = points[index - 1];
|
|
const end = points[index];
|
|
const line = new LineEntity(layerId, new Point(start.x, start.y), new Point(end.x, end.y));
|
|
line.lineColor = jsonEntity.lineColor;
|
|
line.lineWidth = jsonEntity.lineWidth;
|
|
line.lineDash = jsonEntity.lineDash;
|
|
entities.push(line);
|
|
}
|
|
|
|
const polyLineEntity = new PolyLineEntity(layerId, compact(entities));
|
|
polyLineEntity.id = jsonEntity.id;
|
|
polyLineEntity.lineColor = jsonEntity.lineColor;
|
|
polyLineEntity.lineWidth = jsonEntity.lineWidth;
|
|
polyLineEntity.lineDash = jsonEntity.lineDash;
|
|
return polyLineEntity;
|
|
}
|
|
}
|
|
|
|
export type PolyLineJsonData = { points?: { x: number; y: number }[] };
|