SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
fix: an edge joint origin on a body placed by an offset lands on that edge (#6374)
NOWN-DEVa5b640858757
diff --git a/crates/doc/src/joints_edge.rs b/crates/doc/src/joints_edge.rsindex 097d51bbce..c803ca87bf 100644--- a/crates/doc/src/joints_edge.rs+++ b/crates/doc/src/joints_edge.rs@@ -11,7 +11,15 @@use solidcraft_geom::Vec3;use super::{Snap, component_bodies, frame};-use crate::{DocError, Document, Mat, ModelBody, ModelState, Result};+use crate::{DocError, Document, IDENTITY, Mat, ModelBody, ModelState, Result, apply_point, mat_inverse, mat_mul};++/// A body's placement in its component and its inverse: a body moved in from another component+/// (an imported part's) keeps its geometry where it was made, and its offset places it. Edges and+/// faces are looked up in the body's own coordinates and the frame is placed back by the offset.+fn placement(doc: &Document, b: &ModelBody) -> (Mat, Mat) {+ let m = doc.model_body_offset(b).copied().unwrap_or(IDENTITY);+ (m, mat_inverse(&m).unwrap_or(IDENTITY))+}/// Points that close are one corner (as a share of the body's size, at least 1 µm).fn tol(b: &ModelBody) -> f64 {@@ -103,16 +111,20 @@ pub(super) fn edge_frame(doc: &Document, st: &ModelState, comp: u64, snap: &Snaplet Snap::Edge { pick, edge, face, corner } = snap else { return Err(DocError::Invalid("not an edge origin".into())) };let bad = || DocError::Invalid("joint origin: degenerate frame".into());let gone = |what: &str| DocError::Invalid(format!("joint origin: {what} no longer exists (edit the joint to pick again)"));- let found = component_bodies(doc, st, comp).into_iter().find_map(|b| edge_index(b, edge.as_deref(), *pick).map(|i| (b, i)));- let (b, i) = found.ok_or_else(|| match edge {+ let found = component_bodies(doc, st, comp).into_iter().find_map(|b| {+ let (m, inv) = placement(doc, b);+ let local = apply_point(&inv, *pick);+ edge_index(b, edge.as_deref(), local).map(|i| (b, i, local, m))+ });+ let (b, i, pick, placed) = found.ok_or_else(|| match edge {Some(n) => gone(&format!("the edge `{n}`")),None => DocError::Invalid("joint origin: no edge there".into()),})?;let m = b.mesh();let e = m.edges.get(i).ok_or_else(bad)?;- let (_, z, centroid) = face_of(b, i, face.as_deref(), *pick)?;+ let (_, z, centroid) = face_of(b, i, face.as_deref(), pick)?;let flat = |v: Vec3| (v - z * v.dot(z)).normalized();- match corner {+ let local = match corner {None => {let (mid, along) = middle(e).ok_or_else(bad)?;// Into the face: toward its centroid, square to the edge; a face whose centroid lies on@@ -122,7 +134,7 @@ pub(super) fn edge_frame(doc: &Document, st: &ModelState, comp: u64, snap: &Snapframe(mid, z, Some(y.cross(z))).ok_or_else(bad)}Some(other) => {- let j = crate::name_lookup::locate_edge(b, other, *pick).map_err(|_| gone(&format!("the edge `{other}`")))?.edge;+ let j = crate::name_lookup::locate_edge(b, other, pick).map_err(|_| gone(&format!("the edge `{other}`")))?.edge;let f = m.edges.get(j).ok_or_else(bad)?;let t = tol(b);let at = meeting(e, f, t).ok_or_else(|| DocError::Invalid(format!("joint origin: its edges no longer meet at a corner (`{other}`)")))?;@@ -130,7 +142,8 @@ pub(super) fn edge_frame(doc: &Document, st: &ModelState, comp: u64, snap: &Snaplet x = if z.dot(de.cross(df)) >= 0.0 { de } else { df };frame(at, z, Some(x)).ok_or_else(bad)}- }+ }?;+ Ok(mat_mul(&placed, &local))}/// An edge snap for an edge picked at `pick` on component `comp` (its own coordinates): on the@@ -138,11 +151,16 @@ pub(super) fn edge_frame(doc: &Document, st: &ModelState, comp: u64, snap: &Snap/// middle or, with `corner`, at the end nearest it. The edge, the face and the corner's other/// edge are kept by name.pub fn edge_snap(doc: &Document, st: &ModelState, comp: u64, pick: Vec3, on: Option<Vec3>, corner: Option<Vec3>) -> Result<Snap> {- let (b, i) = component_bodies(doc, st, comp)+ let (b, i, inv) = component_bodies(doc, st, comp).into_iter()- .filter_map(|b| nearest_edge(b, pick).filter(|(_, d)| *d <= tol(b)).map(|(i, _)| (b, i)))+ .filter_map(|b| {+ let (_, inv) = placement(doc, b);+ nearest_edge(b, apply_point(&inv, pick)).filter(|(_, d)| *d <= tol(b)).map(|(i, _)| (b, i, inv))+ }).next().ok_or_else(|| DocError::Invalid("joint origin: no edge there".into()))?;+ // The picks in the body's own coordinates; the snap keeps the pick as given.+ let (local, on, corner) = (apply_point(&inv, pick), on.map(|p| apply_point(&inv, p)), corner.map(|c| apply_point(&inv, c)));let names = crate::naming::edge_names(b);let face_names = crate::naming::face_names(b);let m = b.mesh();@@ -158,7 +176,7 @@ pub fn edge_snap(doc: &Document, st: &ModelState, comp: u64, pick: Vec3, on: Opt}f}- None => face_of(b, i, None, pick)?.0,+ None => face_of(b, i, None, local)?.0,};let face = face_names.get(face_index).cloned().filter(|n| !n.is_empty());let corner = match corner {diff --git a/crates/engine/src/cmd/joint_edge_tests.rs b/crates/engine/src/cmd/joint_edge_tests.rsindex 7b0fc9c2c1..e471703825 100644--- a/crates/engine/src/cmd/joint_edge_tests.rs+++ b/crates/engine/src/cmd/joint_edge_tests.rs@@ -106,3 +106,39 @@ fn edge_origins_refuse_bad_picks_and_report_a_lost_edge() {let r = run(&mut s, "joint.solve", json!({}));assert!(r["errors"][0]["why"].as_str().is_some_and(|w| w.contains("no longer exists")), "{r}");}++#[test]+fn an_edge_origin_on_a_body_placed_by_an_offset_lands_on_that_edge() {+ // A 40 x 30 x 5 plate made at the origin, then placed by an offset (turned a quarter about x,+ // moved), as an imported part's body is: its geometry stays where it was made and the offset+ // places it. Edge origins picked where the plate is shown sit on its edges there.+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 5, "corner": [0, 0, 0], "body_name": "Plate"}));+ let offset = solidcraft_doc::rigid(Vec3::new(7.0, 0.0, 3.0), Vec3::ZERO, Vec3::X, std::f64::consts::FRAC_PI_2);+ s.doc_mut().body_offsets.insert("Plate".into(), offset);+ s.refresh();+ let at = |p: Vec3| solidcraft_doc::apply_point(&offset, p);+ let up = solidcraft_doc::apply_vector(&offset, Vec3::Z);+ let st = s.model.state();+ let resolve = |snap: solidcraft_doc::joints::Snap| {+ let o = solidcraft_doc::joints::JointOrigin { occurrence: 0, snap };+ let m = solidcraft_doc::joints::resolve_origin(&s.doc, &st, &o).unwrap_or_else(|e| panic!("{e}"));+ (Vec3::new(m[3][0], m[3][1], m[3][2]), Vec3::new(m[2][0], m[2][1], m[2][2]))+ };+ // The middle of the top face's front edge, and the corner at the plate's own origin.+ let middle = solidcraft_doc::joints::edge_snap(&s.doc, &st, 0, at(Vec3::new(20.0, 0.0, 5.0)), Some(at(Vec3::new(20.0, 10.0, 5.0))), None)+ .unwrap_or_else(|e| panic!("{e}"));+ let (o, z) = resolve(middle);+ assert!(near(o, at(Vec3::new(20.0, 0.0, 5.0))) && near(z, up), "{o:?} {z:?}");+ let corner = solidcraft_doc::joints::edge_snap(+ &s.doc,+ &st,+ 0,+ at(Vec3::new(20.0, 0.0, 5.0)),+ Some(at(Vec3::new(20.0, 10.0, 5.0))),+ Some(at(Vec3::new(1.0, 0.0, 5.0))),+ )+ .unwrap_or_else(|e| panic!("{e}"));+ let (o, z) = resolve(corner);+ assert!(near(o, at(Vec3::new(0.0, 0.0, 5.0))) && near(z, up), "{o:?} {z:?}");+}