Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

32.1 KB
Raw Blame
//! More sketch constraints (curvature, polygon), constraint glyph anchors and snapping
//! inference for interactive tools.

use serde_json::Value;
use solidcore_record::Vec2;
use solidcraft_geom::prelude::*;
use solidcraft_sketch::{ConstraintKind, CurveKind, Sketch};

use super::sketch::{add_c, dof_now, edit, keep_redundant, listed, redundancy, residual_of};
use super::sketch_store;
use super::{CommandSpec, in_sketch};
use crate::params::{bad, num, req_vec2, str_, string_list, vec2};
use crate::schema::sketch_constraints::{
    AutoConstrainDatumParams, AutoConstrainParams, AutoConstrainResult, ConstrainerParams, ConstrainerResult, ConstraintAdded, CurvatureParams,
    FinishAutoConstrainParams, Glyph, GlyphsParams, GlyphsResult, PolygonParams, PolygonResult, Snap, SnapParams, SnapResult, ToggleDiameterParams,
    ToggleDiameterResult, ToggleDrivenParams, ToggleDrivenResult,
};
use crate::schema::{TypedContract, output_schema_for};
use crate::{EngineError, Result, Session};

const TOGGLE_DIAMETER_CONTRACT: TypedContract<ToggleDiameterParams> = TypedContract::new();
const CURVATURE_CONTRACT: TypedContract<CurvatureParams> = TypedContract::new();
const POLYGON_CONTRACT: TypedContract<PolygonParams> = TypedContract::new();
const AUTO_CONSTRAIN_CONTRACT: TypedContract<AutoConstrainParams> = TypedContract::new();
const AUTO_CONSTRAIN_DATUM_CONTRACT: TypedContract<AutoConstrainDatumParams> = TypedContract::new();
const FINISH_AUTO_CONSTRAIN_CONTRACT: TypedContract<FinishAutoConstrainParams> = TypedContract::new();
const CONSTRAINER_CONTRACT: TypedContract<ConstrainerParams> = TypedContract::new();
const TOGGLE_DRIVEN_CONTRACT: TypedContract<ToggleDrivenParams> = TypedContract::new();
const GLYPHS_CONTRACT: TypedContract<GlyphsParams> = TypedContract::new();
const SNAP_CONTRACT: TypedContract<SnapParams> = TypedContract::new();

