Sketch: Reverse Spline Direction (#8224) (#8414)

A new command, sketch.spline.reverse, runs a spline the other way: its
point list is reversed in place, so its last point becomes its first and
the curve keeps its path (chord-length fits, uniform clamped knots and
closed fits read the same both ways). Points and constraints stay. A
projected spline and one an offset uses or makes are refused. The
sketch's right-click menu offers it for one selected spline.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
semmelrinde
2026-10-28 18:56:34 +00:00
committed by GitHub
parent bcdee6e0bb
commit 9628e60ca0
8 changed files with 304 additions and 3 deletions
+22 -1
View File
@@ -15,7 +15,9 @@ macro_rules! text_style_doc {
use super::sketch::{add_c, circumcircle, edit, ids_of, mark_construction, req_parg, result, slot};
use super::{CommandSpec, in_sketch};
use crate::params::{bad, num, req_vec2, str_, string_list};
use crate::{Result, Session};
use crate::schema::sketch_create::{SplineReverseParams, SplineReverseResult};
use crate::schema::{TypedContract, output_schema_for};
use crate::{EngineError, Result, Session};
#[cfg(test)]
#[path = "sketch_text_move_tests.rs"]
@@ -25,6 +27,8 @@ mod text_move_tests;
#[path = "sketch_text_expr_tests.rs"]
mod text_expr_tests;
const SPLINE_REVERSE_CONTRACT: TypedContract<SplineReverseParams> = TypedContract::new();
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("sketch.line.midpoint", "Midpoint Line", midpoint_line)
.at("SKETCH", "CREATE")
@@ -83,6 +87,12 @@ pub static COMMANDS: &[CommandSpec] = &[
.icon("spline")
.enabled(in_sketch)
.params("curve: fit point spline id, at: [x,y]: a fit point is inserted where the curve passes nearest to it, between its neighbours (the curve still runs through it and all its old points; constraints on them stay); end?: start|end: `at` becomes the spline's new first or last point instead, and the curve runs on to it (refused when that end is joined to other geometry). Control point splines take no new points yet. Returns point: the new point's id"),
CommandSpec::new("sketch.spline.reverse", "Reverse Spline Direction", crate::checked!("sketch.spline.reverse", spline_reverse))
.icon("spline")
.enabled(in_sketch)
.contract(&SPLINE_REVERSE_CONTRACT)
.output(output_schema_for::<SplineReverseResult>)
.params("curve: spline id (fit point, control point or closed): its last point becomes its first and the curve runs the other way along the same path; its points and the constraints on them stay. Refused for a projected spline and for one an offset uses or makes. Returns curve"),
CommandSpec::new("sketch.conic", "Conic Curve", conic)
.at("SKETCH", "CREATE")
.icon("conic")
@@ -704,6 +714,17 @@ fn spline_insert_point(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"point": point, "curve": p.get("curve"), "sketch": info}))
}
fn spline_reverse(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.spline.reverse";
let id = str_(p, "curve").ok_or_else(|| bad(cmd, "`curve` must be a spline id"))?.to_string();
let ((), info) = edit(s, p, cmd, false, |sk, _| {
let c = sk.curve_index(&id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))?;
sk.reverse_spline(c).map_err(|e| bad(cmd, e.to_string()))
})?;
let reply = SplineReverseResult { curve: id, sketch: info, param_warnings: Vec::new() };
serde_json::to_value(reply).map_err(|e| EngineError::Other(e.to_string()))
}
fn conic(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.conic";
let rho = num(p, "rho").unwrap_or(0.5);
@@ -1142,6 +1142,67 @@ fn a_point_added_to_a_spline_keeps_its_constraints_and_undoes() {
assert_eq!(sketch(&s), unchanged, "refusals change nothing");
}
/// Reverse Spline Direction (#8224): a fit, a control point and a closed spline each run the
/// other way along the same path, their ends swapped. Their points, the constraints on them and
/// a line joined to an end stay, and undo restores the spline. A line and the source and output
/// of an offset are refused.
#[test]
fn a_reversed_spline_runs_the_other_way_along_the_same_path() {
use solidcraft_geom::Seg2;
let mut s = new_sketch();
let fit =
ids(&run(&mut s, "sketch.spline.fit_point", json!({"points": [[0, 0], [10, 8], [25, 2], [30, 6]], "connect": false}))["curves"])[0].clone();
run(&mut s, "sketch.line", json!({"points": [format!("{fit}.end"), [40, 6]], "connect": false}));
let cv = ids(&run(&mut s, "sketch.spline.control_point", json!({"points": [[0, 20], [10, 30], [25, 18], [30, 26]], "connect": false}))["curves"])
[0]
.clone();
let closed = ids(&run(
&mut s,
"sketch.spline.fit_point",
json!({"points": [[0, 40], [10, 48], [20, 40], [10, 34]], "periodic": true, "connect": false}),
)["curves"])[0]
.clone();
let off = |segs: &[Seg2], q: Vec2| segs.iter().map(|seg| seg.point_at(seg.closest_param(q)).dist(q)).fold(f64::INFINITY, f64::min);
let unit = |v: Vec2| v.normalized().unwrap_or_default();
for id in [&fit, &cv, &closed] {
let before = sketch(&s);
let ci = before.curve_index(id).unwrap();
let path = before.exact_segs(ci).unwrap();
let r = run(&mut s, "sketch.spline.reverse", json!({"curve": id}));
assert_eq!(r["curve"], json!(id), "{r}");
let after = sketch(&s);
let mut reversed = before.curves[ci].kind.point_ids();
reversed.reverse();
assert_eq!(after.curves[ci].kind.point_ids(), reversed, "{id}");
assert_eq!((&after.points, &after.constraints), (&before.points, &before.constraints), "{id}: points and constraints stay");
let segs = after.exact_segs(ci).unwrap();
for (a, b) in [(&path, &segs), (&segs, &path)] {
for seg in a {
for k in 0..=8 {
let q = seg.point_at(k as f64 / 8.0);
assert!(off(b, q) < 1e-7, "{id}: {q:?} is off the other path by {}", off(b, q));
}
}
}
// It starts where it ended, heading back the way it came.
let (first, last) = (segs.first().unwrap(), path.last().unwrap());
assert!(first.point_at(0.0).dist(last.point_at(1.0)) < 1e-9, "{id}");
assert!(unit(first.derivative(0.0)).dist(unit(last.derivative(1.0)) * -1.0) < 1e-7, "{id}");
}
let before = sketch(&s);
run(&mut s, "sketch.spline.reverse", json!({"curve": fit}));
run(&mut s, "edit.undo", json!({}));
assert_eq!(sketch(&s), before, "undo restores the spline");
let line = before.curves.iter().find(|c| matches!(c.kind, CurveKind::Line { .. })).unwrap().id.clone();
let made = ids(&run(&mut s, "sketch.offset", json!({"curves": [cv], "distance": 2, "side": [10, 40]}))["curves"]);
let unchanged = sketch(&s);
for id in [&line, &cv, &made[0]] {
assert!(s.execute("sketch.spline.reverse", &json!({"curve": id})).is_err(), "{id} is refused");
}
assert_eq!(sketch(&s), unchanged, "refusals change nothing");
}
#[test]
fn text_makes_profiles_that_extrude_and_edit() {
let mut s = new_sketch();