[172] Rundmasse: Masszahl-Griff rastet ein (Mitte, innen/aussen, über/unter), Alt = frei, Versatz im Plan

This commit is contained in:
Claude committed 2026-11-10 12:55:41 +00:00
1 parent a1f90f8deb
commit 519806a3a8
3 files changed
+154 -20

No files matched your search

+92 -2
View File
@@ -490,13 +490,44 @@ fn dim_height(g: &Value, scale: f64) -> f64 {
mm_to_model(num_or(field(g, "textHeightMm"), DIM_TEXT_HEIGHT_MM), scale)
}
/// [172] Masszahl eines Radius-/Durchmesser-/Bogenlängenmasses: (Art, Rahmen). Mitte der Zahl bei Versatz 0 =
/// Anker + Querachse · 0.6 h; Leserichtung/Querachse wie im Plan (`plan::drawings::round_label`).
fn round_dim_label(g: &Value) -> Option<(crate::plan::drawings::RoundLabel, (f64, f64), (f64, f64))> {
use crate::model::DimKind;
let kind = match field(g, "kind").and_then(Value::as_str)? {
"radius" => DimKind::Radius,
"diameter" => DimKind::Diameter,
"arc" => DimKind::Arc,
_ => return None,
};
let pts: Vec<crate::model::Vec2> = arr(field(g, "pts")).iter().map(|q| xy(q)).map(|q| crate::model::Vec2 { x: q.0, y: q.1 }).collect();
let l = crate::plan::drawings::round_label(kind, &pts, num_of(field(g, "offset")))?;
let (read, up) = ((jsnum::cos(l.angle), jsnum::sin(l.angle)), (-jsnum::sin(l.angle), jsnum::cos(l.angle)));
Some((l, read, up))
}
/// [172] Lage des Masszahl-Griffs eines Rundmasses (Modell-Meter), samt gespeichertem Versatz.
fn round_dim_text_grip(g: &Value, scale: f64) -> Option<(f64, f64)> {
let (l, read, up) = round_dim_label(g)?;
let h = dim_height(g, scale);
let o = arr(field(g, "textOffsetsMm")).first().cloned().unwrap_or(Value::Null);
let (along, perp) = (mm_to_model(num_or(field(&o, "along"), 0.0), scale), mm_to_model(num_or(field(&o, "perp"), 0.0), scale));
let q = (l.at.x + up.0 * (0.6 * h + perp) + read.0 * along, l.at.y + up.1 * (0.6 * h + perp) + read.1 * along);
(q.0.is_finite() && q.1.is_finite()).then_some(q)
}
/// [172] Einrast-Stellen der Masszahl eines Segments der Länge `len` (Modell-Meter, Schrifthöhe `h`) als (entlang,
/// quer) ab der Mitte über dem Massstrich: Mitte (Vorgabe), innen links/rechts (Zahl bündig an der Hilfslinie),
/// aussen links/rechts, eine Zeile über bzw. unter der Linie. Nächste Stelle zu (`along`, `perp`); passt die Zahl
/// nicht zwischen die Hilfslinien, entfallen Mitte und innen. Mitte = (0, 0) = Vorgabe (läuft über `dim_auto_offsets`).
fn dim_text_slot(len: f64, h: f64, along: f64, perp: f64) -> (f64, f64) {
use crate::plan::drawings::{dim_text_below, dim_text_pad, dim_text_row, dim_text_width};
let (w, pad, row) = (dim_text_width(len, h), dim_text_pad(h), dim_text_row(h));
dim_text_slot_w(len, crate::plan::drawings::dim_text_width(len, h), h, along, perp)
}
/// Wie [`dim_text_slot`] mit gegebener Breite `w` der Zahl (Rundmasse tragen ein Präfix: „R 2.00“).
fn dim_text_slot_w(len: f64, w: f64, h: f64, along: f64, perp: f64) -> (f64, f64) {
use crate::plan::drawings::{dim_text_below, dim_text_pad, dim_text_row};
let (pad, row) = (dim_text_pad(h), dim_text_row(h));
let fits = w + 2.0 * pad <= len;
let (inner, outer) = (len / 2.0 - w / 2.0 - pad, len / 2.0 + w / 2.0 + pad);
let mut slots = vec![(-outer, 0.0), (outer, 0.0), (0.0, row), (0.0, dim_text_below(h))];
@@ -566,6 +597,17 @@ fn drawing_grip(d: &Value, index: f64, x: f64, y: f64, scale: f64, free: bool) -
let ng = with(g, "pts", replace_at(pts, index, &q));
return Some(with_geom(d, crate::dim_assoc::without_ref(&ng, index_in(index, n))));
}
// Masszahl-Griff eines Rundmasses ([172]): rastet wie bei linearen Massen ein (Alt = frei).
if matches!(field(g, "kind").and_then(Value::as_str), Some("radius" | "diameter" | "arc")) && index == n as f64 + 1.0 {
let (l, read, up) = round_dim_label(g)?;
let h = dim_height(g, scale);
let (vx, vy) = (x - l.at.x, y - l.at.y);
let (along, perp) = (vx * read.0 + vy * read.1, vx * up.0 + vy * up.1 - 0.6 * h);
let w = crate::text_layout::text_width_em(&l.text, false) * h;
let (along, perp) = if free { (along, perp) } else { dim_text_slot_w(l.len, w, h, along, perp) };
let next = serde_json::json!({"along": js_num(model_to_mm(along, scale)), "perp": js_num(model_to_mm(perp, scale))});
return set("textOffsetsMm", Value::Array(vec![next]));
}
if index < n as f64 {
// Von Hand gezogener Punkt ist frei ([38]).
let ng = with(g, "pts", replace_at(pts, index, &p));
@@ -1219,6 +1261,11 @@ pub fn drawing_vertices(g: &Value, scale: f64) -> Option<Vec<(f64, f64)>> {
let offset = num_of(field(g, "offset"));
let (nx, ny) = ((-dy / len) * offset, (dx / len) * offset);
out.push(((a.0 + b.0) / 2.0 + nx, (a.1 + b.1) / 2.0 + ny));
// Rundmass ([172]): ein Masszahl-Griff an der Zahl selbst.
if let Some(q) = round_dim_text_grip(g, scale) {
out.push(q);
return Some(out);
}
let h = dim_height(g, scale);
let offs = arr(field(g, "textOffsetsMm"));
// [172] Ohne gespeicherte Abweichung steht der Griff dort, wo der Plan die Zahl zeichnet.
@@ -1387,6 +1434,49 @@ mod tests {
assert!(res.ops[0].to_json()["entity"].get("bulge").is_none());
}
/// [172] Masszahl-Griff eines Radius-/Durchmesser-/Bogenmasses: Lage an der Zahl, rastet ein, Alt = frei.
#[test]
fn rundmass_masszahl_griff_rastet_ein() {
let h = 0.25;
let mk = |kind: &str, pts: Value| json!({"id": "d", "level": "L", "geom": {"shape": "dimension", "kind": kind, "pts": pts, "offset": 0.0}});
// Radius 2: Zahl mittig über der Strecke, Griff genau dort (Index = n + 1).
let d = mk("radius", json!([{"x": 0, "y": 0}, {"x": 2, "y": 0}]));
let v = drawing_vertices(d.get("geom").unwrap(), 100.0).unwrap();
assert_eq!(v.len(), 4, "{v:?}");
assert!((v[3].0 - 1.0).abs() < 1e-9 && (v[3].1 - 0.6 * h).abs() < 1e-9, "{:?}", v[3]);
let r = root(json!({"drawings2d": [d]}));
let run_at = |x: f64, y: f64, free: bool| {
let res = run(&Lookup::plain(&r), &rq(json!({"op": "moveGrip", "drawingId": "d", "index": 3, "pt": [x, y], "scale": 100, "free": free}))).unwrap();
let o = entity(&res)["geom"]["textOffsetsMm"][0].clone();
(o["along"].as_f64().unwrap(), o["perp"].as_f64().unwrap())
};
// Nahe der Mitte: Vorgabe (0, 0).
assert_eq!(run_at(1.05, 0.17, false), (0.0, 0.0));
// Deutlich darüber: eine Zeile höher (1.35 h = 3.375 mm Papier bei 1:100 und h = 2.5 mm).
let (al, pe) = run_at(1.0, 0.15 + 0.4, false);
assert!(al.abs() < 1e-9 && (pe - 3.375).abs() < 1e-6, "{al} {pe}");
// Weit rechts: aussen rechts, bündig neben dem Strecken-Ende.
let (al, pe) = run_at(4.0, 0.15, false);
assert!(al > 0.0 && pe == 0.0, "{al} {pe}");
// Alt: frei, ohne Einrasten.
let (al, pe) = run_at(1.3, 0.35, true);
assert!((al - 3.0).abs() < 1e-6 && (pe - 2.0).abs() < 1e-6, "{al} {pe}");
// Griff steht danach an der verschobenen Zahl.
let moved = json!({"id": "d", "level": "L", "geom": {"shape": "dimension", "kind": "radius", "pts": [{"x": 0, "y": 0}, {"x": 2, "y": 0}], "offset": 0.0, "textOffsetsMm": [{"along": 3, "perp": 2}]}});
let v = drawing_vertices(moved.get("geom").unwrap(), 100.0).unwrap();
assert!((v[3].0 - 1.3).abs() < 1e-9 && (v[3].1 - 0.35).abs() < 1e-9, "{:?}", v[3]);
// Durchmesser: Anker im Mittelpunkt; Bogenlänge: genau ein Text-Griff (Index 4).
let dm = mk("diameter", json!([{"x": 0, "y": 0}, {"x": 2, "y": 0}]));
let v = drawing_vertices(dm.get("geom").unwrap(), 100.0).unwrap();
assert!(v[3].0.abs() < 1e-9, "{v:?}");
let ar = mk("arc", json!([{"x": 0, "y": 0}, {"x": 2, "y": 0}, {"x": 0, "y": 2}]));
let v = drawing_vertices(ar.get("geom").unwrap(), 100.0).unwrap();
assert_eq!(v.len(), 5, "{v:?}");
let r = root(json!({"drawings2d": [ar]}));
let res = run(&Lookup::plain(&r), &rq(json!({"op": "moveGrip", "drawingId": "d", "index": 4, "pt": [1.4, 1.4], "scale": 100}))).unwrap();
assert!(entity(&res)["geom"]["textOffsetsMm"][0].is_object());
}
/// [T2] Bogenlängen-/Radiusmass: Randpunkte-Griffe bleiben auf dem Kreis um den Mittelpunkt.
#[test]
fn bogenmass_griff_auf_dem_kreis() {
+61 -17
View File
@@ -792,7 +792,8 @@ fn emit_inner(c: &Ctx, d: &Drawing2D, attrs: Attrs, geom: &Drawing2DGeom, greyed
line_weight: line_weight_mm.unwrap_or(em.weight),
ext_weight: ext_weight_mm.unwrap_or(em.weight),
tick_weight: tick_weight_mm.unwrap_or(em.weight * 2.0),
text_offset: (0.0, 0.0),
// [172] Masszahl-Griff: gespeicherter Versatz (Papier-mm) wie bei linearen Massen.
text_offset: text_offsets_mm.as_ref().and_then(|l| l.first()).map_or((0.0, 0.0), |o| (mm(o.along), mm(o.perp))),
};
dimension_round(&mut em, pts, *offset, *k, &st);
}
@@ -991,6 +992,41 @@ pub fn dim_text_frame(a: Vec2, b: Vec2, offset: f64, text_height: f64) -> (Vec2,
(v(mid.x + up.x * text_height * 0.6, mid.y + up.y * text_height * 0.6), read, up, angle)
}
/// [172] Lage der Masszahl eines Radius-/Durchmesser-/Bogenlängenmasses: `at` = Ankerpunkt auf der Masslinie
/// (Radius: Mitte der Strecke, Durchmesser: Mittelpunkt, Bogen: Bogenmitte auf der Masslinie), `angle` = Leserichtung
/// (nie kopfüber), `len` = Länge der Masslinie (für die Einrast-Stellen des Masszahl-Griffs), `text` = die Zahl.
pub struct RoundLabel {
pub at: Vec2,
pub angle: f64,
pub len: f64,
pub text: String,
}
pub fn round_label(kind: DimKind, pts: &[Vec2], offset: f64) -> Option<RoundLabel> {
let (&c, &p) = (pts.first()?, pts.get(1)?);
let r = hypot2(p.x - c.x, p.y - c.y);
if !(r > 1e-6) {
return None;
}
let upright = |a: f64| if jsnum::cos(a) < 0.0 { a + std::f64::consts::PI } else { a };
let ang = jsnum::atan2(p.y - c.y, p.x - c.x);
Some(match kind {
DimKind::Radius => RoundLabel { at: v((c.x + p.x) / 2.0, (c.y + p.y) / 2.0), angle: upright(ang), len: r, text: format!("R {}", to_fixed(r, 2)) },
DimKind::Diameter => RoundLabel { at: c, angle: upright(ang), len: 2.0 * r, text: format!("\u{d8} {}", to_fixed(2.0 * r, 2)) },
DimKind::Arc => {
let p1 = *pts.get(2)?;
let a1 = jsnum::atan2(p1.y - c.y, p1.x - c.x);
let sweep = (a1 - ang).rem_euclid(std::f64::consts::TAU);
let rr = r + offset;
if !(rr > 1e-6) || !(sweep > 1e-9) {
return None;
}
let am = ang + sweep / 2.0;
RoundLabel { at: v(c.x + jsnum::cos(am) * rr, c.y + jsnum::sin(am) * rr), angle: upright(am + std::f64::consts::FRAC_PI_2), len: rr * sweep, text: format!("L {}", to_fixed(r * sweep, 2)) }
}
})
}
/// [172] Textanfang so, dass die Zahl `s` (Schrifthöhe `h`) um `mid` zentriert steht: Texte hängen an ihrem Anfang
/// (linke Grundlinie), die Masszahl aber soll mittig über dem Massstrich stehen.
fn centered(mid: Vec2, read: Vec2, s: &str, h: f64) -> Vec2 {
@@ -1223,16 +1259,15 @@ fn dimension_round(em: &mut Em, pts: &[Vec2], offset: f64, kind: DimKind, st: &D
let h = st.tick_len / 2.0;
em.line(v(q.x - dx * h, q.y - dy * h), v(q.x + dx * h, q.y + dy * h), st.tick_weight, None, 0);
};
let label = |em: &mut Em, at: Vec2, ang: f64, s: String| {
let mut a = ang;
if jsnum::cos(a) < 0.0 {
a += std::f64::consts::PI;
}
let up = v(-jsnum::sin(a), jsnum::cos(a));
let q = v(at.x + up.x * st.text_height * 0.6, at.y + up.y * st.text_height * 0.6);
// [172] Mittig über dem Ankerpunkt, nicht mit dem Anfang daran hängend.
let q = centered(q, v(jsnum::cos(a), jsnum::sin(a)), &s, st.text_height);
let pr = text(q, s, st.text_height, a, em.color, em.greyed, Some(em.id));
// [172] Masszahl: Anker, Winkel und Text aus `round_label`, dazu der Versatz (entlang, quer) des Einrast-Griffs.
let label = |em: &mut Em| {
let Some(l) = round_label(kind, pts, offset) else { return };
let (read, up) = (v(jsnum::cos(l.angle), jsnum::sin(l.angle)), v(-jsnum::sin(l.angle), jsnum::cos(l.angle)));
let (al, pe) = st.text_offset;
let q = v(l.at.x + up.x * (st.text_height * 0.6 + pe) + read.x * al, l.at.y + up.y * (st.text_height * 0.6 + pe) + read.y * al);
// Mittig über dem Ankerpunkt, nicht mit dem Anfang daran hängend.
let q = centered(q, read, &l.text, st.text_height);
let pr = text(q, l.text, st.text_height, l.angle, em.color, em.greyed, Some(em.id));
em.sink.push(pr, 0);
};
let (dash, dref) = (em.dash.clone(), em.dash_ref);
@@ -1242,15 +1277,14 @@ fn dimension_round(em: &mut Em, pts: &[Vec2], offset: f64, kind: DimKind, st: &D
em.line(c, p, st.line_weight, dash, dref);
tick(em, p, ux, uy);
tick(em, c, ux, uy);
let mid = v((c.x + p.x) / 2.0, (c.y + p.y) / 2.0);
label(em, mid, jsnum::atan2(uy, ux), format!("R {}", to_fixed(r, 2)));
label(em);
}
DimKind::Diameter => {
let q = v(c.x - ux * r, c.y - uy * r);
em.line(q, p, st.line_weight, dash, dref);
tick(em, p, ux, uy);
tick(em, q, ux, uy);
label(em, c, jsnum::atan2(uy, ux), format!("\u{d8} {}", to_fixed(2.0 * r, 2)));
label(em);
}
DimKind::Arc => {
let Some(&p1) = pts.get(2) else { return };
@@ -1271,9 +1305,7 @@ fn dimension_round(em: &mut Em, pts: &[Vec2], offset: f64, kind: DimKind, st: &D
Primitive::DrawingArc { center: c, r: rr, a0, a1: a0 + sweep, stroke: em.color.into(), weight_mm: st.line_weight, dash: Some(dash.map(|x| x.to_vec())), drawing_id: Some(em.id.into()), greyed: Some(em.greyed), opacity: None },
if dash.is_some() { dref } else { 0 },
);
let am = a0 + sweep / 2.0;
let at = v(c.x + jsnum::cos(am) * rr, c.y + jsnum::sin(am) * rr);
label(em, at, am + std::f64::consts::FRAC_PI_2, format!("L {}", to_fixed(r * sweep, 2)));
label(em);
}
}
}
@@ -2330,6 +2362,18 @@ mod tests {
assert!((m.x - m.y).abs() < 0.2 * h + 1e-9, "{m:?}");
}
/// [172] Rundmass: gespeicherter Masszahl-Versatz (Papier-mm) verschiebt die Zahl in Leserichtung und quer.
#[test]
fn rundmass_masszahl_versatz() {
let p = project(serde_json::json!({}));
let at = |offs: serde_json::Value| -> Vec2 {
let g = serde_json::json!({"shape": "dimension", "kind": "radius", "pts": [{"x": 0, "y": 0}, {"x": 2, "y": 0}], "offset": 0.0, "textOffsetsMm": offs});
render(&p, drawing(g), 100.0).iter().find_map(|pr| if let Primitive::DrawingText { at, .. } = pr { Some(*at) } else { None }).expect("Text")
};
let (a, b) = (at(serde_json::json!([])), at(serde_json::json!([{"along": 3.0, "perp": 2.0}])));
assert!((b.x - a.x - 0.3).abs() < 1e-9 && (b.y - a.y - 0.2).abs() < 1e-9, "{a:?} {b:?}");
}
/// [172] Gespeicherte Abweichung (frei verschobener Text alter Projekte) bleibt, wo sie ist; (0,0) = Vorgabe.
#[test]
fn masszahl_gespeicherte_abweichung_bleibt() {
+1 -1
View File
@@ -1282,7 +1282,7 @@ export function useGripEditing(opts: {
gripFreeRef.current = !!mods.alt;
if (owner?.drawingId) {
const od = project.drawings2d.find((x) => x.id === owner.drawingId);
if (od?.geom.shape === "dimension" && !od.geom.kind && owner.localIndex > od.geom.pts.length) {
if (od?.geom.shape === "dimension" && owner.localIndex > od.geom.pts.length) {
applyGrip(index, raw);
setDraft({ preview: gripPreviewShapes(), vertices: [], snap: null, hud: hudFor(index, raw) });
return;