mirror of
https://github.com/storytold/cadcraft.git
synced 2026-11-26 21:30:06 +00:00
Merge pull request #2790 from 2ez4gcx/fix/join-other-height
JOIN: ends meet at the same height; objects facing the other way stay out
This commit is contained in:
@@ -137,35 +137,50 @@ pub(crate) fn join(s: &mut Session, hs: &[Handle]) -> Result<Option<Handle>> {
|
||||
let d = s.doc()?;
|
||||
// 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<Piece>> = Vec::new();
|
||||
// Each also with whether it faces down: an arc or a polyline whose normal points down Z
|
||||
// (measured with 0,0,-1; a tilted one is taken the same way). A line faces up whatever its
|
||||
// normal (AutoCAD 2024 joins one with normal 0,0,-1 to a polyline facing up, and no line to a
|
||||
// polyline facing down; measured).
|
||||
let mut units: Vec<(bool, Vec<Piece>)> = Vec::new();
|
||||
let down = |e: &Entity| !matches!(e.kind, EntityKind::Line(_)) && e.common.extrusion.z < 0.0;
|
||||
for h in hs {
|
||||
let Some(e) = d.entity(*h) else { continue };
|
||||
let piece = |ends: (Vec2, Vec2), bulge: f64, widths: Option<(f64, f64)>| Piece { owner: *h, ends, bulge, widths };
|
||||
let piece =
|
||||
|ends: (Vec2, Vec2), heights: (f64, f64), bulge: f64, widths: Option<(f64, f64)>| Piece { owner: *h, ends, heights, bulge, widths };
|
||||
let unit: Vec<Piece> = 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::Line(l) => vec![piece((l.a.xy(), l.b.xy()), (l.a.z, l.b.z), 0.0, None)],
|
||||
EntityKind::Arc(a) => {
|
||||
let arc = Arc::new(a.center.xy(), a.radius, a.start, a.end);
|
||||
vec![piece((arc.start_point(), arc.end_point()), cadcraft_geom::arc_to_bulge(arc.sweep()), None)]
|
||||
vec![piece((arc.start_point(), arc.end_point()), (a.center.z, a.center.z), cadcraft_geom::arc_to_bulge(arc.sweep()), None)]
|
||||
}
|
||||
// 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) };
|
||||
piece((w[0].p, w[1].p), w[0].bulge, Some(widths))
|
||||
})
|
||||
.collect(),
|
||||
EntityKind::LwPolyline(p) if !p.closed => {
|
||||
// The elevation is along the polyline's normal: down Z for a mirrored one.
|
||||
let z = if down(e) { -p.elevation } else { p.elevation };
|
||||
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) };
|
||||
piece((w[0].p, w[1].p), (z, z), w[0].bulge, Some(widths))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
_ => Vec::new(),
|
||||
};
|
||||
if !unit.is_empty() {
|
||||
units.push(unit);
|
||||
units.push((down(e), unit));
|
||||
}
|
||||
}
|
||||
if units.iter().map(Vec::len).sum::<usize>() < 2 {
|
||||
if units.iter().map(|u| u.1.len()).sum::<usize>() < 2 {
|
||||
return Err(EngineError::Other("Select at least two objects to join.".into()));
|
||||
}
|
||||
// An object facing the other way from a polyline source stays out (AutoCAD 2024, measured: one
|
||||
// facing up takes no arc facing down, and one facing down no arc or polyline facing up). Onto a
|
||||
// line or an arc it wasn't measured (the core console's JOIN takes only collinear lines or arcs
|
||||
// on one circle onto those), so objects facing either way still join there.
|
||||
let polyline_source = units.first().filter(|u| u.1.first().is_some_and(|p| p.widths.is_some())).map(|u| u.0);
|
||||
let units: Vec<Vec<Piece>> = units.into_iter().filter(|u| polyline_source.is_none_or(|down| u.0 == down)).map(|u| u.1).collect();
|
||||
// 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());
|
||||
@@ -173,7 +188,7 @@ pub(crate) fn join(s: &mut Session, hs: &[Handle]) -> Result<Option<Handle>> {
|
||||
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 closed = links.first().zip(links.last()).is_some_and(|(a, b)| meet(a.start(), b.end(), 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()));
|
||||
@@ -209,9 +224,19 @@ pub(crate) fn join(s: &mut Session, hs: &[Handle]) -> Result<Option<Handle>> {
|
||||
for h in hs.iter().filter(|h| used(h)) {
|
||||
doc.remove_entity(*h);
|
||||
}
|
||||
// A new polyline stays at the source's height, as a polyline source keeps its elevation. The
|
||||
// elevation runs along its normal: negated for one facing down. Not measured: the core
|
||||
// console's JOIN makes no new polyline.
|
||||
let height = links.iter().find(|l| Some(l.owner) == first).map_or(0.0, |l| l.heights.0);
|
||||
let common = common.unwrap_or_default();
|
||||
let elevation = if common.extrusion.z < 0.0 { -height } else { height };
|
||||
let mut kind = lwpoly(vs, closed);
|
||||
if let EntityKind::LwPolyline(p) = &mut kind {
|
||||
p.elevation = elevation;
|
||||
}
|
||||
let nh = doc.new_handle();
|
||||
if let Some(st) = doc.space_mut(&space) {
|
||||
st.push(Entity { handle: nh, common: common.unwrap_or_default(), kind: lwpoly(vs, closed) });
|
||||
st.push(Entity { handle: nh, common, kind });
|
||||
}
|
||||
s.set_selection(Vec::new());
|
||||
Ok(Some(nh))
|
||||
@@ -272,9 +297,11 @@ fn arc_reach(g: &cadcraft_doc::Arc, others: &[&cadcraft_doc::Arc], tol: f64) ->
|
||||
/// 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.
|
||||
/// Ends are compared at the objects' own end points, not ones worked out again from arcs, and at
|
||||
/// their heights.
|
||||
fn chain_units(mut units: Vec<Vec<Piece>>, tol: f64) -> Vec<Piece> {
|
||||
let ends = |u: &[Piece]| u.first().zip(u.last()).map(|(a, b)| (a.ends.0, b.ends.1));
|
||||
let ends = |u: &[Piece]| u.first().zip(u.last()).map(|(a, b)| (a.start(), b.end()));
|
||||
let near = |p, q| meet(p, q, tol);
|
||||
if units.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -285,8 +312,8 @@ fn chain_units(mut units: Vec<Vec<Piece>>, tol: f64) -> Vec<Piece> {
|
||||
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<Piece>| ends(u).is_some_and(|(a, b)| a.near(end, tol) || b.near(end, tol) || a.near(start, tol) || b.near(start, tol));
|
||||
while let (Some(start), Some(end)) = (chain.front().map(Piece::start), chain.back().map(Piece::end)) {
|
||||
let meets = |u: &Vec<Piece>| ends(u).is_some_and(|(a, b)| near(a, end) || near(b, end) || near(a, start) || near(b, start));
|
||||
let Some(i) = units.iter().position(meets) else { break };
|
||||
let unit = units.remove(i);
|
||||
let Some((a, b)) = ends(&unit) else { break };
|
||||
@@ -296,19 +323,19 @@ fn chain_units(mut units: Vec<Vec<Piece>>, tol: f64) -> Vec<Piece> {
|
||||
// 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) {
|
||||
if near(a, start) {
|
||||
(true, true)
|
||||
} else if a.near(end, tol) {
|
||||
} else if near(a, end) {
|
||||
(false, false)
|
||||
} else {
|
||||
(b.near(start, tol), !b.near(start, tol))
|
||||
(near(b, start), !near(b, start))
|
||||
}
|
||||
} else if b.near(end, tol) {
|
||||
} else if near(b, end) {
|
||||
(false, true)
|
||||
} else if a.near(start, tol) {
|
||||
} else if near(a, start) {
|
||||
(true, true)
|
||||
} else {
|
||||
(!a.near(end, tol), false)
|
||||
(!near(a, end), false)
|
||||
};
|
||||
match (before, turned) {
|
||||
(false, false) => chain.extend(unit),
|
||||
@@ -317,7 +344,7 @@ fn chain_units(mut units: Vec<Vec<Piece>>, tol: f64) -> Vec<Piece> {
|
||||
(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)) {
|
||||
if chain.front().zip(chain.back()).is_some_and(|(f, l)| near(f.start(), l.end())) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -325,20 +352,43 @@ fn chain_units(mut units: Vec<Vec<Piece>>, tol: f64) -> Vec<Piece> {
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// them (turned round when it runs backwards in the chain) and their world heights (a line's own
|
||||
/// Z, an arc's centre Z, a polyline's elevation, negated for one facing down), its bulge and, for a
|
||||
/// polyline's own segment, its widths.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Piece {
|
||||
owner: Handle,
|
||||
ends: (Vec2, Vec2),
|
||||
heights: (f64, f64),
|
||||
bulge: f64,
|
||||
widths: Option<(f64, f64)>,
|
||||
}
|
||||
|
||||
impl Piece {
|
||||
fn flipped(self) -> Self {
|
||||
Piece { ends: (self.ends.1, self.ends.0), bulge: -self.bulge, widths: self.widths.map(|(a, b)| (b, a)), ..self }
|
||||
Piece {
|
||||
ends: (self.ends.1, self.ends.0),
|
||||
heights: (self.heights.1, self.heights.0),
|
||||
bulge: -self.bulge,
|
||||
widths: self.widths.map(|(a, b)| (b, a)),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
fn start(&self) -> (Vec2, f64) {
|
||||
(self.ends.0, self.heights.0)
|
||||
}
|
||||
|
||||
fn end(&self) -> (Vec2, f64) {
|
||||
(self.ends.1, self.heights.1)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether two ends meet: in plan and at the same height. AutoCAD 2024 leaves an end that meets
|
||||
/// another only in plan apart (measured: a line or a polyline at 0 and a line at 5, or a polyline
|
||||
/// at 5 and a line at 0, end to end in plan, stay as they are).
|
||||
fn meet(p: (Vec2, f64), q: (Vec2, f64), tol: f64) -> bool {
|
||||
p.0.near(q.0, tol) && (p.1 - q.1).abs() < tol
|
||||
}
|
||||
|
||||
/// The open polyline `sp` (handle `src`) with the chain's other pieces added before and after
|
||||
|
||||
@@ -966,3 +966,296 @@ fn an_arc_closing_a_polyline_and_a_source_of_no_length() {
|
||||
assert!(s.execute("join", &json!({ "handles": [z.hex(), l.hex()] })).is_err());
|
||||
assert_eq!(objects(&s), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn objects_meeting_only_in_plan_at_another_height_stay_apart() {
|
||||
// AutoCAD 2024 (accoreconsole, measured): an end that meets the source only in plan, at
|
||||
// another height, is no join. The line (0,0,0)-(10,0,0) keeps (10,0,5)-(20,0,5) apart ("1
|
||||
// object discarded"), a polyline at elevation 0 that line, and a polyline at elevation 5 the
|
||||
// line (10,0,0)-(20,0,0) ("0 segments added"). Both lines at 5 join into one.
|
||||
let elevated = |s: &mut Session, h: Handle, z: f64| {
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(h, |e| {
|
||||
if let EntityKind::LwPolyline(p) = &mut e.kind {
|
||||
p.elevation = z;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
};
|
||||
for (source_z, other_z, poly) in [(0.0, 5.0, false), (0.0, 5.0, true), (5.0, 0.0, true)] {
|
||||
let mut s = Session::new();
|
||||
let a = if poly { pline(&mut s, &[(0.0, 0.0, 0.0, 0.0, 0.0), (10.0, 0.0, 0.0, 0.0, 0.0)]) } else { line(&mut s, [0.0, 0.0], [10.0, 0.0]) };
|
||||
if poly {
|
||||
elevated(&mut s, a, source_z);
|
||||
} else {
|
||||
raise(&mut s, a, source_z);
|
||||
}
|
||||
let b = line(&mut s, [10.0, 0.0], [20.0, 0.0]);
|
||||
raise(&mut s, b, other_z);
|
||||
let before = objects(&s);
|
||||
let _ = s.execute("join", &json!({ "handles": [a.hex(), b.hex()] }));
|
||||
assert_eq!(objects(&s), before, "source at {source_z}, other at {other_z}, polyline {poly}");
|
||||
}
|
||||
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], [20.0, 0.0]);
|
||||
raise(&mut s, a, 5.0);
|
||||
raise(&mut s, b, 5.0);
|
||||
join(&mut s, &[a, b]);
|
||||
let objs = objects(&s);
|
||||
assert!(objs.len() == 1 && matches!(&objs[0].1, EntityKind::Line(l) if l.b.x == 20.0 && l.a.z == 5.0 && l.b.z == 5.0), "{objs:?}");
|
||||
// A sloped line ends at its own heights (AutoCAD 2024, measured): after a polyline at 0, the
|
||||
// line (20,0,5)-(10,0,0), turned round, leaves the chain at height 5, so a line on from
|
||||
// (20,0,5) joins too ("2 segments added") and one from (20,0,0) stays apart ("1 segments
|
||||
// added"). An arc whose centre is at another height stays apart ("0 segments added").
|
||||
for (next_z, joins) in [(5.0, true), (0.0, false)] {
|
||||
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 sloped = line(&mut s, [20.0, 0.0], [10.0, 0.0]);
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(sloped, |e| {
|
||||
if let EntityKind::Line(l) = &mut e.kind {
|
||||
l.a.z = 5.0;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
let next = line(&mut s, [20.0, 0.0], [30.0, 0.0]);
|
||||
raise(&mut s, next, next_z);
|
||||
assert_eq!(join(&mut s, &[p, sloped, next]), p);
|
||||
let objs = objects(&s);
|
||||
let left: Vec<Handle> = objs.iter().map(|o| o.0).collect();
|
||||
assert_eq!(left, if joins { vec![p] } else { vec![p, next] }, "next at {next_z}");
|
||||
assert_eq!(vertices(&objs[0].1).len(), if joins { 4 } else { 3 }, "next at {next_z}");
|
||||
}
|
||||
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 a = arc(&mut s, [10.0, 5.0], 5.0, 270.0, 0.0);
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(a, |e| {
|
||||
if let EntityKind::Arc(o) = &mut e.kind {
|
||||
o.center.z = 5.0;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
let before = objects(&s);
|
||||
let _ = s.execute("join", &json!({ "handles": [p.hex(), a.hex()] }));
|
||||
assert_eq!(objects(&s), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn objects_facing_the_other_way_from_the_source_stay_apart() {
|
||||
// AutoCAD 2024 (accoreconsole, measured) with polylines whose normal is 0,0,-1, here in world
|
||||
// coordinates as the DXF reader keeps them: one at elevation 5 takes no line on from its end,
|
||||
// at Z -5 or 5; it takes another such polyline at elevation 5 ("1 segments added") but not one
|
||||
// at -5; and a polyline facing up at elevation -5 doesn't take one at 5, though they meet at Z
|
||||
// -5 ("0 segments added").
|
||||
let poly = |s: &mut Session, from: f64, elevation: f64, down: bool| {
|
||||
let h = pline(s, &[(from, 0.0, 0.0, 0.0, 0.0), (from + 10.0, 0.0, 0.0, 0.0, 0.0)]);
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(h, |e| {
|
||||
if down {
|
||||
e.common.extrusion = cadcraft_engine::geom::Vec3::new(0.0, 0.0, -1.0);
|
||||
}
|
||||
if let EntityKind::LwPolyline(p) = &mut e.kind {
|
||||
p.elevation = elevation;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
h
|
||||
};
|
||||
for line_z in [-5.0, 5.0] {
|
||||
let mut s = Session::new();
|
||||
let p = poly(&mut s, 0.0, 5.0, true);
|
||||
let l = line(&mut s, [10.0, 0.0], [20.0, 0.0]);
|
||||
raise(&mut s, l, line_z);
|
||||
let before = objects(&s);
|
||||
let _ = s.execute("join", &json!({ "handles": [p.hex(), l.hex()] }));
|
||||
assert_eq!(objects(&s), before, "line at {line_z}");
|
||||
}
|
||||
for (first, second, joins) in [((5.0, true), (5.0, true), true), ((5.0, true), (-5.0, true), false), ((-5.0, false), (5.0, true), false)] {
|
||||
let mut s = Session::new();
|
||||
let p = poly(&mut s, 0.0, first.0, first.1);
|
||||
let q = poly(&mut s, 10.0, second.0, second.1);
|
||||
let before = objects(&s);
|
||||
let _ = s.execute("join", &json!({ "handles": [p.hex(), q.hex()] }));
|
||||
let objs = objects(&s);
|
||||
if joins {
|
||||
assert!(objs.len() == 1 && objs[0].0 == p && vertices(&objs[0].1).len() == 3, "{first:?} {second:?} {objs:?}");
|
||||
} else {
|
||||
assert_eq!(objs, before, "{first:?} {second:?}");
|
||||
}
|
||||
}
|
||||
// A line faces up whatever its normal (AutoCAD 2024, measured): a polyline facing up takes a
|
||||
// line with normal 0,0,-1 from its end, square to it ("1 segments added") or straight on with
|
||||
// an arc facing up after it ("2 segments added"), and one facing down at elevation 0 takes no
|
||||
// line from its end, whichever way the line's normal points ("0 segments added").
|
||||
let face_down = |s: &mut Session, h: Handle| {
|
||||
s.doc_mut().unwrap().modify_entity(h, |e| e.common.extrusion = cadcraft_engine::geom::Vec3::new(0.0, 0.0, -1.0)).unwrap();
|
||||
};
|
||||
let mut s = Session::new();
|
||||
let p = poly(&mut s, 0.0, 0.0, false);
|
||||
let l = line(&mut s, [10.0, 0.0], [10.0, 10.0]);
|
||||
face_down(&mut s, l);
|
||||
assert_eq!(join(&mut s, &[p, l]), p);
|
||||
assert_eq!(objects(&s).len(), 1);
|
||||
let mut s = Session::new();
|
||||
let p = poly(&mut s, 0.0, 0.0, false);
|
||||
let l = line(&mut s, [10.0, 0.0], [20.0, 0.0]);
|
||||
face_down(&mut s, l);
|
||||
let a = arc(&mut s, [20.0, 5.0], 5.0, 270.0, 0.0);
|
||||
assert_eq!(join(&mut s, &[p, l, a]), p);
|
||||
let objs = objects(&s);
|
||||
assert!(objs.len() == 1 && vertices(&objs[0].1).len() == 4, "{objs:?}");
|
||||
for line_down in [false, true] {
|
||||
let mut s = Session::new();
|
||||
let p = poly(&mut s, 0.0, 0.0, true);
|
||||
let l = line(&mut s, [10.0, 0.0], [10.0, 10.0]);
|
||||
if line_down {
|
||||
face_down(&mut s, l);
|
||||
}
|
||||
let before = objects(&s);
|
||||
let _ = s.execute("join", &json!({ "handles": [p.hex(), l.hex()] }));
|
||||
assert_eq!(objects(&s), before, "line facing down {line_down}");
|
||||
}
|
||||
// An arc faces its normal (AutoCAD 2024, measured, with the arc from (10,0) to (15,5) round
|
||||
// (10,5)): a polyline facing up takes no arc facing down, and one facing down at elevation 0 no
|
||||
// arc facing up, but it takes one facing down, with the bulge and vertex AutoCAD writes (-0.414
|
||||
// and (-15,5) in the polyline's own coordinates). At elevation 5, its world height is -5: it
|
||||
// takes the arc facing down whose centre is at Z -5 and not one at 5.
|
||||
for (poly_down, elevation, arc_down, arc_z, joins) in [
|
||||
(false, 0.0, true, 0.0, false),
|
||||
(true, 0.0, false, 0.0, false),
|
||||
(true, 0.0, true, 0.0, true),
|
||||
(true, 5.0, true, -5.0, true),
|
||||
(true, 5.0, true, 5.0, false),
|
||||
] {
|
||||
let mut s = Session::new();
|
||||
let p = poly(&mut s, 0.0, elevation, poly_down);
|
||||
let a = arc(&mut s, [10.0, 5.0], 5.0, 270.0, 0.0);
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(a, |e| {
|
||||
if let EntityKind::Arc(o) = &mut e.kind {
|
||||
o.center.z = arc_z;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
if arc_down {
|
||||
face_down(&mut s, a);
|
||||
}
|
||||
let before = objects(&s);
|
||||
let r = s.execute("join", &json!({ "handles": [p.hex(), a.hex()] }));
|
||||
let objs = objects(&s);
|
||||
let case = format!("polyline down {poly_down} at {elevation}, arc down {arc_down} at {arc_z}");
|
||||
if joins {
|
||||
assert!(r.is_ok() && objs.len() == 1 && objs[0].0 == p, "{case}: {objs:?}");
|
||||
let v = vertices(&objs[0].1);
|
||||
assert!(v.len() == 3 && near(v[1].0, 10.0) && near(v[2].0, 15.0) && near(v[2].1, 5.0), "{case}: {v:?}");
|
||||
assert!(near(v[1].4, std::f64::consts::FRAC_PI_8.tan()), "{case}: {v:?}");
|
||||
} else {
|
||||
assert_eq!(objs, before, "{case}");
|
||||
}
|
||||
}
|
||||
// A polyline of one vertex has no segment to join: the source is the first object that has
|
||||
// one, for the way it faces as for its properties.
|
||||
let mut s = Session::new();
|
||||
let dot = poly(&mut s, 50.0, 0.0, true);
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(dot, |e| {
|
||||
if let EntityKind::LwPolyline(p) = &mut e.kind {
|
||||
p.vertices.truncate(1);
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
let p = poly(&mut s, 0.0, 0.0, false);
|
||||
let l = line(&mut s, [10.0, 0.0], [20.0, 0.0]);
|
||||
assert_eq!(join(&mut s, &[dot, p, l]), p);
|
||||
assert_eq!(objects(&s).iter().map(|o| o.0).collect::<Vec<_>>(), [dot, p]);
|
||||
// JOIN onto a line source takes a collinear line with normal 0,0,-1 (AutoCAD 2024, measured:
|
||||
// "1 line joined to source").
|
||||
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], [20.0, 0.0]);
|
||||
face_down(&mut s, b);
|
||||
assert_eq!(join(&mut s, &[a, b]), a);
|
||||
let objs = objects(&s);
|
||||
assert!(objs.len() == 1 && matches!(&objs[0].1, EntityKind::Line(l) if l.b.x == 20.0), "{objs:?}");
|
||||
// Onto a line source, objects facing the other way weren't measured: the core console's JOIN
|
||||
// takes only collinear lines onto one. An arc facing down still joins it into a new polyline,
|
||||
// as before.
|
||||
let mut s = Session::new();
|
||||
let a = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
|
||||
let b = arc(&mut s, [10.0, 5.0], 5.0, 270.0, 0.0);
|
||||
face_down(&mut s, b);
|
||||
join(&mut s, &[a, b]);
|
||||
let objs = objects(&s);
|
||||
assert!(objs.len() == 1 && vertices(&objs[0].1).len() == 3, "{objs:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_polyline_stays_at_the_source_height() {
|
||||
// Not measured: the core console's JOIN takes only collinear lines onto a line and arcs on one
|
||||
// circle onto an arc, so it makes no new polyline. The new polyline stays at the source's
|
||||
// height, as a polyline source keeps its elevation (measured), instead of moving to Z 0.
|
||||
let elevation = |s: &Session| {
|
||||
let objs = objects(s);
|
||||
let EntityKind::LwPolyline(p) = &objs[0].1 else { panic!("{objs:?}") };
|
||||
assert_eq!((objs.len(), p.vertices.len()), (1, 3), "{objs:?}");
|
||||
p.elevation
|
||||
};
|
||||
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]);
|
||||
raise(&mut s, a, 5.0);
|
||||
raise(&mut s, b, 5.0);
|
||||
join(&mut s, &[a, b]);
|
||||
assert_eq!(elevation(&s), 5.0);
|
||||
// An arc whose centre is at Z 5 as the source, with a line at 5 from its end.
|
||||
let mut s = Session::new();
|
||||
let a = arc(&mut s, [10.0, 5.0], 5.0, 270.0, 0.0);
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(a, |e| {
|
||||
if let EntityKind::Arc(o) = &mut e.kind {
|
||||
o.center.z = 5.0;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
let l = line(&mut s, [15.0, 5.0], [15.0, 10.0]);
|
||||
raise(&mut s, l, 5.0);
|
||||
join(&mut s, &[a, l]);
|
||||
assert_eq!(elevation(&s), 5.0);
|
||||
// A sloped line that joins before the source, from (0,5,0) to the source's start (10,0,5): the
|
||||
// polyline stays at the source's height, as a polyline source keeps its elevation with a
|
||||
// sloped line on it (measured).
|
||||
let mut s = Session::new();
|
||||
let a = line(&mut s, [10.0, 0.0], [20.0, 0.0]);
|
||||
raise(&mut s, a, 5.0);
|
||||
let sloped = line(&mut s, [0.0, 5.0], [10.0, 0.0]);
|
||||
s.doc_mut()
|
||||
.unwrap()
|
||||
.modify_entity(sloped, |e| {
|
||||
if let EntityKind::Line(l) = &mut e.kind {
|
||||
l.b.z = 5.0;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
join(&mut s, &[a, sloped]);
|
||||
assert_eq!(elevation(&s), 5.0);
|
||||
// A line source whose normal is 0,0,-1 gives that normal to the new polyline, as before, and
|
||||
// the elevation runs along it: at Z 5 the elevation is -5.
|
||||
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]);
|
||||
raise(&mut s, a, 5.0);
|
||||
raise(&mut s, b, 5.0);
|
||||
s.doc_mut().unwrap().modify_entity(a, |e| e.common.extrusion = cadcraft_engine::geom::Vec3::new(0.0, 0.0, -1.0)).unwrap();
|
||||
join(&mut s, &[a, b]);
|
||||
assert_eq!(elevation(&s), -5.0);
|
||||
assert!(s.doc().unwrap().model.iter().all(|e| e.common.extrusion.z < 0.0));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user