Construction toggle on every curve type: the selection switches as a whole (slots, polygons; to construction unless all already are), projected curves too; construction curves are drawn dashed and centerlines dash-dot; profiles update at once
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AS5Tn3xieewUSg3uxrMVdL
This commit is contained in:
1 parent
00ddaa25f5
commit
7757e6d9c7
3 files changed
+109
-9
No files matched your search
@@ -149,7 +149,7 @@ pub static COMMANDS: &[CommandSpec] = &[
|
||||
.icon("construction")
|
||||
.key("X")
|
||||
.enabled(in_sketch)
|
||||
.params("curves: [ids], value?: bool (default toggles)"),
|
||||
.params("curves: [ids], value?: bool (default: construction unless all of them already are)"),
|
||||
CommandSpec::new("sketch.delete", "Delete Sketch Entities", sketch_delete)
|
||||
.enabled(in_sketch)
|
||||
.params("entities: [curve, point or constraint ids]"),
|
||||
@@ -1283,17 +1283,23 @@ fn construction(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
let want = bool_(p, "value");
|
||||
let (n, info) = edit(s, p, cmd, false, |sk, _| {
|
||||
let cs: Vec<usize> =
|
||||
ids.iter().map(|id| sk.curve_index(id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))).collect::<Result<_>>()?;
|
||||
// The selection switches together (a slot or polygon as a whole): to construction
|
||||
// unless all of it already is. Projected curves may switch too.
|
||||
let to = want.unwrap_or_else(|| !cs.iter().all(|c| sk.curves.get(*c).is_some_and(|c| c.construction)));
|
||||
let mut n = 0;
|
||||
for id in &ids {
|
||||
let c = sk.curve_index(id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))?;
|
||||
if let Some(cu) = sk.curves.get_mut(c) {
|
||||
cu.construction = want.unwrap_or(!cu.construction);
|
||||
for c in cs {
|
||||
if let Some(cu) = sk.curves.get_mut(c)
|
||||
&& cu.construction != to
|
||||
{
|
||||
cu.construction = to;
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
Ok(n)
|
||||
Ok((n, to))
|
||||
})?;
|
||||
Ok(json!({"changed": n, "sketch": info}))
|
||||
Ok(json!({"changed": n.0, "construction": n.1, "sketch": info}))
|
||||
}
|
||||
|
||||
fn sketch_delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
@@ -198,3 +198,57 @@ fn projected_geometry_counts_as_fixed() {
|
||||
assert_eq!(r["result"]["changed"], 0);
|
||||
assert!(sketch(&s).curve_locked(ci));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Construction toggle
|
||||
|
||||
fn profiles(s: &mut Session) -> i64 {
|
||||
run(s, "sketch.solve", json!({}))["profiles"].as_i64().unwrap_or(-1)
|
||||
}
|
||||
|
||||
fn curve_ids(v: &Value) -> Vec<String> {
|
||||
v["curves"].as_array().map(|a| a.iter().filter_map(|x| x.as_str().map(str::to_string)).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn construction_toggle_takes_shapes_out_of_profiles_and_back() {
|
||||
let mut s = new_sketch();
|
||||
let rect = curve_ids(&run(&mut s, "ShapeRectangleTwoPoint", json!({"p0": [0, 0], "p1": [20, 10]})));
|
||||
let slot = curve_ids(&run(&mut s, "ShapeSlotCenterToCenter", json!({"p0": [40, 5], "p1": [60, 5], "width": 6})));
|
||||
let poly = curve_ids(&run(&mut s, "ShapePolygonInscribed", json!({"center": [0, 40], "radius": 8, "sides": 6})));
|
||||
let circ = curve_ids(&run(&mut s, "CircleCenterRadius", json!({"center": [40, 40], "radius": 5})));
|
||||
let ell = curve_ids(&run(&mut s, "CircleElipse", json!({"center": [70, 40], "major": [80, 40], "minor_radius": 4})));
|
||||
assert_eq!(profiles(&mut s), 5);
|
||||
for (shape, left) in [(&rect, 4), (&slot, 3), (&poly, 2), (&circ, 1), (&ell, 0)] {
|
||||
let r = run(&mut s, "sketch.construction", json!({ "curves": shape }));
|
||||
assert_eq!(r["construction"], true);
|
||||
assert_eq!(profiles(&mut s), left, "{shape:?}");
|
||||
}
|
||||
// Back again in one go.
|
||||
let all: Vec<String> = [rect, slot, poly, circ, ell].concat();
|
||||
let r = run(&mut s, "sketch.construction", json!({ "curves": all }));
|
||||
assert_eq!(r["construction"], false);
|
||||
assert_eq!(profiles(&mut s), 5);
|
||||
// A mixed selection becomes construction as a whole.
|
||||
run(&mut s, "sketch.construction", json!({"curves": ["l1"]}));
|
||||
let r = run(&mut s, "sketch.construction", json!({"curves": ["l1", "l2", "l3", "l4"]}));
|
||||
assert_eq!(r["construction"], true);
|
||||
assert!(sketch(&s).curves.iter().filter(|c| ["l1", "l2", "l3", "l4"].contains(&c.id.as_str())).all(|c| c.construction));
|
||||
assert!(s.execute("sketch.construction", &json!({"curves": ["nope"]})).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn projected_geometry_can_become_construction() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "SketchCreate", json!({"plane": "XY"}));
|
||||
run(&mut s, "ShapeRectangleTwoPoint", json!({"p0": [0, 0], "p1": [20, 10]}));
|
||||
run(&mut s, "SketchStop", json!({}));
|
||||
run(&mut s, "Extrude", json!({"distance": 5}));
|
||||
run(&mut s, "SketchCreate", json!({"plane": "XY"}));
|
||||
run(&mut s, "ProjectNewCmd", json!({"refs": [{"edge": [10, 0, 5]}]}));
|
||||
let id = sketch(&s).curves.iter().find(|c| c.link.is_some()).map(|c| c.id.clone()).expect("projected");
|
||||
let r = run(&mut s, "sketch.construction", json!({ "curves": [id.clone()] }));
|
||||
assert_eq!(r["changed"], 1);
|
||||
let sk = sketch(&s);
|
||||
assert!(sk.curves.iter().any(|c| c.id == id && c.construction && c.link.is_some()));
|
||||
}
|
||||
Reference in new issue
Block a user