DXF-Import: TEXT/MTEXT -> editierbarer Plan-Text (Drawing2D text-Primitiv, SVG-Render)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { parseDxf } from "./dxfParser";
|
||||
import { contoursToDrawings } from "./dxfToDrawings";
|
||||
import { contoursToDrawings, textsToDrawings } from "./dxfToDrawings";
|
||||
import type { Contour, ContourSet, Vec2 } from "../model/types";
|
||||
|
||||
/** Minimales DXF aus Gruppencode/Wert-Paaren; nur eine ENTITIES-Sektion. */
|
||||
@@ -411,6 +411,66 @@ describe("parseDxf — HATCH", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ── TEXT / MTEXT ──────────────────────────────────────────────────────────────
|
||||
|
||||
/** TEXT-Entity: Position, Höhe, Rotation (Grad), String. */
|
||||
function textEnt(x: number, y: number, h: number, rotDeg: number, s: string): string[] {
|
||||
return ["0", "TEXT", "8", "0", "10", `${x}`, "20", `${y}`, "30", "0", "40", `${h}`, "50", `${rotDeg}`, "1", s];
|
||||
}
|
||||
|
||||
/** MTEXT-Entity: Position, Höhe, String (Rotation 0). */
|
||||
function mtextEnt(x: number, y: number, h: number, s: string): string[] {
|
||||
return ["0", "MTEXT", "8", "0", "10", `${x}`, "20", `${y}`, "30", "0", "40", `${h}`, "50", "0", "1", s];
|
||||
}
|
||||
|
||||
describe("parseDxf — TEXT/MTEXT", () => {
|
||||
it("TEXT → ImportedText mit Position/Höhe/Winkel (Grad→Radiant)", () => {
|
||||
const res = parseDxf(dxf(textEnt(5, 3, 0.5, 90, "Hallo")));
|
||||
expect(res.texts).toHaveLength(1);
|
||||
const t0 = res.texts![0];
|
||||
expect(t0.at.x).toBeCloseTo(5, 9);
|
||||
expect(t0.at.y).toBeCloseTo(3, 9);
|
||||
expect(t0.height).toBeCloseTo(0.5, 9);
|
||||
expect(t0.angle).toBeCloseTo(Math.PI / 2, 9);
|
||||
expect(t0.text).toBe("Hallo");
|
||||
});
|
||||
|
||||
it("MTEXT → Formatcodes gesäubert (\\P → Leerzeichen)", () => {
|
||||
const res = parseDxf(dxf(mtextEnt(1, 2, 0.3, "Welt\\Pzeile2")));
|
||||
expect(res.texts![0].text).toBe("Welt zeile2");
|
||||
expect(res.texts![0].at.x).toBeCloseTo(1, 9);
|
||||
expect(res.texts![0].height).toBeCloseTo(0.3, 9);
|
||||
});
|
||||
|
||||
it("leerer Text wird übersprungen", () => {
|
||||
const res = parseDxf(dxf(textEnt(0, 0, 0.2, 0, "")));
|
||||
expect(res.texts ?? []).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("fehlende Höhe → positiver Default", () => {
|
||||
const g = ["0", "TEXT", "8", "0", "10", "0", "20", "0", "30", "0", "1", "X"];
|
||||
const res = parseDxf(dxf(g));
|
||||
expect(res.texts![0].height).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("textsToDrawings", () => {
|
||||
it("ImportedText → Drawing2D {shape:\"text\"}", () => {
|
||||
const drawings = textsToDrawings(
|
||||
[{ at: { x: 1, y: 2 }, text: "Hi", height: 0.4, angle: 0, layer: "L" }],
|
||||
"lvl", "active", "CODE", (s) => s,
|
||||
);
|
||||
expect(drawings).toHaveLength(1);
|
||||
const g = drawings[0].geom;
|
||||
expect(g.shape).toBe("text");
|
||||
if (g.shape === "text") {
|
||||
expect(g.text).toBe("Hi");
|
||||
expect(g.height).toBeCloseTo(0.4, 9);
|
||||
expect(g.at.x).toBeCloseTo(1, 9);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("contoursToDrawings — HATCH-Füllung", () => {
|
||||
it("gefüllte Kontur → Drawing2D mit fillColor und polyline-Form", () => {
|
||||
const set: ContourSet = {
|
||||
|
||||
+54
-3
@@ -24,9 +24,10 @@
|
||||
// • INSERT → Block-Konturen, transformiert (Scale/Rot/Array, verschachtelt).
|
||||
// • HATCH → gefüllte Kontur(en) je Randpfad (Custom-Handler,
|
||||
// Polyline- + Linien-/Bogen-/Ellipsen-/Spline-Kanten; Fill via `Contour.filled`).
|
||||
// • TEXT / MTEXT → ImportedText (Position/Höhe/Winkel) → Drawing2D `{shape:"text"}`.
|
||||
|
||||
import DxfParser from "dxf-parser";
|
||||
import type { Contour, ContourSet, ImportedMesh, Vec2 } from "../model/types";
|
||||
import type { Contour, ContourSet, ImportedMesh, ImportedText, Vec2 } from "../model/types";
|
||||
|
||||
/**
|
||||
* Optionale Import-Diagnose: Histogramm der gelesenen Entity-Typen (Typ → Anzahl)
|
||||
@@ -41,10 +42,12 @@ export interface ImportDiagnostics {
|
||||
total: number;
|
||||
}
|
||||
|
||||
/** Ergebnis eines DXF-Imports: rohe Meshes + Konturen-Sätze. */
|
||||
/** Ergebnis eines DXF-Imports: rohe Meshes + Konturen-Sätze + Texte. */
|
||||
export interface DxfImportResult {
|
||||
meshes: ImportedMesh[];
|
||||
contours: ContourSet[];
|
||||
/** Importierte TEXT/MTEXT-Elemente (optional; der DWG-Parser füllt sie nicht). */
|
||||
texts?: ImportedText[];
|
||||
/** Optionale Diagnose (Entity-Typ-Histogramm); vom DWG-Parser gefüllt. */
|
||||
diagnostics?: ImportDiagnostics;
|
||||
}
|
||||
@@ -97,6 +100,12 @@ interface DxfEntity {
|
||||
rowSpacing?: number;
|
||||
// HATCH: rohe Gruppencodes (vom Custom-Handler gesammelt, siehe HatchHandler).
|
||||
rawCodes?: RawCode[];
|
||||
// TEXT/MTEXT. `position` (MTEXT-Einfügepunkt) wird tolerant gelesen (oben als
|
||||
// MESH-Vertexliste getippt). `rotation` (Grad) ist bereits oben deklariert.
|
||||
startPoint?: DxfVertex; // TEXT-Einfügepunkt
|
||||
textHeight?: number; // TEXT-Höhe (Code 40)
|
||||
height?: number; // MTEXT-Höhe (Code 40)
|
||||
text?: string; // Textinhalt (Code 1, MTEXT auch 3)
|
||||
[k: string]: unknown;
|
||||
}
|
||||
/** Ein rohes DXF-Gruppencode/Wert-Paar (HATCH-Randpfad-Auswertung). */
|
||||
@@ -140,6 +149,7 @@ export function parseDxf(text: string): DxfImportResult {
|
||||
const meshTriangles: number[] = []; // gesammelte Mesh-Positions (x,y,z…)
|
||||
const meshIndices: number[] = [];
|
||||
const contours: Contour[] = [];
|
||||
const texts: ImportedText[] = [];
|
||||
|
||||
for (const e of entities) {
|
||||
const type = (e.type ?? "").toUpperCase();
|
||||
@@ -156,6 +166,11 @@ export function parseDxf(text: string): DxfImportResult {
|
||||
addPolyfaceMesh(meshTriangles, meshIndices, e);
|
||||
continue;
|
||||
}
|
||||
if (type === "TEXT" || type === "MTEXT") {
|
||||
const it = textEntity(e, type);
|
||||
if (it) texts.push(it);
|
||||
continue;
|
||||
}
|
||||
// Alle übrigen Kontur-Entities (inkl. SPLINE + INSERT-Block-Expansion) über
|
||||
// den gemeinsamen Sammler — dieselbe Logik nutzt die INSERT-Rekursion.
|
||||
collectContours(e, blocks, contours, 0);
|
||||
@@ -182,7 +197,43 @@ export function parseDxf(text: string): DxfImportResult {
|
||||
});
|
||||
}
|
||||
|
||||
return { meshes, contours: contourSets };
|
||||
return { meshes, contours: contourSets, texts };
|
||||
}
|
||||
|
||||
// ── TEXT / MTEXT ─────────────────────────────────────────────────────────────
|
||||
|
||||
/** Fallback-Schrifthöhe (Meter), wenn die DXF-Höhe fehlt/0 ist. */
|
||||
const DEFAULT_TEXT_HEIGHT = 0.25;
|
||||
|
||||
/**
|
||||
* MTEXT-Inhalt von Formatierungscodes säubern (grober erster Wurf): \P → Leer-
|
||||
* zeichen, \~ → Leerzeichen, `\f…;`/`\H…;`/`\C…;`/`\A…;`-Sequenzen und die {}-
|
||||
* Gruppenklammern entfernen, `\{`/`\}`/`\\` entschärfen. Komplexe Verschachte-
|
||||
* lungen werden flachgeklopft (dokumentierte Grenze).
|
||||
*/
|
||||
function sanitizeMText(s: string): string {
|
||||
return s
|
||||
.replace(/\\P/gi, " ")
|
||||
.replace(/\\~/g, " ")
|
||||
.replace(/\\[A-Za-z][^;\\]*;/g, "")
|
||||
.replace(/[{}]/g, "")
|
||||
.replace(/\\([{}\\])/g, "$1")
|
||||
.trim();
|
||||
}
|
||||
|
||||
/** TEXT/MTEXT-Entity → ImportedText (Position/Höhe/Winkel), oder null bei leer. */
|
||||
function textEntity(e: DxfEntity, type: string): ImportedText | null {
|
||||
const rawText = typeof e.text === "string" ? e.text : "";
|
||||
const content = type === "MTEXT" ? sanitizeMText(rawText) : rawText;
|
||||
if (!content) return null;
|
||||
const at: Vec2 =
|
||||
type === "MTEXT"
|
||||
? { x: coord(e.position, "x"), y: coord(e.position, "y") }
|
||||
: { x: e.startPoint?.x ?? 0, y: e.startPoint?.y ?? 0 };
|
||||
const rawHeight = type === "MTEXT" ? numOr(e.height, 0) : numOr(e.textHeight, 0);
|
||||
const height = rawHeight > 0 ? rawHeight : DEFAULT_TEXT_HEIGHT;
|
||||
const angle = (numOr(e.rotation, 0) * Math.PI) / 180; // DXF: Grad CCW → Radiant
|
||||
return { at, text: content, height, angle, layer: e.layer };
|
||||
}
|
||||
|
||||
// ── Mesh-Entities ────────────────────────────────────────────────────────────
|
||||
|
||||
+35
-1
@@ -15,7 +15,7 @@
|
||||
//
|
||||
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import type { Contour, ContourSet, Drawing2D, Drawing2DGeom } from "../model/types";
|
||||
import type { Contour, ContourSet, Drawing2D, Drawing2DGeom, ImportedText } from "../model/types";
|
||||
|
||||
/** Wie DXF-Layer auf Kategorien abgebildet werden. */
|
||||
export type CategoryMode = "active" | "byLayer";
|
||||
@@ -66,6 +66,40 @@ export function contoursToDrawings(
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wandelt importierte Texte in Drawing2D-`text`-Elemente um — analog
|
||||
* `contoursToDrawings` (gleiche Layer→Kategorie-Zuordnung). Winkel/Höhe kommen
|
||||
* 1:1 aus `ImportedText` (Höhe in Modell-Metern, Winkel in Radiant).
|
||||
*/
|
||||
export function textsToDrawings(
|
||||
texts: ImportedText[],
|
||||
levelId: string,
|
||||
categoryMode: CategoryMode,
|
||||
fallbackCode: string,
|
||||
layerToCode: (layerName: string) => string,
|
||||
): Drawing2D[] {
|
||||
const out: Drawing2D[] = [];
|
||||
for (const it of texts) {
|
||||
const layerName = it.layer ?? "";
|
||||
const categoryCode =
|
||||
categoryMode === "byLayer" && layerName ? layerToCode(layerName) : fallbackCode;
|
||||
out.push({
|
||||
id: nextId(),
|
||||
type: "drawing2d",
|
||||
levelId,
|
||||
categoryCode,
|
||||
geom: {
|
||||
shape: "text",
|
||||
at: { ...it.at },
|
||||
text: it.text,
|
||||
height: it.height,
|
||||
angle: it.angle,
|
||||
},
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Eine Kontur → 2D-Geometrie (line bei 2 offenen Punkten, sonst polyline). */
|
||||
function contourGeom(contour: Contour): Drawing2DGeom | null {
|
||||
const pts = contour.pts;
|
||||
|
||||
Reference in New Issue
Block a user