[K3d] SVG-Text-Export aus dem Plan-Puffer im Kern

core/src/plan_svg.rs (+ web/plan_svg.rs): planToSvgString (dossier-cli plan/section/elevation,
MCP, Golden-Tests) schreibt der Kern direkt aus den Abschnitten der Plan-Puffer; Texte aus dem
Text-JSON des Puffers (planParts im Text-Modus). TS-Datei ist nur noch Fassade. Byte-gleich zur
alten Fassung (328 SVGs: Beispielprojekt alle Ebenen × Detailgrade, Massstab/Rand, Überlagerung,
Schnitt, Ansicht, 300 Zufallspläne, grosses Projekt; Goldens unverändert). CLI-Ausgaben
(plan EG/OG svg+json, section, elevation, dxf) md5-gleich zur CLI des Hauptstands.

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
Claude committed 2026-10-28 23:32:53 +00:00
1 parent 1fe83d6349
commit 126e419efa
5 files changed
+305 -91

No files matched your search

+1
View File
@@ -62,6 +62,7 @@ pub mod plan_parts;
pub mod plan_scene;
pub mod plan_pick;
pub mod snap;
pub mod plan_svg;
#[cfg(feature = "web")]
mod web;
+278
View File
@@ -0,0 +1,278 @@
//! Plan → SVG-Text ohne DOM ([K3d], früher `src/export/planToSvgString.ts`): für `dossier-cli plan`,
//! MCP und Referenzbild-Tests, direkt aus den Abschnitten eines zusammengesetzten Plans
//! ([`crate::plan_parts`]). Koordinaten in Metern (Y nach oben → SVG gespiegelt), Strichstärken
//! aus Papier-mm über den Massstab; Schraffur-Muster werden nicht ausgebreitet (nur Grundfüllung),
//! Bilder und Textpfade fehlen — gedacht für Kontrolle und Diffs, nicht für Druck. Byte-gleich zur
//! TS-Fassung (Zahlen wie `String(Math.round(v·1e5)/1e5)`, Texte aus dem Text-JSON des Puffers).
use crate::js_fmt::js_number;
use crate::jsnum;
use crate::plan_buf::{whole, F_COLOR, F_DASH_LIST, F_GREYED, F_OPACITY, T_ARC, T_CIRCLE, T_DRAWING_ARC, T_DRAWING_TEXT, T_LINE, T_POLYGON, T_TEXT};
use crate::plan_parts::Part;
use serde_json::Value;
use std::f64::consts::PI;
/// `num()`: gerundet auf 1e-5, nicht endlich → "0".
fn num(v: f64) -> String {
if v.is_finite() {
js_number(jsnum::round(v * 1e5) / 1e5)
} else {
"0".into()
}
}
/// `esc()` für Attribut- und Textinhalt.
fn esc(s: &str) -> String {
let mut o = String::with_capacity(s.len());
for c in s.chars() {
match c {
'&' => o.push_str("&"),
'<' => o.push_str("&lt;"),
'>' => o.push_str("&gt;"),
'"' => o.push_str("&quot;"),
_ => o.push(c),
}
}
o
}
/// Optionen und Ausmass (wie `SvgStringOptions` + `plan.bounds`).
pub struct SvgOptions {
pub scale_denominator: f64,
pub margin_m: f64,
pub min_x: f64,
pub min_y: f64,
pub max_x: f64,
pub max_y: f64,
}
/// Zahl aus JSON (null/fehlend → NaN wie `undefined`/`NaN` in JS-Rechnungen).
fn jnum(v: &Value, k: &str) -> f64 {
v.get(k).and_then(Value::as_f64).unwrap_or(f64::NAN)
}
fn jstr<'a>(v: &'a Value, k: &str) -> Option<&'a str> {
v.get(k).and_then(Value::as_str)
}
/// `op(p)` aus JSON-Feldern: greyed (wahr) bzw. opacity < 1.
fn op_json(v: &Value) -> String {
let greyed = match v.get("greyed") {
Some(Value::Bool(b)) => *b,
_ => false,
};
op(greyed, v.get("opacity").map(|o| o.as_f64().unwrap_or(f64::NAN)))
}
fn op(greyed: bool, opacity: Option<f64>) -> String {
if greyed {
" opacity=\"0.35\"".into()
} else {
match opacity {
Some(o) if o < 1.0 => format!(" opacity=\"{}\"", num(o)),
_ => String::new(),
}
}
}
/// SVG-Text des Plans; `None` bei kaputtem Puffer.
pub fn plan_svg(parts: &mut [Part], o: &SvgOptions) -> Option<String> {
let n = o.scale_denominator;
let margin = o.margin_m;
let mm_to_m = n / 1000.0;
let x0 = o.min_x - margin;
let w = o.max_x - o.min_x + 2.0 * margin;
let h = o.max_y - o.min_y + 2.0 * margin;
let top = o.max_y + margin;
let xs = |x: f64| num(x);
let ys = |y: f64| num(top - y);
let mut out: Vec<String> = Vec::new();
for part in parts.iter_mut() {
for i in part.from..part.to {
let rec = part.view.rec(i)?;
let f = rec.f.clone();
let fl = rec.flags;
let greyed = fl & F_GREYED != 0;
let opacity = |k: usize| (fl & F_OPACITY != 0).then_some(f[k]);
let dash_attr = |part: &Part| -> Option<String> {
if fl & F_DASH_LIST == 0 {
return Some(String::new());
}
let (o, len) = rec.dash?;
let d = part.view.pool(o, len)?;
if d.is_empty() {
return Some(String::new());
}
Some(format!(" stroke-dasharray=\"{}\"", d.iter().map(|v| num(v * mm_to_m)).collect::<Vec<_>>().join(" ")))
};
match rec.tag {
T_POLYGON => {
let pts = part.view.pool_pts(whole(f[5])?, whole(f[6])?)?;
if pts.len() < 3 {
continue;
}
let pl = pts.iter().map(|q| format!("{},{}", xs(q.x), ys(q.y))).collect::<Vec<_>>().join(" ");
let fill = esc(part.str(f[0]).unwrap_or(""));
let stroke = esc(part.str(f[1]).unwrap_or(""));
out.push(format!(
"<polygon points=\"{pl}\" fill=\"{fill}\" stroke=\"{stroke}\" stroke-width=\"{}\" stroke-linejoin=\"round\"{}/>",
num(f[2] * mm_to_m),
op(greyed, opacity(4))
));
}
T_LINE => {
let color = if fl & F_COLOR != 0 { esc(part.str(f[1]).unwrap_or("")) } else { "#000".into() };
let dash = dash_attr(part)?;
out.push(format!(
"<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" stroke=\"{color}\" stroke-width=\"{}\"{dash}{}/>",
xs(f[4]),
ys(f[5]),
xs(f[6]),
ys(f[7]),
num(f[2] * mm_to_m),
op(greyed, opacity(3))
));
}
T_ARC => {
let (cx, cy, fx, fy, tx, ty, r) = (f[4], f[5], f[6], f[7], f[8], f[9], f[10]);
let a0 = jsnum::atan2(fy - cy, fx - cx);
let mut sweep = jsnum::atan2(ty - cy, tx - cx) - a0;
while sweep < 0.0 && sweep.is_finite() {
sweep += PI * 2.0;
}
let dash = dash_attr(part)?;
out.push(format!(
"<path d=\"M {} {} A {} {} 0 {} 0 {} {}\" fill=\"none\" stroke=\"#000\" stroke-width=\"{}\"{dash}{}/>",
xs(fx),
ys(fy),
num(r),
num(r),
if sweep > PI { 1 } else { 0 },
xs(tx),
ys(ty),
num(f[2] * mm_to_m),
op(greyed, opacity(3))
));
}
T_CIRCLE => {
let fill = esc(part.str(f[0]).unwrap_or(""));
let stroke = esc(part.str(f[1]).unwrap_or(""));
let dash = dash_attr(part)?;
out.push(format!(
"<circle cx=\"{}\" cy=\"{}\" r=\"{}\" fill=\"{fill}\" stroke=\"{stroke}\" stroke-width=\"{}\"{dash}{}/>",
xs(f[4]),
ys(f[5]),
num(f[6]),
num(f[2] * mm_to_m),
op(greyed, opacity(3))
));
}
T_DRAWING_ARC => {
let (cx, cy, r, a0, a1) = (f[3], f[4], f[5], f[6], f[7]);
let sx = cx + r * jsnum::cos(a0);
let sy = cy + r * jsnum::sin(a0);
let ex = cx + r * jsnum::cos(a1);
let ey = cy + r * jsnum::sin(a1);
let mut sweep = a1 - a0;
while sweep < 0.0 && sweep.is_finite() {
sweep += PI * 2.0;
}
let stroke = esc(part.str(f[0]).unwrap_or(""));
let dash = dash_attr(part)?;
out.push(format!(
"<path d=\"M {} {} A {} {} 0 {} 0 {} {}\" fill=\"none\" stroke=\"{stroke}\" stroke-width=\"{}\"{dash}{}/>",
xs(sx),
ys(sy),
num(r),
num(r),
if sweep > PI { 1 } else { 0 },
xs(ex),
ys(ey),
num(f[1] * mm_to_m),
op(greyed, opacity(2))
));
}
T_DRAWING_TEXT | T_TEXT => {
// Texte reisen als JSON (nur im Text-Modus der Abschnitte; sonst fehlen sie).
let Some(js) = part.str(f[0]).map(str::to_owned) else { continue };
let Ok(v) = serde_json::from_str::<Value>(&js) else { continue };
let at = v.get("at").cloned().unwrap_or(Value::Null);
let (ax, ay) = (jnum(&at, "x"), jnum(&at, "y"));
if rec.tag == T_DRAWING_TEXT {
let angle = jnum(&v, "angle");
out.push(format!(
"<text x=\"{}\" y=\"{}\" font-size=\"{}\" fill=\"{}\" font-family=\"sans-serif\" transform=\"rotate({} {} {})\"{}>{}</text>",
xs(ax),
ys(ay),
num(jnum(&v, "heightM")),
esc(jstr(&v, "color").unwrap_or("")),
num(-(angle * 180.0) / PI),
xs(ax),
ys(ay),
op_json(&v),
esc(jstr(&v, "text").unwrap_or(""))
));
} else {
let size = (jnum(&v, "basePt") * 0.3528) * mm_to_m;
// plainText(doc): Absätze (Runs verkettet) mit "\n".
let doc = v.get("doc").and_then(|d| d.get("paragraphs")).and_then(Value::as_array);
let plain = doc
.map(|ps| {
ps.iter()
.map(|p| p.get("runs").and_then(Value::as_array).map(|rs| rs.iter().map(|r| jstr(r, "text").unwrap_or("")).collect::<String>()).unwrap_or_default())
.collect::<Vec<_>>()
.join("\n")
})
.unwrap_or_default();
let mut lines = vec![plain];
if let Some(ex) = v.get("extraLines").and_then(Value::as_array) {
for l in ex {
lines.push(jstr(l, "text").unwrap_or("").to_owned());
}
}
let color = esc(jstr(&v, "color").unwrap_or(""));
let o = op_json(&v);
for (k, ln) in lines.iter().filter(|s| !s.is_empty()).enumerate() {
out.push(format!(
"<text x=\"{}\" y=\"{}\" font-size=\"{}\" fill=\"{color}\" font-family=\"sans-serif\" text-anchor=\"middle\"{o}>{}</text>",
xs(ax),
num(top - ay + k as f64 * size * 1.2),
num(size),
esc(ln)
));
}
}
}
_ => {}
}
}
}
Some(format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"{} 0 {} {}\" width=\"{}mm\" height=\"{}mm\">\n<rect x=\"{}\" y=\"0\" width=\"{}\" height=\"{}\" fill=\"#ffffff\"/>\n{}\n</svg>\n",
num(x0),
num(w),
num(h),
num(w * 1000.0 / n),
num(h * 1000.0 / n),
num(x0),
num(w),
num(h),
out.join("\n")
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zahlen_und_escapes() {
assert_eq!(num(1.234567), "1.23457");
assert_eq!(num(f64::NAN), "0");
assert_eq!(num(-0.000001), "0");
assert_eq!(esc("a<b & \"c\">"), "a&lt;b &amp; &quot;c&quot;&gt;");
assert_eq!(op(false, Some(0.5)), " opacity=\"0.5\"");
assert_eq!(op(true, Some(0.5)), " opacity=\"0.35\"");
assert_eq!(op(false, Some(1.0)), "");
}
}
+1
View File
@@ -35,6 +35,7 @@ mod skeleton;
mod plan_scene;
mod plan_pick;
mod snap;
mod plan_svg;
use wasm_bindgen::prelude::wasm_bindgen;
+18
View File
@@ -0,0 +1,18 @@
//! Bindung des SVG-Text-Exports (`crate::plan_svg`) auf gehaltenen Plan-Puffern (Abschnitte wie
//! `plan_parts`). Leerer Text = Puffer fehlt oder kaputt (TS legt neu ab und fragt nochmals).
use crate::plan_parts::parts_from;
use crate::plan_svg::{plan_svg, SvgOptions};
use wasm_bindgen::prelude::wasm_bindgen;
/// `opts` = `[Massstab-Nenner, Rand m, minX, minY, maxX, maxY]`.
#[wasm_bindgen]
pub fn plan_svg_string(segs: &[f64], opts: &[f64]) -> String {
let [n, margin, min_x, min_y, max_x, max_y] = match <[f64; 6]>::try_from(opts) {
Ok(a) => a,
Err(_) => return String::new(),
};
let Some(held) = super::plan_buf::held_for_segs(segs) else { return String::new() };
let Some(mut parts) = parts_from(segs, |nr| held.iter().find(|e| e.0 == nr).map(|e| &*e.1)) else { return String::new() };
plan_svg(&mut parts, &SvgOptions { scale_denominator: n, margin_m: margin, min_x, min_y, max_x, max_y }).unwrap_or_default()
}
+7 -91
View File
@@ -1,11 +1,13 @@
// Plan → SVG-TEXT ohne DOM (für `dossier-cli` und Referenzbild-Tests, lessons-bimcraft [4]/[17]).
// Der Druck-Export (`planToPrintSvg`) baut ein DOM-Element für svg2pdf; dieser Serializer ist
// Der Druck-Export (`sceneToPrintSvg`) baut ein DOM-Element für svg2pdf; dieser Serializer ist
// die schlanke, headless Variante: Koordinaten in Metern (Y nach oben → SVG gespiegelt),
// Strichstärken aus Papier-mm über den Massstab umgerechnet. Schraffur-Muster werden nicht
// ausgebreitet (nur Grundfüllung), Bilder fehlen — gedacht für Kontrolle und Diffs, nicht für Druck.
// Geschrieben vom Kern direkt aus dem Plan-Puffer ([K3d], `src-tauri/core/src/plan_svg.rs`).
import type { Plan, Primitive } from "../plan/generatePlan";
import { plainText } from "../text/richText";
import type { Plan } from "../plan/generatePlan";
import { planParts, withPlanParts } from "../plan/planParts";
import { core } from "../engine/coreWasm";
export interface SvgStringOptions {
/** Massstab-Nenner N (1:N) — bestimmt die Umrechnung Papier-mm → Meter. Default 100. */
@@ -14,94 +16,8 @@ export interface SvgStringOptions {
marginM?: number;
}
const esc = (s: string): string =>
s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
const num = (v: number): string => (Number.isFinite(v) ? String(Math.round(v * 1e5) / 1e5) : "0");
export function planToSvgString(plan: Plan, opts: SvgStringOptions = {}): string {
const n = opts.scaleDenominator ?? 100;
const margin = opts.marginM ?? 0.5;
const mmToM = n / 1000; // 1 mm Papier = n/1000 m Modell
const b = plan.bounds;
const x0 = b.minX - margin;
const w = b.maxX - b.minX + 2 * margin;
const h = b.maxY - b.minY + 2 * margin;
// Y spiegeln: svgY = (maxY + margin) − y
const top = b.maxY + margin;
const X = (x: number): string => num(x);
const Y = (y: number): string => num(top - y);
const dashAttr = (d: number[] | null | undefined): string =>
d && d.length ? ` stroke-dasharray="${d.map((v) => num(v * mmToM)).join(" ")}"` : "";
const op = (p: { opacity?: number; greyed?: boolean }): string =>
p.greyed ? ' opacity="0.35"' : p.opacity !== undefined && p.opacity < 1 ? ` opacity="${num(p.opacity)}"` : "";
const out: string[] = [];
for (const p of plan.primitives as Primitive[]) {
switch (p.kind) {
case "polygon": {
if (p.pts.length < 3) break;
const pts = p.pts.map((q) => `${X(q.x)},${Y(q.y)}`).join(" ");
out.push(
`<polygon points="${pts}" fill="${esc(p.fill)}" stroke="${esc(p.stroke)}" stroke-width="${num(p.strokeWidthMm * mmToM)}" stroke-linejoin="round"${op(p)}/>`,
);
break;
}
case "line":
out.push(
`<line x1="${X(p.a.x)}" y1="${Y(p.a.y)}" x2="${X(p.b.x)}" y2="${Y(p.b.y)}" stroke="${esc(p.color ?? "#000")}" stroke-width="${num(p.weightMm * mmToM)}"${dashAttr(p.dash)}${op(p)}/>`,
);
break;
case "arc": {
// Bogen von `from` nach `to` um `center`, gegen den Uhrzeigersinn im Modell.
const a0 = Math.atan2(p.from.y - p.center.y, p.from.x - p.center.x);
let sweep = Math.atan2(p.to.y - p.center.y, p.to.x - p.center.x) - a0;
while (sweep < 0) sweep += Math.PI * 2;
out.push(
`<path d="M ${X(p.from.x)} ${Y(p.from.y)} A ${num(p.r)} ${num(p.r)} 0 ${sweep > Math.PI ? 1 : 0} 0 ${X(p.to.x)} ${Y(p.to.y)}" fill="none" stroke="#000" stroke-width="${num(p.weightMm * mmToM)}"${dashAttr(p.dash)}${op(p)}/>`,
);
break;
}
case "drawingCircle":
out.push(
`<circle cx="${X(p.center.x)}" cy="${Y(p.center.y)}" r="${num(p.r)}" fill="${esc(p.fill)}" stroke="${esc(p.stroke)}" stroke-width="${num(p.weightMm * mmToM)}"${dashAttr(p.dash)}${op(p)}/>`,
);
break;
case "drawingArc": {
const sx = p.center.x + p.r * Math.cos(p.a0);
const sy = p.center.y + p.r * Math.sin(p.a0);
const ex = p.center.x + p.r * Math.cos(p.a1);
const ey = p.center.y + p.r * Math.sin(p.a1);
let sweep = p.a1 - p.a0;
while (sweep < 0) sweep += Math.PI * 2;
out.push(
`<path d="M ${X(sx)} ${Y(sy)} A ${num(p.r)} ${num(p.r)} 0 ${sweep > Math.PI ? 1 : 0} 0 ${X(ex)} ${Y(ey)}" fill="none" stroke="${esc(p.stroke)}" stroke-width="${num(p.weightMm * mmToM)}"${dashAttr(p.dash)}${op(p)}/>`,
);
break;
}
case "drawingText":
out.push(
`<text x="${X(p.at.x)}" y="${Y(p.at.y)}" font-size="${num(p.heightM)}" fill="${esc(p.color)}" font-family="sans-serif" transform="rotate(${num(-(p.angle * 180) / Math.PI)} ${X(p.at.x)} ${Y(p.at.y)})"${op(p)}>${esc(p.text)}</text>`,
);
break;
case "text": {
const size = (p.basePt * 0.3528) * mmToM; // pt → mm Papier → m Modell
const lines = [plainText(p.doc), ...p.extraLines.map((l) => l.text)].filter((s) => s);
lines.forEach((ln, i) =>
out.push(
`<text x="${X(p.at.x)}" y="${num(top - p.at.y + i * size * 1.2)}" font-size="${num(size)}" fill="${esc(p.color)}" font-family="sans-serif" text-anchor="middle"${op(p)}>${esc(ln)}</text>`,
),
);
break;
}
default:
break; // Bilder, Textpfade: im headless SVG nicht dargestellt
}
}
return (
`<?xml version="1.0" encoding="UTF-8"?>\n` +
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="${num(x0)} 0 ${num(w)} ${num(h)}" width="${num(w * 1000 / n)}mm" height="${num(h * 1000 / n)}mm">\n` +
`<rect x="${num(x0)}" y="0" width="${num(w)}" height="${num(h)}" fill="#ffffff"/>\n` +
out.join("\n") +
`\n</svg>\n`
);
const o = Float64Array.from([opts.scaleDenominator ?? 100, opts.marginM ?? 0.5, b.minX, b.minY, b.maxX, b.maxY]);
return withPlanParts(planParts(plan, true), (segs) => core().plan_svg_string(segs, o));
}