Skip to content

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

Joints: an origin takes a face name as long as the face's piece name (#6225)

NOWN-DEV7313fd868495
diff --git a/crates/engine/src/cmd/joints.rs b/crates/engine/src/cmd/joints.rs
index ff4f55eeee..98e695a312 100644
--- a/crates/engine/src/cmd/joints.rs
+++ b/crates/engine/src/cmd/joints.rs
@@ -422,6 +422,11 @@ pub(crate) fn origin_param(s: &Session, v: Option<&Value>, cmd: &str) -> Result<
}
}
+/// The longest face name a joint origin takes. A face that cuts split and merged keeps the names
+/// of the faces around it (`<name>#[<name>,…]`): several hundred characters on a part a text or
+/// a pattern of holes was cut through.
+const MAX_FACE_NAME: usize = 16_384;
+
/// The component an occurrence places (0 for the root, or an unknown occurrence).
fn component_of(s: &Session, occurrence: u64) -> u64 {
if occurrence == 0 { 0 } else { s.doc.occurrences.iter().find(|o| o.id == occurrence).map_or(0, |o| o.component) }
@@ -431,8 +436,8 @@ fn component_of(s: &Session, occurrence: u64) -> u64 {
/// a round end of it), on `body?`, of `occurrence?` (default: the first occurrence of the
/// face's component).
fn origin_by_name(s: &Session, v: &Value, what: &str, name: &str, cmd: &str) -> Result<JointOrigin> {
- if name.is_empty() || name.len() > 512 {
- return Err(bad(cmd, "a face name is 1…512 characters"));
+ if name.is_empty() || name.len() > MAX_FACE_NAME {
+ return Err(bad(cmd, format!("a face name is 1…{MAX_FACE_NAME} characters")));
}
let st = s.model.state();
let (component, pick) = joints::named_face_spot(&s.doc, &st, str_(v, "body"), name).map_err(|why| {
@@ -465,8 +470,10 @@ fn origin_by_name(s: &Session, v: &Value, what: &str, name: &str, cmd: &str) ->
fn settle(s: &Session, mut o: JointOrigin, v: &Value, cmd: &str) -> Result<JointOrigin> {
let st = s.model.state();
if let Some(want) = v.get("end") {
- let want =
- want.as_str().filter(|n| !n.is_empty() && n.len() <= 512).ok_or_else(|| bad(cmd, "`end` is the name of a face a round end lies on"))?;
+ let want = want
+ .as_str()
+ .filter(|n| !n.is_empty() && n.len() <= MAX_FACE_NAME)
+ .ok_or_else(|| bad(cmd, "`end` is the name of a face a round end lies on"))?;
let Snap::CircleCenter { end, .. } = &mut o.snap else { return Err(bad(cmd, "`end` is for a round origin (`circle`)")) };
*end = Some(want.to_string());
let names = joints::end_faces(&s.doc, &st, &o.snap);
@@ -493,9 +500,9 @@ fn x_param(s: &Session, v: &Value, component: u64, inv: &solidcraft_doc::Mat, cm
let wrong = || bad(cmd, "`x` is a direction [x, y, z], a face's name, or {face: [x, y, z]}");
match v.get("x") {
None | Some(Value::Null) => Ok(None),
- Some(Value::String(name)) if !name.is_empty() && name.len() <= 512 => named(name),
+ Some(Value::String(name)) if !name.is_empty() && name.len() <= MAX_FACE_NAME => named(name),
Some(o @ Value::Object(_)) => match o.get("face") {
- Some(Value::String(name)) if !name.is_empty() && name.len() <= 512 => named(name),
+ Some(Value::String(name)) if !name.is_empty() && name.len() <= MAX_FACE_NAME => named(name),
Some(p) => {
let pick = vec3(p).map(|p| solidcraft_doc::apply_point(inv, p)).ok_or_else(wrong)?;
let face = joints::face_name_at(&s.doc, &st, component, pick, false).ok_or_else(|| bad(cmd, "`x`: no named face there"))?;
diff --git a/crates/engine/src/cmd/joints_face_tests.rs b/crates/engine/src/cmd/joints_face_tests.rs
index 4a319da25c..7aa1840e1b 100644
--- a/crates/engine/src/cmd/joints_face_tests.rs
+++ b/crates/engine/src/cmd/joints_face_tests.rs
@@ -188,6 +188,44 @@ fn origins_take_faces_by_name_and_an_end_and_turn_by_name() {
}
}
+/// A face split by a cut whose edge runs in many segments is named after the faces around its
+/// piece (`<face>#[<face>,…]`): several hundred characters. Such a name, as `model.faces` gives
+/// it, is taken for a face centre and as a round end (it was refused over 512 characters).
+#[test]
+fn origins_take_a_long_piece_name() {
+ let mut s = Session::default();
+ run(&mut s, "component.create", json!({"name": "Tray"}));
+ run(&mut s, "solid.box", json!({"length": 300, "width": 100, "height": 1, "corner": [0, 0, 0]}));
+ // A slot across the tray whose left edge zigzags in 60 segments: the top left of it names them all.
+ let mut zig: Vec<[f64; 2]> = (0..=60).map(|i| [140.0 + f64::from(i % 2) * 4.0, -10.0 + 2.0 * f64::from(i)]).collect();
+ zig.extend([[170.0, 110.0], [170.0, -10.0]]);
+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Zig"}));
+ run(&mut s, "sketch.line", json!({"points": zig, "closed": true}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"sketch": "Zig", "through_all": true, "direction": "symmetric", "operation": "cut"}));
+ run(&mut s, "solid.hole", json!({"position": [50, 50, 1], "diameter": 6}));
+ let tray = occ(&s, "Tray");
+ run(&mut s, "occurrence.ground", json!({"occurrence": tray, "grounded": true}));
+ run(&mut s, "component.activate", json!({"component": "root"}));
+ run(&mut s, "component.create", json!({"name": "Bracket"}));
+ run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 2, "corner": [100, 200, 0]}));
+ run(&mut s, "solid.hole", json!({"position": [110, 210, 2], "diameter": 6}));
+ let top = face_name(&mut s, "Tray", Vec3::new(20.0, 20.0, 1.0));
+ assert!(top.len() > 512, "{}: {top}", top.len());
+ let wall = face_name(&mut s, "Tray", Vec3::new(53.0, 50.0, 0.5));
+ let foot = face_name(&mut s, "Bracket", Vec3::new(113.0, 210.0, 1.0));
+ let under = face_name(&mut s, "Bracket", Vec3::new(105.0, 205.0, 0.0));
+ run(&mut s, "joint.origin", json!({"name": "Top", "face": top}));
+ run(&mut s, "joint.origin", json!({"name": "Hole", "circle": wall, "end": top, "x": [1, 0, 0]}));
+ let r = run(&mut s, "joint.create", json!({"type": "rigid", "a": "Hole", "b": {"circle": foot, "end": under, "x": [1, 0, 0]}}));
+ assert_eq!(r["errors"].as_array().map(Vec::len), Some(0), "{r}");
+ let hole = place(&s, "Bracket", Vec3::new(110.0, 210.0, 0.0));
+ assert!(near(hole, Vec3::new(50.0, 50.0, 1.0)), "{hole:?}");
+ // Names stay bounded.
+ let huge = format!("F1:+z#[{}]", "F2:side:l1,".repeat(2_000));
+ assert!(s.execute("joint.origin", &json!({"name": "Huge", "face": huge})).is_err());
+}
+
#[test]
fn a_face_centre_by_name_follows_its_face() {
let mut s = tray();