pub static COMMANDS: &[CommandSpec] = &[
    CommandSpec::new("sketch.constraint.curvature", "Curvature", crate::checked!("sketch.constraint.curvature", c_smooth))
        .at("SKETCH", "CONSTRAINTS")
        .icon("c_smooth")
        .enabled(in_sketch)
        .contract(&CURVATURE_CONTRACT)
        .output(output_schema_for::<ConstraintAdded>)
        .params("a, b: two curves sharing an end point (curvature continuous, G2)"),
    CommandSpec::new("sketch.constraint.polygon", "Polygon", crate::checked!("sketch.constraint.polygon", c_polygon))
        .at("SKETCH", "CONSTRAINTS")
        .icon("c_polygon")
        .enabled(in_sketch)
        .contract(&POLYGON_CONTRACT)
        .output(output_schema_for::<PolygonResult>)
        .params("lines: [line ids] of a closed chain: equal sides with every corner on one circle (a regular polygon)"),
    CommandSpec::new("sketch.auto_constrain", "AutoConstrain", crate::checked!("sketch.auto_constrain", auto_constrain))
        .at("SKETCH", "CONSTRAINTS")
        .icon("auto_constrain")
        .enabled(in_sketch)
        .contract(&AUTO_CONSTRAIN_CONTRACT)
        .output(output_schema_for::<AutoConstrainResult>)
        .params("tolerance?: mm (default 0.01), angle_tolerance?: deg (default 1), dimensions?: bool (default true): infer constraints, then dimension until fully constrained"),
    CommandSpec::new("sketch.auto_constrain_from_datum", "AutoConstrain from Datum", crate::checked!("sketch.auto_constrain_from_datum", auto_constrain_datum))
        .at("SKETCH", "CONSTRAINTS")
        .icon("auto_constrain")
        .enabled(in_sketch)
        .contract(&AUTO_CONSTRAIN_DATUM_CONTRACT)
        .output(output_schema_for::<AutoConstrainResult>)
        .params("datum: point ref (default origin); like AutoConstrain with positions measured from the datum"),
    CommandSpec::new("sketch.finish_auto_constrain", "Finish with AutoConstrain", crate::checked!("sketch.finish_auto_constrain", auto_constrain_finish))
        .at("SKETCH", "FINISH SKETCH")
        .icon("finish")
        .enabled(in_sketch)
        .contract(&FINISH_AUTO_CONSTRAIN_CONTRACT)
        .output(output_schema_for::<AutoConstrainResult>)
        .params("like AutoConstrain, then Finish Sketch"),
    CommandSpec::new("sketch.constrainer", "Sketch Constrainer", crate::checked!("sketch.constrainer", smart_constrain))
        .at("SKETCH", "CREATE")
        .icon("constrainer")
        .enabled(in_sketch)
        .contract(&CONSTRAINER_CONTRACT)
        .output(output_schema_for::<ConstrainerResult>)
        .params("entities: [one or two sketch entity ids]: applies the constraint the geometry nearly has (horizontal/vertical, parallel, perpendicular, collinear, coincident, concentric, equal, tangent)"),
    CommandSpec::new("sketch.toggle_driven", "Toggle Driven Dimension", crate::checked!("sketch.toggle_driven", toggle_driven)).icon("dimension")
        .enabled(in_sketch)
        .contract(&TOGGLE_DRIVEN_CONTRACT)
        .output(output_schema_for::<ToggleDrivenResult>)
        .params("constraint: dimension id, driven?: bool (default toggles); a driving dimension gets a parameter with its current value"),
    CommandSpec::new("sketch.toggle_diameter", "Toggle Radius / Diameter", crate::checked!("sketch.toggle_diameter", toggle_diameter))
        .icon("dimension")
        .enabled(in_sketch)
        .contract(&TOGGLE_DIAMETER_CONTRACT)
        .output(output_schema_for::<ToggleDiameterResult>)
        .params("constraint: a circle's or an arc's radius or diameter dimension id; diameter?: bool (default: the other one); sketch?: id|name (default: the one being edited). The circle stays the same size: its parameter is halved or doubled (a plain value is rewritten, an expression becomes `(e) / 2` or `2 * (e)`); refused when anything else uses that parameter"),
    CommandSpec::new("sketch.glyphs", "Constraint Glyphs", crate::checked!("sketch.glyphs", glyphs)).icon("eye")
        .enabled(in_sketch)
        .noundo()
        .contract(&GLYPHS_CONTRACT)
        .output(output_schema_for::<GlyphsResult>)
        .params("sketch?: id|name: every constraint with where to draw its glyph (sketch coordinates)"),
    CommandSpec::new("sketch.snap", "Snap Inference", crate::checked!("sketch.snap", snap)).icon("snap")
        .enabled(in_sketch)
        .noundo()
        .contract(&SNAP_CONTRACT)
        .output(output_schema_for::<SnapResult>)
        .params("at: [x,y], from?: [x,y] (previous click), radius?: mm (snap distance, default 1): what a click there would snap to and infer"),
];

fn c_smooth(s: &mut Session, p: &Value) -> Result<Value> {
    let cmd = "sketch.constraint.curvature";
    let (mut before, mut residual) = (None, None);
    let (id, info) = edit(s, p, cmd, true, |sk, _| {
        let a = curve(sk, p.get("a"), cmd)?;
        let b = curve(sk, p.get("b"), cmd)?;
        before = Some(dof_now(sk));
        let id = add_c(sk, ConstraintKind::Smooth { a, b })?;
        residual = residual_of(sk, &id);
        Ok(id)
    })?;
    let by = redundancy(s, p, cmd, before, &info, residual, &id)?;
    let (redundant, determined_by) = redundant_part(p, cmd, by)?;
    reply(cmd, ConstraintAdded { constraint: id, sketch: info, redundant, determined_by, created: None, param_warnings: Vec::new() })
}

/// A command's reply as JSON: they build their replies from the types their output schemas
/// describe, so the two can't drift apart.
fn reply(cmd: &str, reply: impl serde::Serialize) -> Result<Value> {
    serde_json::to_value(reply).map_err(|e| bad(cmd, e.to_string()))
}

/// What a new constraint's reply says of redundancy: whether it is, and by which constraints. A
/// redundant one is refused when the call asked for `if_redundant: "refuse"`.
fn redundant_part(p: &Value, cmd: &str, by: Option<Vec<String>>) -> Result<(bool, Option<Vec<String>>)> {
    match by {
        Some(by) => {
            keep_redundant(p, cmd, &by)?;
            Ok((true, Some(by)))
        }
        None => Ok((false, None)),
    }
}

