diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 72a83ea63..5987e9651 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -4659,7 +4659,17 @@ fn hole_tools_and_prisms( } HoleKind::Countersink { cs_diameter, cs_angle, tip_angle } => { let (rc, half) = (val(vals, cs_diameter, Kind::Length)? / 2.0, val(vals, cs_angle, Kind::Angle)? / 2.0); - if !(rc > r && half > 1e-3 && half < std::f64::consts::FRAC_PI_2) { + // The angle and the width are told apart: an angle out of range is not a countersink + // too narrow (#9625). The cone's length grows as 1 / tan(half), so a near-flat angle + // is refused at a floor the message names, rather than built kilometres long. + const MIN_ANGLE_DEG: f64 = 0.115; + if !(2.0 * half > MIN_ANGLE_DEG.to_radians() && half < std::f64::consts::FRAC_PI_2) { + return Err(DocError::Invalid(format!( + "the countersink angle must be more than {MIN_ANGLE_DEG}° and less than 180°, not {}°", + ((2.0 * half).to_degrees() * 1e6).round() / 1e6 + ))); + } + if !(rc > r) { return Err(DocError::Invalid("the countersink must be wider than the hole".into())); } // One revolved tool: the cone from its rim on the face to just inside the hole @@ -4864,6 +4874,22 @@ struct ExtrudeConstructor { interval: (f64, f64), } +/// How far from its sketch plane an extrude may reach, in mm. It mirrors the limit the kernel's +/// `finite` check in `crates/kernel/src/build.rs` puts on an extrusion's start and end (a literal +/// there, not exported), so a too-long extrusion is refused here, by name, before the kernel is +/// called: the kernel's own message, `end = 1000000000`, names no field (#9625). +const MAX_EXTRUDE_REACH: f64 = 1e7; + +/// Refuse an extrusion whose ends lie past [`MAX_EXTRUDE_REACH`] from the sketch plane. +fn extrude_within_reach(ends: [f64; 2]) -> Result<()> { + match ends.into_iter().find(|x| x.abs() >= MAX_EXTRUDE_REACH) { + Some(x) => Err(DocError::Invalid(format!( + "the extrusion is too long: its end is {x} mm from the sketch plane, past the limit of {MAX_EXTRUDE_REACH} mm" + ))), + None => Ok(()), + } +} + /// Construct planar caps locally before placing the completed tool. fn centered_extrude( plane: &Plane, @@ -5047,6 +5073,7 @@ fn feature_tools_with_constructor( Some(e) => val(vals, e, Kind::Length)?, None => 0.0, }; + extrude_within_reach([base, base + sign * len])?; let start_plane = ss.plane.offset(base); let caps = [ Some(crate::lineage::extrude_cap_role(base)), @@ -5116,6 +5143,7 @@ fn feature_tools_with_constructor( lo = hs.iter().cloned().fold(lo, f64::min) - margin; } } + extrude_within_reach([lo + off, hi + off])?; let untrimmed = extent.to.is_none() && extent.from.is_none() && !extent.through_all; let mut tools = Vec::new(); for r in ®ions { diff --git a/crates/doc/src/sheet.rs b/crates/doc/src/sheet.rs index cc444eca4..6b1d0ca65 100644 --- a/crates/doc/src/sheet.rs +++ b/crates/doc/src/sheet.rs @@ -451,6 +451,9 @@ pub struct RuleValues { pub angle_display: AngleDisplay, } +/// A sheet rule's thickness is less than this (mm). +const MAX_SHEET_THICKNESS: f64 = 1e4; + impl Document { /// A rule by name, the active one, or the default steel rule. pub fn sheet_rule(&self, name: Option<&str>) -> SheetRule { @@ -474,8 +477,8 @@ impl Document { Some(t) => t, None => expr::eval_with(&r.thickness, &look)?.to_kind(Kind::Length)?, }; - if !(t > 0.0 && t < 1e4) { - return Err(DocError::Invalid("the sheet thickness must be positive".into())); + if !(t > 0.0 && t < MAX_SHEET_THICKNESS) { + return Err(DocError::Invalid("the sheet thickness must be more than 0 and less than 10000 mm".into())); } let with_t = |e: &str, k: Kind| -> Result { let f = |n: &str| if n == "Thickness" && !vals.contains_key("Thickness") { Ok(Value::length(t)) } else { look(n) }; @@ -496,8 +499,12 @@ impl Document { angle_display: AngleDisplay::parse(&r.angle_display) .ok_or_else(|| DocError::Invalid(format!("unknown bend angle display `{}` (bend or included)", r.angle_display)))?, }; - if !(0.0..=1.0).contains(&v.k_factor) || v.bend_radius < 0.0 { - return Err(DocError::Invalid("the K-factor must be 0…1 and the bend radius positive".into())); + // Each value named on its own: a sharp bend (radius 0) is allowed (#9625). + if !(0.0..=1.0).contains(&v.k_factor) { + return Err(DocError::Invalid("the K-factor must be between 0 and 1".into())); + } + if !(v.bend_radius >= 0.0) { + return Err(DocError::Invalid("the bend radius must not be negative".into())); } Ok(v) } diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index 9dc71daea..f5d5f3190 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -2600,6 +2600,18 @@ fn split_body(s: &mut Session, p: &Value) -> Result { add_feature(s, p, FeatureKind::Split { body: BodyName(body), plane, tool: None, sketch: None, line: None }) } +/// The fits [`clearance_diameter`] knows (close, normal, loose, and their older names). +const CLEARANCE_FITS: [&str; 6] = ["close", "fine", "normal", "medium", "loose", "coarse"]; + +/// A value typed by the user, cut short for a message. +fn brief(text: &str) -> String { + const MAX: usize = 32; + match text.char_indices().nth(MAX) { + Some((end, _)) => format!("{}…", &text[..end]), + None => text.to_string(), + } +} + /// ISO 273 clearance hole diameters (close, normal, loose fit) for metric bolts. fn clearance_diameter(size: &str, fit: &str) -> Option { const TABLE: [(f64, f64, f64, f64); 21] = [ @@ -2808,10 +2820,18 @@ fn hole(s: &mut Session, p: &Value) -> Result { let position = if references.is_empty() { position } else { referenced_centre(s, cmd, &references, position, direction)? }; // The diameter: given, or a metric-estimate/Unified-table tap drill, or an ISO 273 // clearance hole for a bolt size (close, normal or loose fit). + // A diameter number out of range says so, rather than that none was given (#9625). + if matches!(p.get("diameter"), Some(Value::Number(_))) && expr(p, "diameter").is_none() { + return Err(bad(cmd, format!("`diameter` {}", expr_problem(p.get("diameter"))))); + } let diameter = match (expr(p, "diameter"), &thread, str_(p, "clearance")) { (Some(d), _, _) => d, (None, _, Some(c)) => { let fit = str_(p, "fit").unwrap_or("normal").to_ascii_lowercase(); + // An unknown fit is not an unknown bolt size (#9625). + if !CLEARANCE_FITS.contains(&fit.as_str()) { + return Err(bad(cmd, format!("`fit` must be close, normal or loose, not `{}`", brief(&fit)))); + } let d = clearance_diameter(c, &fit).ok_or_else(|| bad(cmd, format!("no clearance hole for `{c}` ({fit} fit): M1.6 to M64")))?; Expr::new(format!("{d} mm")) } @@ -2858,7 +2878,12 @@ fn hole(s: &mut Session, p: &Value) -> Result { }, "countersink" => solidcraft_doc::HoleKind::Countersink { cs_diameter: get("cs_diameter")?, - cs_angle: expr(p, "cs_angle").unwrap_or_else(|| "90 deg".into()), + // The default only when no angle is given: a supplied one out of range is refused (#9625). + cs_angle: match p.get("cs_angle") { + None | Some(Value::Null) => "90 deg".into(), + Some(Value::String(t)) if t.trim().is_empty() => "90 deg".into(), + Some(_) => req_expr(cmd, p, "cs_angle")?, + }, tip_angle: expr(p, "tip_angle"), }, other => return Err(bad(cmd, format!("unknown hole type `{other}`"))), diff --git a/crates/engine/src/cmd/joints.rs b/crates/engine/src/cmd/joints.rs index ee8a3db87..d6e65871f 100644 --- a/crates/engine/src/cmd/joints.rs +++ b/crates/engine/src/cmd/joints.rs @@ -620,12 +620,13 @@ pub(crate) fn joint_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result /// A joint value: numbers are degrees for angles and mm for distances; text is an expression. pub(crate) fn value(s: &Session, v: &Value, angle: bool, cmd: &str) -> Result { let kind = if angle { Kind::Angle } else { Kind::Length }; + // A number out of range says so, rather than that it is not a number (#9625). let e = match v { Value::Number(n) => n.as_f64().filter(|x| x.is_finite() && x.abs() < 1e9).map(|x| x.to_string()), Value::String(t) => Some(t.clone()), _ => None, } - .ok_or_else(|| bad(cmd, "values must be numbers or expressions"))?; + .ok_or_else(|| bad(cmd, format!("a joint value {}", crate::params::expr_problem(Some(v)))))?; s.doc.eval(&e, kind).map_err(|e| bad(cmd, e.to_string())) } diff --git a/crates/engine/src/cmd/joints_tests.rs b/crates/engine/src/cmd/joints_tests.rs index bfe07b643..6ecf6a3d8 100644 --- a/crates/engine/src/cmd/joints_tests.rs +++ b/crates/engine/src/cmd/joints_tests.rs @@ -124,6 +124,27 @@ fn revolute_hinge_turns_about_the_knuckle() { assert!(near(place(&s, "LeafB", p), Vec3::new(20.0, 29.0, 37.0))); } +/// A joint value too large says so, rather than that it is not a number (#9625). +#[test] +fn a_joint_value_out_of_range_says_so() { + let mut s = Session::default(); + leaf(&mut s, "LeafA"); + leaf(&mut s, "LeafB"); + let o = occ(&s, "LeafA"); + run(&mut s, "occurrence.ground", json!({"occurrence": o, "grounded": true})); + let hinge = json!({"type": "revolute", "a": {"occurrence": "LeafA", "circle": [1, 37, 4]}, "b": {"occurrence": "LeafB", "circle": [1, 37, 4]}, "flip": true, "name": "Hinge"}); + run(&mut s, "joint.create", hinge); + for value in [json!(1e9), json!(-5e15)] { + let e = s.execute("joint.drive", &json!({"joint": "Hinge", "value": value})).err().map(|e| e.to_string()).unwrap_or_default(); + assert!( + e.contains("a joint value must be a number between -1e9 and 1e9") && !e.contains("a joint value must be a number or"), + "{value}: {e}" + ); + } + let e = s.execute("joint.drive", &json!({"joint": "Hinge", "value": true})).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(e.contains("a joint value must be a number or an expression") && !e.contains("between"), "{e}"); +} + /// Two 10 mm cubes in components A (grounded) and B. fn two_cubes() -> Session { let mut s = Session::default(); diff --git a/crates/engine/src/cmd/sheet_tests.rs b/crates/engine/src/cmd/sheet_tests.rs index 717547480..468fbca09 100644 --- a/crates/engine/src/cmd/sheet_tests.rs +++ b/crates/engine/src/cmd/sheet_tests.rs @@ -527,6 +527,25 @@ fn rules_are_parameters() { assert!(s.execute("sheet.manage_rules", &json!({"name": "Bad", "k_factor": 3})).is_err()); } +/// A refused rule names the value at fault and its range (#9625): a thickness too large is not +/// "not positive", and a negative bend radius is not blamed on the K-factor (or the other way). +#[test] +fn a_refused_rule_names_the_value_at_fault() { + let mut s = Session::default(); + let refused = + |s: &mut Session, p: Value| s.execute("sheet.manage_rules", &p).err().map(|e| e.to_string()).unwrap_or_else(|| panic!("{p} was accepted")); + let e = refused(&mut s, json!({"name": "R", "thickness": 10000})); + assert!(e.contains("the sheet thickness must be more than 0 and less than 10000 mm") && !e.contains("positive"), "{e}"); + let e = refused(&mut s, json!({"name": "R", "thickness": 0})); + assert!(e.contains("the sheet thickness must be more than 0 and less than 10000 mm"), "{e}"); + let e = refused(&mut s, json!({"name": "R", "thickness": 2, "bend_radius": -1})); + assert!(e.contains("the bend radius must not be negative") && !e.contains("K-factor"), "{e}"); + let e = refused(&mut s, json!({"name": "R", "thickness": 2, "k_factor": 3})); + assert!(e.contains("the K-factor must be between 0 and 1") && !e.contains("bend radius"), "{e}"); + // A sharp bend (radius 0) is allowed, and the message no longer says otherwise. + run(&mut s, "sheet.manage_rules", json!({"name": "R", "thickness": 2, "bend_radius": 0})); +} + #[test] fn contour_flange_hem_and_tray() { // Oracle 72. diff --git a/crates/engine/src/hole_matrix_tests.rs b/crates/engine/src/hole_matrix_tests.rs index dc862f211..957f9f6a4 100644 --- a/crates/engine/src/hole_matrix_tests.rs +++ b/crates/engine/src/hole_matrix_tests.rs @@ -1003,3 +1003,47 @@ fn bridged_counterbore_layers_along_x_save_and_refuse_bad_values() { p["cs_diameter"] = json!(12); assert!(refusal(&p).contains("counterbore"), "countersink: {}", refusal(&p)); } + +/// A refused hole names the value at fault, not a check it passed (#9625): an unknown fit is not +/// an unknown bolt size, a countersink angle out of range is not a countersink too narrow, and a +/// diameter too large is not a missing one. +#[test] +fn a_refused_hole_names_the_value_at_fault() { + let refused = |extra: Value| { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 10, "center": [0, 0, 5]})).unwrap(); + let mut p = json!({"position": [0, 0, 10]}); + p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone()); + let before = measure(&mut s).unwrap(); + let e = s.execute("solid.hole", &p).err().map(|e| e.to_string()).unwrap_or_else(|| panic!("{p} was accepted")); + assert_eq!(measure(&mut s).unwrap(), before, "{p} changed the part"); + e + }; + let e = refused(json!({"clearance": "M6", "fit": "tight"})); + assert!(e.contains("`fit` must be close, normal or loose, not `tight`") && !e.contains("M1.6"), "{e}"); + let e = refused(json!({"clearance": "M100"})); + assert!(e.contains("no clearance hole for `M100`") && e.contains("M1.6 to M64"), "{e}"); + for angle in [json!(0), json!(200), json!(-90), json!(0.1)] { + let e = refused(json!({"diameter": 3, "type": "countersink", "cs_diameter": 6, "cs_angle": angle})); + assert!(e.contains("the countersink angle must be more than 0.115° and less than 180°") && !e.contains("wider"), "{angle}: {e}"); + } + // 0.1° is refused by the floor the message names, and gives the angle as typed. + let e = refused(json!({"diameter": 3, "type": "countersink", "cs_diameter": 6, "cs_angle": 0.1})); + assert!(e.contains("not 0.1°"), "{e}"); + // An angle that is no usable number is refused, not replaced by the 90° default. + let e = refused(json!({"diameter": 3, "type": "countersink", "cs_diameter": 6, "cs_angle": 1e9})); + assert!(e.contains("`cs_angle` must be a number between -1e9 and 1e9"), "{e}"); + // A blank angle field still means the 90° default, as an omitted one does. + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 10, "center": [0, 0, 5]})).unwrap(); + let blank = json!({"position": [0, 0, 10], "diameter": 3, "type": "countersink", "cs_diameter": 6, "cs_angle": " "}); + s.execute("solid.hole", &blank).unwrap_or_else(|e| panic!("a blank cs_angle was refused: {e}")); + let e = refused(json!({"diameter": 6, "type": "countersink", "cs_diameter": 3})); + assert!(e.contains("the countersink must be wider than the hole") && !e.contains("angle"), "{e}"); + for d in [json!(1e9), json!(-2e12)] { + let e = refused(json!({"diameter": d})); + assert!(e.contains("`diameter` must be a number between -1e9 and 1e9") && !e.contains("needs"), "{d}: {e}"); + } + let e = refused(json!({})); + assert!(e.contains("needs `diameter`, `thread` (a tapped hole) or `clearance` (a bolt size)"), "{e}"); +} diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index bf5e31d3b..1d9e6b8eb 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1639,6 +1639,39 @@ fn preview_of_an_edit() { assert!(Arc::ptr_eq(&doc, &s.doc)); } +/// An extrusion too long says so and gives the limit, not `end = 1000000000` (#9625), whether +/// the distance is typed as text or as a number inside the parameter range. +#[test] +fn an_extrude_too_long_says_so() { + for distance in [json!("1e9 mm"), json!(5e8), json!("-2e7")] { + let mut s = Session::default(); + run(&mut s, "sketch.create", json!({"plane": "XY"})); + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [30, 20]})); + run(&mut s, "sketch.finish", json!({})); + let e = s.execute("solid.extrude", &json!({"distance": distance})).err().map(|e| e.to_string()).unwrap_or_default(); + assert!( + e.contains("the extrusion is too long: its end is") && e.contains("past the limit of 10000000 mm") && !e.contains("end ="), + "{distance}: {e}" + ); + assert!(s.model.state().bodies.is_empty(), "{distance}"); + } + // The Edit Extrude dialog previews a redefinition of the feature: the same message. + let mut s = Session::default(); + run(&mut s, "sketch.create", json!({"plane": "XY"})); + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [30, 20]})); + run(&mut s, "sketch.finish", json!({})); + let fid = run(&mut s, "solid.extrude", json!({"distance": 10}))["feature"].as_u64().unwrap(); + let idx = s.doc.feature_index(fid).unwrap(); + run(&mut s, "timeline.roll_to", json!({"position": idx})); + let redefine = json!({"feature": fid, "command": "solid.extrude", "params": {"distance": "1e9 mm"}}); + let e = s.preview(&[("timeline.redefine".into(), redefine)]).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(e.contains("the extrusion is too long: its end is 1000000000 mm from the sketch plane, past the limit of 10000000 mm"), "{e}"); + // The kernel's limit on a cylinder, a torus or a revolve is not an extrusion's. + let mut s = Session::default(); + let e = s.execute("solid.cylinder", &json!({"radius": 2e7, "height": 10})).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(!e.is_empty() && !e.contains("extrusion"), "{e}"); +} + /// `operation: "auto"`: into a body cuts; additions default to Join when a solid exists. #[test] fn extrude_auto_operation() {