joints: support custom names and joint type-dependent default names (#10340) (#10165)

* joints: support custom names in dialog and type-based default names

Allow users to specify a custom name in the Joint dialog and generate
type-prefixed default names (e.g. "Planar Joint1", "Revolute Joint1")
instead of generic "Joint{n}".

- Add `name` field to `JointForm` and display a text row in the Joint
  dialog with a dynamic placeholder matching the chosen type.
- Update `new_joint_name` in `cmd/joints.rs` to take `JointKind` and
  prefix auto-generated names with the joint kind label.
- Prioritize non-empty custom names and pass them through `joint.create`,
  `joint.as_built`, and `joint.edit`.
- Pre-populate the existing joint name in `edit_joint` to allow renaming
  directly from the Edit Joint dialog.

* joints: add tests for custom and type-based joint naming

Verify that the joint dialog and engine command layer correctly generate
type-prefixed default names and respect user-specified custom names.

- Add engine-layer test `new_joints_generate_type_prefixed_names_or_keep_custom_name`
  to verify `joint.create` and `joint.as_built` naming logic.
- Add UI-layer test `joint_dialog_applies_custom_name_or_falls_back_to_type_default`
  to ensure the `JointForm` captures and submits names correctly.

* Format dialogs_assembly_tests.rs (trailing blank line)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-05 18:31:43 +00:00
1 parent f1159a5d51
commit b2f5da6ca7
4 files changed
+122 -7

No files matched your search

+22 -6
View File
@@ -895,11 +895,27 @@ fn kind_param(p: &Value, cmd: &str) -> Result<JointKind> {
.ok_or_else(|| bad(cmd, "`type` must be rigid, revolute, slider, cylindrical, pin_slot, planar, ball, parallel, perpendicular, angle, point_on_plane, point_on_line or line_on_plane"))
}
fn new_joint_name(s: &Session, p: &Value) -> String {
str_(p, "name").map(str::to_string).unwrap_or_else(|| {
fn new_joint_name(s: &Session, p: &Value, kind: JointKind) -> String {
str_(p, "name").map(str::trim).filter(|n| !n.is_empty()).map(str::to_string).unwrap_or_else(|| {
let label = match kind {
JointKind::Rigid => "Rigid",
JointKind::Revolute => "Revolute",
JointKind::Slider => "Slider",
JointKind::Cylindrical => "Cylindrical",
JointKind::PinSlot => "Pin_slot",
JointKind::Planar => "Planar",
JointKind::Ball => "Ball",
JointKind::Parallel => "Parallel",
JointKind::Perpendicular => "Perpendicular",
JointKind::Angle => "Angle",
JointKind::PointOnPlane => "Point_on_plane",
JointKind::PointOnLine => "Point_on_line",
JointKind::LineOnPlane => "Line_on_plane",
};
let prefix = format!("{label} Joint");
let names: std::collections::BTreeSet<_> = s.doc.assembly.joints.iter().map(|j| j.name.as_str()).collect();
let number = (1..=s.doc.assembly.joints.len().saturating_add(1)).find(|n| !names.contains(format!("Joint{n}").as_str())).unwrap_or(1);
format!("Joint{number}")
let number = (1..=s.doc.assembly.joints.len().saturating_add(1)).find(|n| !names.contains(format!("{prefix}{n}").as_str())).unwrap_or(1);
format!("{prefix}{number}")
})
}
@@ -909,7 +925,7 @@ fn joint(s: &mut Session, p: &Value) -> Result<Value> {
let a = origin_param(s, p.get("a"), cmd)?;
let b = origin_param(s, p.get("b"), cmd)?;
let id = s.doc_mut().allocate_id()?;
let name = new_joint_name(s, p);
let name = new_joint_name(s, p, kind);
let offset = match p.get("offset") {
Some(v) => value(s, v, false, cmd)?,
None => 0.0,
@@ -961,7 +977,7 @@ fn as_built(s: &mut Session, p: &Value) -> Result<Value> {
let fa = side(oa)?;
let fb = side(ob)?;
let id = s.doc_mut().allocate_id()?;
let name = new_joint_name(s, p);
let name = new_joint_name(s, p, kind);
let n = kind.dofs().len();
let j = Joint {
id,
+43
View File
@@ -2719,3 +2719,46 @@ fn driving_an_angle_reports_the_number_a_person_would_read() {
let j = s.doc.assembly.joints.iter().find(|j| j.name == "Turn").unwrap();
assert_ne!(j.values[0].to_degrees(), 30.0, "the stored angle keeps its bits");
}
#[test]
fn new_joints_generate_type_prefixed_names_or_keep_custom_name() {
let mut s = two_cubes();
let (a, b) = (occ(&s, "A"), occ(&s, "B"));
// 1. Without name: revolute joint defaults to "Revolute Joint1"
let r1 = run(
&mut s,
"joint.create",
json!({
"type": "revolute",
"a": {"occurrence": a, "face": [5, 5, 10]},
"b": {"occurrence": b, "face": [25, 5, 0]}
}),
);
assert_eq!(r1["name"], "Revolute Joint1");
// 2. Without name: slider joint defaults to "Slider Joint1"
let r2 = run(
&mut s,
"joint.as_built",
json!({
"type": "slider",
"a": "A",
"b": "B"
}),
);
assert_eq!(r2["name"], "Slider Joint1");
// 3. With custom name: preserves custom name exactly
let r3 = run(
&mut s,
"joint.create",
json!({
"type": "rigid",
"name": "CustomRigid",
"a": {"occurrence": a, "face": [5, 5, 10]},
"b": {"occurrence": b, "face": [25, 5, 0]}
}),
);
assert_eq!(r3["name"], "CustomRigid");
}
+24 -1
View File
@@ -178,6 +178,7 @@ impl Searched {
/// The Joint and As-built Joint dialogs.
#[derive(Clone, Debug)]
pub struct JointForm {
pub name: String,
pub as_built: bool,
/// Editing this joint (its origins stay; type, alignment and limits change).
pub editing: Option<u64>,
@@ -213,6 +214,7 @@ pub struct JointForm {
impl JointForm {
fn new(as_built: bool, doc: &solidcraft_engine::doc::Document) -> JointForm {
JointForm {
name: String::new(),
as_built,
editing: None,
kind: 0,
@@ -497,11 +499,20 @@ fn nearest_pick(own: &[&solidcraft_engine::doc::ModelBody], p: Vec3, edges: bool
best.map(|(_, sel)| sel)
}
/// Default name for a new joint based on its type (e.g. "Planar Joint1", "Revolute Joint1").
pub(crate) fn default_joint_name(doc: &solidcraft_engine::doc::Document, kind: JointKind) -> String {
let prefix = format!("{} Joint", kind_label(kind));
let names: std::collections::BTreeSet<_> = doc.assembly.joints.iter().map(|j| j.name.as_str()).collect();
let number = (1..=doc.assembly.joints.len().saturating_add(1)).find(|n| !names.contains(format!("{prefix}{n}").as_str())).unwrap_or(1);
format!("{prefix}{number}")
}
/// The Edit Joint dialog for a joint (by id). Its two sides show as the Joint dialog's do, each
/// pre-filled with the pick it sits on.
pub fn edit_joint(app: &SolveApp, id: u64) -> Option<Dialog> {
let j = app.session.doc.assembly.joints.iter().find(|j| j.id == id)?;
let mut f = JointForm::new(false, &app.session.doc);
f.name = j.name.clone();
f.editing = Some(id);
f.sides = [side_pick(app, &j.a), side_pick(app, &j.b)];
let mut inputs = side_inputs();
@@ -799,6 +810,8 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Asm, inputs: &mut [Se
(f.search, f.found_note, f.searched) = (None, None, None);
f.found.clear();
}
let hint = default_joint_name(&app.session.doc, f.joint_kind());
enter |= text_row(ui, "Name", &mut f.name, &hint).0;
f.fit_limits(&app.session.doc);
if !f.as_built {
enter |= expr_row(ui, "Angle", &mut f.angle);
@@ -1474,10 +1487,14 @@ pub fn commands(app: &SolveApp, k: &Asm, inputs: &[SelInput]) -> Result<Vec<(Str
Asm::Joint(f) => {
let kind = TYPES.get(f.kind).copied().unwrap_or("rigid");
let jk = f.joint_kind();
let custom_name = f.name.trim();
let limits: Vec<Value> =
f.limits.iter().take(jk.dofs().len()).map(|(on, lo, hi)| if *on { json!([lo, hi]) } else { Value::Null }).collect();
if let Some(id) = f.editing {
let mut p = json!({"joint": id, "type": kind, "flip": f.flip, "offset": f.offset[2]});
if !custom_name.is_empty() {
p["name"] = json!(custom_name);
}
// The angle only when the user changed it: a side moved without it turns B as joint.create would.
if f.angle != f.angle_shown {
p["angle"] = json!(f.angle);
@@ -1506,6 +1523,9 @@ pub fn commands(app: &SolveApp, k: &Asm, inputs: &[SelInput]) -> Result<Vec<(Str
return Err("select two components".into());
}
let mut p = json!({"type": kind, "a": occs.first(), "b": occs.get(1), "limits": limits});
if !custom_name.is_empty() {
p["name"] = json!(custom_name);
}
if let Some(x) = sel_items(inputs, 1).first().and_then(|x| snap_of(s, x)) {
p["at"] = x.param;
}
@@ -1520,7 +1540,10 @@ pub fn commands(app: &SolveApp, k: &Asm, inputs: &[SelInput]) -> Result<Vec<(Str
// Picked origins mate by default (the engine's default: B's z opposite A's, faces
// meet face to face, a pin goes into its hole); the dialog's Flip is the command's
// `flip`, which turns B the other way.
let p = json!({"type": kind, "a": a_param, "b": b.param, "flip": f.flip, "offset": f.offset[2], "angle": f.angle, "limits": limits});
let mut p = json!({"type": kind, "a": a_param, "b": b.param, "flip": f.flip, "offset": f.offset[2], "angle": f.angle, "limits": limits});
if !custom_name.is_empty() {
p["name"] = json!(custom_name);
}
cmd("joint.create", p)
}
Asm::JointOrigin { name, .. } => {
@@ -1470,3 +1470,36 @@ fn interference_checks_off_the_window_thread_and_drops_a_check_of_a_changed_desi
compute(&mut app, true);
assert_eq!(state(&app), (None, 0, false), "a check of the design before the change is dropped");
}
#[test]
fn joint_dialog_applies_custom_name_or_falls_back_to_type_default() {
// 1. Empty name field: commands() generates payload without "name", backend creates "Rigid Joint1"
let mut app = two_boxes();
app.start("joint.create");
let mut d = app.dialog.clone().unwrap();
d.pick(&app.session, face_sel(&app.session, Vec3::new(10.0, 10.0, 10.0)).unwrap());
d.pick(&app.session, face_sel(&app.session, Vec3::new(40.0, 10.0, 0.0)).unwrap());
let cmds = apply_commands(&app, &d).unwrap();
assert_eq!(cmds[0].0, "joint.create");
assert!(cmds[0].1.get("name").is_none());
for (id, p) in cmds {
let r = app.run(&id, p).unwrap();
assert_eq!(r["name"], "Rigid Joint1");
}
// 2. Custom name in f.name: reset to fresh boxes so B is at its initial coordinates
let mut app = two_boxes();
app.start("joint.create");
let mut d = app.dialog.clone().unwrap();
d.pick(&app.session, face_sel(&app.session, Vec3::new(10.0, 10.0, 10.0)).unwrap());
d.pick(&app.session, face_sel(&app.session, Vec3::new(40.0, 10.0, 0.0)).unwrap());
if let Kind::Assembly(Asm::Joint(f)) = &mut d.kind {
f.name = "MyCustomJoint".into();
}
let cmds = apply_commands(&app, &d).unwrap();
assert_eq!(cmds[0].1["name"], "MyCustomJoint");
for (id, p) in cmds {
let r = app.run(&id, p).unwrap();
assert_eq!(r["name"], "MyCustomJoint");
}
}