mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-01 18:25:32 -06:00
Dynamic blocks: stretch an arc by its ends; hatches follow their lines
An arc with one end inside a stretch frame moved whole. AutoCAD 2024 moves that end and keeps the arc's height over its chord: in its own stretched copies of library blocks, 84 arcs whose ends moved apart kept their height to 1.1e-14 of it. An associative hatch whose loop is made of several lines didn't follow them: only a loop of one closed polyline was filled again. A straight loop whose every vertex is a line's end or a flat polyline's vertex of its boundary objects (within a billionth of the largest coordinate) now takes those points' new places. The points looked through are bounded. A tilted arc, and an arc when the frame can't be tested, still move whole. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
05cd8ac095
commit
8c143f8feb
@@ -388,10 +388,11 @@ fn delta(old: &Val, new: &Val, connection: &str) -> f64 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Move entity `e` by `v`: the listed vertices of a line, polyline or wipeout, else the whole
|
||||
/// object.
|
||||
/// Move entity `e` by `v`: the listed vertices of a line, polyline or wipeout, the listed ends of
|
||||
/// an arc (0 its start, 1 its end), else the whole object.
|
||||
fn move_entity(e: &mut Entity, vertices: Option<&[i32]>, v: Vec2) {
|
||||
match (&mut e.kind, vertices) {
|
||||
(EntityKind::Arc(a), Some(idx)) => stretch_arc(a, idx.contains(&0), idx.contains(&1), v),
|
||||
(EntityKind::LwPolyline(pl), Some(idx)) => {
|
||||
for i in idx {
|
||||
if let Some(p) = usize::try_from(*i).ok().and_then(|i| pl.vertices.get_mut(i)) {
|
||||
@@ -419,6 +420,27 @@ fn move_entity(e: &mut Entity, vertices: Option<&[i32]>, v: Vec2) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Move the start and/or the end of arc `a` by `v`, keeping its height over its chord, as AutoCAD
|
||||
/// 2024 stretches an arc one of whose ends is in the frame. Both ends move it whole. An arc whose
|
||||
/// new ends would meet, or that would come out too flat to be an arc, is left where it was.
|
||||
fn stretch_arc(a: &mut crate::Arc, start: bool, end: bool, v: Vec2) {
|
||||
let c = a.center.xy();
|
||||
let (s, e) = (Vec2::polar(c, a.radius, a.start), Vec2::polar(c, a.radius, a.end));
|
||||
match (start, end) {
|
||||
(true, true) => a.center = a.center + v.to3(0.0),
|
||||
(false, false) => {}
|
||||
_ => {
|
||||
let (s2, e2) = (if start { s + v } else { s }, if end { e + v } else { e });
|
||||
// The height over the chord is half the chord times the bulge, tan(sweep / 4).
|
||||
let height = s.dist(e) * 0.5 * (cadcraft_geom::ccw_sweep(a.start, a.end) / 4.0).tan();
|
||||
let chord = s2.dist(e2);
|
||||
if let Some((g, _)) = (chord > 0.0).then(|| cadcraft_geom::bulge_to_arc(s2, e2, 2.0 * height / chord)).flatten() {
|
||||
*a = crate::Arc { center: Vec3::new(g.center.x, g.center.y, a.center.z), radius: g.radius, start: g.start, end: g.end };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Most points of a stretch frame whose objects' points are tested against it one by one. A frame
|
||||
/// is two corners or a short polygon; testing every point of every object against every edge of a
|
||||
/// longer one would grow with their product, so the action's own vertex lists are used instead.
|
||||
@@ -550,19 +572,24 @@ fn apply(
|
||||
continue;
|
||||
}
|
||||
let Some(c) = copy_of(h) else { continue };
|
||||
// The points of a line, polyline or wipeout that move are those inside the frame where
|
||||
// the dynamic block has them, all of them when none is, as AutoCAD 2024 moves them. It
|
||||
// The points of a line, polyline, wipeout or arc that move are those inside the frame
|
||||
// where the dynamic block has them, all of them when none is, as AutoCAD 2024 moves them. It
|
||||
// doesn't go by the vertex lists the action stores, which a block from another program
|
||||
// can get wrong. A polyline's vertices are in its own plane: in the block's when its
|
||||
// extrusion is exactly along z (a mirrored one is read flipped into it). Any tilt, however
|
||||
// small, moves them by as much as the elevation times the tilt, so a tilted one keeps the
|
||||
// stored list. A wipeout's outline is read into the block's plane; when the frame can't
|
||||
// be tested, it moves whole, as before (its stored list isn't measured).
|
||||
// stored list. A wipeout's outline is read into the block's plane. An arc's ends move as
|
||||
// a polyline's vertices do (see `stretch_arc`). When the frame can't be tested, or an arc
|
||||
// is tilted, a wipeout or arc moves whole, as before (their stored lists aren't measured).
|
||||
let idx = match def.entities.get(*h).map(|e| (&e.kind, e.common.extrusion)) {
|
||||
Some((EntityKind::Line(l), _)) if framed => inside(frame, [l.a.xy(), l.b.xy()].into_iter()),
|
||||
Some((EntityKind::LwPolyline(pl), n)) if framed && n.x == 0.0 && n.y == 0.0 => inside(frame, pl.vertices.iter().map(|v| v.p)),
|
||||
Some((EntityKind::Wipeout(w), _)) if framed => inside(frame, w.boundary.iter().copied()),
|
||||
Some((EntityKind::Wipeout(_), _)) => None,
|
||||
Some((EntityKind::Arc(a), n)) if framed && n.x == 0.0 && n.y == 0.0 => {
|
||||
let c = a.center.xy();
|
||||
inside(frame, [Vec2::polar(c, a.radius, a.start), Vec2::polar(c, a.radius, a.end)].into_iter())
|
||||
}
|
||||
Some((EntityKind::Wipeout(_) | EntityKind::Arc(_), _)) => None,
|
||||
_ => vtx_by_handle.get(h).map(|i| (*i).clone()),
|
||||
};
|
||||
moves.entry(c).or_default().push((idx, v));
|
||||
@@ -628,6 +655,7 @@ fn apply(
|
||||
}
|
||||
b.entities.push(c);
|
||||
}
|
||||
follow_outlines(&mut b, &src, &copies);
|
||||
refill_hatches(&mut b);
|
||||
let def_record = Some(record_handle(d, definition).unwrap_or_else(|| {
|
||||
let h = d.new_handle();
|
||||
@@ -682,6 +710,154 @@ fn apply(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The points of a hatch's boundary object that a loop vertex can sit on: a line's ends, a
|
||||
/// polyline's vertices (in the block's plane).
|
||||
fn outline_points(e: &Entity) -> Vec<Vec2> {
|
||||
(0..outline_len(e)).filter_map(|k| outline_point(e, k)).collect()
|
||||
}
|
||||
|
||||
/// How many points [`outline_points`] gives for `e`.
|
||||
fn outline_len(e: &Entity) -> usize {
|
||||
match &e.kind {
|
||||
EntityKind::Line(_) => 2,
|
||||
EntityKind::LwPolyline(pl) if e.common.extrusion.x == 0.0 && e.common.extrusion.y == 0.0 => pl.vertices.len(),
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Point `k` of [`outline_points`] for `e`, without listing the others.
|
||||
fn outline_point(e: &Entity, k: usize) -> Option<Vec2> {
|
||||
match &e.kind {
|
||||
EntityKind::Line(l) => [l.a.xy(), l.b.xy()].get(k).copied(),
|
||||
EntityKind::LwPolyline(pl) if e.common.extrusion.x == 0.0 && e.common.extrusion.y == 0.0 => pl.vertices.get(k).map(|v| v.p),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The points of a hatch loop's boundary objects, found by place within a rounding of where they
|
||||
/// are: a hatch's loop vertices come within about 3e-10 of the points they were made from in the
|
||||
/// library drawings measured, not always exactly on them.
|
||||
struct OutlinePoints {
|
||||
/// A rounding: a billionth of the largest coordinate (at least of 1).
|
||||
tol: f64,
|
||||
/// The points by grid cell of side `tol`: the object and the point's index.
|
||||
cells: HashMap<(i64, i64), Vec<(Vec2, Handle, usize)>>,
|
||||
}
|
||||
|
||||
/// Most points kept in one cell of [`OutlinePoints`], a bound: a file that piles points on one
|
||||
/// place would make every vertex look at all of them (CWE-400). Points past it, in the order of the
|
||||
/// boundary objects, aren't matched.
|
||||
const MAX_CELL_POINTS: usize = 16;
|
||||
|
||||
/// Most boundary points looked through for the loops of one stretched block: a file whose loops
|
||||
/// each list many boundary objects multiplies them; past this the remaining loops keep what they
|
||||
/// have (CWE-400).
|
||||
const MAX_FOLLOWED_POINTS: usize = 4_000_000;
|
||||
|
||||
impl OutlinePoints {
|
||||
fn new(points: Vec<(Vec2, Handle, usize)>) -> Self {
|
||||
let largest = points.iter().map(|(p, ..)| p.x.abs().max(p.y.abs())).filter(|m| m.is_finite()).fold(1.0, f64::max);
|
||||
let tol = largest * 1e-9;
|
||||
let mut cells: HashMap<(i64, i64), Vec<(Vec2, Handle, usize)>> = HashMap::new();
|
||||
for (p, h, k) in points {
|
||||
if p.is_finite() {
|
||||
let cell = cells.entry(Self::cell(p, tol)).or_default();
|
||||
if cell.len() < MAX_CELL_POINTS {
|
||||
cell.push((p, h, k));
|
||||
}
|
||||
}
|
||||
}
|
||||
OutlinePoints { tol, cells }
|
||||
}
|
||||
|
||||
fn cell(p: Vec2, tol: f64) -> (i64, i64) {
|
||||
// Saturating casts: a far point lands in an edge cell, where only points as far are.
|
||||
((p.x / tol).floor() as i64, (p.y / tol).floor() as i64)
|
||||
}
|
||||
|
||||
/// The nearest point within the rounding of `p`, if any.
|
||||
fn at(&self, p: Vec2) -> Option<(Handle, usize)> {
|
||||
if !p.is_finite() {
|
||||
return None;
|
||||
}
|
||||
let (cx, cy) = Self::cell(p, self.tol);
|
||||
let mut best: Option<(f64, Handle, usize)> = None;
|
||||
for dx in -1..=1 {
|
||||
for dy in -1..=1 {
|
||||
let key = (cx.saturating_add(dx), cy.saturating_add(dy));
|
||||
for (q, h, k) in self.cells.get(&key).into_iter().flatten() {
|
||||
let d = q.dist(p);
|
||||
if d <= self.tol && best.is_none_or(|(bd, ..)| d < bd) {
|
||||
best = Some((d, *h, *k));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
best.map(|(_, h, k)| (h, k))
|
||||
}
|
||||
}
|
||||
|
||||
/// Associative hatches of block `b` follow their boundary objects: a straight loop whose every
|
||||
/// vertex is a point of one of its boundary objects (a line's end, a polyline's vertex, within a
|
||||
/// rounding) in `before`, the block the copies were made from, takes that point's new place (the
|
||||
/// first boundary object's, where two meet at the vertex). AutoCAD 2024 fills such a hatch again
|
||||
/// from its stretched boundary. A loop whose boundary didn't move comes back on its boundary's
|
||||
/// points, within a rounding of where it was. A loop with a curved edge, or with a vertex on no
|
||||
/// such point, keeps what it has.
|
||||
fn follow_outlines(b: &mut Block, before: &Block, copies: &HashMap<Handle, Handle>) {
|
||||
let mut budget = MAX_FOLLOWED_POINTS;
|
||||
for old in before.entities.iter() {
|
||||
let EntityKind::Hatch(h) = &old.kind else { continue };
|
||||
let Some(nh) = copies.get(&old.handle).copied() else { continue };
|
||||
if !h.associative {
|
||||
continue;
|
||||
}
|
||||
let mut loops: Vec<(usize, Vec<Vec2>)> = Vec::new();
|
||||
for (i, l) in h.loops.iter().enumerate() {
|
||||
if l.sources.is_empty() || l.vertices.iter().any(|v| v.bulge != 0.0) {
|
||||
continue;
|
||||
}
|
||||
let count: usize = l.sources.iter().filter_map(|s| before.entities.get(*s)).map(|e| outline_len(e)).sum();
|
||||
let Some(left) = budget.checked_sub(count) else { continue };
|
||||
budget = left;
|
||||
// Where each point of the loop's boundary objects is: the object and the point's index.
|
||||
let at = OutlinePoints::new(
|
||||
l.sources
|
||||
.iter()
|
||||
.filter_map(|s| before.entities.get(*s).map(|e| (*s, e)))
|
||||
.flat_map(|(s, e)| outline_points(e).into_iter().enumerate().map(move |(k, p)| (p, s, k)))
|
||||
.collect(),
|
||||
);
|
||||
let moved: Option<Vec<Vec2>> = l
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let (s, k) = at.at(v.p)?;
|
||||
let e = copies.get(&s).and_then(|ns| b.entities.get(*ns))?;
|
||||
outline_point(e, k)
|
||||
})
|
||||
.collect();
|
||||
if let Some(m) = moved {
|
||||
loops.push((i, m));
|
||||
}
|
||||
}
|
||||
if loops.is_empty() {
|
||||
continue;
|
||||
}
|
||||
b.entities.modify(nh, |e| {
|
||||
if let EntityKind::Hatch(hh) = &mut e.kind {
|
||||
for (i, m) in loops {
|
||||
if let Some(l) = hh.loops.get_mut(i) {
|
||||
for (v, p) in l.vertices.iter_mut().zip(m) {
|
||||
v.p = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Associative hatches of block `b` whose loop is one closed polyline of the block take its
|
||||
/// outline again (it may have been stretched).
|
||||
fn refill_hatches(b: &mut Block) {
|
||||
|
||||
@@ -559,29 +559,190 @@ fn a_stretch_moves_the_points_of_a_wipeout_inside_its_frame() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wipeout_moves_whole_when_the_frame_cant_be_tested() {
|
||||
fn wipeouts_and_arcs_move_whole_when_the_frame_cant_be_tested() {
|
||||
// The frame as a polygon of `n` points, as in the test of the stored lists. Up to 1000 points
|
||||
// the wipeout's points inside it move; past that it moves whole, as before: the list stored for
|
||||
// it (here its first point alone) isn't used, as a wipeout's list isn't measured.
|
||||
// the points of wipeout 34 and the end of arc 35 inside it move; past that both move whole, as
|
||||
// before: the lists stored for them (their first point alone) aren't used, as a wipeout's or an
|
||||
// arc's list isn't measured.
|
||||
let frame = |n: usize| {
|
||||
let mut f = format!("72 {n}\n1011 34\n1021 -2\n1011 38\n1021 -2\n1011 38\n1021 2\n");
|
||||
f.push_str(&"1011 34\n1021 2\n".repeat(n - 3));
|
||||
f
|
||||
};
|
||||
let crossing = [(36.0, -1.0), (36.0, 1.0), (20.0, 1.0), (20.0, -1.0)];
|
||||
let cases = [(1000, [(30.0, -1.0), (30.0, 1.0), (20.0, 1.0), (20.0, -1.0)]), (1001, [(30.0, -1.0), (30.0, 1.0), (14.0, 1.0), (14.0, -1.0)])];
|
||||
for (n, want) in cases {
|
||||
let angle = |x: f64, y: f64| y.atan2(x).to_degrees();
|
||||
let cases = [
|
||||
(1000, [(30.0, -1.0), (30.0, 1.0), (20.0, 1.0), (20.0, -1.0)], ((30.0, 0.0), (20.0, 0.0), 5.125)),
|
||||
(1001, [(30.0, -1.0), (30.0, 1.0), (14.0, 1.0), (14.0, -1.0)], ((30.0, 0.0), (14.0, 0.0), 10.0)),
|
||||
];
|
||||
for (n, want, want_arc) in cases {
|
||||
let src = fixture()
|
||||
.replace("72 2\n1011 34\n1021 -2\n1011 38\n1021 2\n", &frame(n))
|
||||
.replace("71 1\n330 30\n1010 40", "71 2\n330 30\n330 34\n1010 40")
|
||||
.replace("73 1\n331 30\n74 1\n94 1\n75 0", "73 2\n331 30\n74 1\n94 1\n331 34\n74 1\n94 0\n75 0")
|
||||
.replace("21 25\n31 0\n0 ENDBLK\n", &format!("21 25\n31 0\n{}0 ENDBLK\n", wipeout("34", &crossing)));
|
||||
.replace("71 1\n330 30\n1010 40", "71 3\n330 30\n330 34\n330 35\n1010 40")
|
||||
.replace("73 1\n331 30\n74 1\n94 1\n75 0", "73 3\n331 30\n74 1\n94 1\n331 34\n74 1\n94 0\n331 35\n74 1\n94 0\n75 0")
|
||||
.replace(
|
||||
"21 25\n31 0\n0 ENDBLK\n",
|
||||
&format!("21 25\n31 0\n{}{}0 ENDBLK\n", wipeout("34", &crossing), arc("35", 28.0, -6.0, 10.0, angle(8.0, 6.0), angle(-8.0, 6.0))),
|
||||
);
|
||||
let (mut d, r) = door_reference(&src);
|
||||
cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap();
|
||||
assert_eq!(wipeouts(&d, r), [want.to_vec()], "{n} frame points");
|
||||
let got = arcs(&d, r);
|
||||
assert!(got.len() == 1 && near(got[0], want_arc), "{n} frame points: {got:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tilted_arc_moves_whole() {
|
||||
// An arc in the plane whose normal is x: its centre, at (28, -6) seen from above, and its
|
||||
// angles are in that plane, so its ends can't be tested against the frame. It moves whole, as
|
||||
// before (not measured in AutoCAD).
|
||||
let tilted = "0 ARC\n5 34\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbCircle\n10 -6\n20 0\n30 28\n40 10\n210 1\n220 0\n230 0\n100 AcDbArc\n50 36.86989764584402\n51 143.13010235415598\n";
|
||||
let src = fixture()
|
||||
.replace("71 1\n330 30\n1010 40", "71 2\n330 30\n330 34\n1010 40")
|
||||
.replace("21 25\n31 0\n0 ENDBLK\n", &format!("21 25\n31 0\n{tilted}0 ENDBLK\n"));
|
||||
let (mut d, r) = door_reference(&src);
|
||||
let centre = |d: &cadcraft_doc::Drawing| {
|
||||
let b = d.block(&block_of(d, &r.hex())).unwrap();
|
||||
b.entities.iter().find_map(|e| match &e.kind {
|
||||
cadcraft_doc::EntityKind::Arc(a) => Some((a.center, a.radius)),
|
||||
_ => None,
|
||||
})
|
||||
};
|
||||
let (before, radius) = centre(&d).expect("the tilted arc is read as an arc");
|
||||
assert!((before.x - 28.0).abs() < 1e-9 && (before.y + 6.0).abs() < 1e-9, "{before:?}");
|
||||
cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap();
|
||||
let (after, r2) = centre(&d).unwrap();
|
||||
assert_eq!((after.x - before.x, after.y - before.y, after.z - before.z, r2), (-6.0, 0.0, 0.0, radius));
|
||||
}
|
||||
|
||||
/// An ARC `handle` of the door block: centre (`x`, `y`), radius `r`, from `start` to `end` degrees.
|
||||
fn arc(handle: &str, x: f64, y: f64, r: f64, start: f64, end: f64) -> String {
|
||||
format!("0 ARC\n5 {handle}\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbCircle\n10 {x}\n20 {y}\n30 0\n40 {r}\n100 AcDbArc\n50 {start}\n51 {end}\n")
|
||||
}
|
||||
|
||||
/// The arcs of reference `r`'s block, in its order: their start and end points and radius.
|
||||
fn arcs(d: &cadcraft_doc::Drawing, r: Handle) -> Vec<((f64, f64), (f64, f64), f64)> {
|
||||
let b = d.block(&block_of(d, &r.hex())).unwrap();
|
||||
b.entities
|
||||
.iter()
|
||||
.filter_map(|e| match &e.kind {
|
||||
cadcraft_doc::EntityKind::Arc(a) => {
|
||||
let c = a.center.xy();
|
||||
let (s, e) = (cadcraft_doc::geom::Vec2::polar(c, a.radius, a.start), cadcraft_doc::geom::Vec2::polar(c, a.radius, a.end));
|
||||
Some(((s.x, s.y), (e.x, e.y), a.radius))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn near(a: ((f64, f64), (f64, f64), f64), b: ((f64, f64), (f64, f64), f64)) -> bool {
|
||||
let d = |p: (f64, f64), q: (f64, f64)| (p.0 - q.0).hypot(p.1 - q.1);
|
||||
d(a.0, b.0) < 1e-9 && d(a.1, b.1) < 1e-9 && (a.2 - b.2).abs() < 1e-9
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stretch_moves_an_arc_end_inside_its_frame_and_keeps_its_height() {
|
||||
// Arc 34 runs from (36, 0), inside the frame (34,-2)-(38,2), over (28, 4) to (20, 0): 4 above
|
||||
// its chord. Arc 35 has both ends inside the frame. AutoCAD 2024 moves an arc's ends that are
|
||||
// inside the frame and keeps the arc's height over its chord: measured on its own stretched
|
||||
// copies of library blocks, where 21 arcs had one end moved and 63 had their ends moved by two
|
||||
// stretch actions by different amounts, all with their height unchanged to 1.1e-14 of it.
|
||||
let angle = |x: f64, y: f64| y.atan2(x).to_degrees();
|
||||
let src = fixture().replace("71 1\n330 30\n1010 40", "71 3\n330 30\n330 34\n330 35\n1010 40").replace(
|
||||
"21 25\n31 0\n0 ENDBLK\n",
|
||||
&format!(
|
||||
"21 25\n31 0\n{}{}0 ENDBLK\n",
|
||||
arc("34", 28.0, -6.0, 10.0, angle(8.0, 6.0), angle(-8.0, 6.0)),
|
||||
arc("35", 36.0, 0.0, 1.0, 0.0, 180.0)
|
||||
),
|
||||
);
|
||||
let (mut d, r) = door_reference(&src);
|
||||
cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap();
|
||||
// The chord is now (30, 0)-(20, 0), 10 long, and the arc still rises 4 over it: radius
|
||||
// (5² + 4²) / (2 × 4) = 5.125. Arc 35 moves whole.
|
||||
let got = arcs(&d, r);
|
||||
let want = [((30.0, 0.0), (20.0, 0.0), 5.125), ((31.0, 0.0), (29.0, 0.0), 1.0)];
|
||||
assert_eq!(got.len(), 2);
|
||||
for (g, w) in got.iter().zip(want) {
|
||||
assert!(near(*g, w), "{g:?} != {w:?}");
|
||||
}
|
||||
assert_eq!(shapes(&d, r)[0], [(0.0, 0.0), (30.0, 0.0)]);
|
||||
}
|
||||
|
||||
/// A HATCH `handle` of the door block, associative or not, with one straight loop through
|
||||
/// `points` made from boundary objects `sources`.
|
||||
fn hatch(handle: &str, associative: bool, points: &[(f64, f64)], sources: &[&str]) -> String {
|
||||
let mut s = format!(
|
||||
"0 HATCH\n5 {handle}\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbHatch\n10 0\n20 0\n30 0\n210 0\n220 0\n230 1\n2 ANSI31\n70 0\n71 {}\n91 1\n92 3\n72 0\n73 1\n93 {}\n",
|
||||
u8::from(associative),
|
||||
points.len()
|
||||
);
|
||||
for (x, y) in points {
|
||||
s.push_str(&format!("10 {x}\n20 {y}\n"));
|
||||
}
|
||||
s.push_str(&format!("97 {}\n", sources.len()));
|
||||
for h in sources {
|
||||
s.push_str(&format!("330 {h}\n"));
|
||||
}
|
||||
s.push_str("75 0\n76 1\n52 0\n41 1\n77 0\n78 0\n98 0\n");
|
||||
s
|
||||
}
|
||||
|
||||
/// The first loop of each hatch of reference `r`'s block, in its order.
|
||||
fn hatch_loops(d: &cadcraft_doc::Drawing, r: Handle) -> Vec<Vec<(f64, f64)>> {
|
||||
let b = d.block(&block_of(d, &r.hex())).unwrap();
|
||||
b.entities
|
||||
.iter()
|
||||
.filter_map(|e| match &e.kind {
|
||||
cadcraft_doc::EntityKind::Hatch(h) => Some(h.loops.first()?.vertices.iter().map(|v| (v.p.x, v.p.y)).collect()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_associative_hatch_follows_its_stretched_boundary() {
|
||||
// Lines 36 to 39 outline (20,-1)-(36,1). The stretch acts on 36 (bottom), 37 (right) and 38
|
||||
// (top), whose ends at x = 36 are inside the frame, and on hatch 3B. Hatches 3A and 3B are
|
||||
// associative with that outline (3A's second vertex 2e-10 off the lines' ends, as a file's
|
||||
// rounding leaves it); 3C is the same but not associative; 3D's third vertex is 5e-8 off the
|
||||
// lines' ends at (36, 1), more than a rounding (a billionth of 36); 3E's second edge is curved
|
||||
// (bulge 0.5), which its lines can't give back.
|
||||
// AutoCAD 2024 fills an associative hatch again from its stretched boundary: in its own
|
||||
// stretched copies of library blocks, such a hatch's vertices are where its lines' ends went,
|
||||
// whether the stretch acts on the hatch or not. The others keep their loops.
|
||||
let rect = [(20.0, -1.0), (36.0, -1.0), (36.0, 1.0), (20.0, 1.0)];
|
||||
let mut lines = String::new();
|
||||
for (h, a, b) in [("36", rect[0], rect[1]), ("37", rect[1], rect[2]), ("38", rect[2], rect[3]), ("39", rect[3], rect[0])] {
|
||||
lines.push_str(&format!(
|
||||
"0 LINE\n5 {h}\n330 1D\n100 AcDbEntity\n8 0\n100 AcDbLine\n10 {}\n20 {}\n30 0\n11 {}\n21 {}\n31 0\n",
|
||||
a.0, a.1, b.0, b.1
|
||||
));
|
||||
}
|
||||
let sources = ["36", "37", "38", "39"];
|
||||
let rounded = [(20.0, -1.0), (36.000_000_000_2, -1.0), (36.0, 1.0), (20.0, 1.0)];
|
||||
let off = [(20.0, -1.0), (36.0, -1.0), (36.0 - 5e-8, 1.0), (20.0, 1.0)];
|
||||
let curved = hatch("3E", true, &rect, &sources).replace("72 0\n", "72 1\n").replace("10 36\n20 -1\n", "10 36\n20 -1\n42 0.5\n");
|
||||
let hatches = [
|
||||
hatch("3A", true, &rounded, &sources),
|
||||
hatch("3B", true, &rect, &sources),
|
||||
hatch("3C", false, &rect, &sources),
|
||||
hatch("3D", true, &off, &sources),
|
||||
curved,
|
||||
];
|
||||
let src = fixture()
|
||||
.replace("71 1\n330 30\n1010 40", "71 5\n330 30\n330 36\n330 37\n330 38\n330 3B\n1010 40")
|
||||
.replace("21 25\n31 0\n0 ENDBLK\n", &format!("21 25\n31 0\n{lines}{}0 ENDBLK\n", hatches.concat()));
|
||||
let (mut d, r) = door_reference(&src);
|
||||
assert_eq!(hatch_loops(&d, r), [rounded.to_vec(), rect.to_vec(), rect.to_vec(), off.to_vec(), rect.to_vec()]);
|
||||
cadcraft_doc::dynblock::set_property(&mut d, r, "Width", "30").unwrap();
|
||||
let stretched = vec![(20.0, -1.0), (30.0, -1.0), (30.0, 1.0), (20.0, 1.0)];
|
||||
// 3B moved whole with the stretch, then took its boundary's points.
|
||||
assert_eq!(hatch_loops(&d, r), [stretched.clone(), stretched, rect.to_vec(), off.to_vec(), rect.to_vec()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_zero_distance_multiplier_moves_as_one_without_its_angle_offset() {
|
||||
// A stretch written with multiplier 0 and angle offset 1 (radian), and group 280 = 1. AutoCAD
|
||||
|
||||
Reference in New Issue
Block a user