fn curve(sk: &Sketch, v: Option<&Value>, cmd: &str) -> Result<usize> {
    let id = v.and_then(Value::as_str).ok_or_else(|| bad(cmd, "expected a curve id"))?;
    sk.curve_index(id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))
}

fn c_polygon(s: &mut Session, p: &Value) -> Result<Value> {
    let cmd = "sketch.constraint.polygon";
    let ids = string_list(cmd, p, "lines")?;
    if ids.len() < 3 || ids.len() > 200 {
        return Err(bad(cmd, "`lines` must list 3 to 200 lines of a closed chain"));
    }
    let (out, info) = edit(s, p, cmd, true, |sk, _| {
        let ls: Vec<usize> = ids.iter().map(|i| sk.curve_index(i).ok_or_else(|| bad(cmd, format!("unknown curve `{i}`")))).collect::<Result<_>>()?;
        let mut corners: Vec<usize> = Vec::new();
        for l in &ls {
            match sk.curves.get(*l).map(|c| &c.kind) {
                Some(CurveKind::Line { a, b }) => {
                    for q in [*a, *b] {
                        if !corners.contains(&q) {
                            corners.push(q);
                        }
                    }
                }
                _ => return Err(bad(cmd, "the polygon must be made of lines")),
            }
        }
        if corners.len() != ls.len() {
            return Err(bad(cmd, "the lines must form one closed chain"));
        }
        let pts: Vec<Vec2> = corners.iter().filter_map(|q| sk.point(*q)).collect();
        let c = pts.iter().fold(Vec2::ZERO, |a, q| a + *q) / pts.len() as f64;
        let r = pts.iter().map(|q| q.dist(c)).sum::<f64>() / pts.len() as f64;
        let circ = sk.add_circle(c, r.max(1e-6), None, None)?;
        if let Some(cu) = sk.curves.get_mut(circ) {
            cu.construction = true;
        }
        let mut cons = Vec::new();
        for w in ls.windows(2) {
            cons.push(add_c(sk, ConstraintKind::Equal { a: w[0], b: w[1] })?);
        }
        for q in corners {
            cons.push(add_c(sk, ConstraintKind::PointOnCurve { p: q, c: circ })?);
        }
        Ok(cons)
    })?;
    reply(cmd, PolygonResult { constraints: out, sketch: info, created: None, param_warnings: Vec::new() })
}

/// Most constraints AutoConstrain tries.
const MAX_TRIALS: usize = 2000;

/// Add `k` if it removes a degree of freedom and the sketch still solves; else leave the
/// sketch as it was. Returns the new dof when kept.
fn try_add(sk: &mut Sketch, doc: &mut solidcraft_doc::Document, k: ConstraintKind, dof: usize, dim: bool) -> Option<usize> {
    let saved = sk.clone();
    let param = if dim {
        let v = k.value()?;
        let (unit, e) = if k.is_angle() { ("deg".into(), format!("{} deg", (v.to_degrees() * 1e6).round() / 1e6)) } else { doc.design_length(v) };
        Some(doc.new_model_param(&e, &unit))
    } else {
        None
    };
    if sk.add_constraint(k, param.clone()).is_err() {
        if let Some(pn) = &param {
            let _ = doc.remove_param(pn);
        }
        return None;
    }
    let mut trial = sk.clone();
    let rep = solidcraft_sketch::solve_with(&mut trial, &solidcraft_sketch::Hold::default(), None, solidcraft_sketch::Analysis::Freedom, None);
    if rep.ok() && rep.dof < dof {
        *sk = trial;
        return Some(rep.dof);
    }
    *sk = saved;
    if let Some(pn) = &param {
        let _ = doc.remove_param(pn);
    }
    None
}

