[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
@@ -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 =>
|
||||
|
||||
Reference in new issue
Block a user