diff --git a/crates/engine/src/cmd/modify/select_then.rs b/crates/engine/src/cmd/modify/select_then.rs index 408cd4a2b..98e3b7742 100644 --- a/crates/engine/src/cmd/modify/select_then.rs +++ b/crates/engine/src/cmd/modify/select_then.rs @@ -20,6 +20,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, @@ -37,6 +49,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, @@ -57,6 +74,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, ask_circle: false, @@ -87,6 +107,109 @@ 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, 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); + } + (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. Arcs that make a circle ask first, /// as AutoCAD 2024 does. fn join_now(&mut self, s: &mut Session) -> Result { @@ -346,7 +469,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, _) => { @@ -414,10 +537,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) @@ -434,15 +553,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), @@ -568,11 +679,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 { @@ -585,14 +698,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) } @@ -609,10 +734,34 @@ 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", + 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); + } 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 75b11335e..f1ebc61a1 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", @@ -157,6 +158,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; st.dynprompt = d.dynprompt; diff --git a/crates/engine/src/prompt.rs b/crates/engine/src/prompt.rs index c2383a272..f2d46d9ca 100644 --- a/crates/engine/src/prompt.rs +++ b/crates/engine/src/prompt.rs @@ -37,6 +37,7 @@ const NO_OR: &[&str] = &[ "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 094944138..35bcae4df 100644 --- a/crates/engine/src/settings.rs +++ b/crates/engine/src/settings.rs @@ -75,6 +75,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, @@ -180,6 +182,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 51dd06f7d..a491fb7d8 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), @@ -394,6 +396,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..16ab8fb9e --- /dev/null +++ b/crates/engine/tests/it/copy_modes.rs @@ -0,0 +1,258 @@ +//! 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)); + // 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"]); + 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:"); + // 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(); + 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, 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}"); + } + // 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_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", "60", "10,0"]); + assert!(s.current_prompt().is_none()); + 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", "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] +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(); + 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 5af89604f..75f402be9 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -34,6 +34,7 @@ mod compare_export; mod constraint_geometry; mod constraint_infer; mod copy_count; +mod copy_modes; mod count; mod dash_block; mod dash_units;