From 8af4b8a61b944edbce328c8cff23692ca19b024d Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Wed, 4 Nov 2026 19:05:56 +0700 Subject: [PATCH] DIVIDE: take a whole number as typed for the number of segments The count was read as a number with no fraction, so 5.0, 5. and 1e1 were taken; a point gave its distance from the pick as the count; and Enter ended DIVIDE. AutoCAD 2024 takes +5, 05 and 2 to 32767, and refuses all of those, and 4.5, 1, 32768 and x, with "Requires an integer between 2 and 32767, or option keyword." ("...32767." once Block has been chosen) and asks again (measured). DIVIDE now does the same. MEASURE, which shares the prompt machine, is unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/draw2/divide.rs | 47 ++++++++++++++++-------- crates/engine/src/cmd/draw2/tests.rs | 51 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/crates/engine/src/cmd/draw2/divide.rs b/crates/engine/src/cmd/draw2/divide.rs index 4e92f561f..c0855dea0 100644 --- a/crates/engine/src/cmd/draw2/divide.rs +++ b/crates/engine/src/cmd/draw2/divide.rs @@ -137,6 +137,15 @@ impl DivideM { pub(super) fn new(measure: bool) -> Self { DivideM { measure, obj: None, phase: DivPhase::Object, block: None, align: true } } + + /// The JSON form's parameters for the picked object, before the count or length. + fn params(&self, h: Handle, pick: Vec2) -> Value { + let mut params = json!({ "handle": h.hex(), "align": self.align, "from": [pick.x, pick.y] }); + if let Some(b) = &self.block { + params["block"] = json!(b); + } + params + } } impl Interactive for DivideM { @@ -185,27 +194,37 @@ impl Interactive for DivideM { self.phase = DivPhase::Value; Ok(Step::Continue) } - (DivPhase::Value, inp @ (Input::Text(_) | Input::Point(_))) => { + (DivPhase::Value, inp @ (Input::Text(_) | Input::Point(_))) if self.measure => { let Some((h, pick)) = self.obj else { return Ok(Step::Cancel) }; let v = match inp { Input::Text(t) => number(&t).ok_or_else(|| other("Requires a number."))?, Input::Point(q) => pick.dist(q), _ => return Ok(Step::Continue), }; - let mut params = json!({ "handle": h.hex(), "align": self.align, "from": [pick.x, pick.y] }); - if let Some(b) = &self.block { - params["block"] = json!(b); - } - if self.measure { - params["length"] = json!(v); - run_measure(s, ¶ms)?; - } else { - if !(2.0..=32767.0).contains(&v) || v.fract() != 0.0 { - return Err(other("Requires an integer between 2 and 32767.")); - } - params["segments"] = json!(v as u64); - run_divide(s, ¶ms)?; + let mut params = self.params(h, pick); + params["length"] = json!(v); + run_measure(s, ¶ms)?; + Ok(Step::Done) + } + (DivPhase::Value, inp) if !self.measure => { + // A whole number as typed: AutoCAD 2024 takes +5 and 05 here and refuses 5.0, 5., + // 1e1, 4.5, 1, 32768, x, Enter and a point, naming the Block keyword while the + // prompt still offers it (measured). + let n = match inp { + Input::Text(t) => t.trim().parse::().ok().filter(|n| (2..=32767).contains(n)), + _ => None, } + .ok_or_else(|| { + other(if self.block.is_some() { + "Requires an integer between 2 and 32767." + } else { + "Requires an integer between 2 and 32767, or option keyword." + }) + })?; + let Some((h, pick)) = self.obj else { return Ok(Step::Cancel) }; + let mut params = self.params(h, pick); + params["segments"] = json!(n); + run_divide(s, ¶ms)?; Ok(Step::Done) } (_, Input::Enter) => Ok(Step::Cancel), diff --git a/crates/engine/src/cmd/draw2/tests.rs b/crates/engine/src/cmd/draw2/tests.rs index efad025ca..54e365633 100644 --- a/crates/engine/src/cmd/draw2/tests.rs +++ b/crates/engine/src/cmd/draw2/tests.rs @@ -710,6 +710,57 @@ fn divide_and_measure_command_line() { s.cancel(); } +#[test] +fn divide_takes_a_whole_number_as_typed() { + // AutoCAD 2024 at "Enter the number of segments or [Block]:": +5, 05, the LISP real 5.0 and + // 32767 are taken; 5.0, 5., 1e1, 4.5, 1, 0, -3, 32768, x, Enter and a point from LISP are + // refused and asked again (measured). + let points = |s: &Session| kinds(s).iter().filter(|k| **k == "Point").count(); + let mut s = Session::new(); + s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap(); + s.cmdline("divide 5,0").unwrap(); + let asked = s.prompt_text(); + assert_eq!(s.current_prompt().map(|p| p.display()).as_deref(), Some("Enter the number of segments or [Block]:")); + for bad in ["5.0", "5.", "1e1", "4.5", "1", "0", "-3", "32768", "x", "", "(list 1 2)"] { + let logged = s.log.len(); + s.cmdline(bad).unwrap(); + let said: Vec<&str> = s.log.iter().skip(logged).map(String::as_str).filter(|l| l.starts_with("Requires")).collect(); + assert_eq!(said, ["Requires an integer between 2 and 32767, or option keyword."], "{bad:?}"); + assert_eq!(s.prompt_text(), asked, "{bad:?}"); + } + assert_eq!(points(&s), 0); + s.cmdline("+5").unwrap(); + assert!(s.running.is_none()); + assert_eq!(points(&s), 4); + for (typed, n) in [("05", 4), ("(+ 2.5 2.5)", 4), ("2", 1), ("32767", 32766)] { + let mut s = Session::new(); + s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap(); + s.cmdline("divide 5,0").unwrap(); + s.cmdline(typed).unwrap(); + assert!(s.running.is_none(), "{typed}: {}", s.prompt_text()); + assert_eq!(points(&s), n, "{typed}"); + } + // MEASURE shares the prompt machine and is unchanged: Enter at its length still ends it. + let mut s = Session::new(); + s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap(); + s.cmdline("measure 5,0").unwrap(); + s.cmdline("").unwrap(); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(points(&s), 0); + // After Block the prompt offers no keyword, and the message names none (AutoCAD 2024, measured). + let mut s = Session::new(); + s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap(); + let tick = s.execute("circle", &json!({"center": [50, 50], "radius": 0.1})).unwrap(); + s.execute("block", &json!({"name": "DOT", "base": [50, 50], "handles": [h(&tick)], "keep": "delete"})).unwrap(); + s.cmdline("divide 5,0 b DOT y").unwrap(); + assert_eq!(s.current_prompt().map(|p| p.display()).as_deref(), Some("Enter the number of segments:")); + s.cmdline("5.0").unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 32767.")); + s.cmdline("5").unwrap(); + assert!(s.running.is_none()); + assert_eq!(kinds(&s).iter().filter(|k| **k == "Block Reference").count(), 4); +} + // ---------------- REVCLOUD ---------------- #[test]