fn run_auto(sk: &mut Sketch, doc: &mut solidcraft_doc::Document, p: &Value, datum: usize, mut dof: usize) -> (Vec<String>, usize) {
    use ConstraintKind::*;
    let tol = num(p, "tolerance").filter(|t| *t > 0.0).unwrap_or(0.01);
    let atol = num(p, "angle_tolerance").filter(|t| *t > 0.0).unwrap_or(1.0).to_radians();
    let dims = crate::params::bool_(p, "dimensions").unwrap_or(true);
    let n0 = sk.constraints.len();
    let mut trials = 0;
    let mut attempt = |sk: &mut Sketch, doc: &mut solidcraft_doc::Document, k: ConstraintKind, dim: bool, dof: &mut usize| {
        if *dof == 0 || trials >= MAX_TRIALS {
            return;
        }
        trials += 1;
        if let Some(d) = try_add(sk, doc, k, *dof, dim) {
            *dof = d;
        }
    };
    // Coincident points.
    let np = sk.points.len();
    for i in 0..np {
        for j in (i + 1)..np {
            let (Some(a), Some(b)) = (sk.point(i), sk.point(j)) else { continue };
            if a.dist(b) <= tol {
                attempt(sk, doc, Coincident { p: i, q: j }, false, &mut dof);
            }
        }
    }
    // Horizontal / vertical lines, tangent line–arc and arc–arc joints.
    let nc = sk.curves.len();
    for i in 0..nc {
        if let Some(CurveKind::Line { a, b }) = sk.curves.get(i).map(|c| c.kind.clone())
            && let (Some(pa), Some(pb)) = (sk.point(a), sk.point(b))
        {
            let ang = (pb - pa).angle();
            let off_h = ang.sin().abs().asin();
            let off_v = ang.cos().abs().asin();
            if off_h <= atol {
                attempt(sk, doc, Horizontal { l: i }, false, &mut dof);
            } else if off_v <= atol {
                attempt(sk, doc, Vertical { l: i }, false, &mut dof);
            }
        }
    }
    for i in 0..nc {
        for j in (i + 1)..nc {
            let (Some(ci), Some(cj)) = (sk.curves.get(i), sk.curves.get(j)) else { continue };
            let round = |k: &CurveKind| matches!(k, CurveKind::Arc { .. } | CurveKind::Circle { .. });
            if !(round(&ci.kind) || round(&cj.kind)) {
                continue;
            }
            let (Some((a0, a1)), Some((b0, b1))) = (ci.kind.ends(), cj.kind.ends()) else { continue };
            let Some(sh) = [a0, a1].into_iter().find(|x| *x == b0 || *x == b1) else { continue };
            let dir = |c: usize| -> Option<Vec2> {
                let k = sk.curves.get(c)?.kind.clone();
                let q = sk.point(sh)?;
                match k {
                    CurveKind::Line { a, b } => (sk.point(b)? - sk.point(a)?).normalized(),
                    CurveKind::Arc { c, .. } => (q - sk.point(c)?).perp().normalized(),
                    _ => None,
                }
            };
            if let (Some(u), Some(v)) = (dir(i), dir(j))
                && u.cross(v).abs() <= atol.sin()
            {
                attempt(sk, doc, Tangent { a: i, b: j }, false, &mut dof);
            }
        }
    }
    if dims {
        // Construction curves have degrees of freedom too: locating a circle's centre
        // does not constrain its radius. Dimension them just like profile curves.
        for i in 0..nc {
            let k = match sk.curves.get(i).map(|c| c.kind.clone()) {
                Some(CurveKind::Circle { r, .. }) => Diameter { c: i, value: 2.0 * r },
                Some(CurveKind::Arc { .. }) => Radius { c: i, value: sk.radius(i).unwrap_or(1.0) },
                Some(CurveKind::Line { a, b }) => Length { l: i, value: sk.point(a).zip(sk.point(b)).map(|(x, y)| x.dist(y)).unwrap_or(1.0) },
                _ => continue,
            };
            attempt(sk, doc, k, true, &mut dof);
        }
        // Positions from the datum.
        let d = sk.point(datum).unwrap_or_default();
        for i in 0..sk.points.len() {
            if i == datum {
                continue;
            }
            let Some(q) = sk.point(i) else { continue };
            attempt(sk, doc, DistanceX { p: datum, q: i, value: (q.x - d.x).abs() }, true, &mut dof);
            attempt(sk, doc, DistanceY { p: datum, q: i, value: (q.y - d.y).abs() }, true, &mut dof);
        }
    }
    let added = sk.constraints.iter().skip(n0).map(|c| c.id.clone()).collect();
    (added, dof)
}

fn auto(s: &mut Session, p: &Value, cmd: &str) -> Result<Value> {
    let ((added, dof), info) = edit(s, p, cmd, false, |sk, doc| {
        let datum = match str_(p, "datum") {
            Some(r) => sk.resolve_point(r).ok_or_else(|| bad(cmd, format!("unknown point `{r}`")))?,
            None => 0,
        };
        // Values of existing dimensions must be applied (and reached from the stored solved
        // state) before judging degrees of freedom; a sketch they would turn over keeps its
        // stored geometry, as the edit's own solve then refuses them.
        let rep = super::sketch::solve_current(sk, doc)?;
        if !rep.keeps_orientation() {
            return Err(bad(cmd, rep.failure(|f| format!("the sketch constraints conflict ({f})"))));
        }
        // This is the exact state just solved for the current parameter values. Trials update
        // its freedom whenever they change constraints; no separate initial solve is needed.
        Ok(run_auto(sk, doc, p, datum, rep.dof))
    })?;
    reply(cmd, AutoConstrainResult { added, dof, sketch: info, created: None, param_warnings: Vec::new() })
}

