866029aaa2
* Engine: one sketch store for the sketch commands (W38.0, #5425, no behaviour change) The sketch commands found and kept the sketch they work on in about forty places, each its own way. They now go through one module, cmd::sketch_store, so a drawing session (W38a/b) can put sheet and view sketches behind the same calls without the commands changing again. - target: the command's `sketch` param or the sketch being edited (target_sketch, moved). - active: the sketch being edited, if any. The thirteen commands that need one keep their own refusal text; unifying the three texts is a separate, visible change. - read and write: the only calls from the sketch commands to Document::sketch and sketch_mut. - update: change a sketch on a copy of the document and keep both, the edit the text and link commands made by hand (sketch.text, sketch.edit_text, the projection, intersection and include links, sketch.fit_curves_to_section, sketch.break_link). As before, those paths neither solve the sketch nor run its checks. - checked: the checks an edited sketch passes (the periodic splines), shared by edit_solved and sketch.offset.break. edit and edit_solved stay in sketch.rs and load and keep their sketch through read and write. Imported text, snap points and the face projections keep their own shape (a text that can't be linked is skipped, snap points copy the document only when one is needed) through read and write. The reads and writes that are about the design's own sketches stay as they are, each on a line marked `// design-only:` (Redefine Sketch Plane, a joint's curve, a motion trace's wire, and the session's live drag, parameter edits and `created` diff). A source scan, each_sketch_command_goes_through_the_store, fails when a command reads or writes a sketch, or asks for the active one, anywhere but the store or a marked design-only line in the files that work on the design's sketches. Every existing sketch test is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Engine: the sketch store's scan also catches the active sketch taken as a value (#5425, review of #6336) The scan only caught `active_sketch.ok_or`, so main's `let Some(id) = s.active_sketch else …` could have come back unseen (0xlillith's review). It now also flags the value-read forms: `) = s.active_sketch` (let-else and if let), `=> s.active_sketch` (a match arm), `s.active_sketch.and_then(` and `s.active_sketch.map(`. State checks (`is_some`, `is_none`, `== Some(`), assignments and snapshot copies stay allowed. The wider scan found five commands that hand-rolled the store's `target` rule (a `sketch` param or the sketch being edited). They now call `sketch_store::target`: sketch.inspect, sketch.glyphs, sketch.export_dxf, inspect.curvature_comb and inspect.minimum_radius (their `curves` form). Their refusals become target's: - no `sketch` and none being edited: "no sketch is being edited" (was "no such sketch (and none is active)", or "no such sketch" for sketch.glyphs); - an unknown name or id: "no sketch `X`" (an unknown id used to reach "that sketch is not evaluated"); - a feature that is not a sketch: "`X` is not a sketch"; - a `sketch` that is neither an id nor a name (true, a list, an object): refused, where it used to fall back to the sketch being edited. A null `sketch` still means none given (the scenario harness sends one): target now reads it that way for every sketch command (target_reads_a_null_sketch_as_none_given). No test, doc or schema text named the old wording; the params docs ("sketch?: id|name (default active)") still hold. The two remaining value reads, the default sketch for a feature (features.rs) and the sketch a measurement prefers (measure_sel.rs), go through `sketch_store::active`, with no change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Engine: a number in `sketch` is an exact sketch id, never a name (#5425, review of #6336) `sketch_store::target` turned a JSON number into text and looked it up with `Document::find_feature`, which falls back to a name when no feature has that id. So `{"sketch": 999999}` could pick a sketch named "999999" whose id is something else, and export or inspect it. sketch.export_dxf, sketch.glyphs and the two curvature inspects read an exact id before they moved onto the store; the other sketch commands (the old `target_sketch`) and sketch.inspect read a number as text. One rule now, for every command that takes `sketch`: a number is an exact id (`as_u64`, then the feature with that id); text is an id written out or a name, as before. A number that isn't a whole number of 0 or more (negative, fractional) is refused with "`sketch` must be an id or a name"; an id no feature has gives "no sketch `N`", as before. Nothing in the tests, docs, scenarios or examples names a sketch with digits, so nothing relied on the old fallback. Test (sketch_store): a sketch named "999999" with another id. sketch.export_dxf, sketch.glyphs (with another sketch being edited, for its enabled check), inspect.curvature_comb and inspect.minimum_radius (with a real curve id) refuse `{"sketch": 999999}`, find the sketch as `"999999"` and by its own id, and refuse -1 and 1.5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Engine: MidPoint finds its sketch through the store too (#5425, after #5011) #5011 added a caller of the old `target_sketch` (MidPoint's `line_stays`, which swaps a point picked after its line) after this branch's base. It now reads the sketch through `sketch_store::target` and `sketch_store::read`, as the other sketch commands do, so a number there is an exact id too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Engine: standard parts fix their sketch's entities through the store (#5425, after #6376) #6376 (a standard part's sketches are fully defined) reads the sketch being drawn straight from the document's features, after this branch's base. `fix_all` now takes it from `sketch_store::active` and `sketch_store::read`, as the store's scan requires, with the same result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Engine: Toggle Diameter and Delete on a profile find their sketch through the store (#5425) Two sketch commands landed on main after the store: sketch.toggle_diameter (#7729) still called the removed sketch::target_sketch, and Delete on a picked profile (#7107) read the sketch straight from the document. Both now go through sketch_store (target and read), and the store's source scan passes again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Sketch dimensioned: use the sketch store's target after the rebase Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Sketch dimensioned: read the sketch through the store Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
613 lines
28 KiB
Rust
613 lines
28 KiB
Rust
//! More sketch constraints (curvature, polygon), constraint glyph anchors and snapping
|
||
//! inference for interactive tools.
|
||
|
||
use serde_json::{Value, json};
|
||
use solidcraft_geom::Vec2;
|
||
use solidcraft_sketch::{ConstraintKind, CurveKind, Sketch};
|
||
|
||
use super::sketch::{add_c, constraint_result, dof_now, edit, 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::{ToggleDiameterParams, ToggleDiameterResult};
|
||
use crate::schema::{TypedContract, output_schema_for};
|
||
use crate::{EngineError, Result, Session};
|
||
|
||
const TOGGLE_DIAMETER_CONTRACT: TypedContract<ToggleDiameterParams> = TypedContract::new();
|
||
|
||
pub static COMMANDS: &[CommandSpec] = &[
|
||
CommandSpec::new("sketch.constraint.curvature", "Curvature", c_smooth)
|
||
.at("SKETCH", "CONSTRAINTS")
|
||
.icon("c_smooth")
|
||
.enabled(in_sketch)
|
||
.params("a, b: two curves sharing an end point (curvature continuous, G2)"),
|
||
CommandSpec::new("sketch.constraint.polygon", "Polygon", c_polygon)
|
||
.at("SKETCH", "CONSTRAINTS")
|
||
.icon("c_polygon")
|
||
.enabled(in_sketch)
|
||
.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", auto_constrain)
|
||
.at("SKETCH", "CONSTRAINTS")
|
||
.icon("auto_constrain")
|
||
.enabled(in_sketch)
|
||
.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", auto_constrain_datum)
|
||
.at("SKETCH", "CONSTRAINTS")
|
||
.icon("auto_constrain")
|
||
.enabled(in_sketch)
|
||
.params("datum: point ref (default origin); like AutoConstrain with positions measured from the datum"),
|
||
CommandSpec::new("sketch.finish_auto_constrain", "Finish with AutoConstrain", auto_constrain_finish)
|
||
.at("SKETCH", "FINISH SKETCH")
|
||
.icon("finish")
|
||
.enabled(in_sketch)
|
||
.params("like AutoConstrain, then Finish Sketch"),
|
||
CommandSpec::new("sketch.constrainer", "Sketch Constrainer", smart_constrain)
|
||
.at("SKETCH", "CREATE")
|
||
.icon("constrainer")
|
||
.enabled(in_sketch)
|
||
.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", toggle_driven).icon("dimension")
|
||
.enabled(in_sketch)
|
||
.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", glyphs).icon("eye")
|
||
.enabled(in_sketch)
|
||
.noundo()
|
||
.params("sketch?: id|name: every constraint with where to draw its glyph (sketch coordinates)"),
|
||
CommandSpec::new("sketch.snap", "Snap Inference", snap).icon("snap")
|
||
.enabled(in_sketch)
|
||
.noundo()
|
||
.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)?;
|
||
constraint_result(p, cmd, id, info, by, Value::Null)
|
||
}
|
||
|
||
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)
|
||
})?;
|
||
Ok(json!({"constraints": out, "sketch": info}))
|
||
}
|
||
|
||
/// 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) = ¶m {
|
||
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) = ¶m {
|
||
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))
|
||
})?;
|
||
Ok(json!({"added": added, "dof": dof, "sketch": info}))
|
||
}
|
||
|
||
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)?;
|
||
constraint_result(p, cmd, id, info, by, json!({"type": name}))
|
||
}
|
||
|
||
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();
|
||
Ok(json!({"constraint": cid, "driven": driven, "sketch": info}))
|
||
}
|
||
|
||
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),
|
||
PointOnCurve { 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 list: Vec<Value> = ss
|
||
.sketch
|
||
.constraints
|
||
.iter()
|
||
.map(|c| {
|
||
json!({
|
||
"id": c.id,
|
||
"name": c.kind.name(),
|
||
"dimension": c.kind.is_dimension(),
|
||
"driven": c.driven,
|
||
"value": c.kind.value(),
|
||
"param": c.param,
|
||
"at": anchor(&ss.sketch, &c.kind),
|
||
"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();
|
||
Ok(json!({"sketch": id, "glyphs": list, "dof": ss.report.dof, "fully_constrained": ss.report.fully_constrained(), "conflicts": failing}))
|
||
}
|
||
|
||
/// 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, Value)> = 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, json!({"type": "coincident", "point": q.id, "at": q.pos})));
|
||
}
|
||
}
|
||
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, json!({"type": "midpoint", "curve": c.id, "at": m})));
|
||
}
|
||
}
|
||
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, json!({"type": "on_curve", "curve": c.id, "at": q})));
|
||
}
|
||
}
|
||
}
|
||
// 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, json!({"type": "horizontal", "at": [at.x, f.y]})));
|
||
}
|
||
if d.x.abs() <= r && d.y.abs() > r {
|
||
out.push((d.x.abs() + r, json!({"type": "vertical", "at": [f.x, at.y]})));
|
||
}
|
||
}
|
||
out.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||
let list: Vec<Value> = out.into_iter().take(8).map(|x| x.1).collect();
|
||
Ok(json!({"snaps": list}))
|
||
}
|
||
|
||
#[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;
|