diff --git a/src/commands/cmds/solid.test.ts b/src/commands/cmds/solid.test.ts new file mode 100644 index 00000000..7883882a --- /dev/null +++ b/src/commands/cmds/solid.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from "vitest"; +import { sampleProject } from "../../model/sampleProject"; +import type { CommandContext } from "../types"; +import { solidCommand, solidFromInputs } from "./solid"; + +const ctx = { project: sampleProject, level: { id: "eg" } } as unknown as CommandContext; + +function run(shape: string, steps: Array<{ kind: "point"; point: { x: number; y: number } } | { kind: "number"; value: number }>) { + let state = solidCommand.init(ctx); + [state] = solidCommand.onInput(state, { kind: "option", id: shape }, ctx); + let commit: ((p: typeof sampleProject) => typeof sampleProject) | undefined; + for (const st of steps) { + const [ns, res] = solidCommand.onInput(state, st, ctx); + state = ns; + if (res.commit) commit = res.commit as typeof commit; + } + return commit ? commit({ ...sampleProject, extrudedSolids: [] }) : null; +} + +describe("solid-Befehl", () => { + it("Zylinder: Mittelpunkt, Radius, Höhe → ExtrudedSolid mit B-Rep-Spezifikation", () => { + const p = run("cylinder", [ + { kind: "point", point: { x: 2, y: 3 } }, + { kind: "number", value: 0.5 }, + { kind: "number", value: 2.4 }, + ])!; + const s = p.extrudedSolids![0]; + expect(s.levelId).toBe("eg"); + expect(s.height).toBeCloseTo(2.4, 9); + expect(s.brep!.spec).toMatchObject({ kind: "cylinder", cx: 2, cy: 3, r: 0.5, height: 2.4, z0: 0 }); + expect(s.points).toHaveLength(48); + }); + + it("Radius per Klick: Abstand zum Mittelpunkt", () => { + const p = run("sphere", [ + { kind: "point", point: { x: 0, y: 0 } }, + { kind: "point", point: { x: 3, y: 4 } }, + ])!; + expect(p.extrudedSolids![0].brep!.spec).toMatchObject({ kind: "sphere", r: 5, center: [0, 0, 5] }); + expect(p.extrudedSolids![0].height).toBeCloseTo(10, 9); + }); + + it("Quader: zwei Ecken + Höhe; Torus verlangt Hauptradius > Rohrradius", () => { + const q = run("cuboid", [ + { kind: "point", point: { x: 0, y: 0 } }, + { kind: "point", point: { x: 4, y: 2 } }, + { kind: "number", value: 3 }, + ])!; + expect(q.extrudedSolids![0].brep!.spec).toMatchObject({ kind: "cuboid", min: [0, 0, 0], max: [4, 2, 3] }); + expect(solidFromInputs("torus", { x: 0, y: 0 }, null, [0.3, 0.5])).toBeNull(); + expect(solidFromInputs("torus", { x: 0, y: 0 }, null, [2, 0.5])?.height).toBeCloseTo(1, 9); + }); +}); diff --git a/src/commands/cmds/solid.ts b/src/commands/cmds/solid.ts new file mode 100644 index 00000000..6454ca99 --- /dev/null +++ b/src/commands/cmds/solid.ts @@ -0,0 +1,258 @@ +// Solid — B-Rep-Grundkörper (truck): Kugel, Zylinder, Kegel, Torus, Quader. Der Körper wird als +// `ExtrudedSolid` mit `brep`-Spezifikation abgelegt (Grundriss-Fläche für Auswahl/2D-Umriss, +// Verschieben/Kopieren/Löschen laufen über die vorhandene Extrusions-Pipeline); gerendert +// und nach STEP exportiert wird die EXAKTE B-Rep-Geometrie (`SolidSpec`, trucksolid). +// +// Schritte je Form (Zahl tippen ODER in die Zeichnung klicken — ein Klick liefert das Mass +// als Abstand zum Mittelpunkt): +// Kugel Mittelpunkt → Radius +// Zylinder Mittelpunkt → Radius → Höhe +// Kegel Mittelpunkt → Radius → Höhe +// Torus Mittelpunkt → Hauptradius → Rohrradius +// Quader Ecke → Gegenecke → Höhe +// Der Körper steht auf der Geschoss-UK (Z = 0 lokal). + +import type { ExtrudedSolid } from "../../model/types"; +import { uniqueId } from "../../tools/types"; +import type { + Command, + CommandResult, + CommandState, + CmdOption, + DraftShape, + Project, + ToolDraft, + Vec2, +} from "../types"; + +export type SolidShape = "sphere" | "cylinder" | "cone" | "torus" | "cuboid"; + +const SHAPE_OPTIONS: CmdOption[] = [ + { id: "sphere", labelKey: "cmd.solid.shape.sphere" }, + { id: "cylinder", labelKey: "cmd.solid.shape.cylinder" }, + { id: "cone", labelKey: "cmd.solid.shape.cone" }, + { id: "torus", labelKey: "cmd.solid.shape.torus" }, + { id: "cuboid", labelKey: "cmd.solid.shape.cuboid" }, +]; +const SHAPE_IDS = new Set(SHAPE_OPTIONS.map((o) => o.id)); + +type Step = "center" | "radius" | "height" | "major" | "minor" | "corner2"; + +/** Schrittfolge je Form (erste Eingabe ist immer ein Punkt: Mittelpunkt bzw. Ecke). */ +const STEPS: Record = { + sphere: ["center", "radius"], + cylinder: ["center", "radius", "height"], + cone: ["center", "radius", "height"], + torus: ["center", "major", "minor"], + cuboid: ["center", "corner2", "height"], +}; + +const PROMPT: Record = { + center: "cmd.solid.center", + radius: "cmd.solid.radius", + height: "cmd.solid.height", + major: "cmd.solid.major", + minor: "cmd.solid.minor", + corner2: "cmd.solid.corner2", +}; + +interface SolidState extends CommandState { + shape: SolidShape; + /** Index des aktuellen Schritts in {@link STEPS}. */ + stepIdx: number; + /** Erster Punkt (Mittelpunkt/Ecke). */ + p0: Vec2 | null; + /** Zweiter Punkt (nur Quader: Gegenecke). */ + p1: Vec2 | null; + /** Gesammelte Zahlen (Radius/Höhe/…). */ + nums: number[]; + cursor: Vec2 | null; +} + +/** Persistente Defaults über Aufrufe hinweg (Enter übernimmt sie). */ +let lastShape: SolidShape = "sphere"; + +const circlePts = (c: Vec2, r: number): Vec2[] => + Array.from({ length: 48 }, (_, i) => { + const t = (i / 48) * Math.PI * 2; + return { x: c.x + r * Math.cos(t), y: c.y + r * Math.sin(t) }; + }); + +const rectPts = (a: Vec2, b: Vec2): Vec2[] => { + const [x0, x1] = [Math.min(a.x, b.x), Math.max(a.x, b.x)]; + const [y0, y1] = [Math.min(a.y, b.y), Math.max(a.y, b.y)]; + return [{ x: x0, y: y0 }, { x: x1, y: y0 }, { x: x1, y: y1 }, { x: x0, y: y1 }]; +}; + +/** Grundriss-Fläche, Körperhöhe und B-Rep-Spezifikation (lokales Z ab 0) einer Form. */ +export function solidFromInputs( + shape: SolidShape, + p0: Vec2, + p1: Vec2 | null, + nums: number[], +): { points: Vec2[]; height: number; spec: Record; label: string } | null { + const fmt = (v: number) => v.toFixed(2); + switch (shape) { + case "sphere": { + const r = nums[0]; + if (!(r > 1e-6)) return null; + return { + points: circlePts(p0, r), + height: 2 * r, + spec: { kind: "sphere", center: [p0.x, p0.y, r], r }, + label: `Kugel r=${fmt(r)}`, + }; + } + case "cylinder": + case "cone": { + const [r, h] = nums; + if (!(r > 1e-6) || !(h > 1e-6)) return null; + return { + points: circlePts(p0, r), + height: h, + spec: { kind: shape, cx: p0.x, cy: p0.y, z0: 0, r, height: h }, + label: `${shape === "cone" ? "Kegel" : "Zylinder"} r=${fmt(r)} h=${fmt(h)}`, + }; + } + case "torus": { + const [major, minor] = nums; + if (!(minor > 1e-6) || !(major > minor)) return null; + return { + points: circlePts(p0, major + minor), + height: 2 * minor, + spec: { kind: "torus", center: [p0.x, p0.y, minor], major, minor }, + label: `Torus R=${fmt(major)} r=${fmt(minor)}`, + }; + } + case "cuboid": { + const h = nums[0]; + if (!p1 || !(h > 1e-6)) return null; + const pts = rectPts(p0, p1); + const [w, d] = [Math.abs(p1.x - p0.x), Math.abs(p1.y - p0.y)]; + if (w < 1e-6 || d < 1e-6) return null; + return { + points: pts, + height: h, + spec: { kind: "cuboid", min: [pts[0].x, pts[0].y, 0], max: [pts[2].x, pts[2].y, h] }, + label: `Quader ${fmt(w)}×${fmt(d)}×${fmt(h)}`, + }; + } + } +} + +function draftOf(s: SolidState): ToolDraft | null { + if (!s.p0) return null; + const step = STEPS[s.shape][s.stepIdx]; + const preview: DraftShape[] = []; + let hud: string | undefined; + const cur = s.cursor ?? s.p0; + const dist = Math.hypot(cur.x - s.p0.x, cur.y - s.p0.y); + if (s.shape === "cuboid") { + const b = s.p1 ?? cur; + preview.push({ kind: "poly", pts: rectPts(s.p0, b), closed: true }); + hud = s.p1 ? `h = ${(s.nums[0] ?? 0).toFixed(2)} m` : `${Math.abs(b.x - s.p0.x).toFixed(2)} × ${Math.abs(b.y - s.p0.y).toFixed(2)} m`; + } else { + const r = step === "radius" || step === "major" ? dist : (s.nums[0] ?? dist); + preview.push({ kind: "poly", pts: circlePts(s.p0, Math.max(r, 0.01)), closed: true }); + if (s.shape === "torus" && step === "minor") { + preview.push({ kind: "poly", pts: circlePts(s.p0, (s.nums[0] ?? 0) + dist), closed: true }); + } + hud = `${step === "height" ? "h" : "r"} = ${dist.toFixed(2)} m`; + } + return { preview, vertices: [s.p0], hud: { at: cur, text: hud } }; +} + +const reset = (shape: SolidShape): SolidState => ({ + phase: "solid", + shape, + stepIdx: 0, + p0: null, + p1: null, + nums: [], + cursor: null, + lastPoint: null, +}); + +export const solidCommand: Command = { + name: "solid", + labelKey: "cmd.solid.label", + floorOnly: true, + prompt: (st) => { + const s = st as SolidState; + return PROMPT[STEPS[s.shape][s.stepIdx]] as never; + }, + accepts: () => ["point", "number", "option"], + options: () => SHAPE_OPTIONS, + init: (): SolidState => reset(lastShape), + + onInput: (state, input, ctx): [CommandState, CommandResult] => { + const s = state as SolidState; + if (input.kind === "option" && SHAPE_IDS.has(input.id)) { + lastShape = input.id as SolidShape; + // Form-Wechsel setzt die Schritte zurück (der Mittelpunkt bleibt, falls schon gesetzt). + const ns: SolidState = { ...reset(lastShape), p0: s.p0, stepIdx: s.p0 ? 1 : 0, cursor: s.cursor }; + return [ns, { draft: draftOf(ns) }]; + } + const steps = STEPS[s.shape]; + const step = steps[s.stepIdx]; + + // 1) Mittelpunkt / erste Ecke + if (step === "center") { + if (input.kind !== "point") return [s, { draft: draftOf(s) }]; + const ns: SolidState = { ...s, p0: input.point, stepIdx: 1, cursor: input.point, lastPoint: input.point }; + return [ns, { draft: draftOf(ns) }]; + } + + // Wert des aktuellen Schritts: getippte Zahl oder Klick-Abstand zum Mittelpunkt. + let value: number | null = null; + if (input.kind === "number") value = Math.abs(input.value); + else if (input.kind === "point" && s.p0 && step !== "corner2") { + value = Math.hypot(input.point.x - s.p0.x, input.point.y - s.p0.y); + } + + // Quader: Gegenecke als Punkt + if (step === "corner2") { + if (input.kind !== "point") return [s, { draft: draftOf(s) }]; + const ns: SolidState = { ...s, p1: input.point, stepIdx: s.stepIdx + 1, cursor: input.point }; + return [ns, { draft: draftOf(ns) }]; + } + if (value === null || !(value > 1e-6)) return [s, { draft: draftOf(s) }]; + + const nums = [...s.nums, value]; + if (s.stepIdx + 1 < steps.length) { + const ns: SolidState = { ...s, nums, stepIdx: s.stepIdx + 1 }; + return [ns, { draft: draftOf(ns) }]; + } + // Letzter Schritt → committen. + const built = solidFromInputs(s.shape, s.p0!, s.p1, nums); + const levelId = ctx.level.id; + return [ + reset(s.shape), + { + draft: null, + done: true, + commit: (p: Project) => { + if (!built) return p; + const solid: ExtrudedSolid = { + id: uniqueId("body"), + type: "extrudedSolid", + levelId, + points: built.points, + height: built.height, + brep: { spec: built.spec, origin: built.points[0], label: built.label }, + }; + return { ...p, extrudedSolids: [...(p.extrudedSolids ?? []), solid] }; + }, + }, + ]; + }, + + onMove: (state, point): [CommandState, CommandResult] => { + const s = state as SolidState; + const ns: SolidState = { ...s, cursor: point }; + return [ns, { draft: draftOf(ns) }]; + }, + + onConfirm: (state): [CommandState, CommandResult] => [reset((state as SolidState).shape), { draft: null, done: true }], + onCancel: (state): [CommandState, CommandResult] => [reset((state as SolidState).shape), { draft: null, done: true }], +}; diff --git a/src/commands/registry.ts b/src/commands/registry.ts index 5eada83a..2bf83fdf 100644 --- a/src/commands/registry.ts +++ b/src/commands/registry.ts @@ -14,6 +14,7 @@ import { openingCommand, fensterCommand, tuerCommand } from "./cmds/opening"; import { stairCommand } from "./cmds/stair"; import { columnCommand } from "./cmds/column"; import { roofCommand } from "./cmds/roof"; +import { solidCommand } from "./cmds/solid"; import { ceilingOpeningCommand } from "./cmds/ceilingOpening"; import { roomCommand } from "./cmds/room"; import { rectCommand } from "./cmds/rect"; @@ -69,6 +70,7 @@ export const COMMANDS: Record = { stair: stairCommand, column: columnCommand, roof: roofCommand, + solid: solidCommand, ceilingopening: ceilingOpeningCommand, room: roomCommand, line: lineCommand, @@ -139,6 +141,10 @@ export const ALIASES: Record = { stuetze: "column", stütze: "column", dach: "roof", + koerper: "solid", + körper: "solid", + kugel: "solid", + body: "solid", deckenloch: "ceilingopening", aussparung: "ceilingopening", rf: "roof", diff --git a/src/i18n/de.ts b/src/i18n/de.ts index 018920f4..6ffa32c5 100644 --- a/src/i18n/de.ts +++ b/src/i18n/de.ts @@ -1270,6 +1270,18 @@ export const de = { "cmd.ceiling.label": "Decke", "cmd.ceiling.start": "Erster Umrisspunkt der Decke:", "cmd.ceiling.next": "Nächster Punkt ( Schliessen ):", + "cmd.solid.label": "Körper", + "cmd.solid.center": "Mittelpunkt bzw. erste Ecke:", + "cmd.solid.radius": "Radius:", + "cmd.solid.height": "Höhe:", + "cmd.solid.major": "Hauptradius:", + "cmd.solid.minor": "Rohrradius:", + "cmd.solid.corner2": "Gegenecke:", + "cmd.solid.shape.sphere": "Kugel", + "cmd.solid.shape.cylinder": "Zylinder", + "cmd.solid.shape.cone": "Kegel", + "cmd.solid.shape.torus": "Torus", + "cmd.solid.shape.cuboid": "Quader", "cmd.roof.label": "Dach", "cmd.roof.start": "Erste Dachecke:", "cmd.roof.corner": "Gegenecke:", diff --git a/src/i18n/en.ts b/src/i18n/en.ts index dbea96cf..3bf7da87 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -1259,6 +1259,18 @@ export const en: Record = { "cmd.ceiling.label": "Ceiling", "cmd.ceiling.start": "First outline point of ceiling:", "cmd.ceiling.next": "Next point ( Close ):", + "cmd.solid.label": "Solid", + "cmd.solid.center": "Center or first corner:", + "cmd.solid.radius": "Radius:", + "cmd.solid.height": "Height:", + "cmd.solid.major": "Major radius:", + "cmd.solid.minor": "Tube radius:", + "cmd.solid.corner2": "Opposite corner:", + "cmd.solid.shape.sphere": "Sphere", + "cmd.solid.shape.cylinder": "Cylinder", + "cmd.solid.shape.cone": "Cone", + "cmd.solid.shape.torus": "Torus", + "cmd.solid.shape.cuboid": "Cuboid", "cmd.roof.label": "Roof", "cmd.roof.start": "First roof corner:", "cmd.roof.corner": "Opposite corner:", diff --git a/src/panels/ToolsPanel.tsx b/src/panels/ToolsPanel.tsx index 5211a654..b63237d7 100644 --- a/src/panels/ToolsPanel.tsx +++ b/src/panels/ToolsPanel.tsx @@ -355,7 +355,7 @@ const TOOLS_2D: ToolId[] = [ "image", ]; const TOOLS_3D: ToolId[] = ["wall", "ceiling", "window", "door", "stair", "room"]; -const COMMANDS_3D = ["column", "ceilingopening", "roof", "extrude", "sectionline", "viewline"]; +const COMMANDS_3D = ["column", "ceilingopening", "roof", "extrude", "solid", "sectionline", "viewline"]; const COMMANDS_EDIT = ["move", "copy", "array", "rotate", "mirror", "offset", "multiline", "trim", "break", "extend", "fillet", "chamfer", "join", "defblock", "measure"]; type ToolsView = "list" | "icons"; diff --git a/src/ui/ribbon/CommandIcon.tsx b/src/ui/ribbon/CommandIcon.tsx index 774b04ac..9cf7aaab 100644 --- a/src/ui/ribbon/CommandIcon.tsx +++ b/src/ui/ribbon/CommandIcon.tsx @@ -186,6 +186,14 @@ export function CommandIcon({ name }: { name: string }) { ); + case "solid": + // Körper: Kugel mit Äquator (B-Rep-Grundkörper). + return ( + + + + + ); case "sectionline": // Schnittlinie: Achse mit Blickrichtungs-Winkeln (SIA-Schnittsymbol). return (