[77] Zauberstab: Griffe an Lochpunkten, Auflösen liefert Lochkanten, Versetzen/Bruch/Verbinden lassen Löcher weg
This commit is contained in:
3 files changed
+91
-6
No files matched your search
@@ -546,6 +546,16 @@ fn polyline_geom(pts: Vec<Value>, closed: Option<Value>, bulge: Option<Vec<Value
|
||||
Value::Object(m)
|
||||
}
|
||||
|
||||
/// Löcher der Quell-Polylinie in die neue Geometrie übernehmen (nur Verrunden/Fasen: Ecken des Aussenrings
|
||||
/// ändern, Löcher bleiben). Offset, Bruch, Teilen, Trimmen, Verbinden bauen neue Ketten und lassen Löcher
|
||||
/// bewusst weg (kein halb passendes Loch an verschobenem Rand).
|
||||
fn keep_holes(src: &Value, g: Value) -> Value {
|
||||
match field(src, "holes") {
|
||||
Some(h) if truthy(Some(h)) => with(&g, "holes", h.clone()),
|
||||
_ => g,
|
||||
}
|
||||
}
|
||||
|
||||
/// Verrunden der Ecke `index` von `id` mit Radius `r` (Bogen als bulge zwischen den Tangentenpunkten).
|
||||
pub fn fillet_ops(root: &Map<String, Value>, id: &str, index: f64, r: f64) -> Vec<Op> {
|
||||
let Some(h) = corner_of(root, id, index) else { return vec![] };
|
||||
@@ -559,14 +569,14 @@ pub fn fillet_ops(root: &Map<String, Value>, id: &str, index: f64, r: f64) -> Ve
|
||||
if h.index < bulge.len() {
|
||||
bulge[h.index] = js_num(b);
|
||||
}
|
||||
patch_geom(root, id, polyline_geom(pts, h.c.closed_raw.clone(), Some(bulge)))
|
||||
patch_geom(root, id, keep_holes(find(root, id).map_or(&Value::Null, geom), polyline_geom(pts, h.c.closed_raw.clone(), Some(bulge))))
|
||||
}
|
||||
|
||||
/// Fasen der Ecke `index` von `id` um `d`.
|
||||
pub fn chamfer_ops(root: &Map<String, Value>, id: &str, index: f64, d: f64) -> Vec<Op> {
|
||||
let Some(h) = corner_of(root, id, index) else { return vec![] };
|
||||
let Some(c) = chamfer_of(&h, d) else { return vec![] };
|
||||
patch_geom(root, id, polyline_geom(split_corner(&h, c.a, c.b), h.c.closed_raw.clone(), None))
|
||||
patch_geom(root, id, keep_holes(find(root, id).map_or(&Value::Null, geom), polyline_geom(split_corner(&h, c.a, c.b), h.c.closed_raw.clone(), None)))
|
||||
}
|
||||
|
||||
/// Vorschau Verrunden/Fasen einer Ecke (`fillet`: Bogen, sonst die Fasung).
|
||||
@@ -830,6 +840,22 @@ fn explode_geom(g: &Value, root: &Map<String, Value>) -> Option<Vec<Value>> {
|
||||
}
|
||||
out.push(Value::Object(m));
|
||||
}
|
||||
// Löcher (nur geschlossen): ihre Kanten als Linien.
|
||||
if segs == n {
|
||||
for h in arr(field(g, "holes")) {
|
||||
let ring = arr(Some(h));
|
||||
if ring.len() < 3 {
|
||||
continue;
|
||||
}
|
||||
for i in 0..ring.len() {
|
||||
let mut m = Map::new();
|
||||
m.insert("shape".into(), "line".into());
|
||||
m.insert("a".into(), ring[i].clone());
|
||||
m.insert("b".into(), ring[(i + 1) % ring.len()].clone());
|
||||
out.push(Value::Object(m));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
"rect" => {
|
||||
@@ -1092,6 +1118,27 @@ mod tests {
|
||||
assert!(g["bulge"][1].as_f64().unwrap().abs() > 0.1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aufloesen_polylinie_mit_loch_liefert_lochkanten() {
|
||||
let pl = json!({"id": "p", "geom": {"shape": "polyline", "closed": true, "pts": [{"x": 0, "y": 0}, {"x": 4, "y": 0}, {"x": 4, "y": 4}, {"x": 0, "y": 4}],
|
||||
"holes": [[{"x": 1, "y": 1}, {"x": 2, "y": 1}, {"x": 2, "y": 2}]]}});
|
||||
let r = root(json!({"drawings2d": [pl]}));
|
||||
let ops = explode_ops(&r, &["p".into()], &mut IdGen::new("s"));
|
||||
assert_eq!(ops.last().unwrap().to_json()["items"].as_array().unwrap().len(), 4 + 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offset_und_verbinden_mit_loch_ohne_panik_loecher_entfallen() {
|
||||
let pl = json!({"id": "p", "geom": {"shape": "polyline", "closed": true, "pts": [{"x": 0, "y": 0}, {"x": 4, "y": 0}, {"x": 4, "y": 4}, {"x": 0, "y": 4}],
|
||||
"holes": [[{"x": 1, "y": 1}, {"x": 2, "y": 1}, {"x": 2, "y": 2}]]}});
|
||||
let r = root(json!({"drawings2d": [pl, line("a", (0.0, 0.0), (1.0, 0.0))]}));
|
||||
let ops = offset_ops(&r, "p", 0.5, &mut IdGen::new("s"));
|
||||
assert_eq!(ops.len(), 1);
|
||||
assert!(ops[0].to_json()["items"][0]["geom"].get("holes").is_none());
|
||||
let _ = join_ops(&r, &["p".into(), "a".into()], &mut IdGen::new("j"));
|
||||
let _ = break_ops(&r, "p", P::new(0.0, 0.0), P::new(2.0, 0.0), &mut IdGen::new("b"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aufloesen_rechteck_an_ort_und_stelle() {
|
||||
let rect = json!({"id": "r", "geom": {"shape": "rect", "min": {"x": 0, "y": 0}, "max": {"x": 1, "y": 1}}});
|
||||
|
||||
@@ -456,6 +456,20 @@ fn drawing_grip(d: &Value, index: f64, x: f64, y: f64, scale: f64) -> Option<Val
|
||||
if index < n as f64 {
|
||||
return set("pts", replace_at(pts, index, &p));
|
||||
}
|
||||
// Loch-Ecken (Index ≥ 2n, geschlossen): nach den Bogen-Griffen, Löcher der Reihe nach.
|
||||
if truthy(field(g, "closed")) && n > 2 && index >= (2 * n) as f64 && field(g, "holes").is_some() {
|
||||
let mut rest = index as usize - 2 * n;
|
||||
let mut holes: Vec<Value> = arr(field(g, "holes")).to_vec();
|
||||
for h in holes.iter_mut() {
|
||||
let ring = arr(Some(&*h));
|
||||
if rest < ring.len() {
|
||||
*h = replace_at(ring, rest as f64, &p);
|
||||
return set("holes", Value::Array(holes));
|
||||
}
|
||||
rest -= ring.len();
|
||||
}
|
||||
return None;
|
||||
}
|
||||
// Bulge-Griff (Index ≥ n): Segment-Mitte senkrecht zur Sehne → Bogen dieses Segments.
|
||||
let seg = index - n as f64;
|
||||
let k = index_in(seg, n)?;
|
||||
@@ -1088,7 +1102,15 @@ pub fn drawing_vertices(g: &Value, scale: f64) -> Option<Vec<(f64, f64)>> {
|
||||
"line" | "xline" | "ray" => vec![p("a"), p("b")],
|
||||
"polyline" => {
|
||||
let bulge = arr(field(g, "bulge"));
|
||||
polyline_vertices(pts("pts"), truthy(field(g, "closed")), |i| num_or(bulge.get(i), 0.0))
|
||||
let closed = truthy(field(g, "closed"));
|
||||
let mut out = polyline_vertices(pts("pts"), closed, |i| num_or(bulge.get(i), 0.0));
|
||||
// Loch-Ecken hinter den Bogen-Griffen (Index ≥ 2n); nur geschlossene Polylinien tragen Löcher.
|
||||
if closed && out.len() >= 6 {
|
||||
for h in arr(field(g, "holes")) {
|
||||
out.extend(arr(Some(h)).iter().map(xy));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
"rect" | "image" => rect_corners(g).to_vec(),
|
||||
"circle" => {
|
||||
@@ -1328,6 +1350,21 @@ mod tests {
|
||||
assert_eq!(crate::ops::ops_to_json(&ops), r#"[{"kind":"add","collection":"ceilingTypes","items":[{"id":"ct-1","name":"Decke","layers":[{"componentId":"c","thickness":0.25}]}]},{"kind":"replace","collection":"ceilings","entity":{"id":"c1","wallTypeId":"wt","ceilingTypeId":"ct-1"}}]"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loch_ecken_haben_griffe() {
|
||||
let pl = |o: bool| json!({"id": "d", "geom": {"shape": "polyline", "closed": o, "pts": [{"x": 0, "y": 0}, {"x": 4, "y": 0}, {"x": 4, "y": 4}, {"x": 0, "y": 4}],
|
||||
"holes": [[{"x": 1, "y": 1}, {"x": 2, "y": 1}, {"x": 2, "y": 2}]]}});
|
||||
let v = drawing_vertices(&pl(true)["geom"], 100.0).unwrap();
|
||||
assert_eq!(v.len(), 4 + 4 + 3);
|
||||
assert_eq!(v[9], (2.0, 1.0));
|
||||
let r = drawing_grip(&pl(true), 9.0, 3.0, 1.5, 100.0).unwrap();
|
||||
assert_eq!((r["geom"]["holes"][0][1]["x"].as_f64(), r["geom"]["holes"][0][1]["y"].as_f64()), (Some(3.0), Some(1.5)));
|
||||
assert_eq!(r["geom"]["pts"].as_array().unwrap().len(), 4);
|
||||
assert!(drawing_grip(&pl(true), 11.0, 0.0, 0.0, 100.0).is_none());
|
||||
// Bestehende Indizes bleiben: Bogen-Griff 4..8.
|
||||
assert!(drawing_grip(&pl(true), 5.0, 2.0, -1.0, 100.0).unwrap()["geom"]["bulge"].is_array());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn griffe_und_kanten() {
|
||||
let g = json!({"shape": "polyline", "closed": true, "pts": [{"x": 0, "y": 0}, {"x": 2, "y": 0}, {"x": 2, "y": 2}], "bulge": [0, 0.5, 0]});
|
||||
|
||||
Reference in new issue
Block a user