SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Mirror: use an origin axis that is not in the sketch plane (#8201) (#12091)
SixSolidabeb232f174a
diff --git a/assets/locales/pending/sketch-mirror-origin-axis-projection.json b/assets/locales/pending/sketch-mirror-origin-axis-projection.jsonnew file mode 100644index 0000000000..34ef183f8d--- /dev/null+++ b/assets/locales/pending/sketch-mirror-origin-axis-projection.json@@ -0,0 +1,26 @@+{+ "en": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "The origin axis is perpendicular to the sketch plane, so it has no line in the plane"+ },+ "ar": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "محور الأصل عمودي على مستوى الرسم، لذا ليس له خط في المستوى"+ },+ "de": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "Die Ursprungsachse steht senkrecht auf der Skizzenebene und hat daher keine Linie in ihr"+ },+ "he": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "ציר הראשית ניצב למישור הסקיצה, ולכן אין לו קו במישור"+ },+ "ja": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "原点軸はスケッチ平面に垂直なため、平面内に線がありません"+ },+ "ru": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "Ось начала координат перпендикулярна плоскости эскиза, поэтому линии в этой плоскости нет"+ },+ "vi": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "Trục gốc vuông góc với mặt phẳng phác thảo nên không có đường thẳng trong mặt phẳng này"+ },+ "zh": {+ "The origin axis is perpendicular to the sketch plane, so it has no line in the plane": "原点轴垂直于草图平面,因此在该平面内没有直线"+ }+}diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rsindex d11f630041..f64a8b6ba8 100644--- a/crates/engine/src/cmd/sketch_modify.rs+++ b/crates/engine/src/cmd/sketch_modify.rs@@ -99,7 +99,7 @@ pub static COMMANDS: &[CommandSpec] = &[.at("SKETCH", "PATTERN").icon("sketch_mirror").enabled(in_sketch)- .params(concat!("entities: [curve/point ids], line: straight curve id, x / y for a sketch axis, or {axis: X|Y|Z} for an origin axis in the sketch plane (symmetry constraints are added)", created_doc!())),+ .params(concat!("entities: [curve/point ids], line: straight curve id, x / y for a sketch axis, or {axis: X|Y|Z} for an origin axis (the mirror line is the axis's projection into the sketch plane; an axis perpendicular to the plane is refused) (symmetry constraints are added)", created_doc!())),CommandSpec::new("sketch.pattern.circular", "Circular Pattern", pattern_circular).at("SKETCH", "PATTERN").icon("sketch_circ_pattern")@@ -3430,7 +3430,17 @@ fn mirror(s: &mut Session, p: &Value) -> Result<Value> {sk.links.iter().find(|link| link.kind == solidcraft_sketch::LinkKind::Project && link.source == source).map(|link| link.id.clone());let link = match link {Some(link) => link,- None => sk.add_link(solidcraft_sketch::LinkKind::Project, source, &geom)?,+ None => {+ let link = sk.add_link(solidcraft_sketch::LinkKind::Project, source, &geom)?;+ // A mirror line is reference geometry: it stays out of profiles, as the+ // sketch's own axes do.+ for c in sk.link_curves(&link) {+ if let Some(curve) = sk.curves.get_mut(c) {+ curve.construction = true;+ }+ }+ link+ }};sk.link_curves(&link).first().copied().ok_or_else(|| bad(cmd, "the origin axis does not resolve to a straight line"))?} else {diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rsindex f3030ebf68..c7e0696705 100644--- a/crates/engine/src/cmd/sketch_modify_tests.rs+++ b/crates/engine/src/cmd/sketch_modify_tests.rs@@ -1,7 +1,7 @@use serde_json::{Value, json};use solidcore_record::Vec2;use solidcraft_geom::prelude::*;-use solidcraft_sketch::{ConstraintKind, CurveKind};+use solidcraft_sketch::{ConstraintKind, CurveKind, LinkSource};use crate::Session;@@ -4845,6 +4845,51 @@ fn mirror_origin_axis_in_rotated_plane_reuses_projection_and_rejects_invalid_lin}}+/// #8201: a sketch on a face or on any other plane that an origin axis does not lie in still+/// mirrors about that axis, using the axis's projection into the sketch plane.+#[test]+fn mirror_uses_the_projection_of_an_origin_axis_outside_the_sketch_plane() {+ // Slanted through the origin: world X is outside the plane, and its projection is the+ // sketch's y line, so a mirror about it turns the x coordinate around.+ let mut s = Session::default();+ run(&mut s, "sketch.create", json!({"plane":{"origin":[0,0,0],"x_dir":[0,1,-1],"y_dir":[-2,1,1]}}));+ run(&mut s, "sketch.line", json!({"points":[[10,3],[15,8]],"ids":["source"]}));+ let out = run(&mut s, "sketch.mirror", json!({"entities":["source"],"line":{"axis":"X"}}));+ let sk = sketch(&s);+ let copy = out["curves"][0].as_str().unwrap();+ for (source_end, copy_end) in [("start", "end"), ("end", "start")] {+ let source = sk.point(sk.resolve_point(&format!("source.{source_end}")).unwrap()).unwrap();+ let copy = sk.point(sk.resolve_point(&format!("{copy}.{copy_end}")).unwrap()).unwrap();+ assert!(copy.dist(Vec2::new(-source.x, source.y)) < 1e-6, "{source:?}: {copy:?}");+ }+ let link = sk.links.iter().find(|l| matches!(&l.source, LinkSource::Axis { name } if name == "X")).expect("the projected X axis is a link");+ let line = sk.link_curves(&link.id)[0];+ assert!(sk.curves[line].construction && sk.is_linked_curve(line));+ match sk.shape(line) {+ Some(solidcraft_sketch::Shape::Line { a, b }) => {+ assert!(a.x.abs() < 1e-6 && b.x.abs() < 1e-6, "the projected X axis is the sketch y: {a:?} {b:?}");+ }+ other => panic!("the projected X axis is not a line: {other:?}"),+ }+ // A second mirror reuses the same projected axis instead of adding another.+ run(&mut s, "sketch.mirror", json!({"entities":["source"],"line":{"axis":"X"}}));+ assert_eq!(sketch(&s).links.len(), 1);++ // Offset above the origin, as a sketch on a part's top face: the projection is still the+ // world X line, which is the sketch's own x here.+ let mut s = Session::default();+ run(&mut s, "sketch.create", json!({"plane":{"origin":[0,0,10],"x_dir":[1,0,0],"y_dir":[0,1,0]}}));+ run(&mut s, "sketch.line", json!({"points":[[10,3],[15,8]],"ids":["source"]}));+ let out = run(&mut s, "sketch.mirror", json!({"entities":["source"],"line":{"axis":"X"}}));+ let sk = sketch(&s);+ let copy = out["curves"][0].as_str().unwrap();+ for (source_end, copy_end) in [("start", "end"), ("end", "start")] {+ let source = sk.point(sk.resolve_point(&format!("source.{source_end}")).unwrap()).unwrap();+ let copy = sk.point(sk.resolve_point(&format!("{copy}.{copy_end}")).unwrap()).unwrap();+ assert!(copy.dist(Vec2::new(source.x, -source.y)) < 1e-6, "{source:?}: {copy:?}");+ }+}+#[test]fn corner_tools_refuse_all_spatial_curves_with_an_unsupported_message() {let mut s = Session::default();diff --git a/crates/engine/src/cmd/sketch_project.rs b/crates/engine/src/cmd/sketch_project.rsindex 915b596bd5..25949c0934 100644--- a/crates/engine/src/cmd/sketch_project.rs+++ b/crates/engine/src/cmd/sketch_project.rs@@ -251,7 +251,8 @@ pub(super) mod refresh_count {}}-/// Resolve an origin axis as a mirror line, keeping its projection linked and fixed.+/// Resolve an origin axis as a mirror line: the axis's projection into the sketch plane, kept+/// linked and fixed. An axis perpendicular to the plane has no line in it and is refused.pub(super) fn origin_axis_line(s: &Session, p: &Value, line: &Value, cmd: &str) -> Result<(LinkSource, Vec<solidcraft_sketch::LinkGeom>)> {let id = sketch_store::target(s, p, cmd)?;let doc = placed_candidate(s, &s.doc);@@ -266,8 +267,10 @@ pub(super) fn origin_axis_line(s: &Session, p: &Value, line: &Value, cmd: &str)"Z" => Vec3::Z,_ => return Err(bad(cmd, "the origin axis must be X, Y or Z")),};- if plane.normal().dot(dir).abs() > 1e-9 || plane.normal().dot(plane.origin).abs() > 1e-6 {- return Err(bad(cmd, "the origin axis must lie in the sketch plane"));+ // The resolve below projects the axis into the plane: its direction there is the plane's+ // share of `dir`, and empty means a point, not a mirror line.+ if Vec2::new(dir.dot(plane.x), dir.dot(plane.y)).len() <= 1e-9 {+ return Err(bad(cmd, "the origin axis is perpendicular to the sketch plane, so it has no line in it"));}let (vals, _) = doc.param_values();let r = resolve(&doc, &vals, &st, &plane, LinkKind::Project, &src)?;diff --git a/crates/ui-egui/src/sketch_modify_ui.rs b/crates/ui-egui/src/sketch_modify_ui.rsindex f813c22dcf..c2fe2351fa 100644--- a/crates/ui-egui/src/sketch_modify_ui.rs+++ b/crates/ui-egui/src/sketch_modify_ui.rs@@ -965,15 +965,11 @@ pub fn commands(app: &SolveApp, k: &Mod, inputs: &[SelInput], extra: &Map<StringSome(sel @ Sel::Axis { name }) if matches!(name.as_str(), "X" | "Y" | "Z") => {if let Some(axis) = crate::tools::sketch_axis_of(app, name) {json!(axis)- } else {- let (origin, dir) = crate::dialogs::axis_of(app, sel).ok_or("Select an origin axis")?;- let st = crate::open_meshing::session_world(&app.session);- let plane =- st.sketch(app.session.active_sketch.ok_or("No sketch is being edited")?).ok_or("The sketch no longer exists")?.plane;- if plane.normal().dot(dir).abs() > 1e-9 || plane.normal().dot(origin - plane.origin).abs() > 1e-6 {- return Err("The origin axis must lie in the sketch plane".into());- }+ } else if sketch_dir(app, sel).is_some() {+ // An axis outside the plane still gives the line it makes in the plane.json!({"axis":name})+ } else {+ return Err("The origin axis is perpendicular to the sketch plane, so it has no line in the plane".into());}}Some(_) => return Err("The mirror line must be a straight curve of this sketch or an origin axis in its plane".into()),diff --git a/crates/ui-egui/src/sketch_modify_ui_tests.rs b/crates/ui-egui/src/sketch_modify_ui_tests.rsindex 3958145db9..ced008fbf2 100644--- a/crates/ui-egui/src/sketch_modify_ui_tests.rs+++ b/crates/ui-egui/src/sketch_modify_ui_tests.rs@@ -944,3 +944,30 @@ fn mirror_origin_axis_picks_use_the_same_reference_as_symmetry() {assert_eq!(cmds[0].1["line"], json!({"axis":"Y"}));h.app.run(&cmds[0].0, cmds[0].1.clone()).unwrap();}++/// #8201: on a sketch whose plane does not hold the picked origin axis, Mirror sends the axis+/// and the engine mirrors about its projection into the sketch plane.+#[test]+fn mirror_sends_an_origin_axis_outside_the_sketch_plane_for_its_projection() {+ let mut h = Harness::new();+ h.app.run("sketch.create", json!({"plane":{"origin":[0,0,0],"x_dir":[0,1,-1],"y_dir":[-2,1,1]}})).unwrap();+ h.app.run("sketch.line", json!({"points":[[10,3],[15,8]],"ids":["source"]})).unwrap();+ let mut inputs = mirror_inputs();+ inputs[0].items = vec![Sel::SketchCurve { id: "source".into(), sketch: None }];+ inputs[1].items = vec![Sel::Axis { name: "X".into() }];+ let cmds = commands(&h.app, &Mod::Mirror, &inputs, &serde_json::Map::new()).unwrap();+ assert_eq!(cmds[0].1, json!({"entities":["source"], "line":{"axis":"X"}}));+ let out = h.app.run(&cmds[0].0, cmds[0].1.clone()).unwrap();+ assert_eq!(out["curves"].as_array().map(Vec::len), Some(1));+ let state = h.app.session.model.state();+ let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch;+ let copy = out["curves"][0].as_str().unwrap();+ let start = sk.point(sk.resolve_point(&format!("{copy}.start")).unwrap()).unwrap();+ let end = sk.point(sk.resolve_point(&format!("{copy}.end")).unwrap()).unwrap();+ let (start, end) = ((start.x, start.y), (end.x, end.y));+ let mirrored = |p: (f64, f64), x: f64, y: f64| (p.0 - x).abs() < 1e-6 && (p.1 - y).abs() < 1e-6;+ assert!(+ (mirrored(start, -10.0, 3.0) && mirrored(end, -15.0, 8.0)) || (mirrored(start, -15.0, 8.0) && mirrored(end, -10.0, 3.0)),+ "{start:?} {end:?}"+ );+}