Sketch Trim: keep the driving distance when trimming a line offset first (#11442)

This commit is contained in:
cronos authored and GitHub committed 2026-11-04 22:23:12 +00:00
1 parent f87dd73661
commit a137f2d459
4 files changed
+56 -19

No files matched your search

+8 -8
View File
@@ -61,7 +61,7 @@ pub static COMMANDS: &[CommandSpec] = &[
.icon("trim")
.key("T")
.enabled(in_sketch)
.params(concat!("curve: id, at: [x,y] (the piece to remove: between the nearest intersections around it). Trimming a line source releases its dependent straight offsets' linked extents (offset_released: curve ids), retaining their driving normal-distance dimensions and parallel relations", created_doc!())),
.params(concat!("curve: id, at: [x,y] (the piece to remove: between the nearest intersections around it). Trimming a line source or its single-line offset releases linked extents (offset_released: curve ids), retaining their driving normal-distance dimensions and parallel relations", created_doc!())),
CommandSpec::new("sketch.trim.path", "Trim Along Path", trim_path)
.icon("trim")
.enabled(in_sketch)
@@ -506,7 +506,7 @@ fn trim(s: &mut Session, p: &Value) -> Result<Value> {
let at = req_vec2(cmd, p, "at")?;
let (out, info) = edit(s, p, cmd, false, |sk, doc| {
let ci = curve_arg(sk, p, cmd)?;
let released = release_source_line_offsets(sk, doc, ci, cmd)?;
let released = release_line_offsets(sk, doc, ci, cmd)?;
let (mut out, _) = remove_piece(sk, ci, at, cmd)?;
if !released.is_empty() {
out["offset_released"] = json!(released);
@@ -517,10 +517,10 @@ fn trim(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"result": out, "sketch": info}))
}
/// A source line's endpoints may change independently of its parallel offsets. Materialize
/// Source lines and their single-line offsets can have independent endpoints. Materialize
/// the affected line chains before Trim, retaining their original driving distance parameters
/// as point-line dimensions. Directly trimming an associated output remains a separate command policy.
fn release_source_line_offsets(sk: &mut Sketch, doc: &solidcraft_doc::Document, ci: usize, cmd: &str) -> Result<Vec<String>> {
/// as point-line dimensions. Closed or mixed-curve output chains still need their association.
fn release_line_offsets(sk: &mut Sketch, doc: &solidcraft_doc::Document, ci: usize, cmd: &str) -> Result<Vec<String>> {
let Some(curve) = sk.curves.get(ci) else { return Ok(Vec::new()) };
if curve.infinite || sk.is_linked_curve(ci) || !matches!(curve.kind, CurveKind::Line { .. }) {
return Ok(Vec::new());
@@ -532,7 +532,7 @@ fn release_source_line_offsets(sk: &mut Sketch, doc: &solidcraft_doc::Document,
.enumerate()
.filter_map(|(i, offset)| {
(offset.topology.is_none()
&& offset.sources.iter().any(|(source, _)| source == &id)
&& (offset.sources.iter().any(|(source, _)| source == &id) || (offset.sources.len() == 1 && offset.outputs.contains(&id)))
&& offset.sources.len() == offset.outputs.len()
&& offset.sources.iter().zip(&offset.outputs).all(|((source, _), output)| {
[source, output]
@@ -547,7 +547,7 @@ fn release_source_line_offsets(sk: &mut Sketch, doc: &solidcraft_doc::Document,
}
let report = solve_current(sk, doc)?;
if !report.keeps_orientation() {
return Err(bad(cmd, report.failure(|_| "cannot trim a source with conflicting offset dimensions".into())));
return Err(bad(cmd, report.failure(|_| "cannot trim a line with conflicting offset dimensions".into())));
}
let mut released = Vec::new();
for index in affected.into_iter().rev() {
@@ -847,7 +847,7 @@ fn trim_path(s: &mut Session, p: &Value) -> Result<Value> {
after = (si, t);
// A piece trim refuses (a closed spline crossed once) stays as it was.
let before = sk.clone();
let cut = release_source_line_offsets(sk, doc, ci, cmd).and_then(|released| {
let cut = release_line_offsets(sk, doc, ci, cmd).and_then(|released| {
let (mut out, gone) = remove_piece(sk, ci, q, cmd)?;
if !released.is_empty() {
out["offset_released"] = json!(released);
+39 -6
View File
@@ -5274,8 +5274,8 @@ fn move_copy_drawn_wire_failures_leave_the_design_and_history_unchanged() {
/// A construction line and its two offsets each stop at their own crossing. Trimming one
/// must never shorten the others, whatever order the three curves are picked in.
#[test]
fn trim_source_then_two_offsets_independently_in_either_order() {
for order in [[0, 1, 2], [0, 2, 1]] {
fn trim_source_and_two_offsets_independently_in_any_order() {
for order in [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]] {
let mut s = new_sketch();
run(&mut s, "sketch.line", json!({"points": [[0,0],[40,40]], "construction": true, "connect": false, "infer": false, "ids": ["source"]}));
let out = run(&mut s, "sketch.offset", json!({"curves": ["source"], "distance": 2, "symmetric": true, "chain": false}));
@@ -5310,6 +5310,33 @@ fn trim_source_then_two_offsets_independently_in_either_order() {
}
}
#[test]
fn trimming_one_offset_line_keeps_its_source_sibling_and_other_offsets() {
let mut s = new_sketch();
run(&mut s, "sketch.line", json!({"points": [[0,0],[40,40]], "connect": false, "infer": false, "ids": ["source"]}));
run(&mut s, "sketch.constraint.fix", json!({"entities": ["source"]}));
let out = run(&mut s, "sketch.offset", json!({"curves": ["source"], "distance": 2, "symmetric": true, "chain": false}));
let outputs = ids(&out["curves"]);
run(&mut s, "sketch.line", json!({"points": [[-20,20],[60,20]], "connect": false, "infer": false}));
run(&mut s, "sketch.line", json!({"points": [[100,100],[120,100]], "connect": false, "infer": false, "ids": ["other"]}));
let unrelated = ids(&run(&mut s, "sketch.offset", json!({"curves": ["other"], "distance": 3, "chain": false}))["curves"])[0].clone();
let before = sketch(&s);
let document = s.doc.to_json();
let solidcraft_sketch::Shape::Line { a, .. } = before.shape(before.curve_index(&outputs[0]).unwrap()).unwrap() else { panic!("line") };
let r = run(&mut s, "sketch.trim", json!({"curve": outputs[0], "at": [a.x+1.0, a.y+1.0]}));
assert_eq!(r["sketch"]["solved"], true, "{r}");
let after = sketch(&s);
let solidcraft_sketch::Shape::Line { a: start, .. } = after.shape(after.curve_index(&outputs[0]).unwrap()).unwrap() else { panic!("line") };
assert!((start.y - 20.0).abs() < 1e-6, "{start:?}");
for id in ["source", &outputs[1], "other", &unrelated] {
assert_eq!(after.polyline(after.curve_index(id).unwrap()), before.polyline(before.curve_index(id).unwrap()), "{id} must stay put");
}
assert_eq!(after.offset_distance(&outputs[1]), Some(-2.0));
assert_eq!(after.offset_distance(&unrelated), Some(3.0));
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.to_json(), document, "one Undo restores the offset association as well as the trimmed end");
}
#[test]
fn trim_path_cuts_source_and_two_offsets_at_their_own_crossings() {
let mut s = new_sketch();
@@ -5319,11 +5346,11 @@ fn trim_path_cuts_source_and_two_offsets_at_their_own_crossings() {
curves.extend(ids(&out["curves"]));
run(&mut s, "sketch.line", json!({"points": [[-20,20],[60,20]], "connect": false, "infer": false}));
let before = s.doc.to_json();
let path = json!({"points": [[4,5],[15,5],[-10,5]], "dry_run": true});
let path = json!({"points": [[15,5],[-10,5]], "dry_run": true});
let preview = run(&mut s, "sketch.trim.path", path);
assert_eq!(preview["trimmed"].as_array().unwrap().len(), 3, "{preview}");
assert_eq!(s.doc.to_json(), before, "preview releases no stored associations or parameters");
let r = run(&mut s, "sketch.trim.path", json!({"points": [[4,5],[15,5],[-10,5]]}));
let r = run(&mut s, "sketch.trim.path", json!({"points": [[15,5],[-10,5]]}));
assert_eq!(r["trimmed"], preview["trimmed"]);
assert!(r["skipped"].as_array().unwrap().is_empty(), "{r}");
let sk = sketch(&s);
@@ -5379,14 +5406,20 @@ fn trim_keeps_offset_distances_driving_after_trims_drag_and_reopen() {
assert_eq!(sk.constraints.iter().filter(|c| c.param.as_deref() == Some(parameter)).count(), 2, "both sides retain their dimensions");
assert!(state.sketch(1).unwrap().report.ok());
};
for reverse_outputs in [true, false] {
for order in [
["source", "l1", "l2"],
["source", "l2", "l1"],
["l1", "source", "l2"],
["l1", "l2", "source"],
["l2", "source", "l1"],
["l2", "l1", "source"],
] {
let mut s = new_sketch();
run(&mut s, "sketch.line", json!({"points":[[0,0],[40,40]],"construction":true,"connect":false,"infer":false,"ids":["source"]}));
let out = run(&mut s, "sketch.offset", json!({"curves":["source"],"distance":2,"symmetric":true,"chain":false}));
let parameter = out["param"].as_str().unwrap().to_string();
let cutter =
run(&mut s, "sketch.line", json!({"points":[[-20,20],[60,20]],"connect":false,"infer":false}))["curves"][0].as_str().unwrap().to_string();
let order = if reverse_outputs { ["source", "l2", "l1"] } else { ["source", "l1", "l2"] };
for id in order {
let sk = sketch(&s);
let solidcraft_sketch::Shape::Line { a, .. } = sk.shape(sk.curve_index(id).unwrap()).unwrap() else { panic!("line") };
+5 -1
View File
@@ -2906,7 +2906,11 @@ mod tests {
let Shape::Line { a, b } = sk.shape(sk.curve_index(&output).unwrap()).unwrap() else { panic!("offset must be a line") };
let cut = Vec2::new(15.0, a.y);
assert!((a.dist(b) - 20.0).abs() < 1e-6);
assert!(h.app.run("sketch.trim", json!({"curve":output,"at":[cut.x,cut.y]})).is_err());
let before = h.app.session.doc.to_json();
let trimmed = h.app.run("sketch.trim", json!({"curve":output,"at":[cut.x,cut.y]})).unwrap();
assert_eq!(trimmed["sketch"]["solved"], true);
h.app.run("edit.undo", json!({})).unwrap();
assert_eq!(h.app.session.doc.to_json(), before, "Undo restores the association before the explicit Break menu check");
h.call("ui.view", json!({"view":"top"}));
h.app.cam.distance = 60.0;
@@ -171,18 +171,18 @@ fn trimming_construction_source_and_offsets_by_click_or_drag_is_independent() {
let before = h.app.session.doc.to_json();
let undo = h.app.session.undo.len();
if drag {
let a = screen(&mut h, 4.0, 5.0);
let b = screen(&mut h, 15.0, 5.0);
let a = screen(&mut h, 15.0, 5.0);
let b = screen(&mut h, -10.0, 5.0);
let mut path = vec![a];
path.extend(steps(a, b, 16));
let c = screen(&mut h, -10.0, 5.0);
let c = screen(&mut h, 15.0, 5.0);
path.extend(steps(b, c, 16));
path.extend(steps(c, b, 16));
press_and_move(&mut h, &path);
assert_eq!(h.app.session.doc.to_json(), before);
release(&mut h, b);
} else {
let points = [[10.0, 10.0], [10.0 - 2f64.sqrt(), 10.0 + 2f64.sqrt()], [10.0 + 2f64.sqrt(), 10.0 - 2f64.sqrt()]];
let points = [[10.0 + 2f64.sqrt(), 10.0 - 2f64.sqrt()], [10.0 - 2f64.sqrt(), 10.0 + 2f64.sqrt()], [10.0, 10.0]];
for point in points {
h.step(&json!({"click":{"sketch":point}})).unwrap();
h.frames(2);