Skip to content

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

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

N3MI-DG6fffd745fdca
diff --git a/crates/engine/src/cmd/parts.rs b/crates/engine/src/cmd/parts.rs
index 32c6f1afec..b340251d0b 100644
--- a/crates/engine/src/cmd/parts.rs
+++ b/crates/engine/src/cmd/parts.rs
@@ -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]
diff --git a/crates/ui-egui/src/dialogs_parts.rs b/crates/ui-egui/src/dialogs_parts.rs
index f0db864e3d..bb53b007a4 100644
--- a/crates/ui-egui/src/dialogs_parts.rs
+++ b/crates/ui-egui/src/dialogs_parts.rs
@@ -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.")),