Sketch Blend Curve (tangent control-point spline between two curve ends) and the Centerline line type (sketch.centerline: left out of profiles, drawn as an axis)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AS5Tn3xieewUSg3uxrMVdL
This commit is contained in:
bherbruckandClaude Opus 5.5 committed 2026-10-07 22:55:34 +00:00
1 parent 2491c40fef
commit 6e20038906
8 files changed
+116 -12

No files matched your search

+60
View File
@@ -70,6 +70,14 @@ pub static COMMANDS: &[CommandSpec] =
.icon("conic")
.enabled(in_sketch)
.params("start, end, apex: [x,y] or point refs; rho?: 0…1 (default 0.5: parabola)"),
CommandSpec::new("BlendG1CurveSketchCmd", "Blend Curve", blend)
.at("SKETCH", "MODIFY")
.icon("blend")
.enabled(in_sketch)
.params("a, b: end points of two curves (\"l1.end\", \"a2.start\"): a smooth (tangent) spline joining them"),
CommandSpec::new("sketch.centerline", "Centerline", centerline)
.enabled(in_sketch)
.params("curves: [line ids], value?: bool (default toggles): centerline line type (not in profiles; an axis)"),
CommandSpec::new("MTextCmd", "Text", text)
.at("SKETCH", "CREATE")
.icon("text")
@@ -139,6 +147,58 @@ fn edit_text(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"link": l, "curves": curves}))
}
/// The curve ending at point `pi` and the direction leaving it there.
fn end_of(sk: &Sketch, pi: usize) -> Option<(usize, Vec2)> {
sk.curves.iter().enumerate().filter(|(_, c)| !c.construction).find_map(|(i, _)| leaving_dir(sk, i, pi).map(|d| (i, d)))
}
fn blend(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "BlendG1CurveSketchCmd";
let (out, info) = edit(s, p, cmd, false, |sk, _| {
let pa = req_parg(sk, p, "a", cmd)?.idx().ok_or_else(|| bad(cmd, "`a` must be the end point of a curve"))?;
let pb = req_parg(sk, p, "b", cmd)?.idx().ok_or_else(|| bad(cmd, "`b` must be the end point of a curve"))?;
let (ca, ta) = end_of(sk, pa).ok_or_else(|| bad(cmd, "no curve ends at `a`"))?;
let (cb, tb) = end_of(sk, pb).ok_or_else(|| bad(cmd, "no curve ends at `b`"))?;
let (qa, qb) = (sk.point(pa).unwrap_or_default(), sk.point(pb).unwrap_or_default());
let l = qa.dist(qb);
if l < 1e-9 || ca == cb && pa == pb {
return Err(bad(cmd, "the ends must differ"));
}
let first_new = sk.points.len();
let c1 = sk.add_point(qa + ta * (l / 3.0), None)?;
let c2 = sk.add_point(qb + tb * (l / 3.0), None)?;
let sp = sk.add_curve(CurveKind::Spline { pts: vec![pa, c1, c2, pb], control: true, degree: 3 }, None)?;
let cons = vec![add_c(sk, ConstraintKind::Tangent { a: ca, b: sp })?, add_c(sk, ConstraintKind::Tangent { a: cb, b: sp })?];
solve_new_only(sk, first_new);
Ok((ids_of(sk, &[sp]), cons))
})?;
Ok(result(out, info))
}
fn centerline(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.centerline";
let ids = string_list(p, "curves");
if ids.is_empty() {
return Err(bad(cmd, "`curves` must list lines"));
}
let want = crate::params::bool_(p, "value");
let (n, info) = edit(s, p, cmd, false, |sk, _| {
for id in &ids {
let c = sk.curve_index(id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))?;
let cu = sk.curves.get_mut(c).ok_or_else(|| bad(cmd, "curve"))?;
if !matches!(cu.kind, CurveKind::Line { .. }) {
return Err(bad(cmd, format!("`{id}` is not a line")));
}
cu.centerline = want.unwrap_or(!cu.centerline);
if cu.centerline {
cu.construction = false;
}
}
Ok(ids.len())
})?;
Ok(json!({"changed": n, "sketch": info}))
}
fn ellipse(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "CircleElipse";
let (c, m) = (req_vec2(cmd, p, "center")?, req_vec2(cmd, p, "major")?);
+9 -2
View File
@@ -169,7 +169,7 @@ fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64]) -> Result<(Vec<usize>, Vec<u
let mut pieces = vec![ci];
let mut push = |sk: &mut Sketch, kind: CurveKind, prefix: &str| -> Result<()> {
let id = sk.fresh(prefix);
sk.curves.push(Curve { id, kind, construction, reversed: false, link: None });
sk.curves.push(Curve { id, kind, construction, reversed: false, link: None, centerline: false });
pieces.push(sk.curves.len() - 1);
Ok(())
};
@@ -827,7 +827,14 @@ fn copy_through(
}
};
let id = sk.fresh(prefix);
sk.curves.push(Curve { id, kind, construction: cu.construction, reversed: if flip { !cu.reversed } else { cu.reversed }, link: None });
sk.curves.push(Curve {
id,
kind,
construction: cu.construction,
reversed: if flip { !cu.reversed } else { cu.reversed },
link: None,
centerline: false,
});
made.push(sk.curves.len() - 1);
}
Ok((made, map))
@@ -284,3 +284,33 @@ fn text_makes_profiles_that_extrude_and_edit() {
assert!(v > 40.0 && v < 200.0, "{v}");
assert!(s.execute("MTextCmd", &json!({"text": "", "at": [0, 0]})).is_err());
}
#[test]
fn blend_curve_joins_tangentially() {
let mut s = new_sketch();
let a = ids(&run(&mut s, "DrawPolyline", json!({"points": [[0, 0], [10, 0]]}))["curves"])[0].clone();
let b = ids(&run(&mut s, "DrawPolyline", json!({"points": [[20, 10], [20, 20]]}))["curves"])[0].clone();
let r = run(&mut s, "BlendG1CurveSketchCmd", json!({"a": format!("{a}.end"), "b": format!("{b}.start")}));
assert_eq!(r["sketch"]["solved"], true, "{r}");
let sp = ids(&r["curves"])[0].clone();
let sk = sketch(&s);
let pts: Vec<Vec2> = sk.curves[sk.curve_index(&sp).unwrap()].kind.point_ids().iter().map(|i| sk.point(*i).unwrap()).collect();
// Leaves along +x, arrives going +y (control polygon end legs).
let d0 = (pts[1] - pts[0]).normalized().unwrap();
let d1 = (pts[3] - pts[2]).normalized().unwrap();
assert!(d0.y.abs() < 1e-6 && d0.x > 0.0, "{d0:?}");
assert!(d1.x.abs() < 1e-6 && d1.y > 0.0, "{d1:?}");
}
#[test]
fn centerline_is_left_out_of_profiles() {
let mut s = new_sketch();
let r = run(&mut s, "ShapeRectangleTwoPoint", json!({"p0": [0, 0], "p1": [10, 10]}));
let l = ids(&r["curves"]);
let m = ids(&run(&mut s, "DrawPolyline", json!({"points": [[5, -5], [5, 15]]}))["curves"])[0].clone();
assert_eq!(profiles(&s).len(), 2);
run(&mut s, "sketch.centerline", json!({"curves": [m]}));
assert_eq!(profiles(&s).len(), 1);
assert!(s.execute("sketch.centerline", &json!({"curves": ["nope"]})).is_err());
let _ = l;
}