[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
@@ -0,0 +1,449 @@
|
||||
//! Ebenen/Kategorien-Baum ([K4] S3, vorher `model/types.ts`, `state/projectSlice.ts`,
|
||||
//! `state/selectionLayers.ts`, `io/dxfToDrawings.ts`): Abflachen, Nachschlagen, Sichtbarkeit,
|
||||
//! Isolieren, Baum-Umbauten (Umschalten, Ändern, Unterkategorie, Duplikat, Löschen) und freie Codes.
|
||||
//! Die Rechnung läuft auf `serde_json::Value` (verlustfrei: unbekannte Felder und Reihenfolge der Schlüssel
|
||||
//! bleiben), Reihenfolgen und Sonderfälle wie in der TS-Fassung (doppelte Codes: es greifen alle Treffer
|
||||
//! bzw. der erste, fehlende Felder = JS-`undefined`).
|
||||
|
||||
use crate::model::Project;
|
||||
use serde_json::{Map, Value};
|
||||
use std::collections::HashSet;
|
||||
|
||||
type Tree = Vec<Value>;
|
||||
|
||||
/// JS-Wahrheitswert eines JSON-Werts (`if (c.visible)`).
|
||||
fn truthy(v: Option<&Value>) -> bool {
|
||||
match v {
|
||||
None | Some(Value::Null) => false,
|
||||
Some(Value::Bool(b)) => *b,
|
||||
Some(Value::Number(n)) => n.as_f64().is_some_and(|f| f != 0.0 && !f.is_nan()),
|
||||
Some(Value::String(s)) => !s.is_empty(),
|
||||
Some(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
fn code_of(c: &Value) -> Option<&str> {
|
||||
c.get("code").and_then(Value::as_str)
|
||||
}
|
||||
|
||||
fn children(c: &Value) -> Option<&Tree> {
|
||||
c.get("children").and_then(Value::as_array)
|
||||
}
|
||||
|
||||
/// Kopie von `c` mit gesetztem Schlüssel (`{...c, key: v}`: vorhandener Schlüssel behält seine Stelle).
|
||||
fn with(c: &Value, key: &str, v: Value) -> Value {
|
||||
let mut m: Map<String, Value> = c.as_object().cloned().unwrap_or_default();
|
||||
m.insert(key.to_string(), v);
|
||||
Value::Object(m)
|
||||
}
|
||||
|
||||
/// Flacht den Baum (Tiefensuche, Eltern vor Kindern) ab (`flattenCategories`).
|
||||
pub fn flatten(cats: &[Value]) -> Vec<&Value> {
|
||||
fn walk<'a>(list: &'a [Value], out: &mut Vec<&'a Value>) {
|
||||
for c in list {
|
||||
out.push(c);
|
||||
if let Some(ch) = children(c) {
|
||||
walk(ch, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
walk(cats, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
/// Erste Kategorie mit `code` im Baum (`findCategory`, `getLayerCategory`).
|
||||
pub fn find<'a>(cats: &'a [Value], code: &str) -> Option<&'a Value> {
|
||||
flatten(cats).into_iter().find(|c| code_of(c) == Some(code))
|
||||
}
|
||||
|
||||
/// Codes aller sichtbaren Kategorien in Baumreihenfolge (`collectVisibleCodes`, ohne Doppelungen).
|
||||
pub fn visible_codes(cats: &[Value]) -> Vec<String> {
|
||||
let mut seen = HashSet::new();
|
||||
flatten(cats)
|
||||
.into_iter()
|
||||
.filter(|c| truthy(c.get("visible")))
|
||||
.filter_map(|c| code_of(c).map(str::to_string))
|
||||
.filter(|c| seen.insert(c.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Wendet `f` auf jede Kategorie an (Kinder zuerst); `None` = nichts geändert (`mapCategories`).
|
||||
fn map_cats(cats: &[Value], f: &dyn Fn(&Value) -> Option<Value>) -> Option<Tree> {
|
||||
let mut changed = false;
|
||||
let next: Tree = cats
|
||||
.iter()
|
||||
.map(|c| {
|
||||
let base = match children(c).and_then(|ch| map_cats(ch, f)) {
|
||||
Some(ch) => {
|
||||
changed = true;
|
||||
with(c, "children", Value::Array(ch))
|
||||
}
|
||||
None => c.clone(),
|
||||
};
|
||||
match f(&base) {
|
||||
Some(out) => {
|
||||
changed = true;
|
||||
out
|
||||
}
|
||||
None => base,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
changed.then_some(next)
|
||||
}
|
||||
|
||||
/// `visible` genau für `codes` setzen; `None` = nichts zu tun (`setCategoriesVisible`).
|
||||
pub fn set_visible(cats: &[Value], codes: &HashSet<String>, visible: bool) -> Option<Tree> {
|
||||
map_cats(cats, &|c| {
|
||||
let hit = code_of(c).is_some_and(|k| codes.contains(k));
|
||||
(hit && c.get("visible") != Some(&Value::Bool(visible))).then(|| with(c, "visible", Value::Bool(visible)))
|
||||
})
|
||||
}
|
||||
|
||||
/// Nur `codes` sichtbar, alle übrigen aus; `None` = nichts zu tun (`isolateCategories`).
|
||||
pub fn isolate(cats: &[Value], codes: &HashSet<String>) -> Option<Tree> {
|
||||
map_cats(cats, &|c| {
|
||||
let want = code_of(c).is_some_and(|k| codes.contains(k));
|
||||
(c.get("visible") != Some(&Value::Bool(want))).then(|| with(c, "visible", Value::Bool(want)))
|
||||
})
|
||||
}
|
||||
|
||||
/// Anzeigenamen zu den Codes; unbekannter Code → der Code selbst (`categoryNames`).
|
||||
pub fn names(cats: &[Value], codes: &[String]) -> Vec<String> {
|
||||
let all = flatten(cats);
|
||||
codes
|
||||
.iter()
|
||||
.map(|code| {
|
||||
all.iter()
|
||||
.find(|c| code_of(c) == Some(code))
|
||||
.and_then(|c| c.get("name").and_then(Value::as_str))
|
||||
.map_or_else(|| code.clone(), str::to_string)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Sichtbarkeit der Kategorie `code` umschalten; trifft jede Kategorie mit dem Code, ohne unter einem
|
||||
/// Treffer weiterzusuchen (`toggleCategoryByCode`).
|
||||
pub fn toggle(cats: &[Value], code: &str) -> Tree {
|
||||
cats.iter()
|
||||
.map(|c| {
|
||||
if code_of(c) == Some(code) {
|
||||
return with(c, "visible", Value::Bool(!truthy(c.get("visible"))));
|
||||
}
|
||||
match children(c) {
|
||||
Some(ch) => with(c, "children", Value::Array(toggle(ch, code))),
|
||||
None => c.clone(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `visible`-Flags gemäß `codes` (Code → sichtbar) setzen, unbekannte Codes ignorieren (`applyVisibilityByCode`).
|
||||
pub fn apply_visibility(cats: &[Value], codes: &Map<String, Value>) -> Tree {
|
||||
cats.iter()
|
||||
.map(|c| {
|
||||
let next = match code_of(c).and_then(|k| codes.get(k)) {
|
||||
Some(v) => with(c, "visible", v.clone()),
|
||||
None => c.clone(),
|
||||
};
|
||||
match children(&next) {
|
||||
Some(ch) => with(&next, "children", Value::Array(apply_visibility(ch, codes))),
|
||||
None => next,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Kategorie `code` ändern (`{...c, ...patch}`; ein `null` im Patch entfernt das Feld = JS-`undefined`).
|
||||
pub fn patch(cats: &[Value], code: &str, patch_obj: &Map<String, Value>) -> Tree {
|
||||
cats.iter()
|
||||
.map(|c| {
|
||||
if code_of(c) == Some(code) {
|
||||
let mut m = c.as_object().cloned().unwrap_or_default();
|
||||
for (k, v) in patch_obj {
|
||||
if v.is_null() {
|
||||
m.shift_remove(k);
|
||||
} else {
|
||||
m.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
return Value::Object(m);
|
||||
}
|
||||
match children(c) {
|
||||
Some(ch) => with(c, "children", Value::Array(patch(ch, code, patch_obj))),
|
||||
None => c.clone(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `child` als letztes Kind der Kategorie `parent_code` anhängen (`addChildByCode`).
|
||||
pub fn add_child(cats: &[Value], parent_code: &str, child: &Value) -> Tree {
|
||||
cats.iter()
|
||||
.map(|c| {
|
||||
if code_of(c) == Some(parent_code) {
|
||||
let mut ch = children(c).cloned().unwrap_or_default();
|
||||
ch.push(child.clone());
|
||||
return with(c, "children", Value::Array(ch));
|
||||
}
|
||||
match children(c) {
|
||||
Some(ch) => with(c, "children", Value::Array(add_child(ch, parent_code, child))),
|
||||
None => c.clone(),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `clone` direkt nach der Kategorie `code` auf derselben Ebene einfügen (`insertAfterCode`). Anders als die
|
||||
/// TS-Fassung (sie übersprang genau die Ebene, die das Ziel enthält, und verlor so das Duplikat einer
|
||||
/// Unterkategorie) wird in jede Kinderliste hinabgestiegen.
|
||||
pub fn insert_after(cats: &[Value], code: &str, clone: &Value) -> Tree {
|
||||
let mut out = Vec::with_capacity(cats.len() + 1);
|
||||
for c in cats {
|
||||
let next = match children(c) {
|
||||
Some(ch) => with(c, "children", Value::Array(insert_after(ch, code, clone))),
|
||||
None => c.clone(),
|
||||
};
|
||||
out.push(next);
|
||||
if code_of(c) == Some(code) {
|
||||
out.push(clone.clone());
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Kategorie `code` samt Kindern entfernen (`removeByCode`).
|
||||
pub fn remove(cats: &[Value], code: &str) -> Tree {
|
||||
cats.iter()
|
||||
.filter(|c| code_of(c) != Some(code))
|
||||
.map(|c| match children(c) {
|
||||
Some(ch) => with(c, "children", Value::Array(remove(ch, code))),
|
||||
None => c.clone(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// JS-`parseInt(s, 10)`: führende Leerzeichen, Vorzeichen, Ziffern bis zum ersten Fremdzeichen; sonst `None` (NaN).
|
||||
pub fn js_parse_int(s: &str) -> Option<f64> {
|
||||
let t = s.trim_start_matches(|c: char| c.is_whitespace() || c == '\u{feff}');
|
||||
let (neg, rest) = match t.strip_prefix('-') {
|
||||
Some(r) => (true, r),
|
||||
None => (false, t.strip_prefix('+').unwrap_or(t)),
|
||||
};
|
||||
let digits: String = rest.chars().take_while(char::is_ascii_digit).collect();
|
||||
if digits.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let v: f64 = digits.parse().ok()?;
|
||||
Some(if neg { -v } else { v })
|
||||
}
|
||||
|
||||
/// Zweistellig aufgefüllte Zahl (`String(i).padStart(2, "0")`).
|
||||
fn pad2(i: f64) -> String {
|
||||
let s = format!("{i}");
|
||||
if s.len() < 2 {
|
||||
format!("0{s}")
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
/// Alle Codes des Baums als Menge.
|
||||
fn used_codes(cats: &[Value]) -> HashSet<String> {
|
||||
flatten(cats).into_iter().filter_map(|c| code_of(c).map(str::to_string)).collect()
|
||||
}
|
||||
|
||||
/// Höchster numerischer Code (`Math.max(...nums)`), `None` ohne numerische Codes.
|
||||
fn max_numeric(used: &HashSet<String>) -> Option<f64> {
|
||||
used.iter().filter_map(|c| js_parse_int(c)).fold(None, |m, n| Some(m.map_or(n, |m: f64| m.max(n))))
|
||||
}
|
||||
|
||||
/// Nächster freier zweistelliger Code über den ganzen Baum (`nextFreeCode`); ab 999 `now_ms` als Text.
|
||||
pub fn next_free_code(cats: &[Value], now_ms: f64) -> String {
|
||||
let used = used_codes(cats);
|
||||
let start = max_numeric(&used).unwrap_or(0.0);
|
||||
let mut i = start + 1.0;
|
||||
while i < 1000.0 {
|
||||
let c = pad2(i);
|
||||
if !used.contains(&c) {
|
||||
return c;
|
||||
}
|
||||
i += 1.0;
|
||||
}
|
||||
format!("{}", now_ms.trunc())
|
||||
}
|
||||
|
||||
/// Kategorien für Import-Layer ohne gleichnamige Kategorie (`planImportCategories`): die neuen
|
||||
/// Unterkategorien, der Code der (noch anzulegenden) Elternkategorie — leer, wenn keine neue Kategorie
|
||||
/// nötig ist — und das Mapping Layer-Name → Code für alle Layer.
|
||||
pub struct ImportPlan {
|
||||
pub children: Vec<Value>,
|
||||
pub parent_code: String,
|
||||
pub code_by_layer: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
pub fn plan_import(layer_names: &[String], existing: &[Value], colors: &Map<String, Value>, weights: &Map<String, Value>) -> ImportPlan {
|
||||
let mut used = used_codes(existing);
|
||||
// Namensvergleich ohne Gross-/Kleinschreibung; spätere gleichnamige Kategorien überschreiben frühere.
|
||||
let mut by_name: std::collections::HashMap<String, String> = std::collections::HashMap::new();
|
||||
for c in flatten(existing) {
|
||||
if let (Some(code), Some(name)) = (code_of(c), c.get("name").and_then(Value::as_str)) {
|
||||
by_name.insert(name.to_lowercase(), code.to_string());
|
||||
}
|
||||
}
|
||||
let mut next = max_numeric(&used).map_or(0.0, |m| m.max(0.0)) + 1.0;
|
||||
let mut allocate = |used: &mut HashSet<String>| -> String {
|
||||
while used.contains(&pad2(next)) {
|
||||
next += 1.0;
|
||||
}
|
||||
let code = pad2(next);
|
||||
next += 1.0;
|
||||
used.insert(code.clone());
|
||||
code
|
||||
};
|
||||
let mut code_by_layer: Vec<(String, String)> = Vec::new();
|
||||
let mut children = Vec::new();
|
||||
for name in layer_names {
|
||||
let key = name.to_lowercase();
|
||||
if let Some(known) = by_name.get(&key) {
|
||||
code_by_layer.push((name.clone(), known.clone()));
|
||||
continue;
|
||||
}
|
||||
let code = allocate(&mut used);
|
||||
by_name.insert(key, code.clone());
|
||||
code_by_layer.push((name.clone(), code.clone()));
|
||||
let color = colors.get(name).filter(|v| !v.is_null()).cloned().unwrap_or_else(|| Value::String("#707078".into()));
|
||||
let lw = weights.get(name).filter(|v| !v.is_null()).cloned().unwrap_or_else(|| serde_json::json!(0.18));
|
||||
children.push(serde_json::json!({"code": code, "name": name, "color": color, "lw": lw, "visible": true, "locked": false}));
|
||||
}
|
||||
let parent_code = if children.is_empty() { String::new() } else { allocate(&mut used) };
|
||||
ImportPlan { children, parent_code, code_by_layer }
|
||||
}
|
||||
|
||||
/// Auswahl nach Elementart (`SelectionMembers`: ids je Art).
|
||||
#[derive(serde::Deserialize, Default)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct SelectionMembers {
|
||||
pub wall_ids: Vec<String>,
|
||||
pub drawing_ids: Vec<String>,
|
||||
pub ceiling_ids: Vec<String>,
|
||||
pub room_ids: Vec<String>,
|
||||
pub stair_ids: Vec<String>,
|
||||
pub column_ids: Vec<String>,
|
||||
pub roof_ids: Vec<String>,
|
||||
pub opening_ids: Vec<String>,
|
||||
}
|
||||
|
||||
/// Kategorie-Codes aller gewählten Objekte (eindeutig, in Fundreihenfolge; leere Codes zählen nicht).
|
||||
pub fn selection_category_codes(p: &Project, sel: &SelectionMembers) -> Vec<String> {
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
let mut seen: HashSet<String> = HashSet::new();
|
||||
let mut scan = |ids: &[String], items: &mut dyn Iterator<Item = (&str, &str)>| {
|
||||
if ids.is_empty() {
|
||||
return;
|
||||
}
|
||||
let wanted: HashSet<&str> = ids.iter().map(String::as_str).collect();
|
||||
for (id, code) in items {
|
||||
if !code.is_empty() && wanted.contains(id) && seen.insert(code.to_string()) {
|
||||
out.push(code.to_string());
|
||||
}
|
||||
}
|
||||
};
|
||||
scan(&sel.wall_ids, &mut p.walls.iter().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
scan(&sel.drawing_ids, &mut p.drawings2d.iter().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
scan(&sel.ceiling_ids, &mut p.ceilings.iter().flatten().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
scan(&sel.room_ids, &mut p.rooms.iter().flatten().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
scan(&sel.stair_ids, &mut p.stairs.iter().flatten().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
scan(&sel.column_ids, &mut p.columns.iter().flatten().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
scan(&sel.roof_ids, &mut p.roofs.iter().flatten().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
scan(&sel.opening_ids, &mut p.openings.iter().flatten().map(|o| (o.id.as_str(), o.category_code.as_str())));
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn cat(code: &str, name: &str, visible: bool, ch: Option<Vec<Value>>) -> Value {
|
||||
let mut v = json!({"code": code, "name": name, "color": "#000", "lw": 0.2, "visible": visible, "locked": false});
|
||||
if let Some(ch) = ch {
|
||||
v["children"] = Value::Array(ch);
|
||||
}
|
||||
v
|
||||
}
|
||||
fn tree() -> Tree {
|
||||
vec![cat("20", "Wände", true, None), cat("05", "Import", true, Some(vec![cat("100", "01421", true, None), cat("101", "01847", true, None)]))]
|
||||
}
|
||||
fn set(codes: &[&str]) -> HashSet<String> {
|
||||
codes.iter().map(|s| s.to_string()).collect()
|
||||
}
|
||||
fn vis(t: &[Value]) -> Vec<(String, bool)> {
|
||||
flatten(t).iter().map(|c| (code_of(c).unwrap().to_string(), c["visible"].as_bool().unwrap())).collect()
|
||||
}
|
||||
fn codes_of(t: &[Value]) -> Vec<String> {
|
||||
flatten(t).iter().map(|c| code_of(c).unwrap().to_string()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn abflachen_und_finden() {
|
||||
let t = tree();
|
||||
assert_eq!(codes_of(&t), ["20", "05", "100", "101"]);
|
||||
assert_eq!(find(&t, "101").unwrap()["name"], "01847");
|
||||
assert!(find(&t, "999").is_none());
|
||||
assert_eq!(visible_codes(&t), ["20", "05", "100", "101"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sichtbarkeit_isolieren_und_unveraendert() {
|
||||
let t = tree();
|
||||
let n = set_visible(&t, &set(&["100"]), false).unwrap();
|
||||
assert_eq!(vis(&n), [("20".into(), true), ("05".into(), true), ("100".into(), false), ("101".into(), true)]);
|
||||
assert!(set_visible(&t, &set(&["100"]), true).is_none());
|
||||
let iso = isolate(&t, &set(&["101"])).unwrap();
|
||||
assert_eq!(vis(&iso), [("20".into(), false), ("05".into(), false), ("100".into(), false), ("101".into(), true)]);
|
||||
assert_eq!(names(&t, &["100".into(), "20".into(), "999".into()]), ["01421", "Wände", "999"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn umbauten() {
|
||||
let t = tree();
|
||||
assert_eq!(vis(&toggle(&t, "100"))[2], ("100".into(), false));
|
||||
let av = apply_visibility(&t, json!({"20": false, "101": false, "zz": true}).as_object().unwrap());
|
||||
assert_eq!(vis(&av), [("20".into(), false), ("05".into(), true), ("100".into(), true), ("101".into(), false)]);
|
||||
let p = patch(&t, "101", json!({"name": "X", "color": null}).as_object().unwrap());
|
||||
assert_eq!(find(&p, "101").unwrap()["name"], "X");
|
||||
assert!(find(&p, "101").unwrap().get("color").is_none());
|
||||
let ac = add_child(&t, "20", &cat("21", "k", true, None));
|
||||
assert_eq!(find(&ac, "20").unwrap()["children"][0]["code"], "21");
|
||||
assert_eq!(codes_of(&insert_after(&t, "100", &cat("102", "c", true, None))), ["20", "05", "100", "102", "101"]);
|
||||
assert_eq!(codes_of(&remove(&t, "05")), ["20"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn freie_codes() {
|
||||
assert_eq!(js_parse_int(" 12abc"), Some(12.0));
|
||||
assert_eq!(js_parse_int("x1"), None);
|
||||
assert_eq!(js_parse_int("-3"), Some(-3.0));
|
||||
assert_eq!(next_free_code(&[], 5.0), "01");
|
||||
assert_eq!(next_free_code(&tree(), 5.0), "102");
|
||||
assert_eq!(next_free_code(&[cat("999", "x", true, None)], 1234.9), "1234");
|
||||
assert_eq!(next_free_code(&[cat("-5", "x", true, None), cat("ab", "y", true, None)], 0.0), "-4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_plan() {
|
||||
let t = vec![cat("05", "Import", true, None), cat("20", "Wände", true, None)];
|
||||
let weights = json!({"Neu": 0.5});
|
||||
let plan = plan_import(&["Wände".into(), "Neu".into(), "NEU".into()], &t, &Map::new(), weights.as_object().unwrap());
|
||||
assert_eq!(plan.children.len(), 1);
|
||||
assert_eq!(plan.children[0]["code"], "21");
|
||||
assert_eq!(plan.children[0]["lw"], 0.5);
|
||||
assert_eq!(plan.parent_code, "22");
|
||||
assert_eq!(plan.code_by_layer, [("Wände".into(), "20".into()), ("Neu".into(), "21".into()), ("NEU".into(), "21".into())]);
|
||||
let none = plan_import(&["wände".into()], &t, &Map::new(), &Map::new());
|
||||
assert!(none.children.is_empty());
|
||||
assert!(none.parent_code.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//! Zeichnungsebenen ([K4] S3, vorher `model/types.ts`): Geschosshöhen stapeln.
|
||||
//! Rechnung auf `serde_json::Value` (verlustfrei: unbekannte Felder bleiben).
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
/// `recomputeFloorElevations`: Das erste Geschoss beginnt bei 0, jedes weitere bei `baseElevation +
|
||||
/// floorHeight` des vorigen (fehlende Höhe = 0). Nicht-Geschoss-Ebenen verlieren `baseElevation`.
|
||||
pub fn recompute_floor_elevations(levels: &[Value]) -> Vec<Value> {
|
||||
let mut next_base = 0.0_f64;
|
||||
levels
|
||||
.iter()
|
||||
.map(|level| {
|
||||
let mut m = level.as_object().cloned().unwrap_or_default();
|
||||
if level.get("kind").and_then(Value::as_str) != Some("floor") {
|
||||
m.shift_remove("baseElevation");
|
||||
return Value::Object(m);
|
||||
}
|
||||
let base = next_base;
|
||||
next_base = base + level.get("floorHeight").and_then(Value::as_f64).unwrap_or(0.0);
|
||||
m.insert("baseElevation".into(), serde_json::Number::from_f64(base).map_or(Value::Null, Value::Number));
|
||||
Value::Object(m)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn stapelt_geschosse() {
|
||||
let l = vec![
|
||||
json!({"id": "a", "kind": "floor", "floorHeight": 2.6, "baseElevation": 9.0}),
|
||||
json!({"id": "s", "kind": "section", "baseElevation": 1.0}),
|
||||
json!({"id": "b", "kind": "floor"}),
|
||||
json!({"id": "c", "kind": "floor", "floorHeight": 3.0}),
|
||||
];
|
||||
let r = recompute_floor_elevations(&l);
|
||||
assert_eq!(r[0]["baseElevation"], 0.0);
|
||||
assert!(r[1].get("baseElevation").is_none());
|
||||
assert_eq!(r[2]["baseElevation"], 2.6);
|
||||
assert_eq!(r[3]["baseElevation"], 2.6);
|
||||
assert_eq!(l[0]["baseElevation"], 9.0); // Eingabe unverändert
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,8 @@ pub mod ifc_stairs;
|
||||
pub mod ifc_data;
|
||||
pub mod mesh_data;
|
||||
pub mod groups;
|
||||
pub mod layers;
|
||||
pub mod levels;
|
||||
|
||||
#[cfg(feature = "web")]
|
||||
mod web;
|
||||
@@ -52,6 +52,11 @@ pub fn run(p: &Project, name: &str, args: &[f64], text: &str) -> Result<String,
|
||||
let (expanded, group_ids) = crate::groups::expand_to_groups(p, &a.raw, &a.path);
|
||||
json(&serde_json::json!({"expanded": expanded, "groupIds": group_ids}))
|
||||
}
|
||||
// Ebenen ([K4] S3, `layers.rs`): Kategorie-Codes der Auswahl (`text` = JSON der ids je Art).
|
||||
("selectionCategoryCodes", _) => {
|
||||
let sel: crate::layers::SelectionMembers = serde_json::from_str(text).map_err(|e| format!("selectionCategoryCodes: {e}"))?;
|
||||
json(&crate::layers::selection_category_codes(p, &sel))
|
||||
}
|
||||
_ => Err(format!("Unbekannte Abfrage: {name}")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
//! Bindungen der Ebenen-/Geschoss-Logik (`crate::layers`, `crate::levels`): zustandslos, JSON hinein,
|
||||
//! JSON hinaus. Kaputte Eingaben ergeben einen leeren Text (nie ein Panic).
|
||||
|
||||
use crate::layers;
|
||||
use serde_json::{json, Map, Value};
|
||||
use std::collections::HashSet;
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
fn list(json: &str) -> Option<Vec<Value>> {
|
||||
match serde_json::from_str::<Value>(json).ok()? {
|
||||
Value::Array(a) => Some(a),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn strings(v: &Value) -> Vec<String> {
|
||||
v.as_array().map(|a| a.iter().filter_map(|s| s.as_str().map(str::to_string)).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn obj(v: &Value) -> Map<String, Value> {
|
||||
v.as_object().cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
fn text(v: &Value) -> &str {
|
||||
v.as_str().unwrap_or("")
|
||||
}
|
||||
|
||||
/// Rechnung `name` auf dem Kategorie-Baum `layers_json` (Liste); `arg_json`: je nach Rechnung ein Text,
|
||||
/// eine Liste oder ein Objekt. Antwort als JSON, bei Unsinn (kein Baum, unbekannter Name) leerer Text.
|
||||
#[wasm_bindgen]
|
||||
pub fn layers_call(name: &str, layers_json: &str, arg_json: &str, now_ms: f64) -> String {
|
||||
let Some(cats) = list(layers_json) else { return String::new() };
|
||||
let arg: Value = if arg_json.is_empty() { Value::Null } else { serde_json::from_str(arg_json).unwrap_or(Value::Null) };
|
||||
let out: Value = match name {
|
||||
"flatten" => Value::Array(layers::flatten(&cats).into_iter().cloned().collect()),
|
||||
"find" => layers::find(&cats, text(&arg)).cloned().unwrap_or(Value::Null),
|
||||
"visibleCodes" => json!(layers::visible_codes(&cats)),
|
||||
"setVisible" => {
|
||||
let codes: HashSet<String> = strings(&arg["codes"]).into_iter().collect();
|
||||
layers::set_visible(&cats, &codes, arg["visible"].as_bool().unwrap_or(false)).map_or(Value::Null, Value::Array)
|
||||
}
|
||||
"isolate" => {
|
||||
let codes: HashSet<String> = strings(&arg).into_iter().collect();
|
||||
layers::isolate(&cats, &codes).map_or(Value::Null, Value::Array)
|
||||
}
|
||||
"names" => json!(layers::names(&cats, &strings(&arg))),
|
||||
"toggle" => Value::Array(layers::toggle(&cats, text(&arg))),
|
||||
"applyVisibility" => Value::Array(layers::apply_visibility(&cats, &obj(&arg))),
|
||||
"patch" => Value::Array(layers::patch(&cats, text(&arg["code"]), &obj(&arg["patch"]))),
|
||||
"addChild" => Value::Array(layers::add_child(&cats, text(&arg["parent"]), &arg["child"])),
|
||||
"insertAfter" => Value::Array(layers::insert_after(&cats, text(&arg["code"]), &arg["clone"])),
|
||||
"remove" => Value::Array(layers::remove(&cats, text(&arg))),
|
||||
"nextFreeCode" => Value::String(layers::next_free_code(&cats, if now_ms.is_finite() { now_ms } else { 0.0 })),
|
||||
"planImport" => {
|
||||
let plan = layers::plan_import(&strings(&arg["names"]), &cats, &obj(&arg["colors"]), &obj(&arg["weights"]));
|
||||
json!({"children": plan.children, "parentCode": plan.parent_code, "codeByLayer": plan.code_by_layer})
|
||||
}
|
||||
_ => return String::new(),
|
||||
};
|
||||
out.to_string()
|
||||
}
|
||||
|
||||
/// `recomputeFloorElevations` auf der Ebenen-Liste (JSON); leerer Text bei Unsinn.
|
||||
#[wasm_bindgen]
|
||||
pub fn levels_recompute_elevations(levels_json: &str) -> String {
|
||||
match list(levels_json) {
|
||||
Some(l) => Value::Array(crate::levels::recompute_floor_elevations(&l)).to_string(),
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ mod plan_svg;
|
||||
mod plan_dxf;
|
||||
mod plan_print_svg;
|
||||
mod drawings;
|
||||
mod layers;
|
||||
|
||||
use wasm_bindgen::prelude::wasm_bindgen;
|
||||
|
||||
|
||||
Reference in new issue
Block a user