fn auto_constrain(s: &mut Session, p: &Value) -> Result<Value> {
    auto(s, p, "sketch.auto_constrain")
}
fn auto_constrain_datum(s: &mut Session, p: &Value) -> Result<Value> {
    auto(s, p, "sketch.auto_constrain_from_datum")
}
fn auto_constrain_finish(s: &mut Session, p: &Value) -> Result<Value> {
    let r = auto(s, p, "sketch.finish_auto_constrain")?;
    s.active_sketch = None;
    s.revision += 1;
    Ok(r)
}

/// The constraint two entities (or one line) are closest to satisfying.
fn guess(sk: &Sketch, ents: &[String], cmd: &str) -> Result<ConstraintKind> {
    use ConstraintKind::*;
    let ent = |r: &str| -> Result<(Option<usize>, Option<usize>)> {
        if let Some(c) = sk.curve_index(r) {
            return Ok((None, Some(c)));
        }
        sk.resolve_point(r).map(|p| (Some(p), None)).ok_or_else(|| bad(cmd, format!("unknown sketch entity `{r}`")))
    };
    let line_dir = |c: usize| match sk.shape(c) {
        Some(solidcraft_sketch::Shape::Line { a, b }) => (b - a).normalized(),
        _ => None,
    };
    let round = |c: usize| matches!(sk.curves.get(c).map(|c| &c.kind), Some(CurveKind::Circle { .. } | CurveKind::Arc { .. }));
    match ents {
        [a] => {
            let (_, Some(c)) = ent(a)? else { return Err(bad(cmd, "pick a line, or two entities")) };
            let d = line_dir(c).ok_or_else(|| bad(cmd, "a single entity must be a line"))?;
            Ok(if d.x.abs() >= d.y.abs() { Horizontal { l: c } } else { Vertical { l: c } })
        }
        [a, b] => match (ent(a)?, ent(b)?) {
            ((Some(p), _), (Some(q), _)) => Ok(Coincident { p, q }),
            ((Some(p), _), (_, Some(c))) | ((_, Some(c)), (Some(p), _)) => {
                // A point at a curve's centre is concentric-like: coincident with the centre.
                if let Some(cp) = sk.curves.get(c).and_then(|cu| match cu.kind {
                    CurveKind::Circle { c, .. } | CurveKind::Arc { c, .. } => Some(c),
                    _ => None,
                }) && sk.point(cp).zip(sk.point(p)).is_some_and(|(x, y)| x.dist(y) < sk.radius(c).unwrap_or(1.0) * 0.25)
                {
                    return Ok(Coincident { p, q: cp });
                }
                Ok(PointOnCurve { p, c })
            }
            ((_, Some(x)), (_, Some(y))) => {
                if let (Some(u), Some(v)) = (line_dir(x), line_dir(y)) {
                    let cross = u.cross(v).abs();
                    if cross < 0.2 {
                        // Parallel; on one line → collinear.
                        let off = match (sk.shape(x), sk.shape(y)) {
                            (Some(solidcraft_sketch::Shape::Line { a, .. }), Some(solidcraft_sketch::Shape::Line { a: b0, .. })) => {
                                u.cross(b0 - a).abs()
                            }
                            _ => f64::INFINITY,
                        };
                        let len = sk.polyline(x).windows(2).map(|w| w[0].dist(w[1])).sum::<f64>();
                        return Ok(if off < len * 0.05 { Collinear { a: x, b: y } } else { Parallel { a: x, b: y } });
                    }
                    if u.dot(v).abs() < 0.2 {
                        return Ok(Perpendicular { a: x, b: y });
                    }
                    return Ok(Equal { a: x, b: y });
                }
                if round(x) && round(y) {
                    let (cx, cy) = (sk.center(x).unwrap_or_default(), sk.center(y).unwrap_or_default());
                    let (rx, ry) = (sk.radius(x).unwrap_or(1.0), sk.radius(y).unwrap_or(1.0));
                    if cx.dist(cy) < 0.25 * rx.min(ry) {
                        return Ok(Concentric { a: x, b: y });
                    }
                    let d = cx.dist(cy);
                    if (d - (rx + ry)).abs() < 0.15 * rx.min(ry) || (d - (rx - ry).abs()).abs() < 0.15 * rx.min(ry) {
                        return Ok(Tangent { a: x, b: y });
                    }
                    return Ok(Equal { a: x, b: y });
                }
                Ok(Tangent { a: x, b: y })
            }
            _ => Err(bad(cmd, "cannot constrain that combination")),
        },
        _ => Err(bad(cmd, "`entities` must list one or two entities")),
    }
}

