From a9a3b35c9162779ec2da53d7c07f5b0c1568031a Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Tue, 3 Nov 2026 23:38:46 +0700 Subject: [PATCH 1/5] feat(engine): COPY with COPYMODE, Multiple/mOde, Array and Fit as AutoCAD 2024 COPYMODE (profile, 0 copies until Exit, 1 makes one copy) and the base point options mOde (sets it) and Multiple (this command only). Enter at the first second point copies by the base point taken as a displacement. Array asks for 2 to 32767 items, the original included, and spaces them a second point apart or, with Fit, up to it; Undo takes back the last placement, an array at once. MAXARRAY caps an array's copies as it caps ARRAY's. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/modify/select_then.rs | 173 +++++++++++++++-- crates/engine/src/cmd/options.rs | 1 + crates/engine/src/prompt.rs | 3 +- crates/engine/src/settings.rs | 3 + crates/engine/src/sysvars.rs | 3 + crates/engine/tests/it/copy_modes.rs | 205 ++++++++++++++++++++ crates/engine/tests/it/main.rs | 1 + 7 files changed, 372 insertions(+), 17 deletions(-) create mode 100644 crates/engine/tests/it/copy_modes.rs diff --git a/crates/engine/src/cmd/modify/select_then.rs b/crates/engine/src/cmd/modify/select_then.rs index 009a95281..27be601e9 100644 --- a/crates/engine/src/cmd/modify/select_then.rs +++ b/crates/engine/src/cmd/modify/select_then.rs @@ -19,6 +19,18 @@ pub(super) enum Op { Join, } +/// COPY's Array option: asking how many items, then where the second one goes (or, with Fit, the +/// last one). +#[derive(Clone, Copy, PartialEq)] +enum CopyArray { + Off, + Count, + Items { n: u16, fit: bool }, +} + +/// The most items COPY's Array option makes, the original included, as in AutoCAD. +const MAX_COPY_ITEMS: u16 = 32767; + /// Select objects, then collect the points/values the operation needs. pub(super) struct SelectThen { op: Op, @@ -36,6 +48,11 @@ pub(super) struct SelectThen { /// SCALE Reference: the reference length, once known. ref_len: Option, copy_mode: bool, + /// COPY: one copy and done (COPYMODE 1, until the Multiple option), else copies until Exit. + copy_single: bool, + /// COPY: the objects each placement made, so Undo takes back the last one. + copies: Vec>, + copy_array: CopyArray, ask_erase: bool, /// JOIN of a single arc or elliptical arc: the prompt asks for arcs to join to it, or cLose. join_source: Option, @@ -54,6 +71,9 @@ impl SelectThen { ref_from: None, ref_len: None, copy_mode: false, + copy_single: false, + copies: Vec::new(), + copy_array: CopyArray::Off, ask_erase: false, join_source: None, } @@ -83,6 +103,100 @@ impl SelectThen { } } + /// COPY's prompt once the base point is known, as AutoCAD 2024 asks it. + fn copy_prompt(&self, base: Option) -> Prompt { + match self.copy_array { + CopyArray::Count => Prompt::new("Enter number of items to array", Accept::NUMBER), + CopyArray::Items { fit: false, .. } => Prompt::new("Specify second point", Accept::POINT).kw(&["Fit"]).base_opt(base), + CopyArray::Items { fit: true, .. } => Prompt::new("Specify second point", Accept::POINT).kw(&["Array"]).base_opt(base), + CopyArray::Off if self.copies.is_empty() => { + Prompt::new("Specify second point", Accept::POINT).kw(&["Array"]).default("use first point as displacement").base_opt(base) + } + CopyArray::Off => Prompt::new("Specify second point", Accept::POINT).kw(&["Array", "Exit", "Undo"]).default("Exit").base_opt(base), + } + } + + /// COPY once the base point is known (AutoCAD 2024, measured): a point places a copy; Enter + /// at the first prompt copies by the base point taken as a displacement, and after a copy + /// ends the command; Array asks for a number of items, the original included, then puts + /// them a second point apart (or, with Fit, spread up to it, Enter keeping them at the + /// base); Undo takes back the last placement. One placement ends a single-copy COPY. + fn copy_input(&mut self, s: &mut Session, i: Input) -> Result { + let Some(base) = self.pts.first().copied() else { return Ok(Step::Continue) }; + let placed = match (self.copy_array, i) { + (CopyArray::Count, Input::Enter) => return self.copy_done(s), + (CopyArray::Count, Input::Text(t)) => { + // MAXARRAY caps the copies (items after the first, times the objects), as it + // caps ARRAY's. + let per_item = u64::try_from(self.objs.len()).unwrap_or(u64::MAX).max(1); + let max = u16::try_from((s.settings.maxarray / per_item).saturating_add(1)).unwrap_or(MAX_COPY_ITEMS).min(MAX_COPY_ITEMS); + let msg = match max { + MAX_COPY_ITEMS => format!("Requires an integer between 2 and {MAX_COPY_ITEMS}."), + 0 | 1 => "The selection is too large to array (MAXARRAY limits the size of the array).".to_string(), + _ => format!("Requires an integer between 2 and {max} (MAXARRAY limits the size of the array)."), + }; + let n = number(&t).filter(|v| v.fract() == 0.0 && (2.0..=f64::from(max)).contains(v)).ok_or(EngineError::Other(msg))?; + self.copy_array = CopyArray::Items { n: n as u16, fit: false }; + return Ok(Step::Continue); + } + (CopyArray::Items { n, fit }, Input::Keyword(kw)) if kw == if fit { "Array" } else { "Fit" } => { + self.copy_array = CopyArray::Items { n, fit: !fit }; + return Ok(Step::Continue); + } + (CopyArray::Items { n, fit }, i @ (Input::Point(_) | Input::Enter)) => { + let p = if let Input::Point(p) = i { p } else { base }; + let step = if fit { (p - base) * (1.0 / f64::from(n - 1)) } else { p - base }; + self.copy_array = CopyArray::Off; + let mut made = Vec::new(); + for k in 1..n { + match transform_entities(s, &self.objs, &Mat3::translate(step * f64::from(k)), true) { + Ok(hs) => made.extend(hs), + Err(e) => { + // The items made so far are a placement Undo takes back. + self.copies.push(made); + return Err(e); + } + } + } + made + } + (CopyArray::Off, Input::Keyword(kw)) if kw == "Array" => { + self.copy_array = CopyArray::Count; + return Ok(Step::Continue); + } + (CopyArray::Off, Input::Keyword(kw)) if kw == "Undo" => { + if let Some(last) = self.copies.pop() { + let d = s.doc_mut()?; + for h in last { + d.remove_entity(h); + } + if self.copies.is_empty() { + s.echo("Command has been completely undone."); + } + } + return Ok(Step::Continue); + } + (CopyArray::Off, Input::Keyword(kw)) if kw == "Exit" => return self.copy_done(s), + (CopyArray::Off, Input::Enter) if !self.copies.is_empty() => return self.copy_done(s), + (CopyArray::Off, Input::Enter) => { + transform_entities(s, &self.objs, &Mat3::translate(base), true)?; + return self.copy_done(s); + } + (CopyArray::Off, Input::Point(p)) => transform_entities(s, &self.objs, &Mat3::translate(p - base), true)?, + _ => return Ok(Step::Continue), + }; + self.copies.push(placed); + if self.copy_single { + return self.copy_done(s); + } + Ok(Step::Continue) + } + + fn copy_done(&mut self, s: &mut Session) -> Result { + s.set_selection(Vec::new()); + Ok(Step::Done) + } + /// JOIN the selected objects and report what they became. fn join_now(&mut self, s: &mut Session) -> Result { match join(s, &self.objs) { @@ -326,7 +440,7 @@ impl Interactive for SelectThen { match (self.op, k) { (Op::Move | Op::Copy, 0) => Prompt::new("Specify base point", Accept::POINT).kw(&["Displacement"]), (Op::Move, _) => Prompt::new("Specify second point or ", Accept::POINT).base_opt(base), - (Op::Copy, _) => Prompt::new("Specify second point", Accept::POINT).kw(&["Array", "Exit", "Undo"]).base_opt(base), + (Op::Copy, _) => self.copy_prompt(base), (Op::Rotate | Op::Scale, 0) => Prompt::new("Specify base point", Accept::POINT), // ROTATE and SCALE offer the angle/factor used last time. (Op::Rotate, _) => { @@ -385,10 +499,6 @@ impl Interactive for SelectThen { self.reference = true; Ok(Step::Continue) } - (Op::Copy, _, Input::Keyword(kw)) if kw == "Exit" => { - s.set_selection(Vec::new()); - Ok(Step::Done) - } (_, 0, Input::Point(p)) => { self.pts.push(p); Ok(Step::Continue) @@ -405,15 +515,7 @@ impl Interactive for SelectThen { s.set_selection(Vec::new()); Ok(Step::Done) } - (Op::Copy, _, Input::Point(p)) => { - let d = p - self.pts[0]; - transform_entities(s, &self.objs, &Mat3::translate(d), true)?; - Ok(Step::Continue) - } - (Op::Copy, _, Input::Enter) => { - s.set_selection(Vec::new()); - Ok(Step::Done) - } + (Op::Copy, _, i) => self.copy_input(s, i), (Op::Rotate, 1, inp @ (Input::Point(_) | Input::Text(_) | Input::Enter)) => { let a = match inp { Input::Point(p) => self.pts[0].angle_to(p), @@ -533,11 +635,13 @@ impl Interactive for SelectThen { pub(super) struct DisplaceM { inner: SelectThen, displacement: bool, + /// COPY's mOde option: asking Single or Multiple. + ask_mode: bool, } impl DisplaceM { pub(super) fn new(op: Op) -> Self { - DisplaceM { inner: SelectThen::new(op), displacement: false } + DisplaceM { inner: SelectThen::new(op), displacement: false, ask_mode: false } } /// At "Specify base point": objects selected, no point yet. fn at_base(&self) -> bool { @@ -550,14 +654,26 @@ impl Interactive for DisplaceM { self.inner.name() } fn begin(&mut self, s: &mut Session) -> Result { + self.inner.copy_single = self.inner.op == Op::Copy && s.settings.copymode; self.inner.begin(s) } fn prompt(&self, s: &Session) -> Prompt { if self.displacement { return Prompt::new("Specify displacement", Accept::POINT).default("0,0"); } + if self.ask_mode { + let current = if s.settings.copymode { "Single" } else { "Multiple" }; + return Prompt::new("Enter a copy mode option", crate::cmd::curves::KW).kw(&["Single", "Multiple"]).default(current); + } if self.at_base() { - return Prompt::new("Specify base point", Accept::POINT).kw(&["Displacement"]).default("Displacement"); + // COPY: mOde sets COPYMODE; Multiple, offered while it is single, copies until Exit + // this once. + let kw: &[&str] = match self.inner.op { + Op::Copy if self.inner.copy_single => &["Displacement", "mOde", "Multiple"], + Op::Copy => &["Displacement", "mOde"], + _ => &["Displacement"], + }; + return Prompt::new("Specify base point", Accept::POINT).kw(kw).default("Displacement"); } self.inner.prompt(s) } @@ -574,10 +690,35 @@ impl Interactive for DisplaceM { s.set_selection(Vec::new()); return Ok(Step::Done); } + if self.ask_mode { + match &i { + Input::Keyword(k) if k == "Single" || k == "Multiple" => { + s.settings.copymode = k == "Single"; + self.inner.copy_single = s.settings.copymode; + } + Input::Enter => {} + _ => return Ok(Step::Continue), + } + self.ask_mode = false; + return Ok(Step::Continue); + } if self.at_base() && (matches!(&i, Input::Enter) || matches!(&i, Input::Keyword(k) if k == "Displacement")) { self.displacement = true; return Ok(Step::Continue); } + if self.at_base() && self.inner.op == Op::Copy { + match &i { + Input::Keyword(k) if k == "mOde" => { + self.ask_mode = true; + return Ok(Step::Continue); + } + Input::Keyword(k) if k == "Multiple" => { + self.inner.copy_single = false; + return Ok(Step::Continue); + } + _ => {} + } + } self.inner.input(s, i) } fn preview(&self, s: &Session, c: Vec2) -> Vec { diff --git a/crates/engine/src/cmd/options.rs b/crates/engine/src/cmd/options.rs index b9a24580b..f6d877c59 100644 --- a/crates/engine/src/cmd/options.rs +++ b/crates/engine/src/cmd/options.rs @@ -50,6 +50,7 @@ pub const PROFILE_VARS: &[&str] = &[ "CURSORSIZE", "PICKFIRST", "PICKADD", + "COPYMODE", "PICKDRAG", "MAXARRAY", "MEASUREINIT", diff --git a/crates/engine/src/prompt.rs b/crates/engine/src/prompt.rs index e3498dcf0..4b52e7bc5 100644 --- a/crates/engine/src/prompt.rs +++ b/crates/engine/src/prompt.rs @@ -31,12 +31,13 @@ impl Accept { /// [Truecolor/COlorbook] :". A prompt asking for something else besides its keywords has /// it: "Enter block name or [?]:", "Specify insertion base point or [Basepoint/…]:". Read from /// AutoCAD's command line over measured runs, where no prompt was written both ways. -const NO_OR: [&str; 27] = [ +const NO_OR: [&str; 28] = [ "Bring to front", "Calculate rectangle dimensions based on", "Choose mode for selected object conversion", "Create annotative block", "Enter a configuration option", + "Enter a copy mode option", "Enter a plotting preference", "Enter an annotation option", "Enter an option", diff --git a/crates/engine/src/settings.rs b/crates/engine/src/settings.rs index 736cec91c..1610bc501 100644 --- a/crates/engine/src/settings.rs +++ b/crates/engine/src/settings.rs @@ -71,6 +71,8 @@ pub struct Settings { /// FILEDIA: kept for routines that switch file dialogs off around a script; no command reads it yet. pub filedia: bool, pub pickadd: bool, + /// COPYMODE: COPY makes one copy and ends (true, 1), or copies until Enter (false, 0). + pub copymode: bool, /// PICKDRAG: false = two-click windows (0), true = clicks or dragging (2). /// Drag-only mode (1) is not supported yet. pub pickdrag: bool, @@ -168,6 +170,7 @@ impl Default for Settings { cmdecho: true, filedia: true, pickadd: true, + copymode: false, pickdrag: true, pickstyle: 1, gripsize: 5.0, diff --git a/crates/engine/src/sysvars.rs b/crates/engine/src/sysvars.rs index fcecbd2cf..7cef0ea65 100644 --- a/crates/engine/src/sysvars.rs +++ b/crates/engine/src/sysvars.rs @@ -59,6 +59,7 @@ const SESSION_VARS: &[&str] = &[ "CMDECHO", "FILEDIA", "PICKADD", + "COPYMODE", "PICKDRAG", "PICKSTYLE", "GRIPSIZE", @@ -140,6 +141,7 @@ pub fn get(s: &Session, name: &str) -> Option { "CMDECHO" => json!(i32::from(st.cmdecho)), "FILEDIA" => json!(i32::from(st.filedia)), "PICKADD" => json!(i32::from(st.pickadd)), + "COPYMODE" => json!(i32::from(st.copymode)), "PICKDRAG" => json!(i32::from(st.pickdrag) * 2), "PICKSTYLE" => json!(st.pickstyle), "GRIPSIZE" => json!(st.gripsize), @@ -367,6 +369,7 @@ pub fn set(s: &mut Session, name: &str, v: &Value) -> Result<()> { "CMDECHO" => st.cmdecho = small(v, 1).ok_or_else(bad)? == 1, "FILEDIA" => st.filedia = small(v, 1).ok_or_else(bad)? == 1, "PICKADD" => st.pickadd = as_bool(v).ok_or_else(bad)?, + "COPYMODE" => st.copymode = small(v, 1).ok_or_else(bad)? == 1, "PICKDRAG" => st.pickdrag = as_i(v).filter(|b| matches!(b, 0 | 2)).map(|b| b == 2).ok_or_else(bad)?, "PICKSTYLE" => st.pickstyle = as_i(v).filter(|b| (0..=3).contains(b)).and_then(|b| u8::try_from(b).ok()).ok_or_else(bad)?, "GRIPSIZE" => st.gripsize = as_f64(v).ok_or_else(bad)?.clamp(1.0, 255.0), diff --git a/crates/engine/tests/it/copy_modes.rs b/crates/engine/tests/it/copy_modes.rs new file mode 100644 index 000000000..e50ba5232 --- /dev/null +++ b/crates/engine/tests/it/copy_modes.rs @@ -0,0 +1,205 @@ +//! COPY at the command line as AutoCAD 2024 runs it (accoreconsole, a line from (0, 0) to (1, 0) +//! copied with `(command "_.COPY" line "" ...)`, then the start point of every line): +//! +//! - COPYMODE 1 makes one copy and ends; Multiple copies until Exit or Enter, and Undo takes back +//! the last placement, a whole array at once; +//! - Enter at the first second point copies by the base point taken as a displacement; +//! - Array asks for a number of items, the original included (2 to 32767), and puts them the +//! second point apart, or spread up to it with Fit; Enter there leaves them at the base. + +use cadcraft_engine::Session; +use cadcraft_engine::doc::EntityKind; +use serde_json::json; + +/// A session with one line from (0, 0) to (1, 0) and COPYMODE set to `mode`. +fn with_line(mode: i64) -> Session { + let mut s = Session::new(); + s.execute("line", &json!({ "points": [[0, 0], [1, 0]] })).unwrap(); + s.execute("setvar", &json!({ "name": "COPYMODE", "value": mode })).unwrap(); + s +} + +/// The start point of every line, sorted. +fn starts(s: &Session) -> Vec<(f64, f64)> { + let mut v: Vec<(f64, f64)> = + s.doc().unwrap().model.iter().filter_map(|e| if let EntityKind::Line(l) = &e.kind { Some((l.a.x, l.a.y)) } else { None }).collect(); + v.sort_by(|a, b| a.partial_cmp(b).unwrap()); + v +} + +fn copymode(s: &mut Session) -> serde_json::Value { + s.execute("getvar", &json!({ "name": "COPYMODE" })).unwrap()["value"].clone() +} + +/// Types COPY, selects everything, then each of `inputs`. +fn copy(s: &mut Session, inputs: &[&str]) { + for t in ["COPY", "all", ""].iter().chain(inputs) { + s.cmdline(t).unwrap(); + } +} + +#[test] +fn copymode_1_makes_one_copy_and_ends() { + let mut s = with_line(1); + copy(&mut s, &["0,0"]); + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Array] :"); + s.cmdline("10,0").unwrap(); + assert!(s.current_prompt().is_none(), "COPY ended"); + assert_eq!(starts(&s), [(0.0, 0.0), (10.0, 0.0)]); + assert_eq!(copymode(&mut s), json!(1)); +} + +#[test] +fn the_base_point_prompt_offers_mode_and_multiple() { + let mut s = with_line(1); + copy(&mut s, &[]); + assert_eq!(s.current_prompt().unwrap().display(), "Specify base point or [Displacement/mOde/Multiple] :"); + // Multiple, this once: the option goes and COPYMODE stays 1. + s.cmdline("M").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Specify base point or [Displacement/mOde] :"); + for t in ["0,0", "10,0", "20,0"] { + s.cmdline(t).unwrap(); + } + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Array/Exit/Undo] :"); + s.cmdline("E").unwrap(); + assert!(s.current_prompt().is_none()); + assert_eq!(starts(&s), [(0.0, 0.0), (10.0, 0.0), (20.0, 0.0)]); + assert_eq!(copymode(&mut s), json!(1)); + // mOde sets COPYMODE. + let mut s = with_line(0); + copy(&mut s, &["O"]); + assert_eq!(s.current_prompt().unwrap().display(), "Enter a copy mode option [Single/Multiple] :"); + s.cmdline("S").unwrap(); + assert_eq!(copymode(&mut s), json!(1)); + for t in ["0,0", "5,0"] { + s.cmdline(t).unwrap(); + } + assert!(s.current_prompt().is_none(), "one copy, now single"); + assert_eq!(starts(&s), [(0.0, 0.0), (5.0, 0.0)]); +} + +#[test] +fn copymode_0_copies_until_enter_and_undo_takes_back_the_last() { + let mut s = with_line(0); + copy(&mut s, &["0,0", "10,0", "20,0", "U", "30,0", ""]); + assert!(s.current_prompt().is_none()); + assert_eq!(starts(&s), [(0.0, 0.0), (10.0, 0.0), (30.0, 0.0)]); + // Undoing the only copy goes back to the first prompt. + let mut s = with_line(0); + copy(&mut s, &["0,0", "10,0", "U"]); + assert!(s.log.iter().any(|l| l == "Command has been completely undone."), "{:?}", s.log); + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Array] :"); + s.cmdline("30,0").unwrap(); + s.cmdline("").unwrap(); + assert_eq!(starts(&s), [(0.0, 0.0), (30.0, 0.0)]); + // An array is one placement: Undo takes all its items back. + let mut s = with_line(0); + copy(&mut s, &["0,0", "10,0", "A", "3", "0,5", "U", "40,0", ""]); + assert_eq!(starts(&s), [(0.0, 0.0), (10.0, 0.0), (40.0, 0.0)]); + // An array after a copy, and more after it. + let mut s = with_line(0); + copy(&mut s, &["3,4", "10,4", "A", "2", "20,4", ""]); + assert_eq!(starts(&s), [(0.0, 0.0), (7.0, 0.0), (17.0, 0.0)]); +} + +#[test] +fn enter_at_the_first_second_point_copies_by_the_base_point() { + for mode in [0, 1] { + let mut s = with_line(mode); + copy(&mut s, &["3,4", ""]); + assert!(s.current_prompt().is_none(), "COPYMODE {mode}"); + assert_eq!(starts(&s), [(0.0, 0.0), (3.0, 4.0)], "COPYMODE {mode}"); + } + // Displacement, and Enter at the base point (its default), make one copy too. + for inputs in [["D", "5,6"], ["", "7,8"]] { + let mut s = with_line(0); + copy(&mut s, &inputs); + assert!(s.current_prompt().is_none(), "{inputs:?}"); + assert_eq!(starts(&s).len(), 2, "{inputs:?}"); + } +} + +#[test] +fn array_puts_the_items_a_second_point_apart_or_fits_them() { + // (inputs after the base point (0, 0), AutoCAD's lines) + let cases: [(&[&str], &[(f64, f64)]); 5] = [ + (&["A", "4", "10,5"], &[(0.0, 0.0), (10.0, 5.0), (20.0, 10.0), (30.0, 15.0)]), + (&["A", "4", "F", "30,0"], &[(0.0, 0.0), (10.0, 0.0), (20.0, 0.0), (30.0, 0.0)]), + // Fit, then Array again: back to the spacing. + (&["A", "3", "F", "A", "10,0"], &[(0.0, 0.0), (10.0, 0.0), (20.0, 0.0)]), + // The base point again, or Enter: every item at the base. + (&["A", "3", "0,0"], &[(0.0, 0.0); 3]), + (&["A", "3", ""], &[(0.0, 0.0); 3]), + ]; + for (inputs, want) in cases { + let mut s = with_line(1); + copy(&mut s, &["0,0"]); + for t in inputs { + s.cmdline(t).unwrap(); + } + assert!(s.current_prompt().is_none(), "{inputs:?}"); + assert_eq!(starts(&s), want, "{inputs:?}"); + } + // Enter at the array's point with the base at (3, 4): still on the original, the base + // isn't taken as a displacement there. + let mut s = with_line(1); + copy(&mut s, &["3,4", "A", "3", ""]); + assert_eq!(starts(&s), [(0.0, 0.0); 3]); + let mut s = with_line(1); + copy(&mut s, &["0,0", "A", "4"]); + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Fit]:"); + s.cmdline("F").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Array]:"); +} + +#[test] +fn array_takes_2_to_32767_items() { + let mut s = with_line(1); + copy(&mut s, &["0,0", "A"]); + assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:"); + for bad in ["1", "0", "2.5", "32768", "-3"] { + s.cmdline(bad).unwrap(); + assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 32767."), "{bad}"); + assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:", "{bad}"); + } + s.cmdline("32767").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Fit]:"); + // Enter at the number ends COPY with nothing copied. + let mut s = with_line(1); + copy(&mut s, &["0,0", "A", ""]); + assert!(s.current_prompt().is_none()); + assert_eq!(starts(&s), [(0.0, 0.0)]); +} + +#[test] +fn maxarray_caps_the_items_times_the_objects() { + // Two lines and MAXARRAY 100 (its least): 50 copies of each at most, so 51 items. + let mut s = with_line(1); + s.execute("line", &json!({ "points": [[0, 5], [1, 5]] })).unwrap(); + s.execute("setvar", &json!({ "name": "MAXARRAY", "value": 100 })).unwrap(); + copy(&mut s, &["0,0", "A", "52"]); + assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 51 (MAXARRAY limits the size of the array).")); + for t in ["51", "10,0"] { + s.cmdline(t).unwrap(); + } + assert!(s.current_prompt().is_none()); + assert_eq!(starts(&s).len(), 102); + // More objects than MAXARRAY: no array at all. + let mut s = with_line(1); + s.execute("setvar", &json!({ "name": "MAXARRAY", "value": 100 })).unwrap(); + for y in 1..=100 { + s.execute("line", &json!({ "points": [[0, y], [1, y]] })).unwrap(); + } + copy(&mut s, &["0,0", "A", "2"]); + assert_eq!(s.log.last().map(String::as_str), Some("The selection is too large to array (MAXARRAY limits the size of the array).")); +} + +#[test] +fn copymode_takes_0_or_1() { + let mut s = Session::new(); + assert_eq!(copymode(&mut s), json!(0)); + for bad in [json!(2), json!(-1), json!("x")] { + assert!(s.execute("setvar", &json!({ "name": "COPYMODE", "value": bad })).is_err(), "{bad}"); + } + assert_eq!(copymode(&mut s), json!(0)); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 957513621..1f33c13ac 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -27,6 +27,7 @@ mod compare_export; mod constraint_geometry; mod constraint_infer; mod copy_count; +mod copy_modes; mod count; mod dash_block; mod dash_units; From c2b2545d0a444cf6396f7ea410a5fc1eebb1538f Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Wed, 4 Nov 2026 02:55:48 +0700 Subject: [PATCH 2/5] COPY: the array count takes digits only; mOde Enter keeps the mode shown; reset COPYMODE AutoCAD 2024 refuses 4.0, 1e1, 8/2, 1' and 3" at "Enter number of items to array:" (measured), so the count is read as digits, not as a number with no fraction. Enter at the mOde prompt now runs COPY in the mode the prompt shows, and OPTIONS reset puts COPYMODE back to 0. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/modify/select_then.rs | 12 ++++----- crates/engine/src/cmd/options.rs | 1 + crates/engine/tests/it/copy_modes.rs | 28 ++++++++++++++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/modify/select_then.rs b/crates/engine/src/cmd/modify/select_then.rs index 27be601e9..aeae65361 100644 --- a/crates/engine/src/cmd/modify/select_then.rs +++ b/crates/engine/src/cmd/modify/select_then.rs @@ -135,8 +135,9 @@ impl SelectThen { 0 | 1 => "The selection is too large to array (MAXARRAY limits the size of the array).".to_string(), _ => format!("Requires an integer between 2 and {max} (MAXARRAY limits the size of the array)."), }; - let n = number(&t).filter(|v| v.fract() == 0.0 && (2.0..=f64::from(max)).contains(v)).ok_or(EngineError::Other(msg))?; - self.copy_array = CopyArray::Items { n: n as u16, fit: false }; + // Digits only: AutoCAD 2024 refuses 4.0, 1e1, 8/2, 1' and 3" here (measured). + let n = t.trim().parse::().ok().filter(|v| (2..=max).contains(v)).ok_or(EngineError::Other(msg))?; + self.copy_array = CopyArray::Items { n, fit: false }; return Ok(Step::Continue); } (CopyArray::Items { n, fit }, Input::Keyword(kw)) if kw == if fit { "Array" } else { "Fit" } => { @@ -692,13 +693,12 @@ impl Interactive for DisplaceM { } if self.ask_mode { match &i { - Input::Keyword(k) if k == "Single" || k == "Multiple" => { - s.settings.copymode = k == "Single"; - self.inner.copy_single = s.settings.copymode; - } + Input::Keyword(k) if k == "Single" || k == "Multiple" => s.settings.copymode = k == "Single", Input::Enter => {} _ => return Ok(Step::Continue), } + // The mode shown is the mode this COPY runs in, Enter keeping it. + self.inner.copy_single = s.settings.copymode; self.ask_mode = false; return Ok(Step::Continue); } diff --git a/crates/engine/src/cmd/options.rs b/crates/engine/src/cmd/options.rs index f6d877c59..4e3548d86 100644 --- a/crates/engine/src/cmd/options.rs +++ b/crates/engine/src/cmd/options.rs @@ -144,6 +144,7 @@ pub fn reset(s: &mut Session) { st.cursorsize = d.cursorsize; st.pickfirst = d.pickfirst; st.pickadd = d.pickadd; + st.copymode = d.copymode; st.pickdrag = d.pickdrag; st.maxarray = d.maxarray; } diff --git a/crates/engine/tests/it/copy_modes.rs b/crates/engine/tests/it/copy_modes.rs index e50ba5232..0102454c9 100644 --- a/crates/engine/tests/it/copy_modes.rs +++ b/crates/engine/tests/it/copy_modes.rs @@ -65,6 +65,15 @@ fn the_base_point_prompt_offers_mode_and_multiple() { assert!(s.current_prompt().is_none()); assert_eq!(starts(&s), [(0.0, 0.0), (10.0, 0.0), (20.0, 0.0)]); assert_eq!(copymode(&mut s), json!(1)); + // Multiple, then mOde with Enter: the mode shown (Single, COPYMODE 1) is the mode COPY runs in. + let mut s = with_line(1); + copy(&mut s, &["M", "O"]); + assert_eq!(s.current_prompt().unwrap().display(), "Enter a copy mode option [Single/Multiple] :"); + for t in ["", "0,0", "5,0"] { + s.cmdline(t).unwrap(); + } + assert!(s.current_prompt().is_none(), "one copy"); + assert_eq!(starts(&s), [(0.0, 0.0), (5.0, 0.0)]); // mOde sets COPYMODE. let mut s = with_line(0); copy(&mut s, &["O"]); @@ -157,7 +166,7 @@ fn array_takes_2_to_32767_items() { let mut s = with_line(1); copy(&mut s, &["0,0", "A"]); assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:"); - for bad in ["1", "0", "2.5", "32768", "-3"] { + for bad in ["1", "0", "2.5", "32768", "-3", "1'", "8/2", "x", "4.0", "1e1", "3\""] { s.cmdline(bad).unwrap(); assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 32767."), "{bad}"); assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:", "{bad}"); @@ -194,6 +203,23 @@ fn maxarray_caps_the_items_times_the_objects() { assert_eq!(s.log.last().map(String::as_str), Some("The selection is too large to array (MAXARRAY limits the size of the array).")); } +#[test] +fn copymode_is_a_profile_variable() { + // OPTIONS reports it, a saved profile brings it back, reset puts it back to 0, and typing its + // name asks for it as for any variable. + let mut s = with_line(1); + assert_eq!(s.execute("options", &json!({})).unwrap()["COPYMODE"], json!(1)); + let saved = cadcraft_engine::cmd::options::profile(&s); + let mut t = Session::new(); + cadcraft_engine::cmd::options::restore(&mut t, &saved); + assert_eq!(copymode(&mut t), json!(1)); + s.execute("options", &json!({ "reset": true })).unwrap(); + assert_eq!(copymode(&mut s), json!(0)); + s.cmdline("COPYMODE").unwrap(); + s.cmdline("1").unwrap(); + assert_eq!(copymode(&mut s), json!(1)); +} + #[test] fn copymode_takes_0_or_1() { let mut s = Session::new(); From 69b214743d576089f284e9731c45663826a82b77 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Wed, 4 Nov 2026 04:38:00 +0700 Subject: [PATCH 3/5] COPY: pin +3 and 03 at the array count, as AutoCAD 2024 takes them AutoCAD 2024 takes "+3" and "03" at "Enter number of items to array:" (measured: three items each), which the count's integer parse already does. Say so in the comment and test it. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/modify/select_then.rs | 3 ++- crates/engine/tests/it/copy_modes.rs | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/modify/select_then.rs b/crates/engine/src/cmd/modify/select_then.rs index aeae65361..91109fe38 100644 --- a/crates/engine/src/cmd/modify/select_then.rs +++ b/crates/engine/src/cmd/modify/select_then.rs @@ -135,7 +135,8 @@ impl SelectThen { 0 | 1 => "The selection is too large to array (MAXARRAY limits the size of the array).".to_string(), _ => format!("Requires an integer between 2 and {max} (MAXARRAY limits the size of the array)."), }; - // Digits only: AutoCAD 2024 refuses 4.0, 1e1, 8/2, 1' and 3" here (measured). + // A whole number as typed: AutoCAD 2024 takes +3 and 03 here, and refuses 4.0, 1e1, + // 8/2, 1' and 3" (measured). let n = t.trim().parse::().ok().filter(|v| (2..=max).contains(v)).ok_or(EngineError::Other(msg))?; self.copy_array = CopyArray::Items { n, fit: false }; return Ok(Step::Continue); diff --git a/crates/engine/tests/it/copy_modes.rs b/crates/engine/tests/it/copy_modes.rs index 0102454c9..ec607affc 100644 --- a/crates/engine/tests/it/copy_modes.rs +++ b/crates/engine/tests/it/copy_modes.rs @@ -173,6 +173,12 @@ fn array_takes_2_to_32767_items() { } s.cmdline("32767").unwrap(); assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Fit]:"); + // A sign or a leading zero is still a whole number (AutoCAD 2024: three items each). + for three in ["+3", "03"] { + let mut s = with_line(1); + copy(&mut s, &["0,0", "A", three, "5,0"]); + assert_eq!(starts(&s), [(0.0, 0.0), (5.0, 0.0), (10.0, 0.0)], "{three}"); + } // Enter at the number ends COPY with nothing copied. let mut s = with_line(1); copy(&mut s, &["0,0", "A", ""]); From 20463574debb35ff0eec23cee3a239bdd8335d4f Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Wed, 4 Nov 2026 17:35:30 +0700 Subject: [PATCH 4/5] COPY: MAXARRAY doesn't limit Array; refuse a point at the count AutoCAD 2024 doesn't apply MAXARRAY to COPY's Array: with MaxArray 100000, four lines and 26000 items made 104000 lines. The count is now 2 to 32767 whatever the selection, as AutoCAD asks it. A point at "Enter number of items to array:" was ignored; AutoCAD refuses it with "Requires an integer between 2 and 32767." (measured with a LISP point), and so does COPY now. So that a large selection can't run out of memory, the copies (items after the first, times the objects) stay within MAX_ARRAY_ITEMS, ARRAY's own limit whatever MAXARRAY says, checked before anything is made. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/modify/select_then.rs | 33 +++++++------ crates/engine/tests/it/copy_modes.rs | 51 +++++++++++++++------ 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/crates/engine/src/cmd/modify/select_then.rs b/crates/engine/src/cmd/modify/select_then.rs index 91109fe38..2e62c0f1e 100644 --- a/crates/engine/src/cmd/modify/select_then.rs +++ b/crates/engine/src/cmd/modify/select_then.rs @@ -125,19 +125,26 @@ impl SelectThen { let Some(base) = self.pts.first().copied() else { return Ok(Step::Continue) }; let placed = match (self.copy_array, i) { (CopyArray::Count, Input::Enter) => return self.copy_done(s), - (CopyArray::Count, Input::Text(t)) => { - // MAXARRAY caps the copies (items after the first, times the objects), as it - // caps ARRAY's. - let per_item = u64::try_from(self.objs.len()).unwrap_or(u64::MAX).max(1); - let max = u16::try_from((s.settings.maxarray / per_item).saturating_add(1)).unwrap_or(MAX_COPY_ITEMS).min(MAX_COPY_ITEMS); - let msg = match max { - MAX_COPY_ITEMS => format!("Requires an integer between 2 and {MAX_COPY_ITEMS}."), - 0 | 1 => "The selection is too large to array (MAXARRAY limits the size of the array).".to_string(), - _ => format!("Requires an integer between 2 and {max} (MAXARRAY limits the size of the array)."), - }; - // A whole number as typed: AutoCAD 2024 takes +3 and 03 here, and refuses 4.0, 1e1, - // 8/2, 1' and 3" (measured). - let n = t.trim().parse::().ok().filter(|v| (2..=max).contains(v)).ok_or(EngineError::Other(msg))?; + (CopyArray::Count, i) => { + // A whole number as typed, from 2 to 32767 whatever the selection: AutoCAD 2024 takes + // +3 and 03 here, refuses 4.0, 1e1, 8/2, 1', 3", 50002 and a point, and doesn't + // apply MAXARRAY (with MAXARRAY 100000, four lines and 26000 items made 104000 + // lines; measured). + let n = match i { + Input::Text(t) => t.trim().parse::().ok().filter(|v| (2..=MAX_COPY_ITEMS).contains(v)), + _ => None, + } + .ok_or_else(|| EngineError::Other(format!("Requires an integer between 2 and {MAX_COPY_ITEMS}.")))?; + // Not AutoCAD's: the copies (items after the first, times the objects) stay within + // ARRAY's own limit, whatever MAXARRAY says, so a large selection can't run out of + // memory. + let copies = u64::from(n - 1).saturating_mul(u64::try_from(self.objs.len()).unwrap_or(u64::MAX)); + if copies > cadcraft_doc::MAX_ARRAY_ITEMS { + return Err(EngineError::Other(format!( + "The array would create {copies} objects; the limit is {}. Use fewer items or select fewer objects.", + cadcraft_doc::MAX_ARRAY_ITEMS + ))); + } self.copy_array = CopyArray::Items { n, fit: false }; return Ok(Step::Continue); } diff --git a/crates/engine/tests/it/copy_modes.rs b/crates/engine/tests/it/copy_modes.rs index ec607affc..16ab8fb9e 100644 --- a/crates/engine/tests/it/copy_modes.rs +++ b/crates/engine/tests/it/copy_modes.rs @@ -166,15 +166,19 @@ fn array_takes_2_to_32767_items() { let mut s = with_line(1); copy(&mut s, &["0,0", "A"]); assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:"); - for bad in ["1", "0", "2.5", "32768", "-3", "1'", "8/2", "x", "4.0", "1e1", "3\""] { + // A point from LISP is refused too (AutoCAD 2024 with '(1 2): the same message). + for bad in ["1", "0", "2.5", "32768", "-3", "1'", "8/2", "x", "4.0", "1e1", "3\"", "(list 1 2)"] { + let logged = s.log.len(); s.cmdline(bad).unwrap(); - assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 32767."), "{bad}"); + 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."], "{bad}"); assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:", "{bad}"); } s.cmdline("32767").unwrap(); assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Fit]:"); - // A sign or a leading zero is still a whole number (AutoCAD 2024: three items each). - for three in ["+3", "03"] { + // A sign or a leading zero is still a whole number, and so is the LISP real 3.0 (AutoCAD 2024: + // three items each). + for three in ["+3", "03", "(+ 1.5 1.5)"] { let mut s = with_line(1); copy(&mut s, &["0,0", "A", three, "5,0"]); assert_eq!(starts(&s), [(0.0, 0.0), (5.0, 0.0), (10.0, 0.0)], "{three}"); @@ -187,26 +191,43 @@ fn array_takes_2_to_32767_items() { } #[test] -fn maxarray_caps_the_items_times_the_objects() { - // Two lines and MAXARRAY 100 (its least): 50 copies of each at most, so 51 items. +fn maxarray_doesnt_limit_the_items() { + // AutoCAD 2024 with MAXARRAY 100000: four lines and 26000 items made 104000 lines (measured). + // Here MAXARRAY is 100 (its least): two lines and 60 items, and 101 lines and 2 items, go past + // it and are still made. let mut s = with_line(1); s.execute("line", &json!({ "points": [[0, 5], [1, 5]] })).unwrap(); s.execute("setvar", &json!({ "name": "MAXARRAY", "value": 100 })).unwrap(); - copy(&mut s, &["0,0", "A", "52"]); - assert_eq!(s.log.last().map(String::as_str), Some("Requires an integer between 2 and 51 (MAXARRAY limits the size of the array).")); - for t in ["51", "10,0"] { - s.cmdline(t).unwrap(); - } + copy(&mut s, &["0,0", "A", "60", "10,0"]); assert!(s.current_prompt().is_none()); - assert_eq!(starts(&s).len(), 102); - // More objects than MAXARRAY: no array at all. + assert_eq!(starts(&s).len(), 120); let mut s = with_line(1); s.execute("setvar", &json!({ "name": "MAXARRAY", "value": 100 })).unwrap(); for y in 1..=100 { s.execute("line", &json!({ "points": [[0, y], [1, y]] })).unwrap(); } - copy(&mut s, &["0,0", "A", "2"]); - assert_eq!(s.log.last().map(String::as_str), Some("The selection is too large to array (MAXARRAY limits the size of the array).")); + copy(&mut s, &["0,0", "A", "2", "10,0"]); + assert!(s.current_prompt().is_none()); + assert_eq!(starts(&s).len(), 202); +} + +#[test] +fn the_copies_stay_within_ten_million() { + // Not AutoCAD's limit, ARRAY's: 400 lines times 25000 more items is exactly 10 million copies + // and is taken; one item more is refused before anything is made. + let mut s = with_line(1); + for y in 1..400 { + s.execute("line", &json!({ "points": [[0, y], [1, y]] })).unwrap(); + } + copy(&mut s, &["0,0", "A", "25002"]); + assert_eq!( + s.log.last().map(String::as_str), + Some("The array would create 10000400 objects; the limit is 10000000. Use fewer items or select fewer objects.") + ); + assert_eq!(s.current_prompt().unwrap().display(), "Enter number of items to array:"); + assert_eq!(starts(&s).len(), 400); + s.cmdline("25001").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Specify second point or [Fit]:"); } #[test] From 62a1a23288c2c261d12b4aab8197856b194a9ce3 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:06:54 +0700 Subject: [PATCH 5/5] Prompt: declare NO_OR as a slice This PR and #2915 each add a prompt to NO_OR and raise its array length from 28 to 29, so with both merged the array holds 30 entries under a length of 29 (E0308), whichever lands first. A slice has no length to keep in step; both PRs make the same change to this line. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/prompt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine/src/prompt.rs b/crates/engine/src/prompt.rs index 4eb5a201b..44f485e80 100644 --- a/crates/engine/src/prompt.rs +++ b/crates/engine/src/prompt.rs @@ -31,7 +31,7 @@ impl Accept { /// [Truecolor/COlorbook] :". A prompt asking for something else besides its keywords has /// it: "Enter block name or [?]:", "Specify insertion base point or [Basepoint/…]:". Read from /// AutoCAD's command line over measured runs, where no prompt was written both ways. -const NO_OR: [&str; 29] = [ +const NO_OR: &[&str] = &[ "Bring to front", "Calculate rectangle dimensions based on", "Choose mode for selected object conversion",