diff --git a/crates/engine/src/cmd/draw2.rs b/crates/engine/src/cmd/draw2.rs index 1d3b10c6c..3bcbee605 100644 --- a/crates/engine/src/cmd/draw2.rs +++ b/crates/engine/src/cmd/draw2.rs @@ -918,6 +918,11 @@ fn default_arc_len(s: &Session) -> f64 { d.map(|d| d.header.f64("REVCLOUDARCLEN", if metric { 15.0 } else { 0.5 })).unwrap_or(0.5).max(1e-6) } +/// Maximum arc length remembered between REVCLOUD runs (CADCraft-only state, not an AutoCAD variable); never below `min`. +fn default_max_arc_len(s: &Session, min: f64) -> f64 { + s.doc().ok().map_or(min, |d| d.header.f64("CADCRAFT_REVCLOUDMAXARCLEN", min)).max(min) +} + /// Bulged closed polyline vertices for a revision cloud around `boundary`. pub(crate) fn cloud_vertices(boundary: &[Vec2], arc_len: f64, per_edge: bool, reverse: bool) -> Option> { let mut pts: Vec = Vec::new(); @@ -1024,15 +1029,31 @@ struct RevcloudM { kind: CloudKind, pts: Vec, arc_len: f64, + max_arc_len: f64, asking_arc: bool, + asking_max: bool, object: bool, + /// Outline of the picked object while "Reverse direction" is being asked. + picked: Option<(Handle, Vec)>, calligraphy: bool, asking_style: bool, } impl RevcloudM { fn new(s: &Session, kind: CloudKind) -> Self { - RevcloudM { kind, pts: Vec::new(), arc_len: default_arc_len(s), asking_arc: false, object: false, calligraphy: false, asking_style: false } + let arc_len = default_arc_len(s); + RevcloudM { + kind, + pts: Vec::new(), + arc_len, + max_arc_len: default_max_arc_len(s, arc_len), + asking_arc: false, + asking_max: false, + object: false, + picked: None, + calligraphy: false, + asking_style: false, + } } fn boundary(&self, extra: Option) -> Vec { let mut pts = self.pts.clone(); @@ -1067,13 +1088,19 @@ impl Interactive for RevcloudM { CloudKind::Poly => "Polygonal", CloudKind::Free => "Freehand", }; - s.echo(format!("Minimum arc length: {0:.4} Maximum arc length: {0:.4} Style: Normal Type: {t}", self.arc_len)); + s.echo(format!("Minimum arc length: {0:.4} Maximum arc length: {1:.4} Style: Normal Type: {t}", self.arc_len, self.max_arc_len)); Ok(Step::Continue) } fn prompt(&self, _s: &Session) -> Prompt { + if self.picked.is_some() { + return Prompt::new("Reverse direction", curves::KW).kw(&["Yes", "No"]).default("No"); + } if self.asking_arc { return Prompt::new("Specify minimum length of arc", Accept::NUMBER).default(format!("{:.4}", self.arc_len)); } + if self.asking_max { + return Prompt::new("Specify maximum length of arc", Accept::NUMBER).default(format!("{:.4}", self.max_arc_len)); + } if self.asking_style { return Prompt::new("Select arc style", curves::KW).kw(&["Normal", "Calligraphy"]).default("Normal"); } @@ -1092,13 +1119,38 @@ impl Interactive for RevcloudM { } } fn input(&mut self, s: &mut Session, i: Input) -> Result { + if let Some((h, pts)) = self.picked.take() { + let reverse = matches!(&i, Input::Keyword(k) | Input::Text(k) if k.to_ascii_lowercase().starts_with('y')); + let vs = cloud_vertices(&pts, self.arc_len, false, reverse).ok_or_else(|| other("The object has no area."))?; + let nh = s.add_entity(cloud_entity(vs, self.calligraphy, self.arc_len))?; + let common = curves::entity(s, h)?.common; + let d = s.doc_mut()?; + d.modify_entity(nh, |e| e.common = common)?; + d.remove_entity(h); + s.echo("Revision cloud finished."); + return Ok(Step::Done); + } if self.asking_arc { if let Input::Text(t) = &i { let v = number(t).filter(|v| *v > 0.0).ok_or_else(|| other("Requires a positive value."))?; self.arc_len = v; - s.doc_mut()?.header.set_f64("REVCLOUDARCLEN", v); + self.max_arc_len = self.max_arc_len.max(v); + let max = self.max_arc_len; + let d = s.doc_mut()?; + d.header.set_f64("REVCLOUDARCLEN", v); + d.header.set_f64("CADCRAFT_REVCLOUDMAXARCLEN", max); } self.asking_arc = false; + self.asking_max = true; + return Ok(Step::Continue); + } + if self.asking_max { + if let Input::Text(t) = &i { + let v = number(t).filter(|v| *v >= self.arc_len).ok_or_else(|| other("Requires a value not less than the minimum arc length."))?; + self.max_arc_len = v; + s.doc_mut()?.header.set_f64("CADCRAFT_REVCLOUDMAXARCLEN", v); + } + self.asking_max = false; return Ok(Step::Continue); } if self.asking_style { @@ -1112,14 +1164,12 @@ impl Interactive for RevcloudM { let Input::Point(p) = i else { return if i == Input::Enter { Ok(Step::Cancel) } else { Ok(Step::Continue) } }; let h = curves::pick_at(s, p).ok_or_else(|| other("*Invalid selection*"))?; let pts = object_outline(s, h)?; - let vs = cloud_vertices(&pts, self.arc_len, false, false).ok_or_else(|| other("The object has no area."))?; - let nh = s.add_entity(cloud_entity(vs, self.calligraphy, self.arc_len))?; - let common = curves::entity(s, h)?.common; - let d = s.doc_mut()?; - d.modify_entity(nh, |e| e.common = common)?; - d.remove_entity(h); - s.echo("Revision cloud finished."); - return Ok(Step::Done); + if cloud_vertices(&pts, self.arc_len, false, false).is_none() { + return Err(other("The object has no area.")); + } + self.object = false; + self.picked = Some((h, pts)); + return Ok(Step::Continue); } match i { Input::Keyword(k) => { diff --git a/crates/engine/src/cmd/draw2_tests.rs b/crates/engine/src/cmd/draw2_tests.rs index 56be94cc4..8ddc7bc3b 100644 --- a/crates/engine/src/cmd/draw2_tests.rs +++ b/crates/engine/src/cmd/draw2_tests.rs @@ -354,7 +354,7 @@ fn revcloud_json() { #[test] fn revcloud_command_line() { let mut s = Session::new(); - s.cmdline("revcloud a 1").unwrap(); + s.cmdline("revcloud a 1 1").unwrap(); s.cmdline("0,0 4,2").unwrap(); assert!(s.running.is_none(), "{}", s.prompt_text()); assert!(matches!(last(&s), EntityKind::LwPolyline(p) if p.vertices.len() == 12)); @@ -367,11 +367,28 @@ fn revcloud_command_line() { assert_eq!(kinds(&s).len(), 3); // Object option converts a closed polyline. s.execute("rectang", &json!({"p1": [10, 10], "p2": [12, 12]})).unwrap(); - s.cmdline("revcloud o 10,11").unwrap(); + s.cmdline("revcloud o 10,11 n").unwrap(); assert!(s.running.is_none()); assert_eq!(kinds(&s).len(), 4); } +#[test] +fn revcloud_arc_length_asks_max_and_object_asks_reverse() { + let mut s = Session::new(); + s.execute("rectang", &json!({"p1": [10, 10], "p2": [12, 12]})).unwrap(); + s.cmdline("revcloud a 200 800").unwrap(); + assert!(s.prompt_text().contains("first corner"), "{}", s.prompt_text()); + s.cmdline("0,0 2000,1000").unwrap(); + assert!(s.running.is_none()); + s.cmdline("revcloud").unwrap(); + assert!(s.log.iter().any(|l| l.contains("Minimum arc length: 200.0000 Maximum arc length: 800.0000")), "{:?}", s.log); + s.cmdline("o").unwrap(); + s.cmdline("10,11").unwrap(); + assert!(s.running.is_some() && s.prompt_text().contains("Reverse direction"), "{}", s.prompt_text()); + s.cmdline("n").unwrap(); + assert!(s.running.is_none()); +} + // ---------------- BREAKLINE ---------------- fn breakline_points(s: &Session) -> Vec<(f64, f64)> {