Drawing: add sheet reference dimensions without model edits (W38a)

This commit is contained in:
lillith committed 2026-10-28 11:32:32 +01:00
1 parent c474325189
commit 9eccea7ca8
3 files changed
+123 -4

No files matched your search

+71
View File
@@ -1635,3 +1635,74 @@ fn sheet_reviewer_delete_keeps_source_and_refuses_foreign_or_missing_entities()
assert_eq!((s.undo.len(), s.redo.len(), s.evaluations), history, "{command}");
}
}
// W38a R12: drawing-local reference dimensions do not define source parameters.
#[test]
fn sheet_reviewer_reference_dimension_measures_paper_mm_and_tracks_drawing_undo() {
let (mut s, drawing, sketch, _) = sheet_editor();
run(&mut s, "sketch.line", json!({"points": [[10, 20], [50, 20]], "ids": ["measured_line"]}));
let source = s.doc.clone();
let model = s.model.state();
let (steps, evaluations) = (s.undo.len(), s.evaluations);
let measurement = run(&mut s, "sketch.dimension", json!({"entities": ["measured_line"], "driven": true}));
assert_eq!(measurement["param"], Value::Null);
assert_eq!(measurement["driven"], true);
assert_eq!(measurement["unit"], "mm", "paper lengths must not follow inch model display units");
assert!((measurement["measured"].as_f64().unwrap() - 40.0).abs() < 1e-7, "{measurement}");
let after_dimension = s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.clone();
let dimension = after_dimension.constraints.last().unwrap();
assert!(dimension.driven && dimension.param.is_none());
let dimension_id = dimension.id.clone();
assert_eq!((&s.doc.params, &s.doc.features), (&source.params, &source.features));
assert!(Arc::ptr_eq(&s.model.state(), &model));
assert_eq!(s.evaluations, evaluations);
assert_eq!((s.undo.len(), s.undo.last().unwrap().part), (steps + 1, Some(drawing)));
let placed = run(&mut s, "sketch.dimension_text", json!({"dimension": dimension_id, "at": [30, 32]}));
assert_eq!(placed["dimension"], dimension_id);
let with_text = s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.clone();
assert!(with_text.constraints.last().unwrap().text.is_some());
assert_eq!((s.undo.len(), s.undo.last().unwrap().part), (steps + 2, Some(drawing)));
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, after_dimension);
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.constraints.len(), after_dimension.constraints.len() - 1);
run(&mut s, "edit.redo", json!({}));
run(&mut s, "edit.redo", json!({}));
assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, with_text);
let reopened = solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap();
assert_eq!(reopened.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, with_text);
assert_eq!((&s.doc.params, &s.doc.features), (&source.params, &source.features));
assert!(Arc::ptr_eq(&s.model.state(), &model));
assert_eq!(s.evaluations, evaluations);
}
#[test]
fn sheet_reviewer_reference_dimensions_refuse_driving_values_and_wrong_owners_atomically() {
let (mut s, drawing, sketch, _) = sheet_editor();
run(&mut s, "sketch.line", json!({"points": [[10, 20], [50, 20]], "ids": ["measured_line"]}));
let before = s.doc.clone();
let model = s.model.state();
let entry = s.active_sheet_sketch.clone();
let history = (s.undo.len(), s.redo.len(), s.evaluations);
for (command, args) in [
("sketch.dimension", json!({"entities": ["measured_line"], "value": 20})),
("sketch.dimension", json!({"entities": ["measured_line"], "value": "Width = 20"})),
("sketch.dimension", json!({"entities": ["measured_line"], "driven": true, "value": "20 mm"})),
("sketch.dimension", json!({"entities": ["measured_line"], "driven": true, "scale_sketch": true})),
("sketch.dimension", json!({"entities": ["measured_line"], "driven": true, "sketch": -1})),
("sketch.dimension", json!({"entities": ["unknown"], "driven": true})),
("sketch.dimension_text", json!({"dimension": "missing", "at": [0, 0]})),
("sketch.dimension_text", json!({"sketch": -1, "dimension": "missing", "at": [0, 0]})),
] {
refused(&mut s, command, args.clone());
assert!(Arc::ptr_eq(&s.doc, &before), "{command} {args}");
assert!(Arc::ptr_eq(&s.model.state(), &model), "{command} {args}");
assert_eq!(s.active_sheet_sketch, entry, "{command} {args}");
assert_eq!((s.undo.len(), s.redo.len(), s.evaluations), history, "{command} {args}");
assert_eq!(s.open_drawing, Some(drawing));
assert!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.constraints.is_empty());
}
}
+27 -4
View File
@@ -132,8 +132,8 @@ pub static COMMANDS: &[CommandSpec] = &[
.enabled(in_sketch)
.params(concat!("p0, p1: overall ends, width", connect_doc!(), created_doc!())),
CommandSpec::new("sketch.point", "Point", draw_point).in_sketch_part().at("SKETCH", "CREATE").icon("point").enabled(in_sketch).params(concat!("point: [x,y] (each a number or an expression over the design's parameters: the point is then fixed where it comes to and follows them), id?", connect_doc!(), created_doc!())),
CommandSpec::new("sketch.dimension", "Sketch Dimension", dimension).at("SKETCH", "CREATE").icon("dimension").key("D").enabled(in_sketch).params(
"entities: [refs], type?: auto|distance|horizontal|vertical|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",
CommandSpec::new("sketch.dimension", "Sketch Dimension", dimension).in_sketch_part().at("SKETCH", "CREATE").icon("dimension").key("D").enabled(in_sketch).params(
"entities: [refs], type?: auto|distance|horizontal|vertical|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled. On sheet sketches only driven: true reference dimensions without a value are supported: they measure paper millimetres or degrees, never add source-model parameters; driving values and scale_sketch are explicitly refused",
),
CommandSpec::new("sketch.constraint.horizontal_vertical", "Horizontal/Vertical", c_horizontal_vertical)
.in_sketch_part()
@@ -217,7 +217,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("sketch.drag_end", "End Sketch Drag", drag_end).icon("sketch_move").enabled(in_sketch).doc_only().params(
"constraint?: {command: a sketch.constraint.* id, params}: ends a drag made of `sketch.drag` `live: true` frames: adds the constraint it let go on (not kept if it over-constrains the sketch), then evaluates the whole model once; returns the constraint's result and the sketch's solved state and degrees of freedom",
),
CommandSpec::new("sketch.dimension_text", "Move Dimension Text", dimension_text).icon("edit")
CommandSpec::new("sketch.dimension_text", "Move Dimension Text", dimension_text).in_sketch_part().icon("edit")
.enabled(in_sketch)
.params("dimension: constraint id or parameter name; at: [x,y] text centre (sketch coordinates) | reset: true (default place)"),
CommandSpec::new("sketch.options", "Sketch Palette Options", sketch_options).icon("sketch").params(
@@ -2127,6 +2127,19 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {
let value = expr(p, "value");
let text_at = p.get("text_at").and_then(vec2);
let driven = bool_(p, "driven").unwrap_or(false);
// Sheet sketches may measure geometry without changing source parameters. Their
// driving dimensions need a drawing-owned value/parameter contract (W38a),
// so do not reuse the model command's parameter creation path.
let on_sheet = s.open_drawing.is_some();
if on_sheet && !driven {
return Err(bad(cmd, "sheet sketch driving dimensions are not supported yet; use `driven: true` for a reference dimension"));
}
if on_sheet && value.is_some() {
return Err(bad(cmd, "a sheet sketch reference dimension cannot define or take a value"));
}
if on_sheet && bool_(p, "scale_sketch").unwrap_or(false) {
return Err(bad(cmd, "scaling a sheet sketch through a reference dimension is not supported"));
}
// `name = expression` defines the user parameter `name` and dimensions with it (#2916).
let (defines, value) = match value.as_ref().and_then(|v| solidcraft_doc::expr::definition(v.as_str())) {
Some((name, rhs)) => {
@@ -2353,7 +2366,17 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {
Ok((pname, name, measured(is_angle, cur)))
})?;
// Report what the dimension measures once the sketch is solved, not the geometry before.
let current = solved_measure(s, p, cmd).unwrap_or(current);
// The model-only lookup cannot inspect the drawing-owned sketch.
let current = if on_sheet {
let entry = sketch_store::sheet_target(s, p, cmd)?;
let owner = s.drawing(cmd)?;
let sk = &owner.sheet_sketch(entry.sketch).ok_or_else(|| bad(cmd, "the edited sheet sketch no longer exists"))?.sketch;
let latest = sk.constraints.last().ok_or_else(|| bad(cmd, "the sheet dimension was not stored"))?;
let value = solidcraft_sketch::measure_dimension(sk, &latest.kind).ok_or_else(|| bad(cmd, "could not measure the sheet dimension"))?;
measured(latest.kind.is_angle(), value)
} else {
solved_measure(s, p, cmd).unwrap_or(current)
};
if driven {
return Ok(json!({"param": Value::Null, "driven": true, "type": kind_name, "measured": current.0, "unit": current.1, "sketch": info}));
}
+25
View File
@@ -409,3 +409,28 @@ sketch selector and malformed edits fail without a partial publication.
These commands extend the **command-driven** subset, not an independently accepted
native/browser interactive editor. Dimension constraints and annotations attached to
drawing views are separate responsibilities; the latter belong to W19 (#4896).
### W38a R12: reference-only dimensions on paper
The existing `sketch.dimension` command now accepts **reference (driven) dimensions**
on the active, exactly owned sheet sketch. Call it with `driven: true` and a
sheet-local line, circle, arc or point pair in `entities`. It uses the same
sketch constraint builder and solver, reports the stored solved measurement in
physical paper millimetres or degrees, and publishes through the one
`Session::edit_drawing` boundary. The existing `sketch.dimension_text`
command places or resets the reference dimension label in paper coordinates,
using the same drawing-local undo/redo history.
The containing model is **read-only**: reference dimensions have no model
parameter binding, never create named or generated source parameters, and
cannot evaluate or edit model features. A sheet request that supplies a driving
dimension, a dimension `value`, a parameter definition, or `scale_sketch: true`
is refused explicitly instead of passing through the model-only parameter path.
An incorrect drawing/sketch owner or unknown local entity is refused atomically.
Original engine cases cover the 40 mm paper-length measurement when the source
design displays inches, label placement, drawing-only undo/redo and JSON reopen,
source-model identity, and wrong-owner/unsupported-value refusals. A reference
constraint is not a driving sheet-sketch dimension definition, and it is not a
drawing-view annotation (W19/#4896). No new solver, schema, locale, scene,
desktop/browser interactive tool, or full-CI qualification is claimed here.