Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

IGES import: a revolved arc is a cap, not a whole sphere (#3806) (#3813)

bherbruck687caad24c18
diff --git a/crates/kernel/src/iges_in.rs b/crates/kernel/src/iges_in.rs
index 845579b00a..da387dad3c 100644
--- a/crates/kernel/src/iges_in.rs
+++ b/crates/kernel/src/iges_in.rs
@@ -376,6 +376,42 @@ impl Nurbs {
}
}
+/// A circular arc in its definition plane (at height `c[2]`), counter-clockwise from `a0` to
+/// `a1` (radians, `a1 > a0`).
+struct Arc {
+ c: V3,
+ rad: f64,
+ a0: f64,
+ a1: f64,
+}
+
+impl Arc {
+ /// The arc placed by `x`, as an exact rational quadratic B-spline (pieces of at most a
+ /// quarter turn).
+ fn nurbs(&self, x: &Xf) -> Nurbs {
+ let sweep = self.a1 - self.a0;
+ let pieces = (sweep / std::f64::consts::FRAC_PI_2).ceil().clamp(1.0, 8.0) as usize;
+ let step = sweep / pieces as f64;
+ let w = (step / 2.0).cos();
+ let on = |t: f64, k: f64| x.point([self.c[0] + k * t.cos(), self.c[1] + k * t.sin(), self.c[2]]);
+ let mut pts = Vec::with_capacity(2 * pieces + 1);
+ let mut knots = vec![0.0; 3];
+ for i in 0..pieces {
+ let t = self.a0 + step * i as f64;
+ let (p, m) = (on(t, self.rad), on(t + step / 2.0, self.rad / w));
+ pts.push([p[0], p[1], p[2], 1.0]);
+ pts.push([m[0] * w, m[1] * w, m[2] * w, w]);
+ if i > 0 {
+ knots.extend([i as f64 / pieces as f64; 2]);
+ }
+ }
+ let p = on(self.a1, self.rad);
+ pts.push([p[0], p[1], p[2], 1.0]);
+ knots.extend([1.0; 3]);
+ Nurbs { deg: 2, knots, pts }
+ }
+}
+
struct Tr<'a> {
f: &'a Iges,
o: Out,
@@ -489,20 +525,8 @@ impl<'a> Tr<'a> {
Ok(vec![Seg::along(c, |t| add(a, mul(sub(b, a), t)))])
}
100 => {
- let u = self.f.unit;
- let zt = self.n(seq, 1)? * u;
- let c = [self.n(seq, 2)? * u, self.n(seq, 3)? * u, zt];
- let s = [self.n(seq, 4)? * u, self.n(seq, 5)? * u, zt];
- let e = [self.n(seq, 6)? * u, self.n(seq, 7)? * u, zt];
- let rad = norm(sub(s, c));
- if !(rad > 0.0) {
- return Err(format!("entity {seq}: arc of zero radius"));
- }
- let a0 = (s[1] - c[1]).atan2(s[0] - c[0]);
- let mut a1 = (e[1] - c[1]).atan2(e[0] - c[0]);
- while a1 <= a0 + 1e-12 {
- a1 += std::f64::consts::TAU;
- }
+ let Arc { c, rad, a0, a1 } = self.arc(seq)?;
+ let zt = c[2];
let at = |t: f64| x.point([c[0] + rad * t.cos(), c[1] + rad * t.sin(), zt]);
let z = unit(x.dir([0.0, 0.0, 1.0])).ok_or("degenerate arc frame")?;
let xa = unit(x.dir([1.0, 0.0, 0.0])).ok_or("degenerate arc frame")?;
@@ -571,6 +595,56 @@ impl<'a> Tr<'a> {
}
}
+ /// A circular arc (100) in its definition plane, in millimetres.
+ fn arc(&self, seq: i64) -> R<Arc> {
+ let u = self.f.unit;
+ let zt = self.n(seq, 1)? * u;
+ let c = [self.n(seq, 2)? * u, self.n(seq, 3)? * u, zt];
+ let s = [self.n(seq, 4)? * u, self.n(seq, 5)? * u, zt];
+ let e = [self.n(seq, 6)? * u, self.n(seq, 7)? * u, zt];
+ let rad = norm(sub(s, c));
+ if !(rad > 0.0) {
+ return Err(format!("entity {seq}: arc of zero radius"));
+ }
+ let a0 = (s[1] - c[1]).atan2(s[0] - c[0]);
+ let mut a1 = (e[1] - c[1]).atan2(e[0] - c[0]);
+ while a1 <= a0 + 1e-12 {
+ a1 += std::f64::consts::TAU;
+ }
+ Ok(Arc { c, rad, a0, a1 })
+ }
+
+ /// A surface of revolution (120) whose generatrix is the circular arc `seq`, about the axis
+ /// through `o` along `d`. A STEP surface of revolution turns the whole of its curve, and the
+ /// whole circle turns into a whole sphere or torus (a sphere twice over when the arc is
+ /// centred on the axis): the face's trim cannot pick its piece out of that, and the face
+ /// meshed as the full ball. An arc in a plane through the axis is a sphere or a torus; any
+ /// other arc turns as an exact rational B-spline of just the arc.
+ fn revolved_arc(&mut self, seq: i64, xf: &Xf, o: V3, d: V3) -> R<u64> {
+ let x = xf.then(&self.own_xf(seq, 0)?);
+ let arc = self.arc(seq)?;
+ let n = unit(x.dir([0.0, 0.0, 1.0])).ok_or("degenerate arc frame")?;
+ let (centre, rad) = (x.point(arc.c), arc.rad * x.scale());
+ let off = sub(centre, o);
+ let h = dot(off, d);
+ let rho = norm(sub(off, mul(d, h)));
+ let tol = self.tol.max(rad * 1e-9);
+ if dot(n, d).abs() < 1e-7 && dot(off, n).abs() <= tol {
+ let ax = self.o.placement(add(o, mul(d, h)), d, perp(d));
+ if rho <= tol {
+ return Ok(self.o.add(format!("SPHERICAL_SURFACE('',#{ax},{})", r(rad))));
+ }
+ if rad < rho {
+ return Ok(self.o.add(format!("TOROIDAL_SURFACE('',#{ax},{},{})", r(rho), r(rad))));
+ }
+ }
+ let nb = arc.nurbs(&x);
+ let curve = self.write_nurbs_curve(&nb);
+ let (pa, dd) = (self.o.point(o), self.o.dir(d));
+ let a1 = self.o.add(format!("AXIS1_PLACEMENT('',#{pa},#{dd})"));
+ Ok(self.o.add(format!("SURFACE_OF_REVOLUTION('',#{curve},#{a1})")))
+ }
+
fn nurbs_curve(&self, seq: i64, x: &Xf) -> R<Nurbs> {
let k = self.i(seq, 1)?;
let m = self.i(seq, 2)?;
@@ -784,6 +858,9 @@ impl<'a> Tr<'a> {
let ax_x = x.then(&self.own_xf(axis, 0)?);
let (a, b) = (ax_x.point(self.pt(axis, 1)?), ax_x.point(self.pt(axis, 4)?));
let d = unit(sub(b, a)).ok_or("zero-length revolution axis")?;
+ if self.de(generatrix)?.typ == 100 {
+ return self.revolved_arc(generatrix, &x, a, d);
+ }
let segs = self.curve(generatrix, &x, 1)?;
let [seg] = segs.as_slice() else { return Err(format!("entity {seq}: composite generatrix is not read")) };
let (pa, dd) = (self.o.point(a), self.o.dir(d));
diff --git a/crates/kernel/src/iges_in/tests.rs b/crates/kernel/src/iges_in/tests.rs
index c071ada373..6b8052ef08 100644
--- a/crates/kernel/src/iges_in/tests.rs
+++ b/crates/kernel/src/iges_in/tests.rs
@@ -85,10 +85,16 @@ fn hostile_iges_never_panics() {
/// An IGES file from (type, form, parameters) entities, in order (pointers are 2k + 1).
fn iges_file(ents: &[(i64, i64, String)]) -> String {
+ let placed: Vec<(i64, i64, String, i64)> = ents.iter().map(|(t, f, p)| (*t, *f, p.clone(), 0)).collect();
+ iges_file_placed(&placed)
+}
+
+/// Like [`iges_file`], with each entity's transformation matrix pointer (0 for none).
+fn iges_file_placed(ents: &[(i64, i64, String, i64)]) -> String {
let mut out = vec![format!("{:<72}S{:>7}", "test", 1), format!("{:<72}G{:>7}", "1H,,1H;,,,,,,,,,,,1.,2,2HMM,,,,1.E-6;", 1)];
let mut p_lines: Vec<(usize, String)> = Vec::new();
let mut starts = Vec::new();
- for (k, (t, _, p)) in ents.iter().enumerate() {
+ for (k, (t, _, p, _)) in ents.iter().enumerate() {
starts.push(p_lines.len() + 1);
let text = format!("{t},{p};");
let chars: Vec<char> = text.chars().collect();
@@ -96,9 +102,9 @@ fn iges_file(ents: &[(i64, i64, String)]) -> String {
p_lines.push((2 * k + 1, c.iter().collect()));
}
}
- for (k, (t, form, _)) in ents.iter().enumerate() {
+ for (k, (t, form, _, xf)) in ents.iter().enumerate() {
let count = p_lines.iter().filter(|(d, _)| *d == 2 * k + 1).count();
- out.push(format!("{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}D{:>7}", t, starts[k], 0, 0, 0, 0, 0, 0, "00000000", 2 * k + 1));
+ out.push(format!("{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}D{:>7}", t, starts[k], 0, 0, 0, 0, xf, 0, "00000000", 2 * k + 1));
out.push(format!("{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}{:>8}D{:>7}", t, 0, 0, count, form, 0, 0, "", 0, 2 * k + 2));
}
for (i, (d, l)) in p_lines.iter().enumerate() {
@@ -108,6 +114,48 @@ fn iges_file(ents: &[(i64, i64, String)]) -> String {
out.join("\n")
}
+/// A spherical cap written the way analytic IGES exports write one: a surface of revolution
+/// (120) whose generatrix is a short circular arc (100) centred on the axis, turned into the
+/// meridian plane by its transformation matrix (124), trimmed by a circle round the axis. The arc
+/// is only the part of the circle the cap needs; read as the whole circle, the revolution was a
+/// whole sphere twice over and the cap meshed as the full ball.
+#[test]
+fn an_arc_revolved_about_its_centre_is_a_cap_not_a_ball() {
+ let (r, polar) = (50.0f64, 30f64.to_radians());
+ let (rim_r, rim_z) = (r * polar.sin(), r * polar.cos());
+ let ents: Vec<(i64, i64, String, i64)> = vec![
+ // 1: the meridian plane: local x stays x, local y becomes z.
+ (124, 0, "1.,0.,0.,0.,0.,0.,-1.,0.,0.,1.,0.,0.".into(), 0),
+ // 3: the axis, 5: the arc from the rim (60° in its plane) to the pole (90°).
+ (110, 0, "0.,0.,0.,0.,0.,1.".into(), 0),
+ (100, 0, format!("0.,0.,0.,{:?},{:?},0.,{r:?}", rim_r, rim_z), 1),
+ // 7: the surface, 9: the rim (a full circle at the cap's height), 11: on the surface.
+ (120, 0, format!("3,5,0.,{:?}", std::f64::consts::TAU), 0),
+ (100, 0, format!("{rim_z:?},0.,0.,{rim_r:?},0.,{rim_r:?},0."), 0),
+ (142, 0, "0,7,0,9,2".into(), 0),
+ // 13: the cap.
+ (144, 0, "7,1,0,11".into(), 0),
+ ];
+ let imp = iges_import(&iges_file_placed(&ents)).unwrap();
+ assert_eq!(imp.bodies.len(), 1, "{:?}", imp.warnings);
+ let mesh = imp.bodies[0].body.tessellate(0.01).unwrap();
+ let (mut lo, mut hi) = ([f64::MAX; 3], [f64::MIN; 3]);
+ for p in &mesh.positions {
+ for (k, x) in [p.x, p.y, p.z].into_iter().enumerate() {
+ lo[k] = lo[k].min(x);
+ hi[k] = hi[k].max(x);
+ }
+ }
+ for k in 0..2 {
+ assert!((lo[k] + rim_r).abs() < 0.1 && (hi[k] - rim_r).abs() < 0.1, "cap is {lo:?} .. {hi:?}");
+ }
+ assert!((lo[2] - rim_z).abs() < 0.1 && (hi[2] - r).abs() < 0.1, "cap is {lo:?} .. {hi:?}");
+ // Area of a cap: 2π r h.
+ let area = measure(&imp.bodies[0].body).unwrap().area;
+ let cap = std::f64::consts::TAU * r * (r - rim_z);
+ assert!(rel(area, cap) < 1e-3, "area {area} vs {cap}");
+}
+
/// A unit-cube-like box (10 × 20 × 30) made of six trimmed bilinear B-spline patches whose
/// boundaries are lines: sewn into a closed solid.
#[test]