diff --git a/crates/engine/src/cmd/drawing_tests.rs b/crates/engine/src/cmd/drawing_tests.rs index 6b27211f8..2ae026793 100644 --- a/crates/engine/src/cmd/drawing_tests.rs +++ b/crates/engine/src/cmd/drawing_tests.rs @@ -1706,3 +1706,102 @@ fn sheet_reviewer_reference_dimensions_refuse_driving_values_and_wrong_owners_at assert!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.constraints.is_empty()); } } + +// W38a R13: the existing Coincident command reads and edits the exact sheet sketch. +#[test] +fn sheet_reviewer_coincident_points_are_owned_idempotent_and_undoable() { + let (mut s, drawing, sketch, _) = sheet_editor(); + run(&mut s, "sketch.point", json!({"point": [15, 20], "id": "pin_a", "connect": false})); + run(&mut s, "sketch.point", json!({"point": [70, 45], "id": "pin_b", "connect": false})); + let source = s.doc.clone(); + let model = s.model.state(); + let evaluations = s.evaluations; + let steps = s.undo.len(); + let before = s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.clone(); + + let added = run(&mut s, "sketch.constraint.coincident", json!({"a": "pin_a", "b": "pin_b"})); + let after = s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.clone(); + let a = after.resolve_point("pin_a").unwrap(); + let b = after.resolve_point("pin_b").unwrap(); + assert!(after.point(a).unwrap().dist(after.point(b).unwrap()) < 1e-7, "{added}"); + assert_eq!(after.constraints.len(), before.constraints.len() + 1); + assert_eq!((s.undo.len(), s.undo.last().unwrap().part), (steps + 1, Some(drawing))); + let published = s.doc.clone(); + + let again = run(&mut s, "sketch.constraint.coincident", json!({"a": "pin_b", "b": "pin_a"})); + assert_eq!(again["existing"], true); + assert_eq!(again["constraint"], added["constraint"]); + assert!(Arc::ptr_eq(&s.doc, &published), "an existing constraint is a read-only result"); + assert_eq!(s.undo.len(), steps + 1); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, before); + run(&mut s, "edit.redo", json!({})); + assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, after); + let reopened = solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap(); + assert_eq!(reopened.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, after); + assert_eq!((&s.doc.features, &s.doc.params), (&source.features, &source.params)); + assert!(Arc::ptr_eq(&s.model.state(), &model)); + assert_eq!(s.evaluations, evaluations); +} + +#[test] +fn sheet_reviewer_coincident_curve_preflight_refuses_foreign_targets_atomically() { + let (mut s, drawing, sketch, _) = sheet_editor(); + run(&mut s, "sketch.line", json!({"points": [[10, 20], [50, 20]], "ids": ["wall"], "connect": false})); + run(&mut s, "sketch.point", json!({"point": [35, 40], "id": "peg", "connect": false})); + 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 params in [ + json!({"sketch": -1, "a": "peg", "b": "wall"}), + json!({"sketch": ids::encode(drawing), "a": "peg", "b": "wall"}), + json!({"a": "not_here", "b": "wall"}), + json!({"a": "peg", "b": "missing_curve"}), + json!({"a": "wall", "b": "wall"}), + ] { + refused(&mut s, "sketch.constraint.coincident", params.clone()); + assert!(Arc::ptr_eq(&s.doc, &before), "{params}"); + assert!(Arc::ptr_eq(&s.model.state(), &model), "{params}"); + assert_eq!(s.active_sheet_sketch, entry, "{params}"); + assert_eq!((s.undo.len(), s.redo.len(), s.evaluations), history, "{params}"); + } + let applied = run(&mut s, "sketch.constraint.coincident", json!({"a": "wall", "b": "peg"})); + let after = s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.clone(); + assert_eq!(applied["constraint"], after.constraints.last().unwrap().id); + assert!(matches!(&after.constraints.last().unwrap().kind, solidcraft_sketch::ConstraintKind::PointOnCurve { .. })); + assert_eq!(s.undo.last().unwrap().part, Some(drawing)); + assert_eq!((&s.doc.features, &s.doc.params), (&before.features, &before.params)); + assert!(Arc::ptr_eq(&s.model.state(), &model)); + assert_eq!(s.evaluations, history.2); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.constraints.len(), 0); + run(&mut s, "edit.redo", json!({})); + assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, after); +} + +#[test] +fn sheet_reviewer_coincident_lines_join_only_their_local_endpoints() { + let (mut s, drawing, sketch, _) = sheet_editor(); + run(&mut s, "sketch.line", json!({"points": [[20, 20], [60, 20]], "ids": ["first"], "connect": false})); + run(&mut s, "sketch.line", json!({"points": [[70, 25], [70, 55]], "ids": ["second"], "connect": false})); + let before = s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.clone(); + let model = s.model.state(); + let source = s.doc.clone(); + let steps = s.undo.len(); + let applied = run(&mut s, "sketch.constraint.coincident", json!({"a": "first", "b": "second"})); + let after = s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch.clone(); + let matched = after.constraints.last().unwrap(); + assert_eq!(applied["constraint"], matched.id); + let solidcraft_sketch::ConstraintKind::Coincident { p, q } = &matched.kind else { + panic!("joining two lines must join their nearest endpoints"); + }; + assert!(after.point(*p).unwrap().dist(after.point(*q).unwrap()) < 1e-7); + assert_eq!((s.undo.len(), s.undo.last().unwrap().part), (steps + 1, Some(drawing))); + assert_eq!((&s.doc.features, &s.doc.params), (&source.features, &source.params)); + assert!(Arc::ptr_eq(&s.model.state(), &model)); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, before); + run(&mut s, "edit.redo", json!({})); + assert_eq!(s.doc.drawing(drawing).unwrap().sheet_sketch(sketch).unwrap().sketch, after); +} diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 9d5389b11..69790f728 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -142,6 +142,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .enabled(in_sketch) .params("line: id | points: [a, b] (point ids or {midpoint: line_id}; accepted midpoints reuse or create a point with a Midpoint constraint); mode?: horizontal|vertical"), CommandSpec::new("sketch.constraint.coincident", "Coincident", c_coincident) + .in_sketch_part() .at("SKETCH", "CONSTRAINTS") .icon("c_coincident") .enabled(in_sketch) @@ -1759,9 +1760,9 @@ fn c_horizontal_vertical(s: &mut Session, p: &Value) -> Result { fn c_coincident(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.constraint.coincident"; // Already tied (e.g. connected when it was drawn there): nothing to add. - let sk = sketch_store::read(&s.doc, sketch_store::target(s, p, cmd)?)?; + let sk = sketch_store::read_for_command(s, p, cmd)?; let pt = |k: &str| p.get(k).and_then(Value::as_str).and_then(|r| sk.resolve_point(r)); - let line_points = coincident_line_points(sk, p, cmd)?; + let line_points = coincident_line_points(&sk, p, cmd)?; if let Some((a, b)) = line_points.or_else(|| pt("a").zip(pt("b"))) && let Some(c) = sk.constraints.iter().find(|c| matches!(c.kind, ConstraintKind::Coincident { p, q } if (p, q) == (a, b) || (p, q) == (b, a))) { diff --git a/crates/engine/src/cmd/sketch_store.rs b/crates/engine/src/cmd/sketch_store.rs index a9757d831..a5e48a32e 100644 --- a/crates/engine/src/cmd/sketch_store.rs +++ b/crates/engine/src/cmd/sketch_store.rs @@ -43,6 +43,17 @@ pub(super) fn read(doc: &Document, id: u64) -> Result<&Sketch> { Ok(doc.sketch(id)?) } +/// Read the selected sketch for a command that needs an owned preflight before editing. +/// Sheet identities are resolved against the open Drawing, never the model feature ids; +/// resolving paper expressions and solving here does not publish either document. +pub(super) fn read_for_command(s: &Session, p: &Value, cmd: &str) -> Result { + if s.open_drawing.is_some() { + let entry = sheet_target(s, p, cmd)?; + return Ok(s.drawing(cmd)?.solve_sheet_sketch(&s.doc, entry.sketch)?.sketch); + } + Ok(read(&s.doc, target(s, p, cmd)?)?.clone()) +} + /// Keep `sk` as sketch `id`. pub(super) fn write(doc: &mut Document, id: u64, sk: Sketch) -> Result<()> { *doc.sketch_mut(id)? = sk; diff --git a/docs/drawing-sheet-sketches.md b/docs/drawing-sheet-sketches.md index b91911332..30b278296 100644 --- a/docs/drawing-sheet-sketches.md +++ b/docs/drawing-sheet-sketches.md @@ -434,3 +434,29 @@ 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. + +### W38a R13: shared Coincident constraint on a sheet + +The existing `sketch.constraint.coincident` command now admits an active sheet +sketch, without adding another sketcher or constraint algorithm. Its initial +duplicate/endpoint preflight reads a solved candidate through +`sketch_store::read_for_command`, resolving the exact drawing, sheet and sketch +instead of looking up a model feature by the same local id. The same helper +continues to resolve and read model sketches by their existing rules when no +drawing is open. + +Point-to-point coincidence, a point on a curve (in either pick order) and the +closest endpoints of two distinct straight lines retain the existing command +semantics. A coincident point pair already constrained together returns its +existing constraint id without a document write or undo step. A new relation +continues through the shared constraint builder and solver, with one +`Session::edit_drawing` publication, one drawing-local history step and no +source-model mutation. There is no implicit switch to a second sheet. + +Original engine cases check local point coincidence and idempotence, point-on- +curve selection, two-line endpoint selection, undo/redo, JSON reopening, +model/parameter isolation and atomic refusal of foreign sketch ids or missing +local entities. Their executed results and the exact tested commit belong in +the PR. This does **not** complete sheet sketch driving dimensions, model +projection, sheet editor UI, all-tool parity, canonical model/sheet Cancel +composition or the full repository CI gate.