IFC-Datensätze: 26 Geländer, 27 geneigte Decke, 28 Träger (REC_BEAM in ifc_data.rs und fileio). cmd_transform: Geländer Slot 11, Träger Slot 12, 13 Listen. Sammellisten (Umbau, Varianten, Verschieben, Einfügen, KNOWN_KEYS) um beams und railings ergänzt; types.generated.ts neu erzeugt.
156 lines
8.2 KiB
Rust
156 lines
8.2 KiB
Rust
//! Befehle und Werkzeuge im Kern ([K4] S3): die Zustandsautomaten der Befehle (Eingaben, Optionen,
|
|
//! Tasten) bleiben in der TS-Hülle; was aus dem Projekt Geometrie oder Ops rechnet, steht hier.
|
|
//! Eine Anfrage (`cmd` + Argumente, JSON) ergibt Ops (`cmdOps`, Wurzel-Abfrage; `doc_cmd_commit` rechnet
|
|
//! und wendet im selben Aufruf an) bzw. Vorschau-Formen (`doc_cmd_preview`). Module: `cmd_transform`
|
|
//! (Verschieben/Drehen/Spiegeln/Skalieren/Reihe, Ausrichten/Verteilen, Block definieren).
|
|
|
|
use crate::grips::{result_json, GripResult};
|
|
use crate::model::Project;
|
|
use crate::ops::Op;
|
|
use serde_json::{Map, Value};
|
|
|
|
fn parse<T: serde::de::DeserializeOwned>(v: &Value, what: &str) -> Result<T, String> {
|
|
serde_json::from_value(v.clone()).map_err(|e| format!("{what}: {e}"))
|
|
}
|
|
|
|
/// Ops eines Befehls (`text` = JSON `{cmd, …}`, Zahlen dürfen Nicht-endlich-Marken sein). `p` = typisierte
|
|
/// Sicht derselben Wurzel (Lesen, [124]); geänderte Elemente entstehen verlustfrei aus `root`.
|
|
pub fn ops(root: &Map<String, Value>, p: &Project, text: &str) -> Result<Vec<Op>, String> {
|
|
let v: Value = serde_json::from_str(text).map_err(|e| format!("cmdOps: {e}"))?;
|
|
// Zeichnen in einer betretenen Gruppe: neue Elemente werden ihr Mitglied (`cmd_create::group_ops`).
|
|
if let Some(group) = v.get("group").and_then(Value::as_str) {
|
|
let mut inner = v.clone();
|
|
inner.as_object_mut().map(|m| m.remove("group"));
|
|
let mut out = ops(root, p, &inner.to_string())?;
|
|
let extra = crate::cmd_create::group_ops(root, &out, group);
|
|
out.extend(extra);
|
|
return Ok(out);
|
|
}
|
|
let cmd = v.get("cmd").and_then(Value::as_str).unwrap_or("");
|
|
let ids = |k: &str| -> Vec<String> { v.get(k).and_then(|x| serde_json::from_value(x.clone()).ok()).unwrap_or_default() };
|
|
let s = |k: &str| v.get(k).and_then(Value::as_str).unwrap_or("").to_string();
|
|
if let Some(ops) = crate::cmd_edit::ops(root, p, &v) {
|
|
return Ok(ops);
|
|
}
|
|
if let Some(ops) = crate::cmd_create::ops(root, p, &v) {
|
|
// [271] Aktueller Zeichen-Status (`renovation` der Anfrage): neue Bauteile tragen ihn (Bestand = ohne Feld).
|
|
// [272] Aktuelle Zeichen-Variante (`designOption` der Anfrage): neue Elemente gehören ihr.
|
|
return Ok(crate::design_options::stamp_ops(stamp_renovation(ops, v.get("renovation")), v.get("designOption")));
|
|
}
|
|
if let Some(ops) = crate::cmd_context::ops(root, p, &v) {
|
|
return Ok(ops);
|
|
}
|
|
Ok(match cmd {
|
|
"transform" => crate::cmd_transform::transform_ops(root, p, &parse(&v, cmd)?),
|
|
"align" => crate::cmd_transform::align_ops(root, &ids("ids"), &s("mode")),
|
|
"distribute" => crate::cmd_transform::distribute_ops(root, &ids("ids"), &s("axis")),
|
|
"defblock" => crate::cmd_transform::defblock_ops(root, &parse(&v, cmd)?),
|
|
"paste" => crate::cmd_paste::paste_ops(root, v.get("clip").unwrap_or(&Value::Null), &s("levelId"), &s("stamp")),
|
|
"split" => crate::cmd_split::split_ops(root, &ids("ids"), &s("stamp")),
|
|
"cutSegment" => crate::cmd_split::cut_segment_ops(root, &s("id"), crate::cmd_split::point_arg(&v, "at")),
|
|
other => return Err(format!("Unbekannter Befehl: {other}")),
|
|
})
|
|
}
|
|
|
|
/// Listen, deren neue Elemente den Zeichen-Status bekommen ([271]).
|
|
const RENOVATION_COLLECTIONS: [&str; 10] = ["walls", "openings", "ceilings", "roofs", "stairs", "columns", "beams", "extrudedSolids", "rooms", "railings"];
|
|
|
|
/// [271] Neue Bauteile der Ops (`add` in [`RENOVATION_COLLECTIONS`]) erhalten `renovation` (ein Objekt mit gültigem
|
|
/// `status`), sofern sie keines tragen; sonst unverändert.
|
|
pub fn stamp_renovation(mut ops: Vec<Op>, ren: Option<&Value>) -> Vec<Op> {
|
|
let Some(r) = ren.filter(|r| matches!(r.get("status").and_then(Value::as_str), Some("new" | "demolish" | "existing"))) else { return ops };
|
|
let r = match (r.get("status").and_then(Value::as_str), r.get("phase").and_then(Value::as_str).filter(|x| !x.is_empty())) {
|
|
(Some("existing"), None) => return ops,
|
|
(Some(st), Some(ph)) => serde_json::json!({"status": st, "phase": ph}),
|
|
(Some(st), None) => serde_json::json!({"status": st}),
|
|
_ => return ops,
|
|
};
|
|
for op in ops.iter_mut() {
|
|
if let Op::Add { collection, items, .. } = op {
|
|
let coll = collection.as_str();
|
|
// [271] Bäume: ihr Bestand ([149], `status`) ist der Umbau-Status (Neu = neu, Abbruch = zu fällen).
|
|
if coll == "trees" {
|
|
let tree_status = if r.get("status").and_then(Value::as_str) == Some("demolish") { "remove" } else { "new" };
|
|
for it in items.iter_mut().filter_map(Value::as_object_mut) {
|
|
if it.get("status").and_then(Value::as_str).is_none_or(|s| s == "existing") {
|
|
it.insert("status".into(), Value::String(tree_status.into()));
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
if !RENOVATION_COLLECTIONS.contains(&coll) && coll != "drawings2d" {
|
|
continue;
|
|
}
|
|
for it in items.iter_mut() {
|
|
if let Some(o) = it.as_object_mut() {
|
|
// [271] 2D-Elemente: Möbel/Symbole und Linienwerk; Masse, Texte, Koten, Raster u. ä. sind keine Bauteile.
|
|
if coll == "drawings2d" && !o.get("geom").and_then(|g| g.get("shape")).and_then(Value::as_str).is_some_and(|s| STAMPED_SHAPES.contains(&s)) {
|
|
continue;
|
|
}
|
|
o.entry("renovation").or_insert_with(|| r.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ops
|
|
}
|
|
|
|
/// [271] 2D-Formen, die den Zeichen-Status tragen (Linienwerk, Möbel und Symbole).
|
|
const STAMPED_SHAPES: [&str; 9] = ["line", "polyline", "rect", "circle", "arc", "ellipse", "libobj", "block", "bezier"];
|
|
|
|
/// Wie [`ops`], mit Hinweis-Schlüssel (i18n) für die Hülle; bisher nur Verschieben ([144]: Öffnung an
|
|
/// der Nachbaröffnung angehalten).
|
|
pub fn result(root: &Map<String, Value>, p: &Project, text: &str) -> Result<GripResult, String> {
|
|
let v: Value = serde_json::from_str(text).map_err(|e| format!("cmdOps: {e}"))?;
|
|
if v.get("group").is_none() && v.get("cmd").and_then(Value::as_str) == Some("transform") {
|
|
return Ok(crate::cmd_transform::transform_result(root, p, &parse(&v, "transform")?));
|
|
}
|
|
let ops = ops(root, p, text)?;
|
|
// [190] Verbinden ohne Wirkung: Hinweis mit Grund statt stillem Nichts (Wände: Abstand, parallel, Bogenwand …).
|
|
let (mut notice, mut notice_args) = (None, vec![]);
|
|
if ops.is_empty() && v.get("cmd").and_then(Value::as_str) == Some("join") {
|
|
let ids: Vec<String> = v.get("ids").and_then(|x| serde_json::from_value(x.clone()).ok()).unwrap_or_default();
|
|
let hint = crate::cmd_wall_edit::join_hint(p, &ids);
|
|
let mut parts = hint.as_deref().unwrap_or("cmd.join.nothing").split('|');
|
|
notice = Some(match parts.next() {
|
|
Some("cmd.join.far") => "cmd.join.far",
|
|
Some("cmd.join.locked") => "cmd.join.locked",
|
|
Some("cmd.join.arc") => "cmd.join.arc",
|
|
Some("cmd.join.floors") => "cmd.join.floors",
|
|
Some("cmd.join.met") => "cmd.join.met",
|
|
Some("cmd.join.cross") => "cmd.join.cross",
|
|
Some("cmd.join.parallel") => "cmd.join.parallel",
|
|
_ => "cmd.join.nothing",
|
|
});
|
|
notice_args = parts.map(str::to_owned).collect();
|
|
}
|
|
Ok(GripResult { ops, notice, notice_args })
|
|
}
|
|
|
|
/// Wie [`result`], als Antwort-JSON `{"ops": […], "notice": …}` (Zahlen mit Nicht-endlich-Marken).
|
|
pub fn ops_json(root: &Map<String, Value>, p: &Project, text: &str) -> Result<String, String> {
|
|
Ok(result_json(&result(root, p, text)?))
|
|
}
|
|
|
|
/// Schneller Hasher für id-Mengen der Befehle (FNV-1a; je Mausschritt werden Tausende ids geprüft,
|
|
/// SipHash kostete dort den grössten Teil der Vorschau).
|
|
#[derive(Default)]
|
|
pub struct Fnv(u64);
|
|
|
|
impl std::hash::Hasher for Fnv {
|
|
fn finish(&self) -> u64 {
|
|
self.0
|
|
}
|
|
fn write(&mut self, bytes: &[u8]) {
|
|
let mut h = if self.0 == 0 { 0xcbf2_9ce4_8422_2325 } else { self.0 };
|
|
for b in bytes {
|
|
h ^= *b as u64;
|
|
h = h.wrapping_mul(0x0100_0000_01b3);
|
|
}
|
|
self.0 = h;
|
|
}
|
|
}
|
|
|
|
/// Menge mit [`Fnv`].
|
|
pub type FastSet<T> = std::collections::HashSet<T, std::hash::BuildHasherDefault<Fnv>>;
|