Sketch: trim keeps the rest of a dimensioned line where it was (#2910)

Thanks @Jacquesvdberg92.
This commit is contained in:
Jacques
2026-10-19 13:44:50 +03:00
committed by GitHub
parent 1666ca7018
commit 34cb3167b6
2 changed files with 60 additions and 0 deletions
+26
View File
@@ -257,6 +257,31 @@ fn tie(sk: &mut Sketch, pieces: &[usize]) -> Result<()> {
Ok(())
}
/// Dimensions and relations that measure the whole of curve `ci` (a line's length, an equal
/// length, its midpoint, an arc's length or sweep) would stay on the piece that keeps its id
/// after a trim and stretch that piece back to the old size, moving the rest of the sketch. A
/// length becomes the distance between the curve's old ends, as a fillet keeps it to its virtual
/// corner (it goes with an end the trim removes); the others go.
fn release_extent(sk: &mut Sketch, ci: usize) {
let ends = match sk.curves.get(ci).map(|c| &c.kind) {
Some(CurveKind::Line { a, b }) => Some((*a, *b)),
_ => None,
};
sk.constraints.retain_mut(|k| match k.kind {
ConstraintKind::Length { l, value } if l == ci => match ends {
Some((p, q)) => {
k.kind = ConstraintKind::Distance { p, q, value };
true
}
None => false,
},
ConstraintKind::Equal { a, b } if ends.is_some() && (a == ci || b == ci) => false,
ConstraintKind::Midpoint { l, .. } if l == ci => false,
ConstraintKind::ArcLength { c, .. } | ConstraintKind::ArcSweep { c, .. } if c == ci => false,
_ => true,
});
}
/// The cut parameters bracketing `tq` on curve `ci`: (before, after) with their cutter curves.
fn bracket(sk: &Sketch, ci: usize, tq: f64) -> (Vec<(f64, Vec2, usize)>, bool) {
let Some(sh) = sk.shape(ci) else { return (Vec::new(), false) };
@@ -305,6 +330,7 @@ fn trim(s: &mut Session, p: &Value) -> Result<Value> {
doc.prune_model_params();
return Ok(json!({"deleted": id}));
}
release_extent(sk, ci);
let ts: Vec<f64> = cuts.iter().map(|c| c.0).collect();
let (pieces, pts) = split_at(sk, ci, &ts)?;
// The piece containing tq.
@@ -1585,3 +1585,37 @@ fn offset_driver_format18_offsets_keep_fixed_distances_on_upgrade() {
assert!(!sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::OffsetDistance { .. })));
assert_eq!(ids(&result["curves"]).len(), sk.offsets[0].outputs.len());
}
#[test]
fn trim_keeps_the_rest_of_a_dimensioned_line_where_it_was() {
// A 40 x 40 square, its right side dimensioned with the Dimension tool (a line length).
let mut s = new_sketch();
let sq = ids(&run(&mut s, "sketch.line", json!({"points": [[0, 0], [40, 0], [40, 40], [0, 40]], "closed": true, "infer": true}))["curves"]);
let (right, top) = (sq[1].clone(), sq[2].clone());
run(&mut s, "sketch.dimension", json!({"entities": [right]}));
// A step drawn across the top right corner, its ends on the top and right sides.
let step = ids(&run(&mut s, "sketch.line", json!({"points": [[23, 40], [23, 23], [40, 23]], "infer": true}))["curves"]);
run(&mut s, "sketch.constraint.coincident", json!({"a": format!("{}.start", step[0]), "b": top}));
run(&mut s, "sketch.constraint.coincident", json!({"a": format!("{}.end", step[1]), "b": right}));
let corner = |s: &Session| {
let sk = sketch(s);
sk.point(sk.resolve_point(&format!("{}.end", step[1])).unwrap()).unwrap()
};
let forty = |s: &Session| {
sketch(s).constraints.iter().filter(|c| c.kind.value().is_some_and(|v| close(v, 40.0))).map(|c| c.kind.clone()).collect::<Vec<_>>()
};
// Trim the right side above the step: the 40 stays between the square's corners, which are
// both still there, instead of stretching the 23 mm that is left back to 40.
run(&mut s, "sketch.trim", json!({"curve": right, "at": [40, 30]}));
assert!(corner(&s).dist(Vec2::new(40.0, 23.0)) < 1e-6, "the step moved to {:?}", corner(&s));
let sk = sketch(&s);
let ends = sk.shape(sk.curve_index(&right).unwrap()).unwrap();
assert!(matches!(ends, solidcraft_sketch::Shape::Line { a, b } if close(a.dist(b), 23.0)), "{ends:?}");
assert!(matches!(forty(&s)[..], [ConstraintKind::Distance { .. }]), "{:?}", forty(&s));
// Trim the top side right of the step: the corner the 40 measured to goes, and the 40 with it.
run(&mut s, "sketch.trim", json!({"curve": top, "at": [30, 40]}));
assert!(corner(&s).dist(Vec2::new(40.0, 23.0)) < 1e-6, "the step moved to {:?}", corner(&s));
assert!(forty(&s).is_empty(), "{:?}", forty(&s));
}