mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
A closed spline through fit points with chord-length knots and no fit tolerance is now the cubic AutoCAD 2024 builds (measured with accoreconsole): the open chord-length interpolation through the fit points and back to the first, clamped, with the same unit first derivative at both ends. That tangent is the derivative at the first point of the cubic through the first four points of the loop, with square-root-of-chord parameters. AutoCAD flags this spline periodic (group 70 bit 2) although its knots are clamped: it sets the bit from the curve, the same first derivative where it closes. `Spline::is_periodic` now means that (closed and smooth where it closes); the knot shape it meant before is `Spline::wraps` (unclamped knots, control points wrapping round), which SPLINEDIT uses for its control-point edits. DXF: AutoCAD rebuilds a spline with fit data from its fit points and end tangents, and opened a closed spline written with its fit points once as an open spline. The writer now adds the first fit point again at the end and the tangent where the curve closes as 12 and 13, as AutoCAD writes it; the reader drops the closing point and takes the tangent from the curve. AutoCAD reads the written spline back as its own (70 = 1067, the same control points). Closed splines with square-root or uniform knots, or a fit tolerance, are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
2.9 KiB
Rust
70 lines
2.9 KiB
Rust
//! The remaining object snaps (#519, gaps.md U8): Insertion on attribute definitions and
|
|
//! multileaders, Geometric Center on closed splines, and no Midpoint on closed curves.
|
|
|
|
use cadcraft_doc::{EntityKind, Space};
|
|
use cadcraft_engine::Session;
|
|
use cadcraft_engine::snap::{self, mode};
|
|
use cadcraft_geom::Vec2;
|
|
use serde_json::json;
|
|
|
|
/// The snap of `modes` near `p` (a hair off it, inside a 0.1 aperture).
|
|
fn snap_at(s: &Session, p: Vec2, modes: u32) -> Option<(Vec2, u32)> {
|
|
snap::osnap(s.doc().unwrap(), &Space::Model, p + Vec2::new(0.02, -0.01), 0.1, modes, None, false).map(|h| (h.point, h.mode))
|
|
}
|
|
|
|
fn near(a: Option<(Vec2, u32)>, p: Vec2, m: u32) -> bool {
|
|
a.is_some_and(|(q, k)| k == m && q.dist(p) < 1e-6)
|
|
}
|
|
|
|
#[test]
|
|
fn insertion_snaps_to_attribute_definitions() {
|
|
let mut s = Session::new();
|
|
s.execute("attdef", &json!({"tag": "PART", "default": "A-1", "at": [3, 4]})).unwrap();
|
|
assert!(near(snap_at(&s, Vec2::new(3.0, 4.0), mode::INS), Vec2::new(3.0, 4.0), mode::INS));
|
|
}
|
|
|
|
#[test]
|
|
fn insertion_snaps_to_a_multileader_text() {
|
|
let mut s = Session::new();
|
|
s.execute("mleader", &json!({"points": [[0, 0], [10, 5]], "text": "NOTE"})).unwrap();
|
|
let at = s
|
|
.doc()
|
|
.unwrap()
|
|
.model
|
|
.iter()
|
|
.find_map(|e| match &e.kind {
|
|
EntityKind::MLeader(m) => m.text.as_ref().map(|t| t.insert.xy()),
|
|
_ => None,
|
|
})
|
|
.expect("a multileader with text");
|
|
assert!(near(snap_at(&s, at, mode::INS), at, mode::INS));
|
|
}
|
|
|
|
#[test]
|
|
fn geometric_center_of_a_closed_spline() {
|
|
let mut s = Session::new();
|
|
// A closed fit spline through the corners of a square: the centroid of its area, as AutoCAD
|
|
// 2024 gives it (MASSPROP of its region; the curve is not symmetric, its tangent where it
|
|
// closes comes from the first points).
|
|
s.execute("spline", &json!({"fit": [[0, 5], [5, 0], [10, 5], [5, 10]], "closed": true})).unwrap();
|
|
let c = Vec2::new(5.02693946, 5.04916453);
|
|
let hit = snap::osnap(s.doc().unwrap(), &Space::Model, c + Vec2::new(0.05, 0.0), 0.5, mode::GCEN, None, false).expect("a geometric center");
|
|
assert_eq!(hit.mode, mode::GCEN);
|
|
assert!(hit.point.dist(c) < 1e-5, "{:?}", hit.point);
|
|
// An open spline has none.
|
|
let mut s = Session::new();
|
|
s.execute("spline", &json!({"fit": [[0, 5], [5, 0], [10, 5], [5, 10]]})).unwrap();
|
|
assert!(snap::osnap(s.doc().unwrap(), &Space::Model, c, 0.5, mode::GCEN, None, false).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn closed_curves_have_no_midpoint() {
|
|
// As in AutoCAD: Midpoint works on arcs and elliptical arcs, not on a full circle or ellipse.
|
|
let mut s = Session::new();
|
|
s.cmdline("circle 0,0 5").unwrap();
|
|
s.cmdline("ellipse 20,0 30,0 3").unwrap();
|
|
for p in [Vec2::new(-5.0, 0.0), Vec2::new(5.0, 0.0), Vec2::new(10.0, 0.0), Vec2::new(30.0, 0.0), Vec2::new(20.0, 3.0)] {
|
|
assert_eq!(snap_at(&s, p, mode::MID), None, "{p:?}");
|
|
}
|
|
}
|