diff --git a/crates/engine/src/cmd/modify/break_join.rs b/crates/engine/src/cmd/modify/break_join.rs index 65cccbd35..ce3cb00da 100644 --- a/crates/engine/src/cmd/modify/break_join.rs +++ b/crates/engine/src/cmd/modify/break_join.rs @@ -143,89 +143,73 @@ pub(crate) fn join(s: &mut Session, hs: &[Handle]) -> Result> { if hs.iter().any(|h| crate::cmd::curves::is_locked(s, *h)) { return Err(EngineError::Other("An object is on a locked layer.".into())); } - // A handle named twice is one object. - let mut seen = std::collections::HashSet::new(); - let hs: Vec = hs.iter().copied().filter(|h| seen.insert(*h)).collect(); + let hs = joinable_picks(s.doc()?, hs); let hs = &hs[..]; if let Some(h) = join_on_source(s, hs)? { return Ok(Some(h)); } let d = s.doc()?; - let mut segs: Vec = Vec::new(); - // What each segment in `segs` came from, so only entities that end up in the chain are removed. - let mut pieces: Vec = Vec::new(); - let mut common = None; + // Each object JOIN can take, as the pieces it is made of: a line or an arc is one piece, an + // open polyline its segments in order, each keeping its vertices, bulge and widths exactly. + let mut units: Vec> = Vec::new(); for h in hs { let Some(e) = d.entity(*h) else { continue }; - common.get_or_insert_with(|| e.common.clone()); let piece = |ends: (Vec2, Vec2), bulge: f64, widths: Option<(f64, f64)>| Piece { owner: *h, ends, bulge, widths }; - match &e.kind { - EntityKind::Line(l) => { - segs.push(Segment::Line(Line::new(l.a.xy(), l.b.xy()))); - pieces.push(piece((l.a.xy(), l.b.xy()), 0.0, None)); - } + let unit: Vec = match &e.kind { + // A line of no length joins as AutoCAD 2024 joins it, a repeated vertex. + EntityKind::Line(l) => vec![piece((l.a.xy(), l.b.xy()), 0.0, None)], EntityKind::Arc(a) => { let arc = Arc::new(a.center.xy(), a.radius, a.start, a.end); - pieces.push(piece((arc.start_point(), arc.end_point()), cadcraft_geom::arc_to_bulge(arc.sweep()), None)); - segs.push(Segment::Arc { arc, ccw: true }); + vec![piece((arc.start_point(), arc.end_point()), cadcraft_geom::arc_to_bulge(arc.sweep()), None)] } - EntityKind::LwPolyline(p) if !p.closed => { - // Segment by segment, so each keeps its vertices, bulge and widths exactly. - for w in p.vertices.windows(2) { + // Repeated vertices included: AutoCAD 2024 keeps them. + EntityKind::LwPolyline(p) if !p.closed => p + .vertices + .windows(2) + .map(|w| { let widths = if p.const_width > 0.0 { (p.const_width, p.const_width) } else { (w[0].start_width, w[0].end_width) }; - for sg in (Polyline { vertices: w.to_vec(), closed: false }).segments() { - segs.push(sg); - pieces.push(piece((w[0].p, w[1].p), w[0].bulge, Some(widths))); - } - } - } - _ => {} + piece((w[0].p, w[1].p), w[0].bulge, Some(widths)) + }) + .collect(), + _ => Vec::new(), + }; + if !unit.is_empty() { + units.push(unit); } } - if segs.len() < 2 { + if units.iter().map(Vec::len).sum::() < 2 { return Err(EngineError::Other("Select at least two objects to join.".into())); } - // Chain greedily from the first segment. - let mut chain = vec![segs.remove(0)]; - let mut links = vec![pieces.remove(0)]; + // A new polyline takes the source's properties. + let first = units.first().and_then(|u| u.first()).map(|p| p.owner); + let common = first.and_then(|h| d.entity(h)).map(|e| e.common.clone()); let tol = 1e-6; - loop { - let start = chain.first().map(Segment::start).unwrap_or_default(); - let end = chain.last().map(Segment::end).unwrap_or_default(); - let Some(i) = - segs.iter().position(|s| s.start().near(end, tol) || s.end().near(end, tol) || s.start().near(start, tol) || s.end().near(start, tol)) - else { - break; - }; - let (s0, p0) = (segs.remove(i), pieces.remove(i)); - if s0.start().near(end, tol) { - chain.push(s0); - links.push(p0); - } else if s0.end().near(end, tol) { - chain.push(s0.reversed()); - links.push(p0.flipped()); - } else if s0.end().near(start, tol) { - chain.insert(0, s0); - links.insert(0, p0); - } else { - chain.insert(0, s0.reversed()); - links.insert(0, p0.flipped()); - } - } - if chain.len() < 2 { + let links = chain_units(units, tol); + // One object alone joins nothing, even a polyline whose ends meet: AutoCAD 2024 leaves it open + // ("0 segments added to polyline"). + let closed = links.first().zip(links.last()).is_some_and(|(a, b)| a.ends.0.near(b.ends.1, tol)); + let one_object = links.first().is_some_and(|f| links.iter().all(|l| l.owner == f.owner)); + if links.len() < 2 || one_object { return Err(EngineError::Other("Objects are not connected.".into())); } let used = |h: &Handle| links.iter().any(|l| l.owner == *h); - let closed = chain.first().zip(chain.last()).is_some_and(|(a, b)| a.start().near(b.end(), tol)); - let mut vs: Vec = links.iter().map(|l| PolyVertex { p: l.ends.0, bulge: l.bulge, ..Default::default() }).collect(); + // A polyline's own segments keep their widths. + let mut vs: Vec = links + .iter() + .map(|l| { + let (start_width, end_width) = l.widths.unwrap_or_default(); + PolyVertex { p: l.ends.0, bulge: l.bulge, start_width, end_width } + }) + .collect(); if !closed && let Some(l) = links.last() { vs.push(PolyVertex::new(l.ends.1)); } - let space = s.space(); + // A new polyline goes in the source's space, whichever space is current. + let space = first.and_then(|h| d.space_of(h)).unwrap_or_else(|| s.space()); let doc = s.doc_mut()?; // A polyline source takes the other pieces and keeps its handle (AutoCAD 2024: "1 segments // added to polyline"). - if let Some(src) = hs.first().copied() + if let Some(src) = first && let Some(EntityKind::LwPolyline(sp)) = doc.entity(src).map(|e| &e.kind) && let Some(onto) = onto_polyline(sp, src, &links, closed) { @@ -247,6 +231,113 @@ pub(crate) fn join(s: &mut Session, hs: &[Handle]) -> Result> { Ok(Some(nh)) } +/// The picks JOIN can take, each once, in the order picked: lines, arcs and open polylines. The +/// first of them is the source (AutoCAD 2024 passes over a circle picked first and leaves it). +fn joinable_picks(d: &cadcraft_doc::Drawing, hs: &[Handle]) -> Vec { + let mut seen = std::collections::HashSet::new(); + let joinable = |h: &Handle| { + d.entity(*h).is_some_and(|e| match &e.kind { + EntityKind::Line(_) => true, + EntityKind::Arc(a) => a.radius > cadcraft_geom::EPS, + EntityKind::LwPolyline(p) => !p.closed, + _ => false, + }) + }; + hs.iter().copied().filter(|h| joinable(h) && seen.insert(*h)).collect() +} + +/// Whether JOIN would make the picked arcs a circle. AutoCAD 2024 asks first ("Combined arc +/// segments form a circle. Convert to circle? [Yes/No] :"), and No leaves every arc as it +/// is (measured). +pub(crate) fn join_makes_circle(s: &Session, hs: &[Handle]) -> bool { + let Ok(d) = s.doc() else { return false }; + // Elliptical arcs and splines join another way, and a locked object stops the join. + let other_way = |h: &Handle| d.entity(*h).is_some_and(|e| matches!(e.kind, EntityKind::Ellipse(_) | EntityKind::Spline(_))); + if hs.iter().any(|h| other_way(h) || crate::cmd::curves::is_locked(s, *h)) { + return false; + } + let picks = joinable_picks(d, hs); + let arcs: Vec<&cadcraft_doc::Arc> = picks + .iter() + .filter_map(|h| match d.entity(*h).map(|e| &e.kind) { + Some(EntityKind::Arc(a)) => Some(a), + _ => None, + }) + .collect(); + let tol = 1e-6; + match &arcs[..] { + [g, rest @ ..] if !rest.is_empty() && arcs.len() == picks.len() => arc_reach(g, rest, tol).is_some_and(|far| far > cadcraft_geom::TAU - tol), + _ => false, + } +} + +/// How far round the source arc `g`'s circle, counterclockwise from its start, it and the +/// `others` reach; `None` when one of them isn't on that circle (less than `tol` off its centre +/// and radius). An arc that starts even a hair before the source's start reaches on round past +/// it: AutoCAD 2024 makes a circle of 7°-100° and an arc stored from 7° less 5e-16 rad, and +/// leaves 7°-100° with one from exactly 7° or 5e-7 rad after it. +fn arc_reach(g: &cadcraft_doc::Arc, others: &[&cadcraft_doc::Arc], tol: f64) -> Option { + let same_circle = + |o: &cadcraft_doc::Arc| o.center.xy().dist(g.center.xy()) < tol && (o.radius - g.radius).abs() < tol && (o.center.z - g.center.z).abs() < tol; + let reach = |o: &cadcraft_doc::Arc| norm_angle(o.start - g.start) + cadcraft_geom::ccw_sweep(o.start, o.end); + others.iter().all(|o| same_circle(o)).then(|| others.iter().map(|o| reach(o)).fold(reach(g), f64::max)) +} + +/// The chain JOIN builds from the first object: each next object with an end on one of the +/// chain's ends joins whole, turned round where needed, as AutoCAD 2024 joins them, until a join +/// closes the chain; one that the chain meets only at an inner vertex of a polyline stays out. +/// Ends are compared at the objects' own end points, not ones worked out again from arcs. +fn chain_units(mut units: Vec>, tol: f64) -> Vec { + let ends = |u: &[Piece]| u.first().zip(u.last()).map(|(a, b)| (a.ends.0, b.ends.1)); + if units.is_empty() { + return Vec::new(); + } + // A source that already ends where it starts still takes more, unless it has no length at all + // (AutoCAD 2024, measured with a polyline that ends where it starts and with a line of no + // length). + let mut chain: std::collections::VecDeque = units.remove(0).into(); + if chain.iter().all(|p| p.ends.0.near(p.ends.1, tol)) { + return chain.into(); + } + while let (Some(start), Some(end)) = (chain.front().map(|p| p.ends.0), chain.back().map(|p| p.ends.1)) { + let meets = |u: &Vec| ends(u).is_some_and(|(a, b)| a.near(end, tol) || b.near(end, tol) || a.near(start, tol) || b.near(start, tol)); + let Some(i) = units.iter().position(meets) else { break }; + let unit = units.remove(i); + let Some((a, b)) = ends(&unit) else { break }; + // Where an object that meets the chain at more than one place goes, as AutoCAD 2024 puts + // it (measured): a polyline by its first vertex if it can, and before the chain rather + // than after it; a line or an arc turned round if it can, its end on the chain's end + // before its start on the chain's start. + let polyline = unit.first().is_some_and(|p| p.widths.is_some()); + let (before, turned) = if polyline { + if a.near(start, tol) { + (true, true) + } else if a.near(end, tol) { + (false, false) + } else { + (b.near(start, tol), !b.near(start, tol)) + } + } else if b.near(end, tol) { + (false, true) + } else if a.near(start, tol) { + (true, true) + } else { + (!a.near(end, tol), false) + }; + match (before, turned) { + (false, false) => chain.extend(unit), + (false, true) => chain.extend(unit.into_iter().rev().map(Piece::flipped)), + (true, false) => unit.into_iter().rev().for_each(|p| chain.push_front(p)), + (true, true) => unit.into_iter().for_each(|p| chain.push_front(p.flipped())), + } + // A chain that a join closes takes nothing more. + if chain.front().zip(chain.back()).is_some_and(|(f, l)| f.ends.0.near(l.ends.1, tol)) { + break; + } + } + chain.into() +} + /// A segment in the chain JOIN builds: the object it came from, its end points as the object has /// them (turned round when it runs backwards in the chain), its bulge and, for a polyline's own /// segment, its widths. @@ -367,22 +458,15 @@ fn join_on_source(s: &mut Session, hs: &[Handle]) -> Result> { _ => None, }) .collect(); - let same_circle = |o: &cadcraft_doc::Arc| { - o.center.xy().dist(g.center.xy()) < tol && (o.radius - g.radius).abs() < tol && (o.center.z - g.center.z).abs() < tol - }; - if arcs.len() != rest.len() || !arcs.iter().all(|o| same_circle(o)) { - return Ok(None); - } - // How far round from the source's start each arc reaches. - let reach = |o: &cadcraft_doc::Arc| norm_angle(o.start - g.start) + cadcraft_geom::ccw_sweep(o.start, o.end); - let far = arcs.iter().map(|o| reach(o)).fold(reach(g), f64::max); + let Some(far) = arc_reach(g, &arcs, tol).filter(|_| arcs.len() == rest.len()) else { return Ok(None) }; if far > cadcraft_geom::TAU - tol { let circle = Entity { handle: Handle(0), common: src.common.clone(), kind: EntityKind::Circle(cadcraft_doc::Circle { center: g.center, radius: g.radius }), }; - let space = s.space(); + // In the source's space, whichever space is current. + let space = d.space_of(src.handle).unwrap_or_else(|| s.space()); let doc = s.doc_mut()?; for h in hs { doc.remove_entity(*h); @@ -394,7 +478,7 @@ fn join_on_source(s: &mut Session, hs: &[Handle]) -> Result> { s.set_selection(Vec::new()); return Ok(Some(nh)); } - let end = if far > reach(g) { norm_angle(g.start + far) } else { g.end }; + let end = if far > cadcraft_geom::ccw_sweep(g.start, g.end) { norm_angle(g.start + far) } else { g.end }; EntityKind::Arc(cadcraft_doc::Arc { end, ..g.clone() }) } _ => return Ok(None), diff --git a/crates/engine/src/cmd/modify/select_then.rs b/crates/engine/src/cmd/modify/select_then.rs index 26b386aa2..c6de19ae4 100644 --- a/crates/engine/src/cmd/modify/select_then.rs +++ b/crates/engine/src/cmd/modify/select_then.rs @@ -1,4 +1,4 @@ -use super::break_join::join; +use super::break_join::{join, join_makes_circle}; use super::explode::explode; use super::transform::{at_elevation, erase, scale_entities, transform_entities}; use super::{ @@ -39,6 +39,8 @@ pub(super) struct SelectThen { 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, + /// JOIN of arcs that make a circle: asking whether to. + ask_circle: bool, } impl SelectThen { @@ -56,6 +58,7 @@ impl SelectThen { copy_mode: false, ask_erase: false, join_source: None, + ask_circle: false, } } fn apply(&mut self, s: &mut Session) -> Result { @@ -83,8 +86,17 @@ impl SelectThen { } } - /// JOIN the selected objects and report what they became. + /// 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 { + if join_makes_circle(s, &self.objs) { + self.ask_circle = true; + return Ok(Step::Continue); + } + self.join_apply(s) + } + + fn join_apply(&mut self, s: &mut Session) -> Result { match join(s, &self.objs) { Ok(h) => { // Objects outside the joined chain are kept, so count only the ones JOIN consumed @@ -309,6 +321,9 @@ impl Interactive for SelectThen { if self.ask_erase { return Prompt::new("Erase source objects?", Accept::TEXT).kw(&["Yes", "No"]).default("No"); } + if self.ask_circle { + return Prompt::new("Combined arc segments form a circle. Convert to circle?", crate::cmd::curves::KW).kw(&["Yes", "No"]).default("Yes"); + } if let Some(src) = self.join_source { let ellipse = s.doc().is_ok_and(|d| matches!(d.entity(src).map(|e| &e.kind), Some(EntityKind::Ellipse(_)))); let msg = if ellipse { "Select elliptical arcs to join to source" } else { "Select arcs to join to source" }; @@ -361,6 +376,14 @@ impl Interactive for SelectThen { } } } + // No leaves every arc as it is (AutoCAD 2024, measured). + if self.ask_circle { + return match &i { + Input::Keyword(k) if k == "No" => Ok(Step::Done), + Input::Keyword(_) | Input::Enter => self.join_apply(s), + _ => Err(EngineError::Other("Invalid option keyword.".into())), + }; + } if let Some(src) = self.join_source { return self.join_source_input(s, src, i); } diff --git a/crates/engine/tests/it/join_on_source.rs b/crates/engine/tests/it/join_on_source.rs index 6ecdd0b9c..e97ae5235 100644 --- a/crates/engine/tests/it/join_on_source.rs +++ b/crates/engine/tests/it/join_on_source.rs @@ -10,7 +10,7 @@ use cadcraft_engine::Session; use cadcraft_engine::doc::color::Color; -use cadcraft_engine::doc::{EntityKind, Handle}; +use cadcraft_engine::doc::{EntityKind, Handle, Space}; use serde_json::{Value, json}; fn handle(v: &Value) -> Handle { @@ -388,10 +388,124 @@ fn join_names_what_the_objects_became() { assert!(s.log.iter().any(|l| l == "2 objects joined into 1 arc"), "{:?}", s.log); let c = arc(&mut s, [0.0, 0.0], 5.0, 250.0, 0.0); s.execute("select", &json!({ "handles": [a.hex(), c.hex()] })).unwrap(); - s.script("JOIN\n").unwrap(); + // Yes, the default, to "Convert to circle?". + s.script("JOIN\n\n").unwrap(); assert!(s.log.iter().any(|l| l == "2 objects joined into 1 circle"), "{:?}", s.log); } +#[test] +fn arcs_that_come_round_ask_before_making_a_circle() { + // AutoCAD 2024 with 7°-100° and an arc from -353° (stored 5e-16 rad before 7°) to 50°: "Combined + // arc segments form a circle. Convert to circle? [Yes/No] :". Yes makes the circle, No + // leaves both arcs as they are. + let pair = |s: &mut Session| [arc(s, [0.0, 0.0], 10.0, 7.0, 100.0), arc(s, [0.0, 0.0], 10.0, -353.0, 50.0)]; + let mut s = Session::new(); + let [a, b] = pair(&mut s); + let before = objects(&s); + s.execute("select", &json!({ "handles": [a.hex(), b.hex()] })).unwrap(); + s.script("JOIN\n").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Combined arc segments form a circle. Convert to circle? [Yes/No] :"); + s.cmdline("N").unwrap(); + assert!(s.running.is_none()); + assert_eq!(objects(&s), before); + s.execute("select", &json!({ "handles": [a.hex(), b.hex()] })).unwrap(); + s.script("JOIN\n\n").unwrap(); + assert!(matches!(&objects(&s)[..], [(_, EntityKind::Circle(c))] if c.radius == 10.0), "{:?}", objects(&s)); + // The JSON form takes the default, Yes. + let mut s = Session::new(); + let [a, b] = pair(&mut s); + join(&mut s, &[a, b]); + assert!(matches!(&objects(&s)[..], [(_, EntityKind::Circle(_))]), "{:?}", objects(&s)); + // There is no tolerance at the source's start: an arc from exactly 7°, or from 5e-7 rad after + // the source's start, lies inside it and the source stays; from 5e-7 or 2e-6 rad before it, + // the arcs come round (all measured). + for (start, circle) in [(7.0, false), (7.0 + 5e-7f64.to_degrees(), false), (7.0 - 5e-7f64.to_degrees(), true), (7.0 - 2e-6f64.to_degrees(), true)] + { + let mut s = Session::new(); + let a = arc(&mut s, [0.0, 0.0], 10.0, 7.0, 100.0); + let b = arc(&mut s, [0.0, 0.0], 10.0, start, 50.0); + let source = objects(&s)[0].1.clone(); + join(&mut s, &[a, b]); + let after = objects(&s); + if circle { + assert!(matches!(&after[..], [(_, EntityKind::Circle(_))]), "{start}: {after:?}"); + } else { + assert_eq!(after, [(a, source)], "{start}"); + } + } +} + +#[test] +fn join_asks_about_a_circle_only_when_it_would_make_one() { + // Arcs that come round, picked with a line, a spline or on a locked layer: JOIN goes another + // way or refuses, and doesn't ask. + let half_circles = |s: &mut Session| [arc(s, [0.0, 0.0], 5.0, 0.0, 180.0), arc(s, [0.0, 0.0], 5.0, 180.0, 0.0)]; + for spline in [false, true] { + let mut s = Session::new(); + let [a, b] = half_circles(&mut s); + let other = if spline { + handle(&s.execute("spline", &json!({ "control": [[50, 0], [53, 3], [56, -3], [59, 0]] })).unwrap()) + } else { + line(&mut s, [5.0, 0.0], [15.0, 0.0]) + }; + s.execute("select", &json!({ "handles": [a.hex(), b.hex(), other.hex()] })).unwrap(); + s.script("JOIN\n").unwrap(); + assert!(s.running.is_none(), "spline {spline}: {}", s.prompt_text()); + // Not joined onto the arcs' circle: the other object isn't taken with them. + let objs = objects(&s); + assert!(objs.iter().any(|(h, _)| *h == other) && !objs.iter().any(|(_, k)| matches!(k, EntityKind::Circle(_))), "spline {spline}: {objs:?}"); + } + // Arcs that come round exactly, or to 9.9e-7 rad short of the source's start, ask too; 1.01e-6 + // short, they make an arc without asking (measured: CIRCLE and ARC). + for (short, asks) in [(0.0, true), (9.9e-7f64, true), (1.01e-6, false)] { + let mut s = Session::new(); + let a = arc(&mut s, [0.0, 0.0], 5.0, 0.0, 180.0); + let b = arc(&mut s, [0.0, 0.0], 5.0, 180.0, 360.0 - short.to_degrees()); + s.execute("select", &json!({ "handles": [a.hex(), b.hex()] })).unwrap(); + s.script("JOIN\n").unwrap(); + assert_eq!(s.prompt_text().contains("Convert to circle?"), asks, "{short}: {}", s.prompt_text()); + if !asks { + assert!(s.running.is_none() && matches!(&objects(&s)[..], [(h, EntityKind::Arc(_))] if *h == a), "{:?}", objects(&s)); + } + } + // Arcs picked at "Select arcs to join to source" ask too, and No changes nothing. + let mut s = Session::new(); + arc(&mut s, [0.0, 0.0], 5.0, 0.0, 180.0); + arc(&mut s, [0.0, 0.0], 5.0, 180.0, 0.0); + let before = objects(&s); + s.script("JOIN 0,5\n\n").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Select arcs to join to source or [cLose]:"); + s.cmdline("0,-5").unwrap(); + s.cmdline("").unwrap(); + assert_eq!(s.current_prompt().unwrap().display(), "Combined arc segments form a circle. Convert to circle? [Yes/No] :"); + s.cmdline("N").unwrap(); + assert!(s.running.is_none()); + assert_eq!(objects(&s), before); + let mut s = Session::new(); + let [a, b] = half_circles(&mut s); + s.execute("layer.set", &json!({ "name": "0", "locked": true })).unwrap(); + s.execute("select", &json!({ "handles": [a.hex(), b.hex()] })).unwrap(); + s.script("JOIN\n").unwrap(); + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert!(s.log.iter().any(|l| l == "An object is on a locked layer."), "{:?}", s.log); +} + +#[test] +fn what_join_makes_stays_in_the_source_space() { + // Arcs and lines in a layout joined while model space is current: the circle and the new + // polyline go in the layout. + let mut s = Session::new(); + s.execute("layout.set", &json!({ "name": "Layout1" })).unwrap(); + let arcs = [arc(&mut s, [0.0, 0.0], 5.0, 0.0, 180.0), arc(&mut s, [0.0, 0.0], 5.0, 180.0, 0.0)]; + let lines = [line(&mut s, [20.0, 0.0], [30.0, 0.0]), line(&mut s, [30.0, 0.0], [30.0, 10.0])]; + s.execute("layout.set", &json!({ "name": "Model" })).unwrap(); + for hs in [arcs, lines] { + let made = join(&mut s, &hs); + assert_eq!(s.doc().unwrap().space_of(made), Some(Space::Paper("Layout1".into())), "{hs:?}"); + } + assert!(objects(&s).is_empty()); +} + #[test] fn lines_and_arcs_less_than_a_millionth_off_join_and_further_off_do_not() { // AutoCAD 2024 with the line (0, 0)-(30, 0) and (35, d)-(50, d), and the same at survey @@ -459,3 +573,396 @@ fn lines_and_arcs_less_than_a_millionth_off_join_and_further_off_do_not() { assert!(s.execute("join", &json!({ "handles": [a.hex(), b.hex()] })).is_err()); assert_eq!(objects(&s), before); } + +#[test] +fn a_polyline_met_at_an_inner_vertex_stays_whole() { + // AutoCAD 2024: a polyline (0,0)-(10,0) takes the line (10,0)-(10,5); the polyline + // (0,5)-(10,5)-(20,5), which the line meets at its middle vertex, is left as it was. + let mut s = Session::new(); + let p = pline(&mut s, &[(0.0, 0.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0)]); + let l = line(&mut s, [10.0, 0.0], [10.0, 5.0]); + let q_vs = [(0.0, 5.0, 0.0, 0.0, 0.0), (10.0, 5.0, 0.0, 0.0, 0.0), (20.0, 5.0, 0.0, 0.0, 0.0)]; + let q = pline(&mut s, &q_vs); + assert_eq!(join(&mut s, &[p, l, q]), p); + let objs = objects(&s); + assert_eq!(objs.iter().map(|o| o.0).collect::>(), vec![p, q], "{objs:?}"); + assert_eq!(vertices(&objs[0].1), [(0.0, 0.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0), (10.0, 5.0, 0.0, 0.0, 0.0)]); + assert_eq!(vertices(&objs[1].1), q_vs); + // The same through a new polyline: two lines at a corner, and that polyline at the far end. + let mut s = Session::new(); + let a = line(&mut s, [0.0, 0.0], [10.0, 0.0]); + let b = line(&mut s, [10.0, 0.0], [10.0, 5.0]); + let q = pline(&mut s, &q_vs); + let r = join(&mut s, &[a, b, q]); + let objs = objects(&s); + assert_eq!(objs.iter().map(|o| o.0).collect::>(), vec![q, r], "{objs:?}"); + assert_eq!(vertices(&objs[0].1), q_vs); + assert_eq!(vertices(&objs[1].1), [(0.0, 0.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0), (10.0, 5.0, 0.0, 0.0, 0.0)]); + // Nothing else to join (AutoCAD 2024: "0 segments added to polyline", or nothing joined to + // a line): refused, the drawing as it was. + for (first, other) in [ + ( + vec![(0.0, 0.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0)], + vec![(10.0, -5.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0), (10.0, 5.0, 0.0, 0.0, 0.0)], + ), + ( + vec![(-10.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0)], + vec![(10.0, -5.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0), (10.0, 5.0, 0.0, 0.0, 0.0)], + ), + ] { + let mut s = Session::new(); + let p = pline(&mut s, &first); + let q = pline(&mut s, &other); + let before = objects(&s); + assert!(s.execute("join", &json!({ "handles": [p.hex(), q.hex()] })).is_err(), "{first:?}"); + assert_eq!(objects(&s), before); + } + let mut s = Session::new(); + let a = line(&mut s, [0.0, 0.0], [10.0, 0.0]); + let q = pline(&mut s, &[(20.0, 0.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0), (10.0, -10.0, 0.0, 0.0, 0.0)]); + let before = objects(&s); + assert!(s.execute("join", &json!({ "handles": [a.hex(), q.hex()] })).is_err()); + assert_eq!(objects(&s), before); +} + +#[test] +fn objects_join_whole_by_their_ends_whatever_the_order() { + let v = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| (x, y, 0.0, 0.0, 0.0)).collect::>(); + // AutoCAD 2024: the polyline (30,0)-(20,0) takes (0,0)-(10,0)-(20,0) whole, picked before or + // after the line (10,0)-(10,5), which meets only its middle vertex and is left as it was. + for order in [[0, 1, 2], [0, 2, 1]] { + let mut s = Session::new(); + let p0 = pline(&mut s, &v(&[(30.0, 0.0), (20.0, 0.0)])); + let m = line(&mut s, [10.0, 0.0], [10.0, 5.0]); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0), (20.0, 0.0)])); + let picked = order.map(|i| [p0, m, p][i]); + assert_eq!(join(&mut s, &picked), p0, "{order:?}"); + let objs = objects(&s); + assert_eq!(objs.iter().map(|o| o.0).collect::>(), vec![p0, m], "{order:?}"); + assert_eq!(vertices(&objs[0].1), v(&[(30.0, 0.0), (20.0, 0.0), (10.0, 0.0), (0.0, 0.0)]), "{order:?}"); + } + // AutoCAD 2024: (10,0)-(10,5), then the line (10,5)-(20,0), then (0,0)-(10,0)-(20,0) whole + // from its end: (10,0) (10,5) (20,0) (10,0) (0,0), not split at its middle vertex. + let mut s = Session::new(); + let src = pline(&mut s, &v(&[(10.0, 0.0), (10.0, 5.0)])); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0), (20.0, 0.0)])); + let m = line(&mut s, [10.0, 5.0], [20.0, 0.0]); + assert_eq!(join(&mut s, &[src, p, m]), src); + let objs = objects(&s); + assert_eq!(objs.len(), 1, "{objs:?}"); + assert_eq!(vertices(&objs[0].1), v(&[(10.0, 0.0), (10.0, 5.0), (20.0, 0.0), (10.0, 0.0), (0.0, 0.0)])); +} + +#[test] +fn one_object_alone_joins_nothing_even_when_its_ends_meet() { + let v = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| (x, y, 0.0, 0.0, 0.0)).collect::>(); + // AutoCAD 2024 ("0 segments added to polyline"): a polyline that the other meets only at its + // middle vertex. And a circle, which JOIN can't take, beside a polyline: nothing to join it to. + let mut s = Session::new(); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0), (20.0, 0.0)])); + let q = pline(&mut s, &v(&[(10.0, 0.0), (10.0, 5.0), (10.0, 10.0)])); + let c = handle(&s.execute("circle", &json!({ "center": [50, 50], "radius": 5 })).unwrap()); + // A polyline whose last vertex is its first stays open, alone or beside a line it doesn't + // reach (AutoCAD 2024: unchanged, 70 = 0, all four vertices). + let r = pline(&mut s, &v(&[(100.0, 0.0), (110.0, 0.0), (110.0, 10.0), (100.0, 0.0)])); + let l = line(&mut s, [150.0, 50.0], [160.0, 50.0]); + let before = objects(&s); + for hs in [&[p, q][..], &[c, p], &[r], &[r, l]] { + let hex: Vec = hs.iter().map(|h| h.hex()).collect(); + assert!(s.execute("join", &json!({ "handles": hex })).is_err(), "{hs:?}"); + assert_eq!(objects(&s), before, "{hs:?}"); + } +} + +#[test] +fn a_polyline_whose_ends_meet_takes_more_where_they_meet() { + // AutoCAD 2024 with the source (0,0) (10,0) (10,10) (0,0), open, and an object from or to + // (0,0): it keeps its handle and stays open. A line or an arc goes in turned round, its end + // at the source's end if it can, else its start at the source's start; a polyline goes before + // the source, turned round if it starts at (0,0). + let src = [(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 0.0)]; + let v = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| (x, y, 0.0, 0.0, 0.0)).collect::>(); + let tail = [(-5.0, -5.0), (-5.0, 0.0)]; + type Make = fn(&mut Session) -> Handle; + let cases: [(Make, Vec<(f64, f64, f64, f64, f64)>); 5] = [ + (|s| line(s, [0.0, 0.0], [-5.0, 0.0]), v(&[(-5.0, 0.0), (0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 0.0)])), + (|s| line(s, [-5.0, 0.0], [0.0, 0.0]), v(&[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 0.0), (-5.0, 0.0)])), + ( + |s| pline(s, &[(0.0, 0.0, 0.0, 0.0, 0.0), (-5.0, 0.0, 0.0, 0.0, 0.0), (-5.0, -5.0, 0.0, 0.0, 0.0)]), + v(&[tail[0], tail[1], src[0], src[1], src[2], src[3]]), + ), + ( + |s| pline(s, &[(-5.0, -5.0, 0.0, 0.0, 0.0), (-5.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0)]), + v(&[tail[0], tail[1], src[0], src[1], src[2], src[3]]), + ), + (|s| arc(s, [0.0, -5.0], 5.0, 90.0, 180.0), { + let mut w = v(&[(-5.0, -5.0), src[0], src[1], src[2], src[3]]); + w[0].4 = -((std::f64::consts::PI / 8.0).tan() * 1e12).round() / 1e12; + w + }), + ]; + for (i, (make, want)) in cases.into_iter().enumerate() { + let mut s = Session::new(); + let p = pline(&mut s, &v(&src)); + let o = make(&mut s); + assert_eq!(join(&mut s, &[p, o]), p, "case {i}"); + let objs = objects(&s); + assert_eq!(objs.len(), 1, "case {i}: {objs:?}"); + let EntityKind::LwPolyline(pl) = &objs[0].1 else { panic!("not a polyline") }; + assert!(!pl.closed, "case {i}"); + // An arc's end point is worked out from its centre, so to 1e-9. + let got = vertices(&objs[0].1); + let same = |a: &(f64, f64, f64, f64, f64), b: &(f64, f64, f64, f64, f64)| { + [a.0 - b.0, a.1 - b.1, a.2 - b.2, a.3 - b.3, a.4 - b.4].iter().all(|d| d.abs() < 1e-9) + }; + assert!(got.len() == want.len() && got.iter().zip(&want).all(|(a, b)| same(a, b)), "case {i}: {got:?}"); + } + // A join that closes it there closes it, and nothing more joins (AutoCAD 2024): a line of no + // length at (0,0) gives a closed polyline of its four vertices, the line from (0,0) picked + // after it staying; a polyline (0,0) (-5,0) (-5,-5) (0,0) goes before it turned round. + let mut s = Session::new(); + let p = pline(&mut s, &v(&src)); + let zero = line(&mut s, [0.0, 0.0], [1.0, 0.0]); + s.doc_mut() + .unwrap() + .modify_entity(zero, |e| { + if let EntityKind::Line(l) = &mut e.kind { + l.b = l.a; + } + }) + .unwrap(); + let l = line(&mut s, [0.0, 0.0], [-5.0, 0.0]); + assert_eq!(join(&mut s, &[p, zero, l]), p); + let objs = objects(&s); + assert!(matches!(&objs[..], [(a, EntityKind::LwPolyline(pl)), (b, EntityKind::Line(_))] if *a == p && *b == l && pl.closed), "{objs:?}"); + assert_eq!(vertices(&objs[0].1), v(&src)); + let mut s = Session::new(); + let p = pline(&mut s, &v(&src)); + let q = pline(&mut s, &v(&[(0.0, 0.0), (-5.0, 0.0), (-5.0, -5.0), (0.0, 0.0)])); + assert_eq!(join(&mut s, &[p, q]), p); + let objs = objects(&s); + assert!(matches!(&objs[..], [(a, EntityKind::LwPolyline(pl))] if *a == p && pl.closed), "{objs:?}"); + assert_eq!(vertices(&objs[0].1), v(&[(0.0, 0.0), (-5.0, -5.0), (-5.0, 0.0), (0.0, 0.0), (10.0, 0.0), (10.0, 10.0)])); +} + +#[test] +fn a_nearly_straight_arc_segment_keeps_the_chain_whole() { + // A bulge of 3e-10 over 100: as an arc its radius is about 4e10, and its ends worked out again + // from the centre are off by more than the tolerance. The vertices themselves still meet. + let mut s = Session::new(); + let p = pline(&mut s, &[(0.0, 0.0, 0.0, 0.0, 3e-10), (100.0, 0.0, 0.0, 0.0, 0.0), (100.0, 10.0, 0.0, 0.0, 0.0)]); + let l = line(&mut s, [100.0, 10.0], [110.0, 10.0]); + assert_eq!(join(&mut s, &[p, l]), p); + let objs = objects(&s); + assert_eq!(objs.len(), 1, "{objs:?}"); + let EntityKind::LwPolyline(pl) = &objs[0].1 else { panic!("not a polyline") }; + let pts: Vec<(f64, f64)> = pl.vertices.iter().map(|v| (v.p.x, v.p.y)).collect(); + assert_eq!(pts, [(0.0, 0.0), (100.0, 0.0), (100.0, 10.0), (110.0, 10.0)]); + assert_eq!(pl.vertices[0].bulge, 3e-10); +} + +#[test] +fn a_new_polyline_takes_the_first_joined_objects_properties() { + // A circle picked first isn't part of the polyline: the polyline is on the first line's layer + // (AutoCAD 2024 passes over a circle picked as the source; the new polyline's layer itself is + // not measured). + let mut s = Session::new(); + let c = handle(&s.execute("circle", &json!({ "center": [50, 50], "radius": 5 })).unwrap()); + let a = line(&mut s, [0.0, 0.0], [10.0, 0.0]); + let b = line(&mut s, [10.0, 0.0], [10.0, 5.0]); + for (h, layer) in [(c, "C"), (a, "A"), (b, "B")] { + s.doc_mut().unwrap().modify_entity(h, |e| e.common.layer = layer.into()).unwrap(); + } + let r = join(&mut s, &[c, a, b]); + let d = s.doc().unwrap(); + assert_eq!(d.entity(r).unwrap().common.layer, "A"); + assert!(d.entity(c).is_some(), "the circle stays"); +} + +#[test] +fn the_source_is_the_first_object_join_can_take() { + // AutoCAD 2024: a circle picked first, then the polyline (0,0)-(10,0) of width 2, then the line + // (10,0)-(10,5): the polyline takes the line, keeping its handle and its width. + let mut s = Session::new(); + let c = handle(&s.execute("circle", &json!({ "center": [50, 50], "radius": 5 })).unwrap()); + let p = handle(&s.execute("pline", &json!({ "vertices": [[0, 0], [10, 0]], "width": 2.0 })).unwrap()); + let l = line(&mut s, [10.0, 0.0], [10.0, 5.0]); + assert_eq!(join(&mut s, &[c, p, l]), p); + let objs = objects(&s); + assert_eq!(objs.iter().map(|o| o.0).collect::>(), vec![c, p], "{objs:?}"); + let EntityKind::LwPolyline(pl) = &objs[1].1 else { panic!("not a polyline") }; + assert_eq!((pl.const_width, pl.vertices.len()), (2.0, 3)); +} + +#[test] +fn a_closed_chain_takes_nothing_more() { + let v = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| (x, y, 0.0, 0.0, 0.0)).collect::>(); + // AutoCAD 2024: (0,0)-(10,0)-(10,10), the line back to (0,0), then a line on from (0,0): the + // triangle closes and the last line stays as it was. + let mut s = Session::new(); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0)])); + let back = line(&mut s, [10.0, 10.0], [0.0, 0.0]); + let tail = line(&mut s, [0.0, 0.0], [-5.0, 0.0]); + assert_eq!(join(&mut s, &[p, back, tail]), p); + let objs = objects(&s); + assert_eq!(objs.iter().map(|o| o.0).collect::>(), vec![p, tail], "{objs:?}"); + let EntityKind::LwPolyline(pl) = &objs[0].1 else { panic!("not a polyline") }; + assert!(pl.closed, "{pl:?}"); + assert_eq!(vertices(&objs[0].1), v(&[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0)])); + // The line on picked before the line back: it joins first, and the chain no longer closes. + let mut s = Session::new(); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0), (10.0, 10.0)])); + let tail = line(&mut s, [0.0, 0.0], [-5.0, 0.0]); + let back = line(&mut s, [10.0, 10.0], [0.0, 0.0]); + assert_eq!(join(&mut s, &[p, tail, back]), p); + let objs = objects(&s); + assert_eq!(objs.len(), 1, "{objs:?}"); + assert_eq!(vertices(&objs[0].1), v(&[(-5.0, 0.0), (0.0, 0.0), (10.0, 0.0), (10.0, 10.0), (0.0, 0.0)])); +} + +#[test] +fn objects_meeting_the_chain_at_its_start_turned_round() { + let v = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| (x, y, 0.0, 0.0, 0.0)).collect::>(); + // AutoCAD 2024: (0,0)-(10,0) and the polyline (0,0)-(-10,0)-(-10,10), turned round in front. + let mut s = Session::new(); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0)])); + let q = pline(&mut s, &v(&[(0.0, 0.0), (-10.0, 0.0), (-10.0, 10.0)])); + assert_eq!(join(&mut s, &[p, q]), p); + assert_eq!(vertices(&objects(&s)[0].1), v(&[(-10.0, 10.0), (-10.0, 0.0), (0.0, 0.0), (10.0, 0.0)])); + // AutoCAD 2024: a polyline meeting both of the source's ends closes it from its start, in + // front of the source turned round: widths swapped, bulge negated. + let mut s = Session::new(); + let p = pline(&mut s, &[(0.0, 0.0, 1.0, 1.0, 0.0), (20.0, 0.0, 1.0, 1.0, 0.0), (20.0, 20.0, 1.0, 1.0, 0.0)]); + let q = pline(&mut s, &[(0.0, 0.0, 4.0, 5.0, 0.0), (0.0, 20.0, 6.0, 7.0, 0.3), (20.0, 20.0, 8.0, 9.0, 0.0)]); + assert_eq!(join(&mut s, &[p, q]), p); + let objs = objects(&s); + let EntityKind::LwPolyline(pl) = &objs[0].1 else { panic!("not a polyline") }; + assert!(pl.closed && objs.len() == 1, "{objs:?}"); + let want = [(20.0, 20.0, 7.0, 6.0, -0.3), (0.0, 20.0, 5.0, 4.0, 0.0), (0.0, 0.0, 1.0, 1.0, 0.0), (20.0, 0.0, 1.0, 1.0, 0.0)]; + assert_eq!(vertices(&objs[0].1), want); +} + +#[test] +fn lines_of_no_length_and_repeated_vertices() { + let v = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| (x, y, 0.0, 0.0, 0.0)).collect::>(); + // A line from (x,y) to (x,y). + let dot = |s: &mut Session, x: f64, y: f64| { + let h = line(s, [x, y], [x + 1.0, y]); + s.doc_mut() + .unwrap() + .modify_entity(h, |e| { + if let EntityKind::Line(l) = &mut e.kind { + l.b = l.a; + } + }) + .unwrap(); + h + }; + // AutoCAD 2024: two of them alone stay as they are. + let mut s = Session::new(); + let (a, b) = (dot(&mut s, 5.0, 5.0), dot(&mut s, 5.0, 5.0)); + let before = objects(&s); + assert!(s.execute("join", &json!({ "handles": [a.hex(), b.hex()] })).is_err()); + assert_eq!(objects(&s), before); + // AutoCAD 2024: a line takes one at its end and stays as it was. + let mut s = Session::new(); + let l = line(&mut s, [0.0, 0.0], [10.0, 0.0]); + let z = dot(&mut s, 10.0, 0.0); + assert_eq!(join(&mut s, &[l, z]), l); + let objs = objects(&s); + assert!(objs.len() == 1 && matches!(&objs[0].1, EntityKind::Line(k) if (k.b.x, k.b.y) == (10.0, 0.0)), "{objs:?}"); + // AutoCAD 2024: a polyline takes it as a repeated vertex, and keeps another polyline's. + let mut s = Session::new(); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0)])); + let z = dot(&mut s, 10.0, 0.0); + let m = line(&mut s, [10.0, 0.0], [10.0, 5.0]); + assert_eq!(join(&mut s, &[p, z, m]), p); + assert_eq!(vertices(&objects(&s)[0].1), v(&[(0.0, 0.0), (10.0, 0.0), (10.0, 0.0), (10.0, 5.0)])); + let mut s = Session::new(); + let p = pline(&mut s, &v(&[(0.0, 0.0), (10.0, 0.0)])); + let q = pline(&mut s, &v(&[(10.0, 0.0), (10.0, 0.0), (10.0, 5.0)])); + assert_eq!(join(&mut s, &[p, q]), p); + assert_eq!(vertices(&objects(&s)[0].1), v(&[(0.0, 0.0), (10.0, 0.0), (10.0, 0.0), (10.0, 5.0)])); + // An arc of no radius (AutoCAD can't draw one) joins nothing. + let mut s = Session::new(); + let l = line(&mut s, [0.0, 0.0], [10.0, 0.0]); + let g = arc(&mut s, [10.0, 0.0], 1.0, 0.0, 90.0); + s.doc_mut() + .unwrap() + .modify_entity(g, |e| { + if let EntityKind::Arc(k) = &mut e.kind { + k.radius = 0.0; + } + }) + .unwrap(); + let before = objects(&s); + assert!(s.execute("join", &json!({ "handles": [l.hex(), g.hex()] })).is_err()); + assert_eq!(objects(&s), before); +} + +#[test] +fn another_polyline_keeps_its_widths_in_a_new_polyline() { + // A line picked first makes a new polyline; the polyline after it keeps its segment's widths + // (no data lost; AutoCAD's own JOIN takes no polyline onto a line source, not measured). + let mut s = Session::new(); + let l = line(&mut s, [-10.0, 0.0], [0.0, 0.0]); + let q = pline(&mut s, &[(0.0, 0.0, 2.0, 3.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0)]); + let r = join(&mut s, &[l, q]); + let objs = objects(&s); + assert!(objs.len() == 1 && objs[0].0 == r, "{objs:?}"); + assert_eq!(vertices(&objs[0].1), [(-10.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 2.0, 3.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0)]); +} + +#[test] +fn a_circle_picked_first_is_passed_over_by_lines_and_arcs_too() { + // AutoCAD 2024 passes over a circle picked as the source: the first line takes the next. + let mut s = Session::new(); + let c = handle(&s.execute("circle", &json!({ "center": [50, 50], "radius": 5 })).unwrap()); + let a = line(&mut s, [0.0, 0.0], [10.0, 0.0]); + let b = line(&mut s, [10.0, 0.0], [20.0, 0.0]); + assert_eq!(join(&mut s, &[c, a, b]), a); + let objs = objects(&s); + assert_eq!(objs.iter().map(|o| o.0).collect::>(), vec![c, a], "{objs:?}"); + assert!(matches!(&objs[1].1, EntityKind::Line(l) if (l.b.x, l.b.y) == (20.0, 0.0)), "{objs:?}"); + // And the first arc takes the next on its circle. + let mut s = Session::new(); + let c = handle(&s.execute("circle", &json!({ "center": [50, 50], "radius": 5 })).unwrap()); + let g = arc(&mut s, [0.0, 0.0], 5.0, 0.0, 100.0); + let h = arc(&mut s, [0.0, 0.0], 5.0, 130.0, 250.0); + assert_eq!(join(&mut s, &[c, g, h]), g); + let objs = objects(&s); + assert_eq!(objs.iter().map(|o| o.0).collect::>(), vec![c, g], "{objs:?}"); + assert!(matches!(&objs[1].1, EntityKind::Arc(k) if same_angle(k.end, 250f64.to_radians())), "{objs:?}"); +} + +#[test] +fn an_arc_closing_a_polyline_and_a_source_of_no_length() { + let v = |pts: &[(f64, f64)]| pts.iter().map(|&(x, y)| (x, y, 0.0, 0.0, 0.0)).collect::>(); + // AutoCAD 2024: (0,0)-(20,0)-(20,20) and the half circle from (20,20) round to (0,0) close + // into (0,0) (20,0) (20,20) with a bulge of 1. + let mut s = Session::new(); + let p = pline(&mut s, &v(&[(0.0, 0.0), (20.0, 0.0), (20.0, 20.0)])); + let g = arc(&mut s, [10.0, 10.0], 200f64.sqrt(), 45.0, 225.0); + assert_eq!(join(&mut s, &[p, g]), p); + let objs = objects(&s); + let EntityKind::LwPolyline(pl) = &objs[0].1 else { panic!("not a polyline") }; + assert!(objs.len() == 1 && pl.closed, "{objs:?}"); + let got = vertices(&objs[0].1); + assert!(got.len() == 3 && got[..2] == v(&[(0.0, 0.0), (20.0, 0.0)])[..] && near(got[2].0, 20.0) && near(got[2].1, 20.0), "{got:?}"); + assert!(near(got[2].4, 1.0), "{got:?}"); + // AutoCAD 2024: a line of no length picked as the source takes nothing; both stay. + let mut s = Session::new(); + let z = line(&mut s, [5.0, 5.0], [6.0, 5.0]); + s.doc_mut() + .unwrap() + .modify_entity(z, |e| { + if let EntityKind::Line(l) = &mut e.kind { + l.b = l.a; + } + }) + .unwrap(); + let l = line(&mut s, [5.0, 5.0], [10.0, 5.0]); + let before = objects(&s); + assert!(s.execute("join", &json!({ "handles": [z.hex(), l.hex()] })).is_err()); + assert_eq!(objects(&s), before); +}