DXF import: an elliptical arc comes in as exact conic arcs, not a fitted spline (#8155) (#8241)

* claim: #8155

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019FAWPuFoh9qxn2uS2j53QZ

* DXF import: an elliptical arc comes in as exact conic arcs (#8155)

A partial ELLIPSE (how Fusion, and SolidCraft's own DXF export, write an
elliptical arc) was read as a degree-3 spline fitted through 16 samples,
which strays from the ellipse between them: 0.84 mm on a 300-degree arc
of a 200 x 40 mm ellipse. It is now read as exact rational-quadratic
conic pieces of at most a quarter turn each, which the sketch keeps as
conics. A conic arc written by SolidCraft's DXF export now reads back as
the same conic.

The arc's sweep is taken with rem_euclid instead of a loop that added a
turn at a time, which ran for billions of turns on a start angle of 1e11.

sketch.drawing_bounds and the width/height sizing of sketch.insert_dxf
measure a conic by points along it, not by its apex, which can lie
outside the curve.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019FAWPuFoh9qxn2uS2j53QZ

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
austin62jake
2026-10-28 16:56:17 +00:00
committed by GitHub
parent 95ba99d710
commit 86342b8c5c
3 changed files with 113 additions and 20 deletions
+32
View File
@@ -152,6 +152,12 @@ fn bounds_of(geom: &[Geom2]) -> Option<(Vec2, Vec2)> {
pts.extend([*c + Vec2::new(r, r), *c - Vec2::new(r, r)]);
}
Geom2::Spline { pts: sp, .. } => pts.extend(sp.iter().copied()),
// Points along the curve, not its apex, which can lie well outside it (an elliptical
// arc of a DXF comes in as conics, #8155).
Geom2::Conic { a, apex, b, rho } if *rho > 0.0 && *rho < 1.0 => {
let seg = solidcraft_geom::Seg2::Conic { a: *a, apex: *apex, b: *b, w: rho / (1.0 - rho) };
pts.extend((0..=64).map(|i| seg.point_at(f64::from(i) / 64.0)));
}
Geom2::Conic { a, apex, b, .. } => pts.extend([*a, *apex, *b]),
Geom2::Text { at, .. } => pts.push(*at),
}
@@ -352,6 +358,32 @@ mod tests {
}
}
/// #8155: an elliptical arc of a DXF (as Fusion writes one) comes into the sketch as exact
/// conics, and the drawing's size is the arc's own, not the box round the conics' apexes.
#[test]
fn an_elliptical_arc_comes_in_as_conics_at_its_own_size() {
// Centre (0, 0), major radius 10 along X, minor 5, from -45° to 45°: one quarter turn,
// whose apex is at x = 10 / cos 45° = 14.1, past the arc's end at x = 10.
let dxf = format!(
"0\nSECTION\n2\nENTITIES\n0\nELLIPSE\n10\n0\n20\n0\n11\n10\n21\n0\n40\n0.5\n41\n{}\n42\n{}\n0\nENDSEC\n0\nEOF\n",
-std::f64::consts::FRAC_PI_4,
std::f64::consts::FRAC_PI_4
);
let mut s = Session::default();
let b = s.execute("sketch.drawing_bounds", &json!({"text": dxf, "format": "dxf"})).unwrap();
let (w, h) = (b["width"].as_f64().unwrap(), b["height"].as_f64().unwrap());
let half = std::f64::consts::FRAC_1_SQRT_2;
assert!((w - 10.0 * (1.0 - half)).abs() < 1e-3 && (h - 10.0 * half).abs() < 1e-3, "{b}");
let r = s.execute("sketch.insert_dxf", &json!({"text": dxf})).unwrap();
assert_eq!(r["curves"], 1, "{r}");
let st = s.model.state();
let sk = &st.sketch(r["sketch"].as_u64().unwrap()).unwrap().sketch;
assert!(matches!(sk.curves[0].kind, solidcraft_sketch::CurveKind::Conic { .. }), "{:?}", sk.curves[0].kind);
// Every point the sketch draws it with lies on the ellipse.
let worst = sk.polyline(0).iter().map(|p| ((p.x / 10.0).powi(2) + (p.y / 5.0).powi(2) - 1.0).abs()).fold(0.0, f64::max);
assert!(worst < 1e-9, "{worst}");
}
const PLATE: &str =
r#"<svg xmlns="http://www.w3.org/2000/svg" width="20mm" height="10mm" viewBox="0 0 20 10"><rect x="0" y="0" width="20" height="10"/></svg>"#;