Archived
DXF-Import legt fehlende Layer als Kategorien an
\"DXF-Layer als Kategorien übernehmen\" mappte bisher nur auf bestehende Kategorien mit exakt gleichem Namen; alle anderen Layer fielen auf die aktive Kategorie zurück — bei einem Kataster-Import (70 Layer) landete alles auf \"Wände\". planImportCategories legt jetzt für jeden unbekannten Layer eine Unterkategorie an, gruppiert unter EINER neuen Elternkategorie \"Import: <Name>\" (kein Fluten des Ebenen-Panels), mit der Farbe aus der LAYER-Tabelle der DXF (ACI 7 → dunkle Tinte statt unsichtbarem Weiss). Bestehende gleichnamige Kategorien (case-insensitiv, im ganzen Baum) werden weiterverwendet; Layer, die sich nur in der Schreibweise unterscheiden, teilen sich eine Kategorie. Konturen UND Texte werden berücksichtigt.
This commit is contained in:
1 parent
8f1cd49568
commit
819f5c4b70
8 files changed
+178
-4
No files matched your search
@@ -1568,6 +1568,7 @@ export const de = {
|
||||
"import.target.newNamePlaceholder": "Name der neuen Zeichnung",
|
||||
"import.layers.heading": "Ebenen-Handling (DXF-Layer)",
|
||||
"import.layers.asCategories": "DXF-Layer als Kategorien übernehmen",
|
||||
"import.layers.parentName": "Import: {name}",
|
||||
"import.layers.toActive": "Alles auf die aktive Kategorie ({code})",
|
||||
"import.placement.heading": "Platzierung",
|
||||
"import.placement.origin": "An DXF-Koordinaten (relativ zum Nullpunkt)",
|
||||
|
||||
@@ -1556,6 +1556,7 @@ export const en: Record<TranslationKey, string> = {
|
||||
"import.target.newNamePlaceholder": "New drawing name",
|
||||
"import.layers.heading": "Layer handling (DXF layers)",
|
||||
"import.layers.asCategories": "Keep DXF layers as categories",
|
||||
"import.layers.parentName": "Import: {name}",
|
||||
"import.layers.toActive": "Everything onto the active category ({code})",
|
||||
"import.placement.heading": "Placement",
|
||||
"import.placement.origin": "At DXF coordinates (relative to origin)",
|
||||
|
||||
+19
-1
@@ -51,6 +51,8 @@ export interface DxfImportResult {
|
||||
texts?: ImportedText[];
|
||||
/** Optionale Diagnose (Entity-Typ-Histogramm); vom DWG-Parser gefüllt. */
|
||||
diagnostics?: ImportDiagnostics;
|
||||
/** Farbe je DXF-Layer (Hex) aus der LAYER-Tabelle — für neu angelegte Kategorien. */
|
||||
layerColors?: Record<string, string>;
|
||||
}
|
||||
|
||||
// Lose getippte Sicht auf die dxf-parser-Ausgabe (das Paket liefert keine engen
|
||||
@@ -132,6 +134,22 @@ interface DxfBlock {
|
||||
interface DxfDocument {
|
||||
entities?: DxfEntity[];
|
||||
blocks?: Record<string, DxfBlock>;
|
||||
tables?: { layer?: { layers?: Record<string, { name?: string; color?: number }> } };
|
||||
}
|
||||
|
||||
/**
|
||||
* Layer-Farben aus der LAYER-Tabelle als Hex. ACI 7 („weiss/schwarz", je nach
|
||||
* Hintergrund) kommt als #ffffff an — auf dem hellen Plan wäre das unsichtbar,
|
||||
* daher wie in CAD-Programmen auf dunkle Tinte gekippt.
|
||||
*/
|
||||
function readLayerColors(doc: DxfDocument): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
for (const [name, layer] of Object.entries(doc.tables?.layer?.layers ?? {})) {
|
||||
if (typeof layer.color !== "number") continue;
|
||||
const hex = `#${(layer.color & 0xffffff).toString(16).padStart(6, "0")}`;
|
||||
out[name] = hex === "#ffffff" ? "#1a1a1a" : hex;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
let importSeq = 0;
|
||||
@@ -216,7 +234,7 @@ export function parseDxf(text: string, idPrefix = ""): DxfImportResult {
|
||||
});
|
||||
}
|
||||
|
||||
return { meshes, contours: contourSets, texts };
|
||||
return { meshes, contours: contourSets, texts, layerColors: readLayerColors(doc) };
|
||||
}
|
||||
|
||||
// ── TEXT / MTEXT ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -86,12 +86,15 @@ function mergeResults(results: DxfImportResult[]): DxfImportResult {
|
||||
const meshes: ImportedMesh[] = [];
|
||||
const contours: ContourSet[] = [];
|
||||
const texts: ImportedText[] = [];
|
||||
const layerColors: Record<string, string> = {};
|
||||
for (const r of results) {
|
||||
for (const m of r.meshes) meshes.push(m);
|
||||
for (const c of r.contours) contours.push(c);
|
||||
for (const t of r.texts ?? []) texts.push(t);
|
||||
// Jeder Chunk trägt dieselbe Präambel (inkl. LAYER-Tabelle) — Vereinigung reicht.
|
||||
Object.assign(layerColors, r.layerColors);
|
||||
}
|
||||
return { meshes, contours, texts };
|
||||
return { meshes, contours, texts, layerColors };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import type { LayerCategory } from "../model/types";
|
||||
import { planImportCategories } from "./dxfToDrawings";
|
||||
|
||||
const cat = (code: string, name: string, children?: LayerCategory[]): LayerCategory => ({
|
||||
code,
|
||||
name,
|
||||
color: "#000000",
|
||||
lw: 0.18,
|
||||
visible: true,
|
||||
locked: false,
|
||||
children,
|
||||
});
|
||||
|
||||
describe("planImportCategories", () => {
|
||||
it("legt fehlende Layer als Unterkategorien EINER neuen Elternkategorie an", () => {
|
||||
const existing = [cat("20", "Wände"), cat("90", "Referenzen")];
|
||||
const { parent, codeByLayer } = planImportCategories(
|
||||
["01421", "01422"],
|
||||
existing,
|
||||
"Import Luzern",
|
||||
{ "01421": "#ff0000" },
|
||||
);
|
||||
expect(parent?.name).toBe("Import Luzern");
|
||||
expect(parent?.children?.map((c) => c.name)).toEqual(["01421", "01422"]);
|
||||
expect(parent?.children?.[0].color).toBe("#ff0000");
|
||||
// Codes laufen hinter dem grössten bestehenden weiter und sind eindeutig.
|
||||
const codes = [parent!.code, ...parent!.children!.map((c) => c.code)];
|
||||
expect(new Set(codes).size).toBe(3);
|
||||
expect(codes.every((c) => parseInt(c, 10) > 90)).toBe(true);
|
||||
expect(codeByLayer.get("01421")).toBe(parent!.children![0].code);
|
||||
});
|
||||
|
||||
it("nutzt bestehende gleichnamige Kategorien (case-insensitiv, auch verschachtelt)", () => {
|
||||
const existing = [cat("05", "Kontext", [cat("10", "Situation")])];
|
||||
const { parent, codeByLayer } = planImportCategories(["SITUATION"], existing, "Import");
|
||||
expect(parent).toBeNull();
|
||||
expect(codeByLayer.get("SITUATION")).toBe("10");
|
||||
});
|
||||
|
||||
it("teilt sich eine Kategorie bei Layern, die sich nur in der Schreibweise unterscheiden", () => {
|
||||
const { parent, codeByLayer } = planImportCategories(["Gebaeude", "GEBAEUDE"], [], "Import");
|
||||
expect(parent?.children).toHaveLength(1);
|
||||
expect(codeByLayer.get("Gebaeude")).toBe(codeByLayer.get("GEBAEUDE"));
|
||||
});
|
||||
|
||||
it("legt nichts an, wenn keine Layer vorhanden sind", () => {
|
||||
expect(planImportCategories([], [cat("20", "Wände")], "Import").parent).toBeNull();
|
||||
});
|
||||
});
|
||||
+74
-1
@@ -15,7 +15,7 @@
|
||||
//
|
||||
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import type { Contour, ContourSet, Drawing2D, Drawing2DGeom, ImportedText } from "../model/types";
|
||||
import type { Contour, ContourSet, Drawing2D, Drawing2DGeom, ImportedText, LayerCategory } from "../model/types";
|
||||
|
||||
/** Wie DXF-Layer auf Kategorien abgebildet werden. */
|
||||
export type CategoryMode = "active" | "byLayer";
|
||||
@@ -122,6 +122,72 @@ function contourGeom(contour: Contour): Drawing2DGeom | null {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Kategorien für DXF-Layer, die im Projekt noch KEINE gleichnamige Kategorie
|
||||
* haben (Namensvergleich case-insensitiv, über den ganzen Baum). Sie werden als
|
||||
* Unterkategorien EINER neuen Elternkategorie `parentName` angelegt, damit ein
|
||||
* Import mit Dutzenden Layern (amtliche Vermessung) das Ebenen-Panel nicht flutet.
|
||||
* Codes: fortlaufend ab dem grössten bestehenden numerischen Code (wie
|
||||
* `nextFreeCode`). Liefert die neue Elternkategorie (oder null, wenn alle Layer
|
||||
* schon existieren) und das Mapping Layer-Name → Code für ALLE Layer.
|
||||
*/
|
||||
export function planImportCategories(
|
||||
layerNames: readonly string[],
|
||||
existing: readonly LayerCategory[],
|
||||
parentName: string,
|
||||
layerColors: Readonly<Record<string, string>> = {},
|
||||
): { parent: LayerCategory | null; codeByLayer: Map<string, string> } {
|
||||
const used = new Set<string>();
|
||||
const byName = new Map<string, string>();
|
||||
const walk = (list: readonly LayerCategory[]) => {
|
||||
for (const c of list) {
|
||||
used.add(c.code);
|
||||
byName.set(c.name.toLowerCase(), c.code);
|
||||
if (c.children) walk(c.children);
|
||||
}
|
||||
};
|
||||
walk(existing);
|
||||
let next = Math.max(0, ...[...used].map((c) => parseInt(c, 10)).filter((n) => !isNaN(n))) + 1;
|
||||
const allocate = (): string => {
|
||||
while (used.has(String(next).padStart(2, "0"))) next++;
|
||||
const code = String(next++).padStart(2, "0");
|
||||
used.add(code);
|
||||
return code;
|
||||
};
|
||||
|
||||
const codeByLayer = new Map<string, string>();
|
||||
const children: LayerCategory[] = [];
|
||||
for (const name of layerNames) {
|
||||
const known = byName.get(name.toLowerCase());
|
||||
if (known) {
|
||||
codeByLayer.set(name, known);
|
||||
continue;
|
||||
}
|
||||
const code = allocate();
|
||||
byName.set(name.toLowerCase(), code); // Dubletten mit anderer Schreibweise teilen sich die Kategorie
|
||||
codeByLayer.set(name, code);
|
||||
children.push({
|
||||
code,
|
||||
name,
|
||||
color: layerColors[name] ?? "#707078",
|
||||
lw: 0.18,
|
||||
visible: true,
|
||||
locked: false,
|
||||
});
|
||||
}
|
||||
if (children.length === 0) return { parent: null, codeByLayer };
|
||||
const parent: LayerCategory = {
|
||||
code: allocate(),
|
||||
name: parentName,
|
||||
color: "#707078",
|
||||
lw: 0.18,
|
||||
visible: true,
|
||||
locked: false,
|
||||
children,
|
||||
};
|
||||
return { parent, codeByLayer };
|
||||
}
|
||||
|
||||
/** Liste der unterschiedlichen DXF-Layer-Namen über alle Konturen-Sätze. */
|
||||
export function distinctLayers(sets: ContourSet[]): string[] {
|
||||
const seen = new Set<string>();
|
||||
@@ -134,6 +200,13 @@ export function distinctLayers(sets: ContourSet[]): string[] {
|
||||
return [...seen];
|
||||
}
|
||||
|
||||
/** Wie {@link distinctLayers}, aber über Konturen UND Texte (beide tragen einen Layer). */
|
||||
export function distinctImportLayers(sets: ContourSet[], texts: readonly ImportedText[]): string[] {
|
||||
const seen = new Set<string>(distinctLayers(sets));
|
||||
for (const it of texts) if (it.layer) seen.add(it.layer);
|
||||
return [...seen];
|
||||
}
|
||||
|
||||
/** Gesamtzahl der Konturen über alle Sätze (für die Datei-Zusammenfassung). */
|
||||
export function countContours(sets: ContourSet[]): number {
|
||||
return sets.reduce((sum, s) => sum + s.contours.length, 0);
|
||||
|
||||
@@ -12,6 +12,7 @@ import { t } from "../i18n";
|
||||
import type { DxfImportResult } from "../io/dxfParser";
|
||||
import { parseDxfAsync } from "../io/dxfParserAsync";
|
||||
import { buildDrawings } from "../ui/ImportDialog";
|
||||
import { distinctImportLayers, planImportCategories } from "../io/dxfToDrawings";
|
||||
import type { ImportDecision } from "../ui/ImportDialog";
|
||||
import { exportScheduleCsv } from "../export/exportSchedule";
|
||||
import { exportIfcSpf } from "../export/exportIfc";
|
||||
@@ -31,6 +32,7 @@ import {
|
||||
import type { LockInfo } from "../io/projectFile";
|
||||
import { addRecentProject, removeRecentProject } from "./recentProjects";
|
||||
import { emptyProject, sampleProject } from "../model/sampleProject";
|
||||
import { flattenCategories } from "../model/types";
|
||||
import type { ContextObject, Drawing2D, Project, Vec2 } from "../model/types";
|
||||
import type { PlanViewHandle } from "../plan/PlanView";
|
||||
|
||||
@@ -481,6 +483,9 @@ export function useProjectFileActions(
|
||||
r.readAsText(file);
|
||||
};
|
||||
|
||||
const importParentName = (decision: ImportDecision): string =>
|
||||
decision.target.kind === "new" ? decision.target.name : "DXF";
|
||||
|
||||
// Bestätigte Import-Entscheidung ausführen: ggf. neue Zeichnung anlegen, die
|
||||
// 2D-Konturen als Drawing2D anhängen, optional die Meshes als Kontext
|
||||
// übernehmen. Reihenfolge: erst Ebene (echte ID), dann Drawings/Kontext.
|
||||
@@ -489,11 +494,30 @@ export function useProjectFileActions(
|
||||
decision.target.kind === "new"
|
||||
? addDrawingLevel(decision.target.name)
|
||||
: decision.target.levelId;
|
||||
// „DXF-Layer als Kategorien": fehlende Layer als Unterkategorien einer neuen
|
||||
// Elternkategorie anlegen (vorher fielen sie ALLE auf die aktive Kategorie,
|
||||
// bei Kataster-Importen „Wände"). Die Kategorien müssen VOR den Drawings im
|
||||
// Projekt stehen, damit deren `categoryCode` auf etwas zeigt.
|
||||
let codeByLayer: Map<string, string> | undefined;
|
||||
if (decision.categoryMode === "byLayer") {
|
||||
const plan = planImportCategories(
|
||||
distinctImportLayers(decision.parsed.contours, decision.parsed.texts ?? []),
|
||||
flattenCategories(project.layers),
|
||||
t("import.layers.parentName", { name: importParentName(decision) }),
|
||||
decision.parsed.layerColors,
|
||||
);
|
||||
codeByLayer = plan.codeByLayer;
|
||||
if (plan.parent) {
|
||||
const parent = plan.parent;
|
||||
setProject((p) => ({ ...p, layers: [...p.layers, parent] }));
|
||||
}
|
||||
}
|
||||
let drawings = buildDrawings(
|
||||
decision,
|
||||
levelId,
|
||||
project,
|
||||
activeCategoryCode,
|
||||
codeByLayer,
|
||||
);
|
||||
// Platzierung „viewCenter": den Gesamt-Umriss so verschieben, dass seine
|
||||
// Mitte auf die Mitte des aktuellen Ausschnitts fällt (sonst bleibt alles an
|
||||
|
||||
@@ -433,6 +433,7 @@ export function buildDrawings(
|
||||
levelId: string,
|
||||
project: Project,
|
||||
activeCategoryCode: string,
|
||||
codeByLayer?: ReadonlyMap<string, string>,
|
||||
) {
|
||||
// Layer-Name → Kategorie-Code: bevorzugt eine Kategorie mit demselben Namen
|
||||
// (case-insensitiv), sonst der aktive Code. Identifier bleiben so stabil.
|
||||
@@ -443,8 +444,11 @@ export function buildDrawings(
|
||||
// gefunden).
|
||||
const byName = new Map<string, string>();
|
||||
for (const c of flattenCategories(project.layers)) byName.set(c.name.toLowerCase(), c.code);
|
||||
// `codeByLayer` (aus `planImportCategories`) hat Vorrang: dort sind auch die
|
||||
// beim Import NEU angelegten Kategorien enthalten, die `project` hier (Stand
|
||||
// vor dem Import) noch nicht kennt.
|
||||
const layerToCode = (layerName: string): string =>
|
||||
byName.get(layerName.toLowerCase()) ?? activeCategoryCode;
|
||||
codeByLayer?.get(layerName) ?? byName.get(layerName.toLowerCase()) ?? activeCategoryCode;
|
||||
const fromContours = contoursToDrawings(
|
||||
decision.parsed.contours,
|
||||
levelId,
|
||||
|
||||
Reference in new issue
Block a user