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>
318 lines
16 KiB
Rust
318 lines
16 KiB
Rust
//! Shape creation and its driving dimensions form one command and one undo step: the typed
|
|
//! values, with `as_drawn` every other size of the shape at its size as drawn, and with `locate`
|
|
//! its place from the sketch origin until nothing of it is left free.
|
|
//!
|
|
//! [`dimension_roles`] is the one list of the sizes each creator has. The UI asks it which
|
|
//! creators can be dimensioned on creation, so a creator added here (with its entities in
|
|
//! `draw`) needs nothing else to take part. `locate` needs nothing per creator: it works on the
|
|
//! points any creator reports, along the directions of an [`OriginFrame`].
|
|
//!
|
|
//! 3D sketches: their curves (`sketch.line3d`…) take no constraints yet. Once they do, a 3D
|
|
//! creator joins [`dimension_roles`] with its roles and entities, and `locate` gets a frame with a
|
|
//! Z direction; the UI routes the 3D tools through `tools::draw`, which asks `dimension_roles`.
|
|
use serde_json::{Map, Value, json};
|
|
use solidcraft_doc::expr::{Kind, definition};
|
|
use solidcraft_geom::Vec3;
|
|
|
|
use crate::{
|
|
Result, Session,
|
|
params::{bad, bool_, expr, str_},
|
|
};
|
|
|
|
/// The dimension roles of a sketch creator, in the order `as_drawn` adds them, or None when
|
|
/// `sketch.draw.dimensioned` cannot dimension it. Roles of one [`ALTERNATIVES`] pair are two
|
|
/// readings of one size: only the first given (or listed) of them is placed.
|
|
pub fn dimension_roles(creator: &str) -> Option<&'static [&'static str]> {
|
|
Some(match creator {
|
|
"sketch.line" | "sketch.line.midpoint" => &["length", "angle"],
|
|
"sketch.rectangle.two_point" | "sketch.rectangle.center" | "sketch.rectangle.three_point" | "sketch.rectangle.rounded" => {
|
|
&["width", "height"]
|
|
}
|
|
// A circle is dimensioned by its diameter unless a radius is given.
|
|
"sketch.circle.center" | "sketch.circle.two_point" | "sketch.circle.three_point" => &["diameter", "radius"],
|
|
"sketch.arc.center_point" => &["radius", "sweep"],
|
|
"sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point" => &["length", "width"],
|
|
"sketch.slot.arc_center" => &["radius", "sweep", "width"],
|
|
"sketch.slot.arc_three_point" => &["width"],
|
|
"sketch.polygon.inscribed" | "sketch.polygon.circumscribed" | "sketch.polygon.edge" => &["radius"],
|
|
_ => return None,
|
|
})
|
|
}
|
|
|
|
/// Roles that measure the same size two ways: a shape takes one of each pair.
|
|
const ALTERNATIVES: &[[&str; 2]] = &[["radius", "diameter"]];
|
|
|
|
/// The roles to place: the given ones, then (`as_drawn`) each other role of `roles` that is not
|
|
/// an alternative of one already placed. None stands for the size as drawn.
|
|
fn wanted<'a>(roles: &[&'a str], given: &'a Map<String, Value>, as_drawn: bool) -> Vec<(&'a str, Option<&'a Value>)> {
|
|
let mut wanted: Vec<(&str, Option<&Value>)> = given.iter().map(|(role, value)| (role.as_str(), Some(value))).collect();
|
|
if as_drawn {
|
|
for role in roles {
|
|
let placed = |r: &str| wanted.iter().any(|(w, _)| *w == r);
|
|
let alternative_placed = ALTERNATIVES.iter().any(|pair| pair.contains(role) && pair.iter().any(|other| other != role && placed(other)));
|
|
if !placed(role) && !alternative_placed {
|
|
wanted.push((role, None));
|
|
}
|
|
}
|
|
}
|
|
wanted
|
|
}
|
|
|
|
pub(super) fn draw(s: &mut Session, p: &Value) -> Result<Value> {
|
|
let cmd = "sketch.draw.dimensioned";
|
|
let create = str_(p, "command").ok_or_else(|| bad(cmd, "`command` must name a supported sketch creator"))?;
|
|
let roles = dimension_roles(create).ok_or_else(|| bad(cmd, "unsupported sketch creator"))?;
|
|
let as_drawn = bool_(p, "as_drawn").unwrap_or(false);
|
|
let locate = bool_(p, "locate").unwrap_or(false);
|
|
let mut params = p.get("params").filter(|v| v.is_object()).cloned().ok_or_else(|| bad(cmd, "`params` must be an object"))?;
|
|
let none = Map::new();
|
|
let dimensions = match p.get("dimensions") {
|
|
None if as_drawn || locate => &none,
|
|
given => given.and_then(Value::as_object).ok_or_else(|| bad(cmd, "`dimensions` must be an object"))?,
|
|
};
|
|
if dimensions.len() > roles.len() || dimensions.keys().any(|key| !roles.contains(&key.as_str())) {
|
|
return Err(bad(cmd, "unsupported dimension role for this creator"));
|
|
}
|
|
if dimensions.contains_key("radius") && dimensions.contains_key("diameter") {
|
|
return Err(bad(cmd, "choose radius or diameter, not both"));
|
|
}
|
|
let mut overall_length = None;
|
|
let mut slot_width = None;
|
|
for (role, value) in dimensions {
|
|
let e = expr(&json!({"value": value}), "value")
|
|
.filter(|e| e.len() <= 4096)
|
|
.ok_or_else(|| bad(cmd, "dimension values must be bounded expressions or finite numbers"))?;
|
|
let kind = if role == "angle" || role == "sweep" { Kind::Angle } else { Kind::Length };
|
|
// `name = expression` is checked by its right-hand side, read as the user parameter
|
|
// `sketch.dimension` defines will hold it (a bare number in the design's unit) (#7567).
|
|
let rhs = match definition(&e) {
|
|
Some((name, "")) => return Err(bad(cmd, format!("{role}: `{name} =` needs a value after the `=`"))),
|
|
Some((_, rhs)) => rhs,
|
|
None => e.as_str(),
|
|
};
|
|
let evaluated = s.doc.eval(&s.doc.with_design_unit(rhs, kind), kind)?;
|
|
if role == "length" {
|
|
overall_length = Some(evaluated);
|
|
}
|
|
if role == "width" {
|
|
slot_width = Some(evaluated);
|
|
}
|
|
if !evaluated.is_finite() || evaluated.abs() > 1e9 || kind == Kind::Length && evaluated <= 1e-9 {
|
|
return Err(bad(cmd, format!("invalid {role} dimension")));
|
|
}
|
|
if role == "sweep" && !(evaluated.abs() > 1e-9 && evaluated.abs() < std::f64::consts::TAU - 1e-9) {
|
|
return Err(bad(cmd, "sweep must be nonzero and less than a full turn"));
|
|
}
|
|
if role == "sweep" && create == "sketch.slot.arc_center" && evaluated <= 0.0 {
|
|
return Err(bad(cmd, "arc slot sweep must be greater than zero and less than a full turn"));
|
|
}
|
|
if role == "sweep" {
|
|
params["sweep"] = json!(evaluated.to_degrees());
|
|
if let Some(o) = params.as_object_mut() {
|
|
o.remove("end");
|
|
}
|
|
}
|
|
}
|
|
if create == "sketch.slot.overall"
|
|
&& let Some(length) = overall_length
|
|
&& let Some(width) = slot_width.or_else(|| params.get("width").and_then(Value::as_f64))
|
|
&& length <= width + 1e-9
|
|
{
|
|
return Err(bad(cmd, "overall slot length must exceed its width"));
|
|
}
|
|
if create == "sketch.line" {
|
|
if params.get("closed").and_then(Value::as_bool).unwrap_or(false)
|
|
|| params.get("points").and_then(Value::as_array).is_none_or(|points| points.len() != 2)
|
|
{
|
|
return Err(bad(cmd, "dimensioned line creation needs exactly two points"));
|
|
}
|
|
if dimensions.contains_key("angle") {
|
|
params["infer"] = json!(false);
|
|
}
|
|
}
|
|
let mut scratch = s.scratch();
|
|
let mut out = scratch.execute(create, ¶ms)?;
|
|
let curves = out.get("curves").and_then(Value::as_array).cloned().unwrap_or_default();
|
|
let curve = |index: usize| -> Result<Value> {
|
|
curves.get(index).filter(|v| v.is_string()).cloned().ok_or_else(|| bad(cmd, "creator did not return the expected curve"))
|
|
};
|
|
let mut added = serde_json::Map::new();
|
|
let mut defined = Vec::new();
|
|
for (role, value) in wanted(roles, dimensions, as_drawn) {
|
|
let (entities, ty) = match (create, role) {
|
|
("sketch.line" | "sketch.line.midpoint", "length") => (vec![curve(0)?], "length"),
|
|
("sketch.line" | "sketch.line.midpoint", "angle") => (vec![curve(0)?], "angle"),
|
|
// A rounded rectangle's sides stop at the arcs: its size is between opposite sides.
|
|
("sketch.rectangle.rounded", "width" | "height") => {
|
|
let [a, b] = if role == "width" { ["left", "right"] } else { ["bottom", "top"] };
|
|
let side =
|
|
|k: &str| out["sides"].get(k).filter(|v| v.is_string()).cloned().ok_or_else(|| bad(cmd, "creator did not return its sides"));
|
|
(vec![side(a)?, side(b)?], "distance")
|
|
}
|
|
(c, "width" | "height") if c.starts_with("sketch.rectangle.") => (vec![curve(usize::from(role == "height"))?], "length"),
|
|
(c, "radius" | "diameter") if c.starts_with("sketch.circle.") => (vec![curve(0)?], role),
|
|
("sketch.arc.center_point", "radius" | "sweep") => (vec![curve(0)?], role),
|
|
("sketch.slot.center_to_center" | "sketch.slot.center_point", "length") => (vec![curve(0)?], "length"),
|
|
("sketch.slot.overall", "length") => (vec![curve(1)?, curve(3)?], "overall_length"),
|
|
(c, "width") if c.starts_with("sketch.slot.") => (vec![curve(1)?], "diameter"),
|
|
("sketch.slot.arc_center", "radius" | "sweep") => {
|
|
let centre = out.get("centre_arc").filter(|v| v.is_string()).cloned().ok_or_else(|| bad(cmd, "slot centre arc missing"))?;
|
|
(vec![centre], role)
|
|
}
|
|
(c, "radius") if c.starts_with("sketch.polygon.") => {
|
|
let support =
|
|
out.get("support_circle").filter(|v| v.is_string()).cloned().ok_or_else(|| bad(cmd, "polygon support circle missing"))?;
|
|
(vec![support], "radius")
|
|
}
|
|
_ => return Err(bad(cmd, "unsupported dimension role")),
|
|
};
|
|
let mut dimension = json!({"entities": entities, "type": ty});
|
|
if let Some(sketch) = params.get("sketch") {
|
|
dimension["sketch"] = sketch.clone();
|
|
}
|
|
match value {
|
|
// A typed size the sketch already has (a rectangle drawn onto another's side, the
|
|
// same height) is placed as a driven (reference) dimension rather than refused. A
|
|
// typed `name = expression` defines the parameter `name` (the reply's `defined`).
|
|
Some(value) => {
|
|
dimension["value"] = value.clone();
|
|
dimension["if_redundant"] = json!("driven");
|
|
let result = scratch.execute("sketch.dimension", &dimension)?;
|
|
if let Some(name) = result.get("defined").filter(|v| v.is_string()) {
|
|
defined.push(name.clone());
|
|
}
|
|
added.insert(role.to_string(), result);
|
|
}
|
|
// A size as drawn (no value: the current one) that the sketch already determines, such
|
|
// as the angle of a line that snapped horizontal, is left out; a failed command leaves
|
|
// the sketch as it was.
|
|
None => {
|
|
if let Ok(result) = scratch.execute("sketch.dimension", &dimension) {
|
|
added.insert(role.to_string(), result);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let located = if locate {
|
|
// The freedom left once the shape and its sizes are in: the last reply that reports it.
|
|
let dof = added.values().rev().chain([&out]).find_map(dof_of);
|
|
place_from_origin(&mut scratch, &out, ¶ms, dof)
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
// Session::execute records the wrapper's before snapshot. Nested history must not leak.
|
|
scratch.undo.clear();
|
|
s.accept_computed(scratch);
|
|
out["dimensions"] = Value::Object(added);
|
|
if !defined.is_empty() {
|
|
out["defined"] = Value::Array(defined);
|
|
}
|
|
if locate {
|
|
out["located"] = Value::Array(located);
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// The sketch's degrees of freedom as a command reported them.
|
|
fn dof_of(reply: &Value) -> Option<u64> {
|
|
reply.get("sketch").and_then(|s| s.get("dof")).and_then(Value::as_u64)
|
|
}
|
|
|
|
/// More points than any creator makes: a cap on the work `locate` does.
|
|
const MAX_LOCATED_POINTS: usize = 64;
|
|
|
|
/// What `locate` places a shape with: the directions it measures from the origin, and the
|
|
/// commands that fix a distance along one of them or put a point on one of its axes. The loop in
|
|
/// [`place_from_origin`] only reads this, so a 3D sketch joins with a frame of its own (a third
|
|
/// direction, Z, and its 3D commands) once its geometry can take constraints.
|
|
struct OriginFrame {
|
|
directions: &'static [OriginDirection],
|
|
/// Fixes a point's distance from the origin along a direction: `{entities: ["origin", point], type}`.
|
|
distance_command: &'static str,
|
|
/// Puts a point on an axis: `{a: point, b: axis}`.
|
|
onto_command: &'static str,
|
|
}
|
|
|
|
/// One direction of an [`OriginFrame`].
|
|
struct OriginDirection {
|
|
/// The point's coordinate along this direction.
|
|
along: fn(Vec3) -> f64,
|
|
/// The distance dimension's `type` along it.
|
|
distance: &'static str,
|
|
/// The axis a point at 0 along this direction lies on (a distance of 0 keeps no side).
|
|
onto: &'static str,
|
|
}
|
|
|
|
fn x_of(p: Vec3) -> f64 {
|
|
p.x
|
|
}
|
|
fn y_of(p: Vec3) -> f64 {
|
|
p.y
|
|
}
|
|
|
|
/// A sketch on a plane: horizontal and vertical from its origin; x = 0 is its own y axis, y = 0 its
|
|
/// x axis.
|
|
const PLANE: OriginFrame = OriginFrame {
|
|
directions: &[
|
|
OriginDirection { along: x_of, distance: "horizontal", onto: "y" },
|
|
OriginDirection { along: y_of, distance: "vertical", onto: "x" },
|
|
],
|
|
distance_command: "sketch.dimension",
|
|
onto_command: "sketch.constraint.coincident",
|
|
};
|
|
|
|
/// `locate`: distances from the sketch origin to the shape's points along each direction of the
|
|
/// frame, centres first (a circle, arc or polygon is placed by its centre), each kept only where it
|
|
/// takes freedom away, so a shape anchored to the origin or to placed geometry gets none and the
|
|
/// rest end fully constrained. A point at 0 along a direction goes onto that axis instead. Nothing
|
|
/// here is per creator (it reads the points the creator reports) or per number of directions (it
|
|
/// reads the frame).
|
|
fn place_from_origin(scratch: &mut Session, out: &Value, params: &Value, mut dof: Option<u64>) -> Vec<Value> {
|
|
let frame = &PLANE;
|
|
let mut points: Vec<String> = out["created"]["points"].as_array().into_iter().flatten().filter_map(|p| p.as_str().map(str::to_string)).collect();
|
|
points.sort_by_key(|p| !p.ends_with(".center"));
|
|
let mut placed = Vec::new();
|
|
for point in points.iter().take(MAX_LOCATED_POINTS) {
|
|
for direction in frame.directions {
|
|
if dof == Some(0) {
|
|
return placed;
|
|
}
|
|
let Some(at) = coordinate(scratch, params, point) else { break };
|
|
let (command, mut request) = if (direction.along)(at).abs() < 1e-6 {
|
|
(frame.onto_command, json!({"a": point, "b": direction.onto}))
|
|
} else {
|
|
(frame.distance_command, json!({"entities": ["origin", point], "type": direction.distance}))
|
|
};
|
|
if let Some(sketch) = params.get("sketch") {
|
|
request["sketch"] = sketch.clone();
|
|
}
|
|
// Refused (the sketch already determines it): nothing changed.
|
|
let Ok(result) = scratch.execute(command, &request) else { continue };
|
|
match (dof, dof_of(&result)) {
|
|
(Some(before), Some(after)) if after >= before => {
|
|
// Accepted but redundant (a constraint the sketch already implies): take it back.
|
|
let _ = scratch.execute("edit.undo", &json!({}));
|
|
}
|
|
(_, after) => {
|
|
dof = after.or(dof);
|
|
placed.push(result);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
placed
|
|
}
|
|
|
|
/// Where a point of the target sketch is now, in the frame's coordinates: a plane sketch's
|
|
/// points have z = 0 (a 3D frame would read its points' own z).
|
|
fn coordinate(s: &Session, params: &Value, point: &str) -> Option<Vec3> {
|
|
let id = super::sketch_store::target(s, params, "sketch.draw.dimensioned").ok()?;
|
|
let sketch = super::sketch_store::read(&s.doc, id).ok()?;
|
|
let at = sketch.point(sketch.resolve_point(point)?)?;
|
|
Some(Vec3::new(at.x, at.y, 0.0))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "sketch_dimensioned_tests.rs"]
|
|
mod tests;
|