[K4] Ebenen-/Geschoss-Logik im Kern: Kategorien-Baum, Import-Kategorien, Auswahl-Codes, recomputeFloorElevations
layers.rs/levels.rs (Rechnung auf serde_json::Value), zustandslose Bindungen, TS-Fassade model/layers.ts; TS-Rechnung in types.ts, projectSlice, selectionLayers, selectionInfo, dxfToDrawings, contextCategories gelöscht. Duplikat einer Unterkategorie geht nicht mehr verloren.
This commit is contained in:
21 files changed
+857
-338
No files matched your search
+1
-1
@@ -206,7 +206,7 @@ import { applyDrawingStyleOps, readDrawingStyle } from "./editors/matchProps";
|
||||
|
||||
// Globaler Store (Slices: project/selection/view/layout).
|
||||
import { useStore } from "./state/appStore";
|
||||
import { categoryLabel, findCategory } from "./state/projectSlice";
|
||||
import { categoryLabel, findCategory } from "./model/layers";
|
||||
import { reconcileLayout } from "./state/layoutSlice";
|
||||
|
||||
// Panel-System.
|
||||
|
||||
@@ -45,6 +45,7 @@ export type QueryName =
|
||||
| "meshExportData"
|
||||
| "groupMembership"
|
||||
| "containingGroup"
|
||||
| "selectionCategoryCodes"
|
||||
| "expandToGroups"
|
||||
| "drawingUnderlay"
|
||||
| "drawingLines"
|
||||
|
||||
+3
-39
@@ -17,6 +17,7 @@
|
||||
|
||||
import type { Contour, ContourSet, Drawing2D, Drawing2DGeom, ImportedText, LayerCategory } from "../model/types";
|
||||
import type { ImportContour } from "./dxfParser";
|
||||
import { planImportLayers } from "../model/layers";
|
||||
|
||||
/** Wie DXF-Layer auf Kategorien abgebildet werden. */
|
||||
export type CategoryMode = "active" | "byLayer";
|
||||
@@ -158,47 +159,10 @@ export function planImportCategories(
|
||||
layerColors: Readonly<Record<string, string>> = {},
|
||||
layerLineWeights: Readonly<Record<string, number>> = {},
|
||||
): { 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: layerLineWeights[name] ?? 0.18,
|
||||
visible: true,
|
||||
locked: false,
|
||||
});
|
||||
}
|
||||
const { children, parentCode, codeByLayer } = planImportLayers(layerNames, existing, layerColors, layerLineWeights);
|
||||
if (children.length === 0) return { parent: null, codeByLayer };
|
||||
const parent: LayerCategory = {
|
||||
code: allocate(),
|
||||
code: parentCode,
|
||||
name: parentName,
|
||||
color: "#707078",
|
||||
lw: 0.18,
|
||||
|
||||
@@ -12,7 +12,7 @@ import { canSetVertexType, vertexTypeOf, type PolylineVertexType } from "../core
|
||||
import { nearestPolylineTarget, toleranceFor } from "../core";
|
||||
import type { LayerCategory, Project, Vec2 } from "../model/types";
|
||||
import { flattenCategories } from "../model/types";
|
||||
import { findCategory } from "../state/projectSlice";
|
||||
import { findCategory } from "../model/layers";
|
||||
import { MenuIcon } from "../ui/ContextMenu";
|
||||
import type { ContextMenuItem } from "../ui/ContextMenu";
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md).
|
||||
|
||||
import type { GeoCategory } from "../io/geoContext";
|
||||
import { flattenCategories } from "./layers";
|
||||
import type { LayerCategory } from "./types";
|
||||
|
||||
/** Eine Kategorie-Definition (Code + Default-Name/Farbe/Strichstärke). */
|
||||
@@ -133,7 +134,7 @@ export function ensureContextCategories(
|
||||
layers: LayerCategory[],
|
||||
codes: Iterable<string>,
|
||||
): LayerCategory[] {
|
||||
const existing = new Set(collectCodes(layers));
|
||||
const existing = new Set(flattenCategories(layers).map((c) => c.code));
|
||||
const missing = new Set<string>();
|
||||
for (const code of codes) {
|
||||
if (!existing.has(code)) missing.add(code);
|
||||
@@ -163,13 +164,3 @@ export function ensureContextCategories(
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Alle Codes eines Kategorie-Baums (rekursiv über `children`). */
|
||||
function collectCodes(layers: LayerCategory[]): string[] {
|
||||
const out: string[] = [];
|
||||
for (const l of layers) {
|
||||
out.push(l.code);
|
||||
if (l.children) out.push(...collectCodes(l.children));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sampleProject } from "./sampleProject";
|
||||
import type { DrawingLevel, LayerCategory } from "./types";
|
||||
import {
|
||||
addChildByCode,
|
||||
applyVisibilityByCode,
|
||||
categoryLabel,
|
||||
collectVisibleCodes,
|
||||
findCategory,
|
||||
flattenCategories,
|
||||
insertAfterCode,
|
||||
nextFreeCode,
|
||||
patchCategoryByCode,
|
||||
planImportLayers,
|
||||
recomputeFloorElevations,
|
||||
removeCategoryByCode,
|
||||
toggleCategoryByCode,
|
||||
} from "./layers";
|
||||
|
||||
const cat = (code: string, name: string, visible = true, children?: LayerCategory[]): LayerCategory => ({
|
||||
code, name, color: "#000", lw: 0.2, visible, locked: false, ...(children ? { children } : {}),
|
||||
});
|
||||
const tree = () => [cat("20", "Wände"), cat("05", "Import", true, [cat("100", "a"), cat("101", "b", false)])];
|
||||
const codes = (t: LayerCategory[]) => flattenCategories(t).map((c) => c.code);
|
||||
|
||||
describe("Kategorie-Baum (Kern)", () => {
|
||||
it("flach, suchen, sichtbare Codes, Beschriftung", () => {
|
||||
const t = tree();
|
||||
expect(codes(t)).toEqual(["20", "05", "100", "101"]);
|
||||
expect(findCategory(t, "101")?.name).toBe("b");
|
||||
expect(findCategory(t, "x")).toBeUndefined();
|
||||
expect([...collectVisibleCodes(t)]).toEqual(["20", "05", "100"]);
|
||||
expect(categoryLabel(t, "20")).toBe("20 Wände");
|
||||
expect(categoryLabel(t, "zz")).toBe("zz");
|
||||
});
|
||||
|
||||
it("Umschalten, Sichtbarkeit nach Codes, Ändern, Unterkategorie, Entfernen", () => {
|
||||
const t = tree();
|
||||
expect(findCategory(toggleCategoryByCode(t, "101"), "101")?.visible).toBe(true);
|
||||
expect(findCategory(applyVisibilityByCode(t, { "20": false, nix: true }), "20")?.visible).toBe(false);
|
||||
expect(findCategory(patchCategoryByCode(t, "100", { name: "neu", hatch: undefined }), "100")?.name).toBe("neu");
|
||||
expect(findCategory(addChildByCode(t, "20", cat("21", "k")), "20")?.children?.[0].code).toBe("21");
|
||||
expect(codes(removeCategoryByCode(t, "05"))).toEqual(["20"]);
|
||||
expect(t[1].children?.length).toBe(2); // Eingabe unverändert
|
||||
});
|
||||
|
||||
it("Duplikat direkt nach dem Original, auch bei Unterkategorien", () => {
|
||||
expect(codes(insertAfterCode(tree(), "20", cat("22", "c")))).toEqual(["20", "22", "05", "100", "101"]);
|
||||
expect(codes(insertAfterCode(tree(), "100", cat("102", "c")))).toEqual(["20", "05", "100", "102", "101"]);
|
||||
});
|
||||
|
||||
it("nächster freier Code", () => {
|
||||
expect(nextFreeCode([])).toBe("01");
|
||||
expect(nextFreeCode(tree())).toBe("102");
|
||||
});
|
||||
|
||||
it("Import-Layer: bestehende Namen (case-insensitiv) behalten ihren Code", () => {
|
||||
const r = planImportLayers(["wände", "Neu", "NEU"], tree(), { Neu: "#123" }, {});
|
||||
expect(r.children).toHaveLength(1);
|
||||
expect(r.children[0]).toMatchObject({ code: "102", name: "Neu", color: "#123", lw: 0.18 });
|
||||
expect(r.parentCode).toBe("103");
|
||||
expect([...r.codeByLayer]).toEqual([["wände", "20"], ["Neu", "102"], ["NEU", "102"]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Geschoss-Stapelung (Kern)", () => {
|
||||
it("stapelt floorHeight, Nicht-Geschosse verlieren baseElevation", () => {
|
||||
const l = [
|
||||
{ id: "a", name: "A", kind: "floor", visible: true, locked: false, floorHeight: 2.6, baseElevation: 9 },
|
||||
{ id: "s", name: "S", kind: "section", visible: true, locked: false, baseElevation: 1 },
|
||||
{ id: "b", name: "B", kind: "floor", visible: true, locked: false, floorHeight: 3 },
|
||||
] as DrawingLevel[];
|
||||
const r = recomputeFloorElevations(l);
|
||||
expect(r.map((x) => x.baseElevation)).toEqual([0, undefined, 2.6]);
|
||||
expect(l[0].baseElevation).toBe(9);
|
||||
});
|
||||
|
||||
it("Musterprojekt trägt die gestapelten Höhen ausgeschrieben (Modul wird vor dem Kern geladen)", () => {
|
||||
expect(recomputeFloorElevations(sampleProject.drawingLevels)).toEqual(sampleProject.drawingLevels);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,144 @@
|
||||
// Ebenen-/Kategorien-Baum und Geschoss-Höhen im Rust-Kern ([K4] S3, `src-tauri/core/src/layers.rs`,
|
||||
// `levels.rs`): Abflachen, Nachschlagen, Sichtbarkeit, Isolieren, Baum-Umbauten, freie Codes,
|
||||
// Import-Kategorien, Geschoss-Stapelung. Dünne Fassade über zustandslose Kern-Bindungen (JSON hinein,
|
||||
// JSON hinaus; die Bäume sind klein). Die Op-Bauer (`state/projectSlice.ts`, `state/selectionLayers.ts`)
|
||||
// bleiben in TS und rechnen über diese Funktionen. Ergebnis-Objekte sind Kopien aus dem Kern, nicht die
|
||||
// Eingabe-Objekte; „nichts zu tun" meldet der Kern, die Fassade gibt dann die Eingabe zurück.
|
||||
|
||||
import type { DrawingLevel, LayerCategory, Project } from "./types.generated";
|
||||
import { core } from "../engine/coreWasm";
|
||||
import { documentFor } from "../core/document";
|
||||
|
||||
function call<T>(name: string, layers: readonly LayerCategory[], arg?: unknown): T {
|
||||
const out = core().layers_call(name, JSON.stringify(layers), arg === undefined ? "" : JSON.stringify(arg), Date.now());
|
||||
if (out === "") throw new Error(`Kern: Ebenen-Rechnung „${name}" fehlgeschlagen`);
|
||||
return JSON.parse(out) as T;
|
||||
}
|
||||
|
||||
interface FlatIndex {
|
||||
flat: LayerCategory[];
|
||||
byCode: Map<string, LayerCategory>;
|
||||
}
|
||||
/** Abgeflachter Baum je Baum-Array (die Arrays gelten als unveränderlich): Aufrufe je Bauteil bleiben billig. */
|
||||
const flatCache = new WeakMap<readonly LayerCategory[], FlatIndex>();
|
||||
|
||||
function indexOf(cats: readonly LayerCategory[]): FlatIndex {
|
||||
let ix = flatCache.get(cats);
|
||||
if (!ix) {
|
||||
const flat = call<LayerCategory[]>("flatten", cats);
|
||||
const byCode = new Map<string, LayerCategory>();
|
||||
for (const c of flat) if (!byCode.has(c.code)) byCode.set(c.code, c);
|
||||
ix = { flat, byCode };
|
||||
flatCache.set(cats, ix);
|
||||
}
|
||||
return ix;
|
||||
}
|
||||
|
||||
/** Flacht den Kategorie-Baum (Tiefensuche) in eine Liste ab. */
|
||||
export const flattenCategories = (cats: LayerCategory[]): LayerCategory[] => [...indexOf(cats).flat];
|
||||
|
||||
/** Findet eine Kategorie (per Code) im Baum oder `undefined` (erster Treffer). */
|
||||
export function findCategory(cats: LayerCategory[], code: string): LayerCategory | undefined {
|
||||
return indexOf(cats).byCode.get(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Liefert die LayerCategory zu einem Kategorie-Code (Baum durchsucht), oder `undefined`, falls der Code auf
|
||||
* keine Ebene verweist (verwaister `categoryCode`). Nicht-werfend, damit die Attribut-Resolve-Kette
|
||||
* (`plan/generatePlan.ts`) robust auf einen fehlenden Ebenen-Wert zurückfallen kann.
|
||||
*/
|
||||
export const getLayerCategory = (project: Project, code: string): LayerCategory | undefined => findCategory(project.layers, code);
|
||||
|
||||
/** Menge aller Codes sichtbarer Kategorien (Baum berücksichtigt). */
|
||||
export const collectVisibleCodes = (layers: LayerCategory[]): Set<string> => new Set(call<string[]>("visibleCodes", layers));
|
||||
|
||||
/** Beschriftung einer Kategorie für die Status-Leiste: „Code Name". */
|
||||
export function categoryLabel(cats: LayerCategory[], code: string): string {
|
||||
const c = findCategory(cats, code);
|
||||
return c ? `${c.code} ${c.name}` : code;
|
||||
}
|
||||
|
||||
/** Anzeigenamen der Kategorien zu den Codes (für Menü-Beschriftungen). */
|
||||
export const categoryNames = (layers: LayerCategory[], codes: readonly string[]): string[] => call<string[]>("names", layers, codes);
|
||||
|
||||
/** Setzt `visible` genau für die gegebenen Codes; nichts zu tun → dieselbe Liste. */
|
||||
export function setCategoriesVisible(layers: LayerCategory[], codes: ReadonlySet<string>, visible: boolean): LayerCategory[] {
|
||||
return call<LayerCategory[] | null>("setVisible", layers, { codes: [...codes], visible }) ?? layers;
|
||||
}
|
||||
|
||||
/** Nur die gegebenen Codes sichtbar, alle übrigen ausgeblendet; nichts zu tun → dieselbe Liste. */
|
||||
export function isolateCategories(layers: LayerCategory[], codes: ReadonlySet<string>): LayerCategory[] {
|
||||
return call<LayerCategory[] | null>("isolate", layers, [...codes]) ?? layers;
|
||||
}
|
||||
|
||||
/** Schaltet die Sichtbarkeit der Kategorie mit `code` im Baum um. */
|
||||
export const toggleCategoryByCode = (cats: LayerCategory[], code: string): LayerCategory[] => call("toggle", cats, code);
|
||||
|
||||
/** Setzt im Baum die `visible`-Flags gemäß `codes` (Code → sichtbar?); unbekannte Codes werden ignoriert. */
|
||||
export const applyVisibilityByCode = (cats: LayerCategory[], codes: Record<string, boolean>): LayerCategory[] =>
|
||||
call("applyVisibility", cats, codes);
|
||||
|
||||
/** Nächster freier zweistelliger Code über den ganzen Baum hinweg. */
|
||||
export const nextFreeCode = (cats: LayerCategory[]): string => call("nextFreeCode", cats);
|
||||
|
||||
/** Patcht die Kategorie mit `code` im Baum (`undefined`-Werte entfernen das Feld). */
|
||||
export const patchCategoryByCode = (cats: LayerCategory[], code: string, patch: Partial<LayerCategory>): LayerCategory[] =>
|
||||
call(
|
||||
"patch",
|
||||
cats,
|
||||
{ code, patch: Object.fromEntries(Object.entries(patch).map(([k, v]) => [k, v === undefined ? null : v])) },
|
||||
);
|
||||
|
||||
/** Hängt `child` als letztes Kind an die Kategorie mit `parentCode`. */
|
||||
export const addChildByCode = (cats: LayerCategory[], parentCode: string, child: LayerCategory): LayerCategory[] =>
|
||||
call("addChild", cats, { parent: parentCode, child });
|
||||
|
||||
/** Fügt `clone` direkt nach der Kategorie mit `code` auf derselben Ebene ein. */
|
||||
export const insertAfterCode = (cats: LayerCategory[], code: string, clone: LayerCategory): LayerCategory[] =>
|
||||
call("insertAfter", cats, { code, clone });
|
||||
|
||||
/** Entfernt die Kategorie mit `code` (samt Kindern) aus dem Baum. */
|
||||
export const removeCategoryByCode = (cats: LayerCategory[], code: string): LayerCategory[] => call("remove", cats, code);
|
||||
|
||||
/** Neue Kategorien für Import-Layer ohne gleichnamige Kategorie (s. `planImportCategories`, `io/dxfToDrawings.ts`). */
|
||||
export function planImportLayers(
|
||||
layerNames: readonly string[],
|
||||
existing: readonly LayerCategory[],
|
||||
colors: Readonly<Record<string, string>>,
|
||||
weights: Readonly<Record<string, number>>,
|
||||
): { children: LayerCategory[]; parentCode: string; codeByLayer: Map<string, string> } {
|
||||
const r = call<{ children: LayerCategory[]; parentCode: string; codeByLayer: [string, string][] }>("planImport", existing as LayerCategory[], {
|
||||
names: layerNames,
|
||||
colors,
|
||||
weights,
|
||||
});
|
||||
return { children: r.children, parentCode: r.parentCode, codeByLayer: new Map(r.codeByLayer) };
|
||||
}
|
||||
|
||||
/** Auswahl nach Elementart (ids je Art, wie `selectionLockMembers` in App.tsx). */
|
||||
export interface SelectionMembers {
|
||||
wallIds?: readonly string[];
|
||||
drawingIds?: readonly string[];
|
||||
ceilingIds?: readonly string[];
|
||||
roomIds?: readonly string[];
|
||||
stairIds?: readonly string[];
|
||||
columnIds?: readonly string[];
|
||||
roofIds?: readonly string[];
|
||||
openingIds?: readonly string[];
|
||||
}
|
||||
|
||||
/** Kategorie-Codes aller gewählten Objekte (eindeutig, in Fundreihenfolge). */
|
||||
export function selectionCategoryCodes(project: Project, sel: SelectionMembers): string[] {
|
||||
return documentFor(project).query<string[]>("selectionCategoryCodes", [], JSON.stringify(sel));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stapelt baseElevation der Geschosse in Dokumentreihenfolge: Das erste Geschoss beginnt bei 0, jedes weitere
|
||||
* bei baseElevation + floorHeight des vorigen Geschosses. Nicht-Geschoss-Ebenen behalten baseElevation
|
||||
* undefined. Liefert eine neue Liste; die Eingabe bleibt unverändert.
|
||||
*/
|
||||
export function recomputeFloorElevations(levels: DrawingLevel[]): DrawingLevel[] {
|
||||
const out = core().levels_recompute_elevations(JSON.stringify(levels));
|
||||
if (out === "") throw new Error("Kern: Geschoss-Höhen fehlgeschlagen");
|
||||
return JSON.parse(out) as DrawingLevel[];
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { Project } from "./types";
|
||||
import { recomputeFloorElevations } from "./types";
|
||||
import { MATERIAL_LIBRARY } from "../materials/library";
|
||||
import { materialFromAsset } from "../materials/runtime";
|
||||
|
||||
@@ -341,10 +340,11 @@ export const sampleProject: Project = {
|
||||
},
|
||||
],
|
||||
// Oberste Schnitte: Geschosse + Schnitt/Ansicht + freie Zeichnung.
|
||||
// baseElevation wird über recomputeFloorElevations gestapelt: EG=0, OG=2.6.
|
||||
drawingLevels: recomputeFloorElevations([
|
||||
{ id: "eg", name: "Erdgeschoss", kind: "floor", visible: true, locked: false, floorHeight: 2.6, cutHeight: 1.0 },
|
||||
{ id: "og", name: "Obergeschoss", kind: "floor", visible: true, locked: false, floorHeight: 2.6, cutHeight: 1.0 },
|
||||
// baseElevation = gestapelt wie `recomputeFloorElevations` (Kern): EG=0, OG=2.6. Ausgeschrieben, weil dieses
|
||||
// Modul vor dem Laden des Kerns ausgewertet wird (Test: sampleProject.test.ts).
|
||||
drawingLevels: [
|
||||
{ id: "eg", name: "Erdgeschoss", kind: "floor", visible: true, locked: false, floorHeight: 2.6, cutHeight: 1.0, baseElevation: 0 },
|
||||
{ id: "og", name: "Obergeschoss", kind: "floor", visible: true, locked: false, floorHeight: 2.6, cutHeight: 1.0, baseElevation: 2.6 },
|
||||
{
|
||||
id: "schnitt-a",
|
||||
name: "Schnitt A",
|
||||
@@ -367,7 +367,7 @@ export const sampleProject: Project = {
|
||||
shadows: true,
|
||||
},
|
||||
{ id: "detail-1", name: "Detail 1", kind: "drawing", visible: true, locked: false },
|
||||
]),
|
||||
],
|
||||
// Grafik-Kategorie-Baum (DOSSIER-Codes 1:1).
|
||||
layers: [
|
||||
{ code: "00", name: "Raster", color: "#484850", lw: 0.13, visible: true, locked: false },
|
||||
|
||||
+3
-53
@@ -24,7 +24,6 @@ import type {
|
||||
DrawingLevel,
|
||||
HatchStyle,
|
||||
Layer,
|
||||
LayerCategory,
|
||||
LineStyle,
|
||||
Opening,
|
||||
Project,
|
||||
@@ -255,58 +254,9 @@ export const getFloor = (project: Project, id: string): DrawingLevel => {
|
||||
return g;
|
||||
};
|
||||
|
||||
/**
|
||||
* Stapelt baseElevation der Geschosse in Dokumentreihenfolge: Das erste
|
||||
* Geschoss beginnt bei 0, jedes weitere bei baseElevation + floorHeight des
|
||||
* vorigen Geschosses. Nicht-Geschoss-Ebenen behalten baseElevation undefined.
|
||||
* Liefert eine neue Liste (mit neuen Geschoss-Objekten); die Eingabe bleibt
|
||||
* unverändert.
|
||||
*/
|
||||
export const recomputeFloorElevations = (
|
||||
levels: DrawingLevel[],
|
||||
): DrawingLevel[] => {
|
||||
let nextBase = 0;
|
||||
return levels.map((level) => {
|
||||
if (level.kind !== "floor") {
|
||||
return { ...level, baseElevation: undefined };
|
||||
}
|
||||
const baseElevation = nextBase;
|
||||
nextBase = baseElevation + (level.floorHeight ?? 0);
|
||||
return { ...level, baseElevation };
|
||||
});
|
||||
};
|
||||
|
||||
/** Flacht den Kategorie-Baum (Tiefensuche) in eine Liste ab. */
|
||||
export const flattenCategories = (cats: LayerCategory[]): LayerCategory[] => {
|
||||
const out: LayerCategory[] = [];
|
||||
const walk = (list: LayerCategory[]) => {
|
||||
for (const c of list) {
|
||||
out.push(c);
|
||||
if (c.children) walk(c.children);
|
||||
}
|
||||
};
|
||||
walk(cats);
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* Liefert die LayerCategory zu einem Kategorie-Code (Baum durchsucht), oder
|
||||
* `undefined`, falls der Code auf keine Ebene verweist (verwaister
|
||||
* `categoryCode`). Nicht-werfend, damit die Attribut-Resolve-Kette
|
||||
* (`plan/generatePlan.ts`) robust auf einen fehlenden Ebenen-Wert zurückfallen
|
||||
* kann (→ Bauteil-Fallback).
|
||||
*/
|
||||
export const getLayerCategory = (project: Project, code: string): LayerCategory | undefined =>
|
||||
flattenCategories(project.layers).find((c) => c.code === code);
|
||||
|
||||
/** Menge aller Codes sichtbarer Kategorien (Baum berücksichtigt). */
|
||||
export const collectVisibleCodes = (layers: LayerCategory[]): Set<string> => {
|
||||
const codes = new Set<string>();
|
||||
for (const c of flattenCategories(layers)) {
|
||||
if (c.visible) codes.add(c.code);
|
||||
}
|
||||
return codes;
|
||||
};
|
||||
// Ebenen-Baum und Geschoss-Stapelung liegen im Rust-Kern ([K4] S3, `model/layers.ts`); Re-Export, damit
|
||||
// die Aufrufer gleich bleiben.
|
||||
export { collectVisibleCodes, flattenCategories, getLayerCategory, recomputeFloorElevations } from "./layers";
|
||||
|
||||
/** Gesamtdicke eines Wandtyps = Summe der Schichtdicken. */
|
||||
export const wallTypeThickness = (wt: WallType): number =>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { isTauriRuntime } from "./projectFile";
|
||||
import { findCategory } from "../state/projectSlice";
|
||||
import { findCategory } from "../model/layers";
|
||||
import type { HatchStyle, LayerCategory, Project } from "../model/types";
|
||||
import { BORDERLESS_WINDOW_CHROME } from "./windowChrome";
|
||||
|
||||
|
||||
+10
-131
@@ -25,9 +25,17 @@ import {
|
||||
import { withVertexType, type PolylineVertexType } from "../core";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import {
|
||||
addChildByCode,
|
||||
applyVisibilityByCode,
|
||||
findCategory,
|
||||
flattenCategories,
|
||||
insertAfterCode,
|
||||
nextFreeCode,
|
||||
patchCategoryByCode,
|
||||
recomputeFloorElevations,
|
||||
} from "../model/types";
|
||||
removeCategoryByCode,
|
||||
toggleCategoryByCode,
|
||||
} from "../model/layers";
|
||||
import type {
|
||||
BuiltinPaletteBank,
|
||||
ColorPen,
|
||||
@@ -741,7 +749,7 @@ export function createProjectSlice(
|
||||
get().notify(t("alert.layerInUse", { code: src.code, name: src.name }), "warning");
|
||||
return [];
|
||||
}
|
||||
return fieldOps(p, "layers", removeByCode(p.layers, code));
|
||||
return fieldOps(p, "layers", removeCategoryByCode(p.layers, code));
|
||||
}),
|
||||
|
||||
assignWallCategory: (wallId, code) =>
|
||||
@@ -2310,132 +2318,3 @@ function moveEdge(
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
// ── Immutable Tree-Helfer (verschoben aus App.tsx, 1:1) ──────────────────────
|
||||
|
||||
/** Schaltet die Sichtbarkeit der Kategorie mit `code` im Baum um (immutabel). */
|
||||
export function toggleCategoryByCode(
|
||||
cats: LayerCategory[],
|
||||
code: string,
|
||||
): LayerCategory[] {
|
||||
return cats.map((c) => {
|
||||
if (c.code === code) return { ...c, visible: !c.visible };
|
||||
if (c.children)
|
||||
return { ...c, children: toggleCategoryByCode(c.children, code) };
|
||||
return c;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt im Baum die `visible`-Flags gemäß `codes` (Code → sichtbar?, immutabel).
|
||||
* Kategorien, deren Code nicht in der Map steht, bleiben unverändert; Codes der
|
||||
* Map, die es nicht mehr gibt, werden stillschweigend ignoriert.
|
||||
*/
|
||||
export function applyVisibilityByCode(
|
||||
cats: LayerCategory[],
|
||||
codes: Record<string, boolean>,
|
||||
): LayerCategory[] {
|
||||
return cats.map((c) => {
|
||||
const next = c.code in codes ? { ...c, visible: codes[c.code] } : c;
|
||||
if (next.children)
|
||||
return { ...next, children: applyVisibilityByCode(next.children, codes) };
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
/** Nächster freier zweistelliger Code über den ganzen Baum hinweg. */
|
||||
export function nextFreeCode(cats: LayerCategory[]): string {
|
||||
const used = new Set<string>();
|
||||
const walk = (list: LayerCategory[]) => {
|
||||
for (const c of list) {
|
||||
used.add(c.code);
|
||||
if (c.children) walk(c.children);
|
||||
}
|
||||
};
|
||||
walk(cats);
|
||||
const nums = [...used].map((c) => parseInt(c, 10)).filter((n) => !isNaN(n));
|
||||
const start = nums.length ? Math.max(...nums) : 0;
|
||||
for (let i = start + 1; i < 1000; i++) {
|
||||
const c = String(i).padStart(2, "0");
|
||||
if (!used.has(c)) return c;
|
||||
}
|
||||
return String(Date.now());
|
||||
}
|
||||
|
||||
/** Findet eine Kategorie (per Code) im Baum oder `undefined`. */
|
||||
export function findCategory(
|
||||
cats: LayerCategory[],
|
||||
code: string,
|
||||
): LayerCategory | undefined {
|
||||
for (const c of cats) {
|
||||
if (c.code === code) return c;
|
||||
if (c.children) {
|
||||
const hit = findCategory(c.children, code);
|
||||
if (hit) return hit;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Patcht die Kategorie mit `code` im Baum (immutabel). */
|
||||
function patchCategoryByCode(
|
||||
cats: LayerCategory[],
|
||||
code: string,
|
||||
patch: Partial<LayerCategory>,
|
||||
): LayerCategory[] {
|
||||
return cats.map((c) => {
|
||||
if (c.code === code) return { ...c, ...patch };
|
||||
if (c.children)
|
||||
return { ...c, children: patchCategoryByCode(c.children, code, patch) };
|
||||
return c;
|
||||
});
|
||||
}
|
||||
|
||||
/** Hängt `child` als letztes Kind an die Kategorie mit `parentCode` (immutabel). */
|
||||
function addChildByCode(
|
||||
cats: LayerCategory[],
|
||||
parentCode: string,
|
||||
child: LayerCategory,
|
||||
): LayerCategory[] {
|
||||
return cats.map((c) => {
|
||||
if (c.code === parentCode)
|
||||
return { ...c, children: [...(c.children ?? []), child] };
|
||||
if (c.children)
|
||||
return { ...c, children: addChildByCode(c.children, parentCode, child) };
|
||||
return c;
|
||||
});
|
||||
}
|
||||
|
||||
/** Fügt `clone` direkt nach der Kategorie mit `code` auf derselben Ebene ein. */
|
||||
function insertAfterCode(
|
||||
cats: LayerCategory[],
|
||||
code: string,
|
||||
clone: LayerCategory,
|
||||
): LayerCategory[] {
|
||||
const out: LayerCategory[] = [];
|
||||
for (const c of cats) {
|
||||
const next =
|
||||
c.children && !c.children.some((k) => k.code === code)
|
||||
? { ...c, children: insertAfterCode(c.children, code, clone) }
|
||||
: c;
|
||||
out.push(next);
|
||||
if (c.code === code) out.push(clone);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Entfernt die Kategorie mit `code` (samt Kindern) aus dem Baum (immutabel). */
|
||||
function removeByCode(cats: LayerCategory[], code: string): LayerCategory[] {
|
||||
const out: LayerCategory[] = [];
|
||||
for (const c of cats) {
|
||||
if (c.code === code) continue;
|
||||
out.push(c.children ? { ...c, children: removeByCode(c.children, code) } : c);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Beschriftung einer Kategorie für die Status-Leiste: „Code Name". */
|
||||
export function categoryLabel(cats: LayerCategory[], code: string): string {
|
||||
const c = findCategory(cats, code);
|
||||
return c ? `${c.code} ${c.name}` : code;
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import { extrudedSolidSpec } from "../core";
|
||||
import type { SolidSpec } from "../engine/truckSolid";
|
||||
import {
|
||||
ceilingThickness,
|
||||
flattenCategories,
|
||||
getCeilingType,
|
||||
getComponent,
|
||||
getWallType,
|
||||
@@ -31,7 +30,6 @@ import type {
|
||||
Drawing2D,
|
||||
Drawing2DGeom,
|
||||
ExtrudedSolid,
|
||||
LayerCategory,
|
||||
Opening,
|
||||
Project,
|
||||
Roof,
|
||||
@@ -45,6 +43,7 @@ import type {
|
||||
Wall,
|
||||
WallReferenceLine,
|
||||
} from "../model/types";
|
||||
import { findCategory } from "../model/layers";
|
||||
import type { DetailLevel } from "../model/viewTypes";
|
||||
import { localToLv95 } from "../io/lv95";
|
||||
import { resolveForeground, resolveBackground, resolveHatchId, resolveStrokeWeight } from "../core";
|
||||
@@ -546,14 +545,6 @@ const WALL_FALLBACK_MM = 0.18;
|
||||
const DRAW_FALLBACK_COLOR = "#d0d0d0";
|
||||
const WALL_FALLBACK_COLOR = "#2b3039";
|
||||
|
||||
/** Kategorie (per Code) im Baum suchen — flach, da Codes baumweit eindeutig sind. */
|
||||
function findCategory(
|
||||
layers: LayerCategory[],
|
||||
code: string,
|
||||
): LayerCategory | undefined {
|
||||
return flattenCategories(layers).find((c) => c.code === code);
|
||||
}
|
||||
|
||||
/** Ob eine 2D-Form geschlossen ist (Füllung/Schraffur sinnvoll). */
|
||||
function isClosedGeom(g: Drawing2DGeom): boolean {
|
||||
return (
|
||||
|
||||
@@ -21,11 +21,11 @@ describe("Ebenen der Auswahl", () => {
|
||||
expect(selectionCategoryCodes(project, { drawingIds: ["nix"] })).toEqual([]);
|
||||
});
|
||||
|
||||
it("blendet genau die Codes aus (auch verschachtelt), unveränderte Teile bleiben dieselben Objekte", () => {
|
||||
it("blendet genau die Codes aus (auch verschachtelt), nichts zu tun liefert dieselbe Liste", () => {
|
||||
const next = setCategoriesVisible(layers, new Set(["100"]), false);
|
||||
expect(next[1].children![0].visible).toBe(false);
|
||||
expect(next[1].children![1].visible).toBe(true);
|
||||
expect(next[0]).toBe(layers[0]); // Wände unverändert (gleiches Objekt)
|
||||
expect(next[0]).toEqual(layers[0]); // Wände unverändert (Kopie aus dem Kern)
|
||||
expect(setCategoriesVisible(layers, new Set(["100"]), true)).toBe(layers); // nichts zu tun → gleiche Referenz
|
||||
});
|
||||
|
||||
|
||||
@@ -1,80 +1,11 @@
|
||||
// Ebenen (Kategorien) der aktuellen Auswahl: ausblenden bzw. isolieren
|
||||
// ("nur diese Ebene(n) zeigen"). Reine Funktionen — der Aufrufer (App) legt sie
|
||||
// über `setProject`, also EIN Undo-Schritt.
|
||||
// Ebenen (Kategorien) der aktuellen Auswahl: ausblenden bzw. isolieren ("nur diese Ebene(n) zeigen").
|
||||
// Die Rechnung liegt im Rust-Kern ([K4] S3, `core/src/layers.rs`, Fassade `model/layers.ts`); dieses Modul
|
||||
// bleibt als Einstieg für die UI. Der Aufrufer (App) legt die Ergebnisse über `applyChange`, also EIN Undo-Schritt.
|
||||
|
||||
import type { LayerCategory, Project } from "../model/types";
|
||||
import { flattenCategories } from "../model/types";
|
||||
|
||||
/** Ausgewählte Objekt-IDs je Art (wie `selectionLockMembers` in App.tsx). */
|
||||
export interface SelectionMembers {
|
||||
wallIds?: readonly string[];
|
||||
drawingIds?: readonly string[];
|
||||
ceilingIds?: readonly string[];
|
||||
roomIds?: readonly string[];
|
||||
stairIds?: readonly string[];
|
||||
columnIds?: readonly string[];
|
||||
roofIds?: readonly string[];
|
||||
openingIds?: readonly string[];
|
||||
}
|
||||
|
||||
type WithCat = { id: string; categoryCode?: string };
|
||||
|
||||
/** Kategorie-Codes aller gewählten Objekte (eindeutig, in Fundreihenfolge). */
|
||||
export function selectionCategoryCodes(project: Project, sel: SelectionMembers): string[] {
|
||||
const p = project as unknown as Record<string, readonly WithCat[] | undefined>;
|
||||
const sources: Array<[readonly string[] | undefined, readonly WithCat[]]> = [
|
||||
[sel.wallIds, p.walls ?? []],
|
||||
[sel.drawingIds, p.drawings2d ?? []],
|
||||
[sel.ceilingIds, p.ceilings ?? []],
|
||||
[sel.roomIds, p.rooms ?? []],
|
||||
[sel.stairIds, p.stairs ?? []],
|
||||
[sel.columnIds, p.columns ?? []],
|
||||
[sel.roofIds, p.roofs ?? []],
|
||||
[sel.openingIds, p.openings ?? []],
|
||||
];
|
||||
const out = new Set<string>();
|
||||
for (const [ids, objects] of sources) {
|
||||
if (!ids || ids.length === 0) continue;
|
||||
const wanted = new Set(ids);
|
||||
for (const o of objects) if (o.categoryCode && wanted.has(o.id)) out.add(o.categoryCode);
|
||||
}
|
||||
return [...out];
|
||||
}
|
||||
|
||||
function mapCategories(
|
||||
layers: LayerCategory[],
|
||||
fn: (c: LayerCategory) => LayerCategory,
|
||||
): LayerCategory[] {
|
||||
let changed = false;
|
||||
const next = layers.map((c) => {
|
||||
const children = c.children ? mapCategories(c.children, fn) : undefined;
|
||||
const base = children !== c.children ? { ...c, children } : c;
|
||||
const out = fn(base);
|
||||
if (out !== c) changed = true;
|
||||
return out;
|
||||
});
|
||||
return changed ? next : layers;
|
||||
}
|
||||
|
||||
/** Setzt `visible` genau für die gegebenen Codes; unveränderte Teilbäume bleiben dieselben Objekte. */
|
||||
export function setCategoriesVisible(
|
||||
layers: LayerCategory[],
|
||||
codes: ReadonlySet<string>,
|
||||
visible: boolean,
|
||||
): LayerCategory[] {
|
||||
return mapCategories(layers, (c) => (codes.has(c.code) && c.visible !== visible ? { ...c, visible } : c));
|
||||
}
|
||||
|
||||
/** Nur die gegebenen Codes sichtbar, alle übrigen ausgeblendet. */
|
||||
export function isolateCategories(layers: LayerCategory[], codes: ReadonlySet<string>): LayerCategory[] {
|
||||
return mapCategories(layers, (c) => {
|
||||
const want = codes.has(c.code);
|
||||
return c.visible !== want ? { ...c, visible: want } : c;
|
||||
});
|
||||
}
|
||||
|
||||
/** Anzeigenamen der Kategorien zu den Codes (für Menü-Beschriftungen). */
|
||||
export function categoryNames(layers: LayerCategory[], codes: readonly string[]): string[] {
|
||||
const all = flattenCategories(layers);
|
||||
return codes.map((code) => all.find((c) => c.code === code)?.name ?? code);
|
||||
}
|
||||
export {
|
||||
categoryNames,
|
||||
isolateCategories,
|
||||
selectionCategoryCodes,
|
||||
setCategoriesVisible,
|
||||
type SelectionMembers,
|
||||
} from "../model/layers";
|
||||
Reference in new issue
Block a user