fn smart_constrain(s: &mut Session, p: &Value) -> Result<Value> {
    let cmd = "sketch.constrainer";
    let ents = string_list(cmd, p, "entities")?;
    let (mut before, mut residual) = (None, None);
    let mut name = "";
    let (id, info) = edit(s, p, cmd, true, |sk, _| {
        let k = guess(sk, &ents, cmd)?;
        name = k.name();
        before = Some(dof_now(sk));
        let id = add_c(sk, k)?;
        residual = residual_of(sk, &id);
        Ok(id)
    })?;
    let by = redundancy(s, p, cmd, before, &info, residual, &id)?;
    let (redundant, determined_by) = redundant_part(p, cmd, by)?;
    reply(
        cmd,
        ConstrainerResult {
            constraint: id,
            sketch: info,
            redundant,
            determined_by,
            kind: name.to_string(),
            created: None,
            param_warnings: Vec::new(),
        },
    )
}

fn toggle_driven(s: &mut Session, p: &Value) -> Result<Value> {
    let cmd = "sketch.toggle_driven";
    let cid = str_(p, "constraint").ok_or_else(|| bad(cmd, "`constraint` must be a dimension id"))?.to_string();
    let want = crate::params::bool_(p, "driven");
    let mut before = None;
    let (driven, info) = edit(s, p, cmd, true, |sk, doc| {
        let c = sk.constraints.iter().find(|c| c.id == cid).cloned().ok_or_else(|| bad(cmd, format!("no constraint `{cid}`")))?;
        let v = c.kind.value().ok_or_else(|| bad(cmd, format!("`{cid}` is not a dimension")))?;
        let driven = want.unwrap_or(!c.driven);
        if driven == c.driven {
            return Ok(driven);
        }
        let slot = sk.constraints.iter_mut().find(|c| c.id == cid).ok_or_else(|| bad(cmd, "constraint"))?;
        if driven {
            slot.driven = true;
            slot.param = None;
        } else {
            let (unit, e) =
                if c.kind.is_angle() { ("deg".into(), format!("{} deg", (v.to_degrees() * 1e6).round() / 1e6)) } else { doc.design_length(v) };
            slot.driven = false;
            slot.param = Some(doc.new_model_param(&e, &unit));
            before = None;
        }
        Ok(driven)
    })?;
    if !driven {
        // Making it driving must not over-constrain: compare with the sketch without it.
        let id = sketch_store::active(s).ok_or_else(|| bad(cmd, "no sketch"))?;
        let mut sk = sketch_store::read(&s.doc, id)?.clone();
        if let Some(c) = sk.constraints.iter_mut().find(|c| c.id == cid) {
            c.driven = true;
        }
        before = Some(dof_now(&sk));
        // It holds (its parameter takes the value it measures), so it can only be redundant.
        if let Some(by) = redundancy(s, p, cmd, before, &info, Some(0.0), &cid)? {
            return Err(bad(cmd, format!("that dimension is already determined by {}; it stays driven", listed(&by))));
        }
    }
    s.doc_mut().prune_model_params();
    reply(cmd, ToggleDrivenResult { constraint: cid, driven, sketch: info, created: None, param_warnings: Vec::new() })
}

