Insert Part: the Length list opens on the default length, with no "Default" item (#8020) (#8022)

* claim: #8020

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

* Insert Part: the Length list opens on the default length (#8020)

The list carried an item called "Default" above the standard lengths. It
never said which length it stood for, and one of the lengths below it
always was that length, so the list offered the same length twice under
two names. Change Part Size has always listed concrete lengths.

The item is gone and the list opens on the length the part would be given
anyway. The default cannot be worked out in the dialog, because it is not
one rule: a cap screw or bolt takes the first standard length at or above
2.5 d, a set screw the first at or above d, a dowel pin the first at or
above 4 d. parts.library now reports default_length beside each size's
table values, taken from the shape the engine would build, and the dialog
selects it; the test checks that report against what an insert without a
length is actually built to, for every size of every family.

Leaving the length alone behaves as "Default" did: a new size resolves
the list again, so M6 to M12 moves the selection from 20 to 30. A length
chosen by hand stays until the size changes.

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

* Insert Part: the labels test skips default_length too

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

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-28 17:58:03 +00:00
1 parent 4a7a8f31f8
commit 6fffd745fd
2 files changed
+75 -8

No files matched your search

+38 -1
View File
@@ -335,7 +335,7 @@ fn build(s: &mut Session, sh: &Shape) -> Result<()> {
fn library(_s: &mut Session, p: &Value) -> Result<Value> {
let fam = |id: &str| -> Value {
let sizes: Vec<Value> = match id {
let mut sizes: Vec<Value> = match id {
"socket_head_cap_screw" => SHCS.iter().map(|r| json!({"size": r.0, "d": metric(r.0).map(|m| m.0), "pitch": metric(r.0).map(|m| m.1), "head_diameter": r.1, "head_height": r.2, "socket": r.3, "socket_depth": r.4})).collect(),
"hex_bolt" => HEX_BOLT.iter().map(|r| json!({"size": r.0, "d": metric(r.0).map(|m| m.0), "pitch": metric(r.0).map(|m| m.1), "across_flats": r.1, "head_height": r.2})).collect(),
"hex_nut" => HEX_NUT.iter().map(|r| json!({"size": r.0, "d": metric(r.0).map(|m| m.0), "pitch": metric(r.0).map(|m| m.1), "across_flats": r.1, "height": r.2})).collect(),
@@ -347,6 +347,15 @@ fn library(_s: &mut Session, p: &Value) -> Result<Value> {
};
let (_, name, standard) = FAMILIES.iter().find(|f| f.0 == id).copied().unwrap_or(("", "", ""));
let lengths = matches!(id, "socket_head_cap_screw" | "hex_bolt" | "set_screw" | "dowel_pin").then_some(LENGTHS.to_vec());
if lengths.is_some() {
for row in &mut sizes {
let Some(size) = row.get("size").and_then(Value::as_str).map(str::to_string) else { continue };
let Some(l) = shape(id, &size, None).ok().as_ref().and_then(shape_len) else { continue };
if let Value::Object(m) = row {
m.insert("default_length".into(), json!(l));
}
}
}
json!({"family": id, "name": name, "standard": standard, "sizes": sizes, "lengths": lengths})
};
match str_(p, "family") {
@@ -599,6 +608,34 @@ mod tests {
assert!(b.execute("parts.insert", &json!({"family": "hex_nut", "size": "M7"})).is_err());
}
/// The Length list of a dialog opens on the length an insert gets when none is asked for,
/// so the library reports that length for every size of every family that has lengths. The
/// rule differs by family, so the reported value is the one the part is actually built to.
#[test]
fn the_library_reports_the_default_length_of_every_size() {
let all = run(&mut Session::default(), "parts.library", json!({}));
let families = all["families"].as_array().cloned().unwrap_or_default();
assert!(!families.is_empty(), "{all}");
for fam in families {
let id = fam["family"].as_str().unwrap_or_default().to_string();
let standard: Vec<f64> = fam["lengths"].as_array().map(|a| a.iter().filter_map(Value::as_f64).collect()).unwrap_or_default();
for row in fam["sizes"].as_array().cloned().unwrap_or_default() {
let size = row["size"].as_str().unwrap_or_default().to_string();
let reported = row["default_length"].as_f64();
if standard.is_empty() {
assert!(reported.is_none(), "{id} {size} has no lengths of its own but reports a default");
continue;
}
// What an insert that asks for no length is actually built to.
let mut s = Session::default();
let out = run(&mut s, "parts.insert", json!({"family": id, "size": size}));
let built = s.doc.parts.get(&out["component"].as_u64().unwrap_or(0)).and_then(|p| p.length);
assert_eq!(reported, built, "{id} {size}: reported default is not what an insert builds");
assert!(standard.iter().any(|l| Some(*l) == reported), "{id} {size}: {reported:?} is not one of the standard lengths");
}
}
}
/// #2091: the second and larger ISO sizes, M2, M2.5, M14, M24 and M30, are in the screw, bolt,
/// nut and washer tables (they stopped at M3 and M20).
#[test]
+37 -7
View File
@@ -108,6 +108,18 @@ pub fn insert_params(lib: &[Family], k: &Pt, hole: Option<&Sel>) -> Result<Value
Ok(p)
}
/// Where the Length list opens for a size: the index of the length an insert of it gets when
/// none is asked for, which `parts.library` reports beside the size's table values. The list
/// carries no item of its own for it, so a size whose default is missing opens on the first.
fn default_length(f: &Family, size: usize, lengths: &[f64]) -> usize {
f.sizes
.get(size)
.and_then(|(_, row)| row.get("default_length"))
.and_then(Value::as_f64)
.and_then(|d| lengths.iter().position(|l| (*l - d).abs() < 1e-9))
.unwrap_or(0)
}
/// A picture of the part alone.
fn picture(p: &Value) -> Option<egui::ColorImage> {
let mut s = Session::default();
@@ -149,15 +161,19 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pt, inputs: &[SelInpu
}
let Some(f) = lib.get(k.family) else { return false };
let sizes: Vec<&str> = f.sizes.iter().map(|s| s.0.as_str()).collect();
let was_size = k.size;
choice_row(ui, "Size", "pt_size", &mut k.size, &sizes);
// Each size has a default length of its own, so a new size resolves the Length list again.
if k.size != was_size {
k.length = None;
}
if let Some(ls) = &f.lengths {
let mut labels = vec!["Default".to_string()];
labels.extend(ls.iter().map(|l| crate::prefs::show_mm(app, *l, 1)));
let labels: Vec<String> = ls.iter().map(|l| crate::prefs::show_mm(app, *l, 1)).collect();
let refs: Vec<&str> = labels.iter().map(String::as_str).collect();
let mut i = k.length.map_or(0, |x| x + 1);
let mut i = k.length.unwrap_or_else(|| default_length(f, k.size, ls));
form_row(ui, "Length", |ui| {
combo(ui, "pt_len", &refs, &mut i);
k.length = i.checked_sub(1);
k.length = Some(i);
});
}
text_row(ui, "Name", &mut k.name, crate::i18n::tr("parts.automatic_name"));
@@ -188,7 +204,8 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Pt, inputs: &[SelInpu
if let Some((_, row)) = f.sizes.get(k.size)
&& let Some(o) = row.as_object()
{
for (key, v) in o.iter().filter(|(key, _)| key.as_str() != "size") {
// `default_length` picks the Length list's opening value; it is not a dimension.
for (key, v) in o.iter().filter(|(key, _)| !matches!(key.as_str(), "size" | "default_length")) {
let label = dimension_label(key).map_or_else(|| key.replace('_', " "), str::to_string);
form_row(ui, &label, |ui| {
crate::ui_kit::text(
@@ -252,7 +269,8 @@ mod tests {
fn lib() -> Vec<Family> {
families(&json!({"families": [
{"family": "socket_head_cap_screw", "name": "Socket Head Cap Screw", "standard": "ISO 4762",
"sizes": [{"size": "M5", "head_diameter": 8.5}, {"size": "M6", "head_diameter": 10}], "lengths": [10, 16, 20]},
"sizes": [{"size": "M5", "head_diameter": 8.5, "default_length": 16},
{"size": "M6", "head_diameter": 10, "default_length": 20}], "lengths": [10, 16, 20]},
{"family": "bearing", "name": "Deep Groove Ball Bearing", "standard": "", "sizes": [{"size": "608", "bore": 8}], "lengths": null},
]}))
}
@@ -280,6 +298,18 @@ mod tests {
assert_eq!(family_name(&unknown), "Spring");
}
/// The Length list is lengths only: it opens on the size's default rather than on an item
/// called “Default”, and a size that reports none opens on the first length.
#[test]
fn the_length_list_opens_on_the_size_s_default() {
let l = lib();
let ls = l[0].lengths.clone().unwrap_or_default();
assert_eq!(default_length(&l[0], 0, &ls), 1, "M5 opens on 16");
assert_eq!(default_length(&l[0], 1, &ls), 2, "M6 opens on 20");
assert_eq!(default_length(&l[1], 0, &ls), 0, "a size without one opens on the first length");
assert_eq!(default_length(&l[0], 99, &ls), 0, "so does a size that is not there");
}
/// A hole pick seats the part (`at`); without one it goes to the origin along Z.
#[test]
fn insert_params_for_picks() {
@@ -304,7 +334,7 @@ mod tests {
assert!(l.len() >= 7, "{}", l.len());
for f in &l {
for (size, row) in &f.sizes {
for key in row.as_object().into_iter().flat_map(|o| o.keys()).filter(|k| k.as_str() != "size") {
for key in row.as_object().into_iter().flat_map(|o| o.keys()).filter(|k| !matches!(k.as_str(), "size" | "default_length")) {
let label = dimension_label(key).map(crate::i18n::tr);
assert!(
label.is_some_and(|l| l.starts_with(char::is_uppercase) && !l.contains('_') && !l.starts_with("parts.")),