Sketch: explicit planes take a frame (world or component) and the reply says which (#6000) (#6015)

* claim: #6000

* Sketch: explicit planes take a frame, world or component (#6000)

sketch.create and sketch.redefine take frame: "world" (the default,
as before) or "component" for an explicit {origin, x_dir, y_dir}
plane. The reply says which frame the plane was read in, with a note
when a world plane goes into a moved or turned component. frame on a
named plane or a face is refused: those are where they are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DuBearteandClaude Opus 5.5 authored and GitHub committed 2026-10-25 01:56:10 +00:00
1 parent 2e3f27f06e
commit ab03aa5bc5
4 files changed
+89 -6

No files matched your search

+6 -2
View File
@@ -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<Value> {
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<Value> {
}),
_ => 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)]
+39 -2
View File
@@ -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<PlaneRef> {
})
}
/// `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<Option<&'static str>> {
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<Value> {
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
+5 -2
View File
@@ -409,8 +409,11 @@ pub fn to_local(s: &Session, id: &str, params: &Value) -> Option<Value> {
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);
+39
View File
@@ -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}");
}
}