Skip to content

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

Thread: the refusal for a fine-only size says what to type (#4971) (#4977)

raykholo6224cbf52645
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index 00a9ff49fa..fe0fd54cfb 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -1883,6 +1883,59 @@ fn clearance_diameter(size: &str, fit: &str) -> Option<f64> {
}
}
+/// The refusal for a thread designation `parse_metric_thread` does not know. A plain `M<d>` with
+/// a sane diameter but no ISO coarse pitch (M28, M7) gets two pitches to try: the largest common
+/// pitch up to about an eighth of the diameter, and the next finer one.
+fn unknown_metric_thread(t: &str, examples: &str) -> String {
+ fine_only_hint(t).unwrap_or_else(|| format!("unknown thread `{t}` (ISO metric, like {examples})"))
+}
+
+/// The pitches to try for a plain `M<d>` that has no ISO coarse pitch, `None` for anything else.
+fn fine_only_hint(t: &str) -> Option<String> {
+ const PITCHES: [f64; 9] = [2.0, 1.5, 1.25, 1.0, 0.75, 0.5, 0.35, 0.25, 0.2];
+ let diameter = t.trim().strip_prefix(['M', 'm']).and_then(|d| d.trim().parse::<f64>().ok());
+ if let Some(d) = diameter.filter(|d| d.is_finite() && *d > 0.2 && *d <= 1e4) {
+ let first = PITCHES.iter().position(|p| *p <= 0.12 * d).unwrap_or(PITCHES.len() - 1);
+ let pitches: Vec<String> = PITCHES.iter().skip(first).take(2).filter(|p| **p < d).map(|p| format!("M{d}x{p}")).collect();
+ if !pitches.is_empty() {
+ return Some(format!("M{d} has no coarse ISO pitch: give one, e.g. {}", pitches.join(" or ")));
+ }
+ }
+ None
+}
+
+/// Largest nominal diameter automatic thread sizing knows (the last ISO coarse size).
+const LARGEST_COARSE: f64 = 56.0;
+
+/// The refusal for automatic sizing on a cylinder too big for the coarse table (a Ø75 shaft):
+/// `None` when sizing finds a thread, or the cylinder is not past the table's end (the feature's
+/// own error then explains). It suggests sizes with a pitch to type, scaled to the diameter.
+fn beyond_coarse_range(st: &solidcraft_doc::ModelState, face: Vec3) -> Option<String> {
+ if solidcraft_doc::suggested_thread(st, face).is_ok() {
+ return None;
+ }
+ let cyl = st.bodies.iter().filter_map(|b| solidcraft_doc::kernel::cylinder_face_at(&b.body, face)).min_by(|a, b| {
+ let dist =
+ |c: &solidcraft_doc::kernel::CylinderFace| ((face - c.axis_point - c.axis * (face - c.axis_point).dot(c.axis)).len() - c.radius).abs();
+ dist(a).total_cmp(&dist(b))
+ })?;
+ let dia = cyl.radius * 2.0;
+ if !dia.is_finite() || dia <= LARGEST_COARSE * 0.9 || dia > 1e4 {
+ return None;
+ }
+ // ISO fine pitches for large sizes: the largest up to about a 25th of the diameter, then the
+ // next finer one. A hole's major diameter is about its bore plus one pitch.
+ const PITCHES: [f64; 4] = [4.0, 3.0, 2.0, 1.5];
+ let first = PITCHES.iter().position(|p| *p <= dia / 25.0 + 1e-6).unwrap_or(PITCHES.len() - 1);
+ let sizes: Vec<String> =
+ PITCHES.iter().skip(first).take(2).map(|p| format!("M{}x{p}", if cyl.internal { dia + p } else { dia }.round())).collect();
+ let kind = if cyl.internal { "hole" } else { "cylinder" };
+ Some(format!(
+ "a {dia:.2} mm {kind} is beyond the coarse thread range (up to M{LARGEST_COARSE}): type a size with its pitch, e.g. {}",
+ sizes.join(" or ")
+ ))
+}
+
fn hole(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "solid.hole";
// At sketch points: the holes go perpendicular to the sketch and follow its points.
@@ -1920,7 +1973,10 @@ fn hole(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(t) = &thread
&& solidcraft_doc::parse_tapped_thread(t).is_none()
{
- return Err(bad(cmd, format!("unknown tapped thread `{t}` (ISO metric or a catalogued UNC/UNF, G or Rp size)")));
+ return Err(bad(
+ cmd,
+ fine_only_hint(t).unwrap_or_else(|| format!("unknown tapped thread `{t}` (ISO metric or a catalogued UNC/UNF, G or Rp size)")),
+ ));
}
// A position given as length expressions (#1678), say `["leg_h*0.4", "width/2", "t"]`: kept,
// so the hole follows the parameters; drilled where they evaluate now.
@@ -2354,13 +2410,18 @@ fn thread(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(t) = &designation
&& solidcraft_doc::parse_metric_thread(t).is_none()
{
- return Err(bad(cmd, format!("unknown thread `{t}` (ISO metric, like M8 or M8x1)")));
+ return Err(bad(cmd, unknown_metric_thread(t, "M8 or M8x1")));
}
let length = expr(p, "length");
if let Some(l) = &length {
check_expr(s, l, Kind::Length, cmd, "length")?;
}
let modeled = p.get("modeled").and_then(Value::as_bool).unwrap_or(false);
+ if designation.is_none()
+ && let Some(msg) = beyond_coarse_range(&s.model.state(), face)
+ {
+ return Err(bad(cmd, msg));
+ }
let v = add_feature(s, p, FeatureKind::Thread { face: FacePick(face), designation, length, modeled })?;
let st = s.model.state();
let t = v.get("feature").and_then(Value::as_u64).and_then(|id| st.threads.iter().find(|t| t.feature == id).cloned());
diff --git a/crates/engine/src/thread_tests.rs b/crates/engine/src/thread_tests.rs
index 7778a3a818..52b10e8bf7 100644
--- a/crates/engine/src/thread_tests.rs
+++ b/crates/engine/src/thread_tests.rs
@@ -176,6 +176,45 @@ fn a_thin_shaft_or_one_the_part_surrounds_is_refused() {
assert!(e.contains("this 8 mm shaft is too thick for a modelled M6 thread") && e.contains("part around it"), "{e}");
}
+#[test]
+fn a_thread_without_a_coarse_pitch_suggests_explicit_pitches() {
+ let mut s = Session::default();
+ run(&mut s, "solid.cylinder", json!({"radius": 14, "height": 10, "body_name": "Shaft"}));
+ let saved = s.doc.to_json();
+ for designation in ["M28", "m28", " M28 "] {
+ let e = s.execute("solid.thread", &json!({"face": [14, 0, 5], "designation": designation})).unwrap_err().to_string();
+ assert!(e.contains("has no coarse ISO pitch") && e.contains("M28x2") && e.contains("M28x1.5"), "{e}");
+ assert_eq!(s.doc.to_json(), saved);
+ }
+ for designation in ["MNaN", "Minf", "M-28", "M0", "M1e9", "M1e999", "M28x", "M28xNaN"] {
+ let e = s.execute("solid.thread", &json!({"face": [14, 0, 5], "designation": designation})).unwrap_err().to_string();
+ assert!(e.contains("unknown thread"), "{designation}: {e}");
+ assert_eq!(s.doc.to_json(), saved);
+ }
+ run(&mut s, "solid.thread", json!({"face": [14, 0, 5], "designation": "M28x2"}));
+}
+
+#[test]
+fn automatic_sizing_past_the_coarse_range_suggests_a_size_with_its_pitch() {
+ let mut s = Session::default();
+ run(&mut s, "solid.cylinder", json!({"radius": 37.5, "height": 10, "body_name": "Shaft"}));
+ let saved = s.doc.to_json();
+ let e = s.execute("solid.thread", &json!({"face": [37.5, 0, 5]})).unwrap_err().to_string();
+ assert!(e.contains("beyond the coarse thread range (up to M56)") && e.contains("M75x3 or M75x2"), "{e}");
+ assert_eq!(s.doc.to_json(), saved);
+ run(&mut s, "solid.thread", json!({"face": [37.5, 0, 5], "designation": "M75x2"}));
+}
+
+#[test]
+fn a_threaded_hole_without_a_coarse_pitch_suggests_explicit_pitches() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 10}));
+ let saved = s.doc.to_json();
+ let e = s.execute("solid.hole", &json!({"position": [20, 20, 10], "thread": "M28"})).unwrap_err().to_string();
+ assert!(e.contains("has no coarse ISO pitch") && e.contains("M28x2") && e.contains("M28x1.5"), "{e}");
+ assert_eq!(s.doc.to_json(), saved);
+}
+
#[test]
fn non_finite_thread_designations_are_refused_without_changing_the_design() {
let mut s = Session::default();