fn toggle_diameter(s: &mut Session, p: &Value) -> Result<Value> {
    let cmd = "sketch.toggle_diameter";
    let cid = str_(p, "constraint").ok_or_else(|| bad(cmd, "`constraint` must be a dimension id"))?.to_string();
    let want = crate::params::bool_(p, "diameter");
    let sketch = sketch_store::target(s, p, cmd)?;
    let (diameter, info) = edit(s, p, cmd, true, |sk, doc| {
        let c = sk.constraints.iter().find(|c| c.id == cid).cloned().ok_or_else(|| bad(cmd, format!("no constraint `{cid}`")))?;
        let round = |curve: usize| matches!(sk.curves.get(curve).map(|c| &c.kind), Some(CurveKind::Circle { .. } | CurveKind::Arc { .. }));
        let (curve, value, now) = match c.kind {
            ConstraintKind::Radius { c, value } if round(c) => (c, value, false),
            ConstraintKind::Diameter { c, value } if round(c) => (c, value, true),
            _ => return Err(bad(cmd, format!("`{cid}` is not a circle's or an arc's radius or diameter dimension"))),
        };
        let diameter = want.unwrap_or(!now);
        if diameter == now {
            return Ok(diameter);
        }
        let (kind, value) = if diameter {
            (ConstraintKind::Diameter { c: curve, value: 2.0 * value }, 2.0 * value)
        } else {
            (ConstraintKind::Radius { c: curve, value: value / 2.0 }, value / 2.0)
        };
        // Its parameter now holds the other measure, so the circle keeps its size; another user
        // of that parameter would change size with it.
        if let Some(name) = &c.param {
            let others: Vec<String> = doc
                .param_users(name)
                .into_iter()
                .filter(
                    |u| !matches!(u, solidcraft_doc::ParamUser::Dimension { feature, constraint, .. } if *feature == sketch && *constraint == cid),
                )
                .map(|u| u.describe())
                .collect();
            if !others.is_empty() {
                return Err(bad(cmd, format!("`{name}` is also used by {}; switching `{cid}` would change them", others.join(", "))));
            }
            let e = doc.param(name).map(|p| p.expr.clone()).ok_or_else(|| bad(cmd, format!("no parameter `{name}`")))?;
            let e = if solidcraft_doc::expr::references(&e).is_empty() {
                doc.design_length(value).1
            } else if diameter {
                format!("2 * ({e})")
            } else {
                format!("({e}) / 2")
            };
            doc.set_param(name, &e, None, None).map_err(|e| bad(cmd, e.to_string()))?;
        }
        let slot = sk.constraints.iter_mut().find(|c| c.id == cid).ok_or_else(|| bad(cmd, "constraint"))?;
        slot.kind = kind;
        Ok(diameter)
    })?;
    let reply = ToggleDiameterResult { constraint: cid, diameter, sketch: info, param_warnings: Vec::new() };
    serde_json::to_value(reply).map_err(|e| EngineError::Other(e.to_string()))
}

/// Where a constraint's glyph goes (sketch coordinates): beside the middle of its first curve,
/// or at its point.
fn anchor(sk: &Sketch, k: &ConstraintKind) -> Option<Vec2> {
    use ConstraintKind::*;
    // Half way along the curve.
    let mid = |c: usize| -> Option<Vec2> {
        let poly = sk.polyline(c);
        let total: f64 = poly.windows(2).map(|w| w[0].dist(w[1])).sum();
        let mut acc = 0.0;
        for w in poly.windows(2) {
            let l = w[0].dist(w[1]);
            if acc + l >= total * 0.5 && l > 0.0 {
                return Some(w[0].lerp(w[1], (total * 0.5 - acc) / l));
            }
            acc += l;
        }
        poly.first().copied()
    };
    let pt = |p: usize| sk.point(p);
    match *k {
        OffsetDistance { ref output, .. } => sk.offset_dimension_points(output).map(|(a, b)| (a + b) * 0.5),
        PatternPoint { q, center, .. } => pt(center.unwrap_or(q)),
        PatternRadius { b, .. } => mid(b),
        Coincident { p, .. } | Fix { p } | Midpoint { p, .. } => pt(p),
        // A pierce is drawn at the point it holds.
        Pierce { p, .. } => pt(p),
        PointOnCurve { p, .. } | PointAlong { p, .. } => pt(p),
        HorizontalPoints { p, q } | VerticalPoints { p, q } | Symmetric { p, q, .. } => Some((pt(p)? + pt(q)?) * 0.5),
        Horizontal { l } | Vertical { l } => mid(l),
        Parallel { a, .. } | Perpendicular { a, .. } | Collinear { a, .. } | Equal { a, .. } | Concentric { a, .. } | Angle { a, .. } => mid(a),
        Tangent { a, b } | Smooth { a, b } => {
            // At the shared end when there is one.
            let ends = |c: usize| sk.curves.get(c).and_then(|c| c.kind.ends());
            match (ends(a), ends(b)) {
                (Some((a0, a1)), Some((b0, b1))) => [a0, a1].into_iter().find(|x| *x == b0 || *x == b1).and_then(pt).or_else(|| mid(a)),
                _ => mid(a),
            }
        }
        Distance { p, q, .. } | DistanceX { p, q, .. } | DistanceY { p, q, .. } => Some((pt(p)? + pt(q)?) * 0.5),
        PointLineDistance { p, .. } | LinearDiameter { p, .. } => pt(p),
        LineCircleDistance { c, .. } => sk.center(c),
        PointCircleDistance { p, .. } => pt(p),
        CircleCircleDistance { a, b, .. } => Some((sk.center(a)? + sk.center(b)?) * 0.5),
        Length { l, .. } | LineAngle { l, .. } => mid(l),
        Radius { c, .. } | Diameter { c, .. } | ArcLength { c, .. } | ArcSweep { c, .. } => mid(c),
        SlotOverallLength { a, b, .. } => Some((sk.center(a)? + sk.center(b)?) * 0.5),
    }
}

