Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Joints: a point origin keeps the x it is given (#2597) (#2598)

NOWN-DEVaf436493a241
diff --git a/crates/doc/src/format/pending/joint-origin-point-x.rs b/crates/doc/src/format/pending/joint-origin-point-x.rs
new file mode 100644
index 0000000000..e2f18f0f87
--- /dev/null
+++ b/crates/doc/src/format/pending/joint-origin-point-x.rs
@@ -0,0 +1,4 @@
+//! A point joint origin (`Snap::Point`) can save `x`, the direction its frame's x takes (laid
+//! square to z). Older designs need no transformation: a point origin without `x` takes its x
+//! from z alone, as before; the last release asks for an update instead of dropping the `x` and
+//! turning the part.
diff --git a/crates/doc/src/joints.rs b/crates/doc/src/joints.rs
index 7f26578f42..9b9942b595 100644
--- a/crates/doc/src/joints.rs
+++ b/crates/doc/src/joints.rs
@@ -76,11 +76,14 @@ pub enum Snap {
#[serde(default, skip_serializing_if = "Option::is_none")]
corner: Option<String>,
},
- /// A point, z along `z` (default +Z).
+ /// A point, z along `z` (default +Z) and x toward `x` laid square to z (default: one chosen
+ /// from z alone).
Point {
point: Vec3,
#[serde(default, skip_serializing_if = "Option::is_none")]
z: Option<Vec3>,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ x: Option<Vec3>,
},
}
@@ -175,7 +178,7 @@ impl Snap {
match self {
Snap::Frame { origin, z, x } => origin.is_finite() && z.is_finite() && x.is_none_or(|x| x.is_finite()),
Snap::FaceCenter { pick, x, .. } | Snap::CircleCenter { pick, x, .. } => pick.is_finite() && x_ok(x),
- Snap::Point { point, z } => point.is_finite() && z.is_none_or(|z| z.is_finite()),
+ Snap::Point { point, z, x } => point.is_finite() && z.is_none_or(|z| z.is_finite()) && x.is_none_or(|x| x.is_finite()),
Snap::Edge { pick, .. } => pick.is_finite(),
}
}
@@ -749,7 +752,7 @@ pub fn resolve_origin(doc: &Document, st: &ModelState, o: &JointOrigin) -> Resul
let bad = || DocError::Invalid("joint origin: degenerate frame".into());
match &o.snap {
Snap::Frame { origin, z, x } => frame(*origin, *z, *x).ok_or_else(bad),
- Snap::Point { point, z } => frame(*point, z.unwrap_or(Vec3::Z), None).ok_or_else(bad),
+ Snap::Point { point, z, x } => frame(*point, z.unwrap_or(Vec3::Z), *x).ok_or_else(bad),
Snap::FaceCenter { pick, face, x } => {
// A named planar face gives its own centre and normal; another face (or none) the
// planar face nearest the point.
@@ -1932,7 +1935,7 @@ mod tests {
/// Revolute joints 1…n (joint 1 at `start`, the others at 0) linked 1 → 2 → … → n with
/// ratio 2, the links stored from the end of the chain; joint 3 limited to ±50.
fn chain(n: u64, start: f64) -> Assembly {
- let origin = JointOrigin { occurrence: 0, snap: Snap::Point { point: Vec3::ZERO, z: None } };
+ let origin = JointOrigin { occurrence: 0, snap: Snap::Point { point: Vec3::ZERO, z: None, x: None } };
let joints = (1..=n)
.map(|id| Joint {
id,
diff --git a/crates/doc/src/joints_cam.rs b/crates/doc/src/joints_cam.rs
index e0ad1095d2..16fafc6f5e 100644
--- a/crates/doc/src/joints_cam.rs
+++ b/crates/doc/src/joints_cam.rs
@@ -954,7 +954,7 @@ mod tests {
/// a guide only on the cam's own side is refused.
#[test]
fn the_guide_is_the_side_fewer_moving_joints_from_the_frame() {
- let origin = JointOrigin { occurrence: 0, snap: super::super::Snap::Point { point: Vec3::ZERO, z: None } };
+ let origin = JointOrigin { occurrence: 0, snap: super::super::Snap::Point { point: Vec3::ZERO, z: None, x: None } };
let joint = |id: u64, kind: JointKind, a: u64, b: u64| super::super::Joint {
id,
name: format!("J{id}"),
diff --git a/crates/doc/src/joints_relations.rs b/crates/doc/src/joints_relations.rs
index f22677a343..cd3f5fe0ff 100644
--- a/crates/doc/src/joints_relations.rs
+++ b/crates/doc/src/joints_relations.rs
@@ -82,7 +82,7 @@ mod tests {
use solidcraft_geom::Vec3;
fn joint(kind: JointKind) -> Joint {
- let at = |occurrence| JointOrigin { occurrence, snap: Snap::Point { point: Vec3::ZERO, z: None } };
+ let at = |occurrence| JointOrigin { occurrence, snap: Snap::Point { point: Vec3::ZERO, z: None, x: None } };
Joint {
id: 1,
name: "R".into(),
diff --git a/crates/engine/src/cmd/component_clone_assembly_tests.rs b/crates/engine/src/cmd/component_clone_assembly_tests.rs
index cb0a2e08eb..14475aaadf 100644
--- a/crates/engine/src/cmd/component_clone_assembly_tests.rs
+++ b/crates/engine/src/cmd/component_clone_assembly_tests.rs
@@ -6,7 +6,7 @@ use solidcraft_doc::{IDENTITY, Occurrence};
use solidcraft_geom::Vec3;
fn at(occurrence: u64) -> JointOrigin {
- JointOrigin { occurrence, snap: Snap::Point { point: Vec3::ZERO, z: None } }
+ JointOrigin { occurrence, snap: Snap::Point { point: Vec3::ZERO, z: None, x: None } }
}
fn joint(id: u64, a: u64, b: u64) -> Joint {
diff --git a/crates/engine/src/cmd/joints.rs b/crates/engine/src/cmd/joints.rs
index 6bfccac5d7..8481682ee5 100644
--- a/crates/engine/src/cmd/joints.rs
+++ b/crates/engine/src/cmd/joints.rs
@@ -17,11 +17,11 @@ use crate::{EngineError, Result, Session};
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("joint.origin", "Joint Origin", joint_origin).at("SOLID", "CREATE").icon("joint_origin").params(
- "name?, occurrence?: id|name (default: from the pick, or the named face's component), face: [x,y,z] | \"<name>\" (centre of a planar face) | circle: [x,y,z] | \"<name>\" (centre of a round end: a point on its wall or rim, the end's centre, or the round face's name as model.faces lists it) | edge: [x,y,z] (a point on an edge of a planar face: the edge's middle, z out of the face, y into it; on?: [x,y,z] a point on the face (default: the larger planar face beside the edge); corner?: [x,y,z] near one end: the corner there, x and y along its two edges into the face; found again by name, so it follows the face as it changes size) | point: [x,y,z], z?: [x,y,z]; body?: the named face's body; end?: \"<name>\" (a circle's end: the face across its rim there; default the end nearer the pick); x?: [x,y,z] (a direction, world) | \"<face name>\" | {face: [x,y,z]} (the frame's x: a direction, a plane's normal, or the way to another hole's axis; keeps a round origin's turn). Faces are kept by name: the origin follows its face, and a circle its end, when the design changes",
+ "name?, occurrence?: id|name (default: from the pick, or the named face's component), face: [x,y,z] | \"<name>\" (centre of a planar face) | circle: [x,y,z] | \"<name>\" (centre of a round end: a point on its wall or rim, the end's centre, or the round face's name as model.faces lists it) | edge: [x,y,z] (a point on an edge of a planar face: the edge's middle, z out of the face, y into it; on?: [x,y,z] a point on the face (default: the larger planar face beside the edge); corner?: [x,y,z] near one end: the corner there, x and y along its two edges into the face; found again by name, so it follows the face as it changes size) | point: [x,y,z], z?: [x,y,z]; body?: the named face's body; end?: \"<name>\" (a circle's end: the face across its rim there; default the end nearer the pick); x?: [x,y,z] (a direction, world) | \"<face name>\" | {face: [x,y,z]} (the frame's x: a direction, a plane's normal, or the way to another hole's axis; keeps a round origin's turn; a point's x is a direction, laid square to its z, default: chosen from z alone). Faces are kept by name: the origin follows its face, and a circle its end, when the design changes",
),
CommandSpec::new("joint.create", "Joint", joint).at("SOLID", "ASSEMBLE").icon("joint").key("J").params(
"type: rigid|revolute|slider|cylindrical|pin_slot|planar|ball, or a relation: parallel|perpendicular|angle|point_on_plane|point_on_line|line_on_plane \
- (ties only some directions or points, made where the parts are; several between two parts add up); a, b: joint origins ({occurrence, face|circle|edge|point: [x,y,z] in world, or face|circle: \"<face name>\"; end?, x? as joint.origin} | origin name); \
+ (ties only some directions or points, made where the parts are; several between two parts add up); a, b: joint origins ({occurrence, face|circle|edge|point: [x,y,z] in world, or face|circle: \"<face name>\"; z? for a point, end?, x? as joint.origin} | origin name); \
values?: [angles in deg or expressions, distances]; limits?: [[min, max] | null…]; flip?: bool (default false: the origins are mated, \
b's z against a's as two faces meet; true: b's z along a's, so two origins given at one point with the same z stay as they are); \
offset?: mm or an expression; angle?: deg or an expression (an angle relation's tilt from mated; for other joints the default \
@@ -346,7 +346,20 @@ fn origin_param(s: &Session, v: Option<&Value>, cmd: &str) -> Result<JointOrigin
let (on, corner) = (v.get("on").and_then(vec3).map(local), v.get("corner").and_then(vec3).map(local));
joints::edge_snap(&s.doc, &st, component, local(world), on, corner).map_err(|e| bad(cmd, e.to_string()))?
}
- "point" => Snap::Point { point: local(world), z: v.get("z").and_then(vec3).map(local_dir) },
+ "point" => {
+ let z = v.get("z").and_then(vec3).map(local_dir);
+ let x = match v.get("x") {
+ None | Some(Value::Null) => None,
+ Some(x) => Some(vec3(x).map(local_dir).ok_or_else(|| bad(cmd, "a point origin's `x` is a direction [x, y, z]"))?),
+ };
+ // An x along z (or no direction at all) would leave the frame's turn to chance.
+ if let Some(x) = x
+ && (!x.is_finite() || x.cross(z.unwrap_or(Vec3::Z)).len() <= 1e-9 * x.len().max(1.0))
+ {
+ return Err(bad(cmd, "a point origin's `x` must be a direction off its z axis"));
+ }
+ Snap::Point { point: local(world), z, x }
+ }
_ => {
let f = v.get("frame").cloned().unwrap_or_default();
let z = f.get("z").and_then(vec3).ok_or_else(|| bad(cmd, "`frame` needs a z axis"))?;
@@ -463,7 +476,7 @@ fn circle_centre(s: &Session, occurrence: u64, pick: Vec3) -> Option<Snap> {
.find_map(|b| solidcraft_doc::kernel::cylinder_end_at(&b.body, pick))?;
let along = (pick - c.axis_point).dot(c.axis);
let (at, dir) = if (along - c.start).abs() <= (c.end - along).abs() { (c.start, -1.0) } else { (c.end, 1.0) };
- Some(Snap::Point { point: c.axis_point + c.axis * at, z: Some(c.axis * dir) })
+ Some(Snap::Point { point: c.axis_point + c.axis * at, z: Some(c.axis * dir), x: None })
}
pub(crate) fn joint_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result<u64> {
@@ -1394,6 +1407,9 @@ mod face_tests;
#[path = "joint_frames_tests.rs"]
mod frames_tests;
#[cfg(test)]
+#[path = "joints_point_x_tests.rs"]
+mod point_x_tests;
+#[cfg(test)]
#[path = "joints_reach_tests.rs"]
mod reach_tests;
#[cfg(test)]
diff --git a/crates/engine/src/cmd/joints_face_tests.rs b/crates/engine/src/cmd/joints_face_tests.rs
index 9ef057f721..002c6fdddc 100644
--- a/crates/engine/src/cmd/joints_face_tests.rs
+++ b/crates/engine/src/cmd/joints_face_tests.rs
@@ -51,7 +51,7 @@ fn on_the_hole(s: &Session, w: f64) {
fn origin(s: &Session, joint: &str, a: bool) -> Snap {
let j = s.doc.assembly.joints.iter().find(|j| j.name == joint).map(|j| if a { &j.a } else { &j.b });
- j.map(|o| o.snap.clone()).unwrap_or(Snap::Point { point: Vec3::ZERO, z: None })
+ j.map(|o| o.snap.clone()).unwrap_or(Snap::Point { point: Vec3::ZERO, z: None, x: None })
}
#[test]
@@ -179,8 +179,11 @@ fn a_face_centre_by_name_follows_its_face() {
#[test]
fn an_origin_without_an_end_or_turn_opens_as_before() {
// A design saved before origins named their end and turn: the snap reads without them.
- let snap: Snap = serde_json::from_value(json!({"type": "circle_center", "pick": [1, 2, 3], "face": "F1:side"}))
- .unwrap_or(Snap::Point { point: Vec3::ZERO, z: None });
+ let snap: Snap = serde_json::from_value(json!({"type": "circle_center", "pick": [1, 2, 3], "face": "F1:side"})).unwrap_or(Snap::Point {
+ point: Vec3::ZERO,
+ z: None,
+ x: None,
+ });
assert_eq!(snap, Snap::CircleCenter { pick: Vec3::new(1.0, 2.0, 3.0), face: Some("F1:side".into()), end: None, x: None });
let back: Value = serde_json::to_value(&snap).unwrap_or_default();
assert_eq!(back, json!({"type": "circle_center", "pick": [1.0, 2.0, 3.0], "face": "F1:side"}));
diff --git a/crates/engine/src/cmd/joints_point_x_tests.rs b/crates/engine/src/cmd/joints_point_x_tests.rs
new file mode 100644
index 0000000000..c74b0b8bdb
--- /dev/null
+++ b/crates/engine/src/cmd/joints_point_x_tests.rs
@@ -0,0 +1,59 @@
+//! A point joint origin keeps the x it is given: two origins given at one point with the same z
+//! and x meet as they are, whichever way their parts are turned.
+
+use serde_json::json;
+use solidcraft_geom::Vec3;
+
+use super::tests::{occ, place, run};
+use crate::Session;
+
+/// A 100 x 20 x 10 base at the origin and an 80 x 20 x 10 block on it, the block turned a quarter
+/// turn about z (about (60, 10)).
+fn blocks() -> Session {
+ let mut s = Session::default();
+ run(&mut s, "component.create", json!({"name": "Base"}));
+ run(&mut s, "solid.box", json!({"length": 100, "width": 20, "height": 10, "corner": [0, 0, 0]}));
+ run(&mut s, "component.activate", json!({"component": "root"}));
+ run(&mut s, "component.create", json!({"name": "Block"}));
+ run(&mut s, "solid.box", json!({"length": 80, "width": 20, "height": 10, "corner": [20, 0, 10]}));
+ run(&mut s, "component.activate", json!({"component": "root"}));
+ let (base, block) = (occ(&s, "Base"), occ(&s, "Block"));
+ run(&mut s, "occurrence.ground", json!({"occurrence": base, "grounded": true}));
+ run(&mut s, "occurrence.move", json!({"occurrence": block, "axis": [0, 0, 1], "angle": "90 deg", "origin": [60, 10, 10]}));
+ s
+}
+
+#[test]
+fn origins_given_the_same_x_meet_as_they_are() {
+ let mut s = blocks();
+ let corner = |s: &Session| place(s, "Block", Vec3::new(20.0, 0.0, 10.0));
+ let before = corner(&s);
+ // Both origins at the block's base centre, z up and x along world X: the block is already there.
+ let at = |o: &str| json!({"occurrence": o, "point": [60, 10, 10], "z": [0, 0, 1], "x": [1, 0, 0]});
+ let r = run(&mut s, "joint.create", json!({"type": "rigid", "a": at("Base"), "b": at("Block"), "flip": true}));
+ assert_eq!(r["conflicts"], json!([]), "{r}");
+ assert!(corner(&s).dist(before) < 1e-9, "the block moved from {before:?} to {:?}", corner(&s));
+ // Saved and read back, the x stays.
+ let saved = serde_json::to_string(&s.doc.assembly.joints).unwrap_or_default();
+ assert!(saved.contains("\"x\""), "{saved}");
+ let back: Vec<solidcraft_doc::joints::Joint> = serde_json::from_str(&saved).unwrap_or_default();
+ assert_eq!(back, s.doc.assembly.joints);
+}
+
+#[test]
+fn a_point_origin_without_x_is_placed_as_before_and_a_bad_x_is_refused() {
+ let mut s = blocks();
+ // No x: the frame's x comes from z alone, as designs made before had it (the turned block
+ // turns back a quarter turn to line its frame up with the base's).
+ let at = |o: &str| json!({"occurrence": o, "point": [60, 10, 10], "z": [0, 0, 1]});
+ run(&mut s, "joint.create", json!({"type": "rigid", "a": at("Base"), "b": at("Block"), "flip": true, "name": "J"}));
+ let snap = s.doc.assembly.joints.first().map(|j| j.b.snap.clone());
+ assert!(matches!(snap, Some(solidcraft_doc::joints::Snap::Point { x: None, .. })), "{snap:?}");
+ let far = place(&s, "Block", Vec3::new(100.0, 10.0, 10.0));
+ assert!(far.dist(Vec3::new(100.0, 10.0, 10.0)) < 1e-9, "{far:?}");
+ // An x along z, or no direction at all, is refused.
+ for x in [json!([0, 0, 5]), json!([0, 0, -1]), json!([0, 0, 0])] {
+ let p = json!({"name": "O", "occurrence": "Base", "point": [60, 10, 10], "z": [0, 0, 1], "x": x});
+ assert!(s.execute("joint.origin", &p).is_err(), "{p}");
+ }
+}