mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-01 19:03:56 +01:00
Merge pull request #2782 from 2ez4gcx/fix/arraypath-closed-spacing
ARRAYPATH: no extra item where a closed path closes, by spacing
This commit is contained in:
2 files changed
+33
-1
No files matched your search
@@ -26,7 +26,13 @@ pub(crate) fn arraypath(
|
||||
return Err(other("Select objects to array."));
|
||||
}
|
||||
let (n, step) = match (count, spacing) {
|
||||
(_, Some(sp)) if sp.is_finite() && sp > 0.0 => (((l / sp).floor() as usize).saturating_add(1), sp),
|
||||
// As AutoCAD 2024 fills a path by distance: floor(length / spacing) items on a closed path,
|
||||
// so none lands on or near the first where the path closes, and one more, at the end, on
|
||||
// an open one (measured). The 1e-9 lets a length that comes out a hair short still fit.
|
||||
(_, Some(sp)) if sp.is_finite() && sp > 0.0 => {
|
||||
let fit = (l / sp + 1e-9).floor() as usize;
|
||||
(if c.closed { fit.max(1) } else { fit.saturating_add(1) }, sp)
|
||||
}
|
||||
(Some(n), _) => {
|
||||
let n = n.max(1);
|
||||
(n, if c.closed { l / n as f64 } else { l / (n.max(2) - 1) as f64 })
|
||||
|
||||
@@ -639,6 +639,32 @@ fn arraypath_json_and_command_line() {
|
||||
assert!(matches!(last(&s), EntityKind::Line(l) if near(l.a.xy(), -5.0, 20.0) && near(l.b.xy(), -6.0, 20.0)));
|
||||
assert!(s.execute("arraypath", &json!({"handles": [dot], "path": dot})).is_err());
|
||||
|
||||
// By spacing, AutoCAD 2024 fills a closed path with floor(length / spacing) items and an open
|
||||
// one with one more, at the end (measured on a 100 square and on the same square open).
|
||||
let square = [[0, 0], [100, 0], [100, 100], [0, 100]];
|
||||
let r400 = 400.0 / std::f64::consts::TAU;
|
||||
for (closed, spacing, items) in
|
||||
[(true, 100, 4), (true, 30, 13), (true, 200, 2), (true, 400, 1), (true, 399, 1), (false, 100, 4), (false, 30, 11), (false, 300, 2)]
|
||||
{
|
||||
let mut s = Session::new();
|
||||
let dot = h(&s.execute("circle", &json!({"center": [0, 0], "radius": 1})).unwrap());
|
||||
let path = h(&s.execute("pline", &json!({"points": square, "closed": closed})).unwrap());
|
||||
let r = s.execute("arraypath", &json!({"handles": [dot], "path": path, "spacing": spacing})).unwrap();
|
||||
assert_eq!(r["handles"].as_array().map(Vec::len), Some(items - 1), "closed {closed}, spacing {spacing}");
|
||||
}
|
||||
// A circle 400 round: 4 items at 100, none on the first (AutoCAD 2024, measured).
|
||||
let mut s = Session::new();
|
||||
let dot = h(&s.execute("circle", &json!({"center": [0, 0], "radius": 1})).unwrap());
|
||||
let path = h(&s.execute("circle", &json!({"center": [0, r400], "radius": r400})).unwrap());
|
||||
let r = s.execute("arraypath", &json!({"handles": [dot], "path": path, "spacing": 100})).unwrap();
|
||||
assert_eq!(r["handles"].as_array().map(Vec::len), Some(3));
|
||||
// Three sides of 0.35 add up to a hair under 3 × 0.35; the item at the end still fits.
|
||||
let mut s = Session::new();
|
||||
let dot = h(&s.execute("circle", &json!({"center": [0, 0], "radius": 0.01})).unwrap());
|
||||
let path = h(&s.execute("pline", &json!({"points": [[0, 0], [0.35, 0], [0.35, 0.35], [0, 0.35]]})).unwrap());
|
||||
let r = s.execute("arraypath", &json!({"handles": [dot], "path": path, "spacing": 0.35})).unwrap();
|
||||
assert_eq!(r["handles"].as_array().map(Vec::len), Some(3));
|
||||
|
||||
let mut s = Session::new();
|
||||
s.execute("circle", &json!({"center": [0, 1], "radius": 0.25})).unwrap();
|
||||
s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap();
|
||||
|
||||
Reference in new issue
Block a user