diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rs index 07dedf807..263872d39 100644 --- a/crates/engine/src/cmd/browser.rs +++ b/crates/engine/src/cmd/browser.rs @@ -21,7 +21,7 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("body.rename", "Rename Body", rename_body).icon("rename").params("body: its name, name: the new name (features that use the body follow)"), CommandSpec::new("sketch.redefine", "Redefine Sketch Plane", redefine_sketch) .icon("plane") - .params("sketch: id or name; plane: as sketch.create; author_path?: exact owner occurrence ids; explicitly rebinds or freezes the plane while preserving geometry and dimension names"), + .params("sketch: id or name; plane: as sketch.create; frame?: as sketch.create; author_path?: exact owner occurrence ids; explicitly rebinds or freezes the plane while preserving geometry and dimension names"), CommandSpec::new("browser.group", "New Group", group) .icon("folder") .params("folder: bodies|sketches|construction; component?: id (default 0); name?; items?: [keys] (they leave other groups)"), @@ -900,6 +900,8 @@ fn redefine_sketch(s: &mut Session, p: &Value) -> Result { let component = feature.component; let index = s.doc.feature_index(id).ok_or_else(|| bad(cmd, "sketch disappeared"))?; let author_path = super::sketch::author_path_for_rebind(s, feature, p, cmd)?; + let frame = super::sketch::plane_frame(p, cmd)?; + let placement = solidcraft_doc::EmbossFace::component_frame(&s.placed_document(), component, &author_path)?; let marker = s.doc.marker; let mut baseline = s.scratch(); baseline.doc_mut().marker = Some(index + 1); @@ -984,7 +986,9 @@ fn redefine_sketch(s: &mut Session, p: &Value) -> Result { }), _ => None, }); - Ok(json!({ "sketch": id,"attachment":attachment })) + let mut out = json!({ "sketch": id,"attachment":attachment }); + super::sketch::frame_reply(&mut out, frame, &placement); + Ok(out) } #[cfg(test)] diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 2062fc7b1..ff28f854c 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -29,7 +29,7 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("sketch.create", "Create Sketch", create_sketch) .at("SOLID", "CREATE") .icon("sketch") - .params("plane: XY|XZ|YZ | construction plane name or id | {origin?: [x,y,z] numbers in mm, default [0,0,0] (not an expression), x_dir, y_dir} | {face: [world x,y,z], body?: exact world body, index?: face index, path?: complete occurrence ids, fixed?: bool}; new face attachments require a unique certified source (a flat face of a sheet metal body attaches to its panel instead, in the flat pattern's coordinates), fixed:true explicitly freezes the selected plane; offset?: expr, flip?: bool | \"x\" | \"y\" (not on a face: sketch the plane from its other side, so the normal and the default extrude direction are reversed; a half turn about the plane's own x or y axis, true picks the one keeping the sketch's y axis upward; offset still moves along the unflipped normal), project_edges?: bool, name?"), + .params("plane: XY|XZ|YZ | construction plane name or id | {origin?: [x,y,z] numbers in mm, default [0,0,0] (not an expression), x_dir, y_dir} (in world coordinates unless frame says otherwise; a named plane is the active component's own) | {face: [world x,y,z], body?: exact world body, index?: face index, path?: complete occurrence ids, fixed?: bool}; new face attachments require a unique certified source (a flat face of a sheet metal body attaches to its panel instead, in the flat pattern's coordinates), fixed:true explicitly freezes the selected plane; offset?: expr, flip?: bool | \"x\" | \"y\" (not on a face: sketch the plane from its other side, so the normal and the default extrude direction are reversed; a half turn about the plane's own x or y axis, true picks the one keeping the sketch's y axis upward; offset still moves along the unflipped normal), frame?: world (default) | component (which coordinates an explicit plane is in; the reply's `frame` says which, with a `note` when the active component is moved or turned), project_edges?: bool, name?"), CommandSpec::new("sketch.edit", "Edit Sketch", edit_sketch).noundo().icon("sketch").params("sketch: id or name"), CommandSpec::new("sketch.finish", "Finish Sketch", finish_sketch).at("SKETCH", "FINISH SKETCH").icon("finish").enabled(in_sketch).noundo(), CommandSpec::new("sketch.cancel", "Cancel Sketch", cancel_sketch) @@ -666,6 +666,40 @@ pub(super) fn plane_ref(s: &Session, p: &Value, cmd: &str) -> Result { }) } +/// `frame` of `sketch.create` and `sketch.redefine` (#6000): which coordinates an explicit +/// `{origin, x_dir, y_dir}` plane is in, `world` (the default) or the component's own. `None` +/// for a named plane or a face, which are where they are. +pub(super) fn plane_frame(p: &Value, cmd: &str) -> Result> { + let explicit = p.get("plane").is_some_and(Value::is_object) && !face::is_face(p); + let frame = match p.get("frame") { + None | Some(Value::Null) => None, + Some(Value::String(f)) if f == "world" => Some("world"), + Some(Value::String(f)) if f == "component" => Some("component"), + Some(_) => return Err(bad(cmd, "`frame` must be \"world\" or \"component\"")), + }; + match (explicit, frame) { + (true, f) => Ok(Some(f.unwrap_or("world"))), + (false, None) => Ok(None), + (false, Some(_)) => { + Err(bad(cmd, "`frame` is for an explicit plane {origin, x_dir, y_dir}: a named plane is the component's own, and a face is where it is")) + } + } +} + +/// The reply's `frame` for an explicit plane, with a note when a world plane went into a moved +/// or turned component (`placement`: that component's), where it differs from the component's own. +pub(super) fn frame_reply(out: &mut Value, frame: Option<&str>, placement: &solidcraft_doc::Mat) { + let Some(o) = out.as_object_mut() else { return }; + let Some(frame) = frame else { return }; + o.insert("frame".into(), json!(frame)); + if frame == "world" && !solidcraft_doc::is_identity(placement) { + o.insert( + "note".into(), + json!("the plane was read in world coordinates and the component is moved or turned, so it is stored at that world place in the component; frame: \"component\" reads it in the component's own coordinates"), + ); + } +} + /// Does triangle set of face `fi` contain point `p` (within `tol` of the face plane)? pub(super) fn face_contains(m: &solidcraft_geom::Mesh, fi: usize, p: Vec3, tol: f64) -> bool { m.triangles.iter().zip(&m.tri_face).filter(|(_, f)| **f as usize == fi).any(|(t, _)| { @@ -699,11 +733,14 @@ fn create_sketch(s: &mut Session, p: &Value) -> Result { return Err(bad(cmd, format!("`{key}` belongs inside `plane`: {example}"))); } } + let frame = plane_frame(p, cmd)?; let mut plane = plane_ref(s, p, cmd)?; if let Some(about_y) = flip_param(s, p, &plane, cmd)? { plane = PlaneRef::Flipped { base: Box::new(plane), about_y }; } - create_sketch_with_plane(s, p, plane, None) + let mut out = create_sketch_with_plane(s, p, plane, None)?; + frame_reply(&mut out, frame, &s.active_component_frame()); + Ok(out) } /// `flip` of `sketch.create`: `None` keeps the plane's own side, otherwise the axis to turn diff --git a/crates/engine/src/frames.rs b/crates/engine/src/frames.rs index 611739b91..bd3a85efc 100644 --- a/crates/engine/src/frames.rs +++ b/crates/engine/src/frames.rs @@ -409,8 +409,11 @@ pub fn to_local(s: &Session, id: &str, params: &Value) -> Option { out["params"] = inner; return Some(out); } - // Exact face capture retains the original world pick and occurrence identity. - if matches!(id, "sketch.create" | "sketch.redefine") && params.get("plane").and_then(|plane| plane.get("face")).is_some() { + // Exact face capture retains the original world pick and occurrence identity; an explicit + // plane with `frame: "component"` is already in the component's own coordinates (#6000). + if matches!(id, "sketch.create" | "sketch.redefine") + && (params.get("plane").and_then(|plane| plane.get("face")).is_some() || params.get("frame").and_then(Value::as_str) == Some("component")) + { return None; } let keys = table(id); diff --git a/crates/engine/src/frames_tests.rs b/crates/engine/src/frames_tests.rs index 52adb9556..ff6ee83f3 100644 --- a/crates/engine/src/frames_tests.rs +++ b/crates/engine/src/frames_tests.rs @@ -286,3 +286,42 @@ fn root_picks_on_a_moved_component_build_where_it_is_shown() { let across = crate::view::construction_planes(&s).into_iter().find(|(_, n, _)| n == "Across").unwrap().2; assert!((across.origin.y - 40.0).abs() < 1e-6, "{across:?}"); } + +/// #6000: in a turned component, a named plane ("XY") is the component's own; an explicit +/// `{origin, x_dir, y_dir}` is world unless `frame: "component"` says otherwise. The reply says +/// which, with a note when the two differ. +#[test] +fn an_explicit_sketch_plane_is_read_in_the_frame_asked_for() { + let mut s = moved_part(); + run(&mut s, "occurrence.move", json!({"occurrence": "Part:1", "axis": [1, 0, 0], "angle": "30 deg", "move_grounded": true})); + let explicit = json!({"origin": [0, 0, 0], "x_dir": [1, 0, 0], "y_dir": [0, 1, 0]}); + let centre = |s: &mut Session, name: &str, p: Value| { + let r = run(s, "sketch.create", p); + run(s, "sketch.circle.center", json!({"center": [20, 10], "radius": 6})); + run(s, "sketch.finish", json!({})); + (r, *sketch_world_points(s, name).last().unwrap()) + }; + let (named, own) = centre(&mut s, "Named", json!({"plane": "XY", "name": "Named"})); + assert!(named.get("frame").is_none(), "{named}"); + assert!((own.z - 5.0).abs() < 1e-6, "the component's XY is turned with it: {own:?}"); + + let (world, at) = centre(&mut s, "World", json!({"plane": explicit, "name": "World"})); + assert_eq!(world["frame"], "world"); + assert!(world["note"].as_str().is_some_and(|n| n.contains("frame")), "{world}"); + assert!(at.z.abs() < 1e-6, "today's reading: on the world XY plane: {at:?}"); + + let (comp, at) = centre(&mut s, "Own", json!({"plane": explicit, "frame": "component", "name": "Own"})); + assert_eq!(comp["frame"], "component"); + assert!(comp.get("note").is_none(), "{comp}"); + assert!(near(at, own), "the component's XY written out: {at:?} vs {own:?}"); + + // Redefine reads the same way. + let r = run(&mut s, "sketch.redefine", json!({"sketch": "World", "plane": explicit, "frame": "component"})); + assert_eq!(r["frame"], "component"); + assert!(near(*sketch_world_points(&mut s, "World").last().unwrap(), own)); + + for (p, why) in [(json!({"plane": "XY", "frame": "component"}), "explicit"), (json!({"plane": explicit, "frame": "local"}), "world")] { + let e = s.execute("sketch.create", &p).expect_err("refused").to_string(); + assert!(e.contains(why), "{p}: {e}"); + } +}