Joint origin: a circle given by its centre (#393) (#1686)

Thanks @SychPL.
This commit is contained in:
Mateusz Sych authored and GitHub committed 2026-10-16 08:26:14 -04:00
1 parent 9194f4be09
commit d84d8c3222
4 files changed
+89 -5

No files matched your search

+27 -3
View File
@@ -305,8 +305,29 @@ fn origin_param(s: &Session, v: Option<&Value>, cmd: &str) -> Result<JointOrigin
}
};
let o = JointOrigin { occurrence, snap };
joints::resolve_origin(&s.doc, &s.model.state(), &o).map_err(|e| bad(cmd, e.to_string()))?;
Ok(o)
match joints::resolve_origin(&s.doc, &s.model.state(), &o) {
Ok(_) => Ok(o),
// A circle given by its centre, where no face lies (#393).
Err(e) => match (what == "circle").then(|| circle_centre(s, occurrence, local(world))).flatten() {
Some(snap) => Ok(JointOrigin { occurrence, snap }),
None => Err(bad(cmd, e.to_string())),
},
}
}
/// The round end of a cylindrical face of the occurrence's bodies centred at `pick`: a point on
/// its axis there, pointing out of that end (as picking its circular edge gives).
fn circle_centre(s: &Session, occurrence: u64, pick: Vec3) -> Option<Snap> {
let component = if occurrence == 0 { 0 } else { s.doc.occurrences.iter().find(|o| o.id == occurrence)?.component };
let st = s.model.state();
let c = st
.bodies
.iter()
.filter(|b| s.doc.model_body_component(b) == component)
.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) })
}
pub(crate) fn joint_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result<u64> {
@@ -699,7 +720,10 @@ fn size_param(s: &Session, p: &Value, key: &str, cmd: &str) -> Result<Size> {
match p.get(key) {
Some(Value::Number(n)) => n.as_f64().map(Size::Length).ok_or_else(|| bad(cmd, format!("`{key}` must be a finite length"))),
Some(Value::String(t)) if t.len() <= 1000 => Ok(Size::Expr(t.trim().to_string())),
Some(o @ Value::Object(m)) if m.contains_key("circle") => Ok(Size::Circle(origin_param(s, Some(o), cmd)?)),
Some(o @ Value::Object(m)) if m.contains_key("circle") => match origin_param(s, Some(o), cmd)? {
o @ JointOrigin { snap: Snap::CircleCenter { .. }, .. } => Ok(Size::Circle(o)),
_ => Err(bad(cmd, format!("`{key}`: give a point on the round face, not its centre"))),
},
_ => Err(bad(cmd, format!("`{key}` must be a length (mm or an expression) or a round face ({{circle: [x, y, z]}})"))),
}
}
+34
View File
@@ -34,6 +34,40 @@ fn leaf(s: &mut Session, name: &str) {
run(s, "solid.cylinder", json!({"base": [0, 33, 4], "axis": [1, 0, 0], "radius": 4, "height": 40, "operation": "new"}));
}
#[test]
fn circle_given_by_its_centre_takes_that_round_end() {
// #393: `circle` at the centre of a round end, as the parameters document, was refused with
// "no cylindrical face there" (no face lies at a centre).
let mut s = Session::default();
run(&mut s, "component.create", json!({"name": "Plate"}));
run(&mut s, "solid.box", json!({"length": 30, "width": 20, "height": 6, "corner": [-15, -10, 0]}));
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "component.create", json!({"name": "Knob"}));
run(&mut s, "solid.cylinder", json!({"base": [0, 0, 6], "axis": [0, 0, 1], "radius": 10, "height": 10, "operation": "new"}));
let r = run(
&mut s,
"joint.create",
json!({"type": "cylindrical", "a": {"occurrence": "Plate", "face": [0, 0, 6]}, "b": {"occurrence": "Knob", "circle": [0, 0, 6]}, "name": "J"}),
);
assert_eq!(r["conflicts"].as_array().map(Vec::len), Some(0), "{r}");
// The knob's bottom circle already sits on the plate's top: its axis stays put (it may turn
// about it, to line up the two frames' x).
for z in [6.0, 16.0] {
assert!(near(place(&s, "Knob", Vec3::new(0.0, 0.0, z)), Vec3::new(0.0, 0.0, z)), "{:?}", place(&s, "Knob", Vec3::new(0.0, 0.0, z)));
}
// The top end's centre: its axis points up, out of the knob.
run(&mut s, "joint.origin", json!({"occurrence": "Knob", "circle": [0, 0, 16], "name": "Top"}));
let top = s.doc.assembly.origins.iter().find(|o| o.name == "Top").map(|o| o.origin.snap.clone());
assert!(
matches!(top, Some(solidcraft_doc::joints::Snap::Point { point, z: Some(z) }) if near(point, Vec3::new(0.0, 0.0, 16.0)) && near(z, Vec3::Z)),
"{top:?}"
);
// Off the axis is still refused, and so is a coupling size (it follows a face).
assert!(s.execute("joint.origin", &json!({"occurrence": "Knob", "circle": [3, 0, 6]})).is_err());
let e = s.execute("joint.screw", &json!({"joint": "J", "pitch": {"occurrence": "Knob", "circle": [0, 0, 6]}})).unwrap_err();
assert!(e.to_string().contains("not its centre"), "{e}");
}
#[test]
fn revolute_hinge_turns_about_the_knuckle() {
let mut s = Session::default();