From 80516c5f6da796dcf6f51afe4702b7f150e53ba9 Mon Sep 17 00:00:00 2001 From: karim Date: Tue, 20 Oct 2026 00:56:59 +0200 Subject: [PATCH] Verschieben/Kopieren/Drehen: Vorschau fuer ALLE 2D-Formen (Kreis, Bogen, Text, Bemassung, Polylinien-Boegen ...) --- src/tools/drawingPreview.test.ts | 28 ++++++++++ src/tools/drawingPreview.ts | 93 ++++++++++++++++++++++++++++++++ src/tools/transform.ts | 6 +++ 3 files changed, 127 insertions(+) create mode 100644 src/tools/drawingPreview.test.ts create mode 100644 src/tools/drawingPreview.ts diff --git a/src/tools/drawingPreview.test.ts b/src/tools/drawingPreview.test.ts new file mode 100644 index 0000000..70d1c53 --- /dev/null +++ b/src/tools/drawingPreview.test.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from "vitest"; +import type { Drawing2D, Project } from "../model/types"; +import { drawingPreviewShapes, primitivesToDraftShapes } from "./drawingPreview"; +import type { Primitive } from "../plan/generatePlan"; + +const project = { lineStyles: [], hatches: [], layers: [], drawings2d: [], blocks: [] } as unknown as Project; +const base = { id: "d", levelId: "L", categoryCode: "10" } as unknown as Drawing2D; + +describe("Verschiebe-Vorschau für alle 2D-Formen", () => { + it("Kreis → Liniensegmente rundum", () => { + const s = drawingPreviewShapes(project, { ...base, geom: { shape: "circle", center: { x: 0, y: 0 }, r: 1 } } as Drawing2D); + expect(s.length).toBeGreaterThan(10); + expect(s.every((x) => x.kind === "line")).toBe(true); + }); + it("Polylinie mit Bogen-Segment → Bogen abgetastet (nicht nur die Sehne)", () => { + const s = drawingPreviewShapes(project, { + ...base, + geom: { shape: "polyline", pts: [{ x: 0, y: 0 }, { x: 2, y: 0 }], closed: false, bulge: [0.5] }, + } as Drawing2D); + expect(s.length).toBeGreaterThan(3); + }); + it("Text → Rahmen-Polygon", () => { + const p = { kind: "drawingText", at: { x: 0, y: 0 }, text: "Hallo", heightM: 1, angle: 0, color: "#000" } as unknown as Primitive; + const s = primitivesToDraftShapes([p]); + expect(s).toHaveLength(1); + expect(s[0].kind).toBe("poly"); + }); +}); diff --git a/src/tools/drawingPreview.ts b/src/tools/drawingPreview.ts new file mode 100644 index 0000000..1fc3424 --- /dev/null +++ b/src/tools/drawingPreview.ts @@ -0,0 +1,93 @@ +// Vorschau-Formen eines (transformierten) 2D-Elements für Verschieben/Kopieren/ +// Drehen/Spiegeln/Skalieren: das Element läuft durch DIESELBE Primitiv-Erzeugung +// wie der Plan (`addDrawing2D`), die Primitive werden in einfache Vorschau-Formen +// (Linien/Polygone) übersetzt. So hat JEDE Form eine Vorschau — Kreis, Bogen, +// Spline, Text, Bemassung, Kote, Block, Polylinien-Bögen … (vorher nur Linie/ +// Polylinie/Rechteck/Bild, alles andere war beim Verschieben unsichtbar). + +import type { Drawing2D, Project, Vec2 } from "../model/types"; +import type { Primitive } from "../plan/generatePlan"; +import { addDrawing2D } from "../plan/generatePlan/drawing2d"; +import { textFrameCorners } from "../text/textFrame"; +import type { DraftShape } from "./types"; + +/** Obergrenze je Element (Schraffur-Muster u. Ä. können sehr viele Linien erzeugen). */ +const MAX_SHAPES_PER_ITEM = 4000; + +const EMPTY_LW = new Map(); +const EMPTY_COLOR = new Map(); +const EMPTY_CAT = new Map(); + +function ringLines(center: Vec2, r: number, a0: number, a1: number, out: DraftShape[]): void { + const sweep = a1 - a0; + const n = Math.max(8, Math.ceil((Math.abs(sweep) / (Math.PI * 2)) * 48)); + let prev = { x: center.x + r * Math.cos(a0), y: center.y + r * Math.sin(a0) }; + for (let i = 1; i <= n; i++) { + const a = a0 + (sweep * i) / n; + const p = { x: center.x + r * Math.cos(a), y: center.y + r * Math.sin(a) }; + out.push({ kind: "line", a: prev, b: p }); + prev = p; + } +} + +/** Plan-Primitive → Vorschau-Formen. */ +export function primitivesToDraftShapes(prims: readonly Primitive[]): DraftShape[] { + const out: DraftShape[] = []; + for (const p of prims) { + if (out.length >= MAX_SHAPES_PER_ITEM) break; + switch (p.kind) { + case "line": + out.push({ kind: "line", a: p.a, b: p.b }); + break; + case "polygon": + if (p.pts.length >= 2) out.push({ kind: "poly", pts: p.pts, closed: true }); + break; + case "drawingCircle": + ringLines(p.center, p.r, 0, Math.PI * 2, out); + break; + case "drawingArc": { + const TAU = Math.PI * 2; + const sweep = (((p.a1 - p.a0) % TAU) + TAU) % TAU || TAU; + ringLines(p.center, p.r, p.a0, p.a0 + sweep, out); + break; + } + case "arc": { + const a0 = Math.atan2(p.from.y - p.center.y, p.from.x - p.center.x); + const a1 = Math.atan2(p.to.y - p.center.y, p.to.x - p.center.x); + let d = a1 - a0; + while (d <= -Math.PI) d += 2 * Math.PI; + while (d > Math.PI) d -= 2 * Math.PI; + const r = Math.hypot(p.from.x - p.center.x, p.from.y - p.center.y); + ringLines(p.center, r, a0, a0 + d, out); + break; + } + case "drawingText": + out.push({ kind: "poly", pts: textFrameCorners(p), closed: true }); + break; + case "drawingTextPath": + for (let i = 0; i + 1 < p.pathPts.length; i++) out.push({ kind: "line", a: p.pathPts[i], b: p.pathPts[i + 1] }); + break; + case "drawingImage": + out.push({ + kind: "poly", + pts: [p.min, { x: p.max.x, y: p.min.y }, p.max, { x: p.min.x, y: p.max.y }], + closed: true, + }); + break; + default: + break; + } + } + return out; +} + +/** Vorschau-Formen eines 2D-Elements (Geometrie bereits transformiert). */ +export function drawingPreviewShapes(project: Project, d: Drawing2D, scaleDenominator?: number): DraftShape[] { + const prims: Primitive[] = []; + try { + addDrawing2D(prims, project, d, false, EMPTY_LW, EMPTY_COLOR, EMPTY_CAT, scaleDenominator); + } catch { + return []; + } + return primitivesToDraftShapes(prims); +} diff --git a/src/tools/transform.ts b/src/tools/transform.ts index c871a2d..2874fce 100644 --- a/src/tools/transform.ts +++ b/src/tools/transform.ts @@ -27,6 +27,7 @@ import { wallCorners } from "../model/geometry"; import { columnFootprint } from "../geometry/column"; import { stairOutline } from "../geometry/stair"; import type { DraftShape } from "./types"; +import { drawingPreviewShapes } from "./drawingPreview"; import { uniqueId } from "./types"; import type { CommandSelection } from "../commands/types"; @@ -543,6 +544,11 @@ function itemPreview(project: Project, item: AnyItem, fn: (p: Vec2) => Vec2): Dr return stairPreviewShapes(item.stair, fn); } const g = mvGeom(item.drawing.geom, fn); + // Allgemein: dieselbe Primitiv-Erzeugung wie im Plan (alle Formen, inkl. Bögen + // in Polylinien, Text, Bemassung, Kreis …). Fallback unten nur, falls daraus + // nichts entsteht. + const generic = drawingPreviewShapes(project, { ...item.drawing, geom: g }); + if (generic.length) return generic; switch (g.shape) { case "line": return [{ kind: "line", a: g.a, b: g.b }];