Skip to content

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

Joints: a typed joint that closes a loop starts where its parts are (#6061) (#10336)

raykholoe91f5d73bd08
diff --git a/crates/doc/src/joints_relations.rs b/crates/doc/src/joints_relations.rs
index cd3f5fe0ff..c3c0f1a0cf 100644
--- a/crates/doc/src/joints_relations.rs
+++ b/crates/doc/src/joints_relations.rs
@@ -16,14 +16,11 @@ const TURNS: [f64; 4] = [0.0, std::f64::consts::FRAC_PI_2, std::f64::consts::PI,
/// Starts tried at most (angle values beyond the first three start at 0).
const MAX_STARTS: usize = 64;
-/// The values of relation `j` that move B least from where it is now: B's joint frame as it sits
-/// in A's, met as nearly as the relation allows (its origin as near as it can be to where it is,
-/// and its axes, each weighed as a point a part's size along it). `None` for a joint that is not a relation, or whose
-/// origins can't be found.
+/// The values of joint `j` that move B least from where it is now: B's joint frame as it sits
+/// in A's, met as nearly as the joint allows (its origin as near as it can be to where it is,
+/// and its axes, each weighed as a point a part's size along it). Used for relations and for
+/// typed joints that close a loop. `None` when the origins or finite values can't be found.
pub fn relation_values(doc: &Document, st: &ModelState, j: &Joint) -> Option<Vec<f64>> {
- if !j.kind.is_relation() {
- return None;
- }
let world = |occ: u64| if occ == 0 { IDENTITY } else { occurrence_world(doc, occ) };
let fa = mat_mul(&world(j.a.occurrence), &resolve_origin(doc, st, &j.a).ok()?);
let fb = mat_mul(&world(j.b.occurrence), &resolve_origin(doc, st, &j.b).ok()?);
diff --git a/crates/engine/src/cmd/joints.rs b/crates/engine/src/cmd/joints.rs
index 2099373278..8d9a09fd6b 100644
--- a/crates/engine/src/cmd/joints.rs
+++ b/crates/engine/src/cmd/joints.rs
@@ -697,11 +697,47 @@ fn check_sides(a: &JointOrigin, b: &JointOrigin, cmd: &str) -> Result<()> {
Ok(())
}
+/// A path already joins the two sides, including paths through grounded occurrences.
+fn closes_loop(s: &Session, j: &Joint) -> bool {
+ let mut at: BTreeMap<u64, Vec<u64>> = BTreeMap::new();
+ for joint in s.doc.assembly.joints.iter().filter(|j| !j.suppressed).take(joints::MAX_JOINTS) {
+ at.entry(joint.a.occurrence).or_default().push(joint.b.occurrence);
+ at.entry(joint.b.occurrence).or_default().push(joint.a.occurrence);
+ }
+ // The solver fixes the root and every grounded occurrence in the same world frame.
+ for o in s.doc.occurrences.iter().filter(|o| o.grounded) {
+ at.entry(0).or_default().push(o.id);
+ at.entry(o.id).or_default().push(0);
+ }
+ let mut seen = BTreeSet::from([j.a.occurrence]);
+ let mut pending = vec![j.a.occurrence];
+ while let Some(occ) = pending.pop() {
+ if occ == j.b.occurrence {
+ return true;
+ }
+ for other in at.get(&occ).into_iter().flatten() {
+ if seen.insert(*other) {
+ pending.push(*other);
+ }
+ }
+ }
+ false
+}
+
fn add_joint(s: &mut Session, mut j: Joint, p: &Value) -> Result<Value> {
if s.doc.assembly.joints.len() >= 10_000 {
return Err(EngineError::Other("too many joints".into()));
}
check_sides(&j.a, &j.b, "joint.create")?;
+ // Relations and loop-closing typed joints start where their parts are. A typed joint in
+ // an open chain keeps its usual initial values; explicitly supplied values also keep theirs.
+ if p.get("values").is_none()
+ && p.get("value").is_none()
+ && (j.kind.is_relation() || closes_loop(s, &j))
+ && let Some(q) = joints::relation_values(&s.doc, &s.model.state(), &j)
+ {
+ j.values = q;
+ }
// It starts at its values inside its limits. A value moved to a limit moves the part as it
// is joined, which the answer says.
let mut q = j.values.clone();
@@ -798,13 +834,6 @@ fn joint(s: &mut Session, p: &Value) -> Result<Value> {
suppressed: false,
exprs: JointExprs { offset: typed(p.get("offset")), angle: typed(p.get("angle")), values: value_exprs(p) },
};
- // A relation given no values starts from where its parts are: B moves no further than it must.
- if p.get("values").is_none()
- && p.get("value").is_none()
- && let Some(q) = joints::relation_values(&s.doc, &s.model.state(), &j)
- {
- j.values = q;
- }
// No angle given: B keeps its turn about the joint's axis where a half turn would otherwise
// come only from how its origin's x was chosen. (An angle relation's angle is its tilt.)
if angle.is_none() && !j.kind.is_relation() {
@@ -1515,7 +1544,7 @@ fn edit(s: &mut Session, p: &Value) -> Result<Value> {
// A relation whose type, offset, tilt or flip changes starts again from where its parts are.
let reseat = kind.is_some() || offset.is_some() || angle.is_some() || p.get("flip").is_some() || sides;
if reseat {
- let edited = s.doc.assembly.joints.iter().find(|j| j.id == id).cloned();
+ let edited = s.doc.assembly.joints.iter().find(|j| j.id == id).filter(|j| j.kind.is_relation()).cloned();
if let Some(q) = edited.and_then(|j| joints::relation_values(&s.doc, &s.model.state(), &j))
&& let Some(j) = s.doc_mut().assembly.joints.iter_mut().find(|j| j.id == id)
{
diff --git a/crates/engine/src/cmd/joints_relations_tests.rs b/crates/engine/src/cmd/joints_relations_tests.rs
index e858ea0f22..2c09d1eaec 100644
--- a/crates/engine/src/cmd/joints_relations_tests.rs
+++ b/crates/engine/src/cmd/joints_relations_tests.rs
@@ -60,6 +60,43 @@ fn clean(r: &Value) -> bool {
r["conflicts"] == json!([]) && r["errors"] == json!([])
}
+#[test]
+fn closing_planar_joint_starts_from_the_current_placement() {
+ let mut s = Session::default();
+ for name in ["Base", "Part"] {
+ run(&mut s, "component.create", json!({"name": name}));
+ run(&mut s, "solid.box", json!({"length": 20, "width": 10, "height": 6}));
+ run(&mut s, "component.activate", json!({"component": "root"}));
+ }
+ let base = occ(&s, "Base");
+ run(&mut s, "occurrence.ground", json!({"occurrence": base, "grounded": true}));
+ let r = run(
+ &mut s,
+ "joint.create",
+ json!({"type": "planar", "name": "First", "flip": true, "angle": 0,
+ "a": {"occurrence": "Base", "point": [0, 0, 0], "x": [1, 0, 0]},
+ "b": {"occurrence": "Part", "point": [0, 0, 0], "x": [1, 0, 0]}, "values": [50, 30, 120]}),
+ );
+ assert!(clean(&r), "{r}");
+ // Check a frame, so a turn as well as a translation is observable.
+ let points = [Vec3::ZERO, Vec3::X, Vec3::Y, Vec3::Z];
+ let before = points.map(|p| place(&s, "Part", p));
+ let at = before[0];
+ let x = before[1] - at;
+ let r = run(
+ &mut s,
+ "joint.create",
+ json!({"type": "planar", "name": "Closing", "flip": true, "angle": 0,
+ "a": {"occurrence": "Base", "point": [0, 0, 0], "x": [1, 0, 0]},
+ "b": {"occurrence": "Part", "point": [at.x, at.y, at.z], "x": [x.x, x.y, x.z]}}),
+ );
+ assert!(clean(&r), "{r}");
+ for (p, want) in points.into_iter().zip(before) {
+ let got = place(&s, "Part", p);
+ assert!(near(got, want, 1e-6), "{p:?}: {got:?}, expected {want:?}");
+ }
+}
+
#[test]
fn stacked_relations_place_a_part_and_follow_the_panel() {
let mut s = panel();