fn glyphs(s: &mut Session, p: &Value) -> Result<Value> {
    let id = sketch_store::target(s, p, "sketch.glyphs")?;
    let st = s.model.state();
    let ss = st.sketch(id).ok_or_else(|| EngineError::Other("that sketch is not evaluated".into()))?;
    let failing = &ss.report.failing;
    let glyphs: Vec<Glyph> = ss
        .sketch
        .constraints
        .iter()
        .map(|c| Glyph {
            id: c.id.clone(),
            name: c.kind.name().to_string(),
            dimension: c.kind.is_dimension(),
            driven: c.driven,
            value: c.kind.value(),
            param: c.param.clone(),
            at: anchor(&ss.sketch, &c.kind).map(|a| [a.x, a.y]),
            failing: failing.contains(&c.id),
            // Implied by the others (it removes no freedom) but holding: shown dimmed.
            redundant: ss.report.redundant.contains(&c.id),
        })
        .collect();
    let result = GlyphsResult {
        sketch: id,
        glyphs,
        dof: ss.report.dof,
        fully_constrained: ss.report.fully_constrained(),
        conflicts: failing.clone(),
        param_warnings: Vec::new(),
    };
    reply("sketch.glyphs", result)
}

/// Snap inference for a click at `at`: the nearest sketch point (coincident), the midpoint of a
/// line, a point on a curve, and horizontal/vertical alignment with `from` or with other
/// points. Results are ordered by priority; the first one is what a click would use.
fn snap(s: &mut Session, p: &Value) -> Result<Value> {
    let cmd = "sketch.snap";
    let at = req_vec2(cmd, p, "at")?;
    let from = p.get("from").and_then(vec2);
    let r = num(p, "radius").filter(|r| *r > 0.0).unwrap_or(1.0);
    let id = sketch_store::active(s).ok_or_else(|| bad(cmd, "no sketch is being edited"))?;
    let st = s.model.state();
    let ss = st.sketch(id).ok_or_else(|| EngineError::Other("the sketch is not evaluated".into()))?;
    let sk = &ss.sketch;
    let mut out: Vec<(f64, Snap)> = Vec::new();
    // Points (end points, centres, the origin).
    for q in &sk.points {
        let d = q.pos.dist(at);
        if d <= r {
            out.push((d * 0.5, Snap::Coincident { point: q.id.clone(), at: [q.pos.x, q.pos.y] }));
        }
    }
    for (i, c) in sk.curves.iter().enumerate() {
        if let CurveKind::Line { a, b } = c.kind
            && let (Some(pa), Some(pb)) = (sk.point(a), sk.point(b))
        {
            let m = (pa + pb) * 0.5;
            let d = m.dist(at);
            if d <= r {
                out.push((d * 0.6, Snap::Midpoint { curve: c.id.clone(), at: [m.x, m.y] }));
            }
        }
        if let Some(sh) = sk.shape(i) {
            let q = sh.project(at);
            if sh.dist(at) <= r && q.dist(at) <= r {
                out.push((sh.dist(at) + r * 0.5, Snap::OnCurve { curve: c.id.clone(), at: [q.x, q.y] }));
            }
        }
    }
    // Alignment with the previous click.
    if let Some(f) = from {
        let d = at - f;
        if d.y.abs() <= r && d.x.abs() > r {
            out.push((d.y.abs() + r, Snap::Horizontal { at: [at.x, f.y] }));
        }
        if d.x.abs() <= r && d.y.abs() > r {
            out.push((d.x.abs() + r, Snap::Vertical { at: [f.x, at.y] }));
        }
    }
    out.sort_by(|a, b| a.0.total_cmp(&b.0));
    let snaps: Vec<Snap> = out.into_iter().take(8).map(|x| x.1).collect();
    reply(cmd, SnapResult { snaps, param_warnings: Vec::new() })
}

#[cfg(test)]
#[path = "sketch_constraints_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "sketch_fuzz_tests.rs"]
mod fuzz;

#[cfg(test)]
#[path = "sketch_perf_tests.rs"]
mod perf;