SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
joint: let the origin be the design origin or a sketch point (#10290) (#10584)
PapaEchoa9b219bc9f96
diff --git a/crates/ui-egui/src/dialogs_assembly.rs b/crates/ui-egui/src/dialogs_assembly.rsindex 3e34c1d41a..2656a14c05 100644--- a/crates/ui-egui/src/dialogs_assembly.rs+++ b/crates/ui-egui/src/dialogs_assembly.rs@@ -21,7 +21,9 @@ use solidcraft_engine::geom::{Aabb3, Mesh};use crate::SolveApp;use crate::dialogs::{Dialog, Kind, combo, field, form_row};use crate::preview::{Built, Colors};-use crate::selection::{BODIES, EDGES, FACES, SelInput, SelectionExt as _, VERTICES, items as sel_items, point_value as pt, polyline_mid};+use crate::selection::{+ BODIES, EDGES, FACES, ORIGIN, POINTS, SelInput, SelectionExt as _, VERTICES, items as sel_items, point_value as pt, polyline_mid,+};use crate::theme::Tokens;use crate::viewport::Proj;@@ -72,8 +74,9 @@ pub(crate) fn joint_choice(name: &str, kind: JointKind) -> String {format!("{name} ({})", crate::i18n::tr_label(kind_label(kind)))}-/// What a joint origin input accepts (snaps on faces, edges and vertices).-pub const SNAPS: u16 = FACES | EDGES | VERTICES;+/// What a joint origin input accepts: snaps on faces, edges and vertices, the world origin, and+/// a sketch point (so a joint can be placed on the design origin or a hole centre, like Fusion's).+pub const SNAPS: u16 = FACES | EDGES | VERTICES | ORIGIN | POINTS;/// Commands whose effect is where occurrences sit (previewed on the placed model).const MOVES: &[&str] = &["joint.create",@@ -650,13 +653,13 @@ pub fn snap_of(s: &Session, sel: &Sel) -> Option<SnapPt> {let tol = (b.body.size() * 2e-3).max(1e-3);let f = b.body.faces(tol).ok()?.into_iter().find(|f| f.index == *index)?;match f.plane_normal {- Some(n) => (body, SnapPt { at: f.centroid, z: n, kind: SnapKind::FaceCenter, param: json!({"face": pt(*point)}) }),+ Some(n) => (body.as_str(), SnapPt { at: f.centroid, z: n, kind: SnapKind::FaceCenter, param: json!({"face": pt(*point)}) }),None => {let c = solidcraft_engine::kernel::cylinder_face_at(&b.body, *point)?;let sv = (*point - c.axis_point).dot(c.axis);let (at, dir) = if (sv - c.start).abs() <= (c.end - sv).abs() { (c.start, -1.0) } else { (c.end, 1.0) };(- body,+ body.as_str(),SnapPt {at: c.axis_point + c.axis * at,z: c.axis * dir,@@ -679,7 +682,7 @@ pub fn snap_of(s: &Session, sel: &Sel) -> Option<SnapPt> {let z = if out.dot(n) < 0.0 { -n } else { n };// Its round end, kept by name so it follows the hole or boss as the part// changes (a point where it has none).- (body, SnapPt { at: c, z, kind: SnapKind::CircleCenter, param: json!({"circle": pt(c), "z": pt(z)}) })+ (body.as_str(), SnapPt { at: c, z, kind: SnapKind::CircleCenter, param: json!({"circle": pt(c), "z": pt(z)}) })}None => {let mid = polyline_mid(e);@@ -687,7 +690,7 @@ pub fn snap_of(s: &Session, sel: &Sel) -> Option<SnapPt> {.or_else(|| m.edge_faces.get(*index).and_then(|fs| fs.first()).and_then(|f| face_normal_near(&m, *f, mid))).unwrap_or(Vec3::Z);// Its middle, kept by the edge's name.- (body, SnapPt { at: mid, z, kind: SnapKind::EdgeMid, param: json!({"edge": pt(mid), "z": pt(z)}) })+ (body.as_str(), SnapPt { at: mid, z, kind: SnapKind::EdgeMid, param: json!({"edge": pt(mid), "z": pt(z)}) })}}}@@ -696,7 +699,23 @@ pub fn snap_of(s: &Session, sel: &Sel) -> Option<SnapPt> {// as a point's is.let z = occurrence_of_body(s, body).map_or(Vec3::Z, |o| solidcraft_engine::doc::apply_vector(&solidcraft_engine::doc::joints::occurrence_world(&s.doc, o), Vec3::Z));- (body, SnapPt { at: *point, z: Vec3::Z, kind: SnapKind::Vertex, param: json!({"edge": pt(*point), "corner": pt(*point), "z": pt(z)}) })+ (+ body.as_str(),+ SnapPt { at: *point, z: Vec3::Z, kind: SnapKind::Vertex, param: json!({"edge": pt(*point), "corner": pt(*point), "z": pt(z)}) },+ )+ }+ Sel::SketchPoint { id } => {+ // A sketch point's world position, its plane's normal as the joint's z, kept by its+ // sketch and id so it follows the sketch as the part changes. No body names it, so the+ // engine finds the occurrence from the world point (its own body).+ let (sketch, point) = match id.split_once(':') {+ Some((sketch, point)) => (sketch.parse().ok()?, point),+ None => (s.active_sketch?, id.as_str()),+ };+ let ss = st.sketch(sketch)?;+ let at = ss.plane.to_world(ss.sketch.point(ss.sketch.resolve_point(point)?)?);+ let z = ss.plane.normal();+ ("", SnapPt { at, z, kind: SnapKind::Vertex, param: json!({"point": pt(at), "z": pt(z)}) })}_ => return None,};diff --git a/crates/ui-egui/src/dialogs_assembly_tests.rs b/crates/ui-egui/src/dialogs_assembly_tests.rsindex 90630f4fce..f0e8057f98 100644--- a/crates/ui-egui/src/dialogs_assembly_tests.rs+++ b/crates/ui-egui/src/dialogs_assembly_tests.rs@@ -60,7 +60,46 @@ fn joint_from_two_snaps() {assert_eq!(app.session.doc.assembly.joints.len(), 1);}-/// A straight edge is the revolute axis, even when both adjacent faces are perpendicular to it.+/// A joint origin can be the design origin or a sketch point, not just faces, edges and vertices+/// (#10290). The origin snaps to the world origin with z up; a sketch point snaps to its world+/// position with the sketch plane's normal as z, and both make a joint.+#[test]+fn joint_origin_can_be_the_origin_or_a_sketch_point() {+ let mut app = two_boxes();+ // A sketch point on B's top face, at world (35, 5, 10).+ app.session.execute("component.activate", &json!({"component": "B"})).unwrap();+ app.session+ .execute("sketch.create", &json!({"plane": {"origin": [30.0, 0.0, 10.0], "x_dir": [1.0, 0.0, 0.0], "y_dir": [0.0, 1.0, 0.0]}, "name": "S"}))+ .unwrap();+ app.session.execute("sketch.point", &json!({"point": [5.0, 5.0], "id": "q"})).unwrap();+ app.session.execute("sketch.finish", &json!({})).unwrap();+ let sketch = app.session.doc.find_feature("S").unwrap().id;++ // The two picks a joint origin input now accepts.+ let origin = Sel::Vertex { body: String::new(), point: Vec3::ZERO };+ let sketch_point = Sel::SketchPoint { id: format!("{sketch}:q") };+ let so = |s: &Sel| snap_of(&app.session, s).expect("a snap for the pick");+ let o = so(&origin);+ let sp = so(&sketch_point);+ assert!(o.at.dist(Vec3::ZERO) < 1e-6, "the world origin snaps to the origin: {o:?}");+ assert!(o.z.dist(Vec3::Z) < 1e-9, "the origin's z is up: {o:?}");+ assert!(sp.at.dist(Vec3::new(35.0, 5.0, 10.0)) < 1e-6, "the sketch point snaps to its world position: {sp:?}");+ assert!(sp.z.dist(Vec3::Z) < 1e-9, "the sketch point's z is the plane normal: {sp:?}");++ // And a joint can be made from them.+ app.start("joint.create");+ let mut d = app.dialog.clone().unwrap();+ d.pick(&app.session, origin);+ d.pick(&app.session, sketch_point);+ if let Kind::Assembly(Asm::Joint(f)) = &mut d.kind {+ f.kind = 1;+ f.fit_limits(&app.session.doc);+ }+ for (id, p) in apply_commands(&app, &d).unwrap() {+ app.run(&id, p).unwrap();+ }+ assert_eq!(app.session.doc.assembly.joints.len(), 1, "a joint was made from the origin and the sketch point");+}#[test]fn revolute_joint_uses_the_picked_straight_edges() {let mut app = two_boxes();