diff --git a/crates/kernel/src/step_in/conic_tests.rs b/crates/kernel/src/step_in/conic_tests.rs index f5942541a..05d4ddf47 100644 --- a/crates/kernel/src/step_in/conic_tests.rs +++ b/crates/kernel/src/step_in/conic_tests.rs @@ -18,6 +18,47 @@ const CONTEXT: &str = "#90=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#81),#91);\n#9 /// Height of the cone pieces: the 45° cone `x² + y² = z²` up to `z = H`. const H: f64 = 10.0; +#[test] +fn extruded_ellipse_seam_uses_profile_angles_for_oblique_sweeps() { + use mt::{MetricSpace, ParametricSurface}; + + let text = format!( + "{HEAD} +#1=CARTESIAN_POINT('',(10.,-20.,3.)); +#2=DIRECTION('',(0.,0.,1.)); +#3=DIRECTION('',(1.,0.,0.)); +#4=AXIS2_PLACEMENT_3D('',#1,#2,#3); +#5=ELLIPSE('',#4,9.,4.); +#6=DIRECTION('',(0.5,0.25,1.)); +#7=VECTOR('',#6,1.); +#8=SURFACE_OF_LINEAR_EXTRUSION('',#5,#7); +{TAIL}" + ); + let ex = p21::parse(&text).unwrap(); + let cx = Ctx { ex: &ex, len: 1.0, ang: 1.0, tol: 1e-6 }; + let origin = mt::Point3::new(10.0, -20.0, 3.0); + let d = mt::Vector3::new(0.5, 0.25, 1.0).normalize(); + let point = |angle: f64, h: f64| origin + mt::Vector3::new(9.0 * angle.cos(), 4.0 * angle.sin(), 0.0) + d * h; + let extent: Vec<_> = [-0.04, 0.0, 0.04].into_iter().flat_map(|a| [point(a, 0.0), point(a, 10.0)]).collect(); + for seam in [Vec::new(), vec![point(-0.04, 0.0), point(-0.04, 10.0)]] { + let surface = geom::surface(&cx, 8, &extent, &seam, true, 0).unwrap(); + for i in 0..=16 { + for j in 0..=4 { + let q = surface.subs(i as f64 / 16.0, j as f64 / 4.0) - origin; + let q = q - d * (q.z / d.z); + assert!(((q.x / 9.0).powi(2) + (q.y / 4.0).powi(2) - 1.0).abs() < 1e-12); + } + } + let start = surface.subs(0.0, 0.5); + let q = start - d * ((start.z - origin.z) / d.z); + if seam.is_empty() { + assert!(q.distance(point(std::f64::consts::PI, 0.0)) < 1e-9); + } else { + assert!(q.distance(point(-0.04, 0.0)) < 1e-9); + } + } +} + fn rel(a: f64, b: f64) -> f64 { (a - b).abs() / b.abs().max(1e-9) } diff --git a/crates/kernel/src/step_in/geom.rs b/crates/kernel/src/step_in/geom.rs index c0ecd7c26..f61bb09dc 100644 --- a/crates/kernel/src/step_in/geom.rs +++ b/crates/kernel/src/step_in/geom.rs @@ -1126,7 +1126,24 @@ pub(crate) fn surface(cx: &Ctx, id: u64, extent: &[mt::Point3], seam: &[mt::Poin let g = curve(cx, p.get(1).and_then(Param::as_ref_id).ok_or("extrusion without profile")?, depth + 1)?; let (_, v) = ent_of(cx, p.get(2), "extrusion vector")?; let d = direction(cx, v.params().get(1).and_then(Param::as_ref_id).ok_or("VECTOR without direction")?)?; - let nurbs: mt::NurbsCurve = match bounded_curve(&g, extent)? { + let profile = match &g { + CurveGeo::Conic { f, a, b } if d.dot(f.z).abs() > 1e-12 => { + // Keep the closed profile's parameter seam outside the trim. Otherwise a + // tooth tip crossing it can mesh as almost the whole extruded ellipse. + let dz = d.dot(f.z); + let angle = |p: &mt::Point3| { + let q = p - f.o; + let q = q - d * (q.dot(f.z) / dz); + (q.dot(f.y) / b).atan2(q.dot(f.x) / a) + }; + let start = common_angle(seam.iter().map(angle)) + .or_else(|| largest_gap(&extent.iter().map(angle).collect::>()).map(|(mid, _)| mid)) + .unwrap_or(0.0); + mt::Curve::NurbsCurve(conic_arc(f, *a, *b, start, TAU)) + } + _ => bounded_curve(&g, extent)?, + }; + let nurbs: mt::NurbsCurve = match profile { mt::Curve::Line(l) => mt::BSplineCurve::new(mt::KnotVec::bezier_knot(1), vec![l.0, l.1]).into(), mt::Curve::BSplineCurve(c) => c.into(), mt::Curve::NurbsCurve(c) => c, diff --git a/crates/kernel/src/step_in/tests.rs b/crates/kernel/src/step_in/tests.rs index 80cfb0d5e..c7c294c3a 100644 --- a/crates/kernel/src/step_in/tests.rs +++ b/crates/kernel/src/step_in/tests.rs @@ -1709,3 +1709,40 @@ fn explicit_outer_bound_crossing_a_closed_nurbs_seam_stays_trimmed() { assert!((mesh.measure().area - 42.0).abs() < 0.01, "sense={sense}: area {}", mesh.measure().area); } } + +#[test] +fn extruded_ellipse_patch_crossing_profile_seam_stays_trimmed() { + for (sense, outer) in [(true, true), (false, true), (true, false)] { + for radius in [9.0, 3000.0] { + let text = shallow_cylinder_patch_at_radius(sense, outer, radius); + let old = format!("#5=CYLINDRICAL_SURFACE('',#4,{radius});"); + let angle = 3.0 / radius; + let surface = format!( + "#5=SURFACE_OF_LINEAR_EXTRUSION('',#35,#18); +#35=ELLIPSE('',#36,{radius},{radius}); +#36=AXIS2_PLACEMENT_3D('',#1,#2,#37); +#37=DIRECTION('',({},{},0.));", + angle.cos(), + angle.sin() + ); + assert!(text.contains(&old)); + let text = text.replacen(&old, &surface, 1); + let imported = step_import(&text).unwrap(); + assert_eq!(imported.warnings, ["solid 'Strip': imported as an open body (the shell is not closed)"]); + assert_eq!(imported.bodies.len(), 1); + let body = &imported.bodies[0].body; + assert_eq!(imported.bodies[0].file_faces, 1); + let plan = body.display_plan(0.01).unwrap(); + for mesh in [ + plan.evaluate(), + body.display_mesh_angular(0.01, crate::DISPLAY_ANGLE).unwrap(), + body.reference_display_mesh_angular(0.01, crate::DISPLAY_ANGLE).unwrap(), + body.tessellate(0.01).unwrap(), + ] { + assert!(!mesh.triangles.is_empty()); + assert!(mesh.bounds().diagonal() < 10.0, "radius={radius}, sense={sense}, outer={outer}: {:?}", mesh.bounds()); + assert!((mesh.measure().area - 42.0).abs() < 0.1, "radius={radius}, sense={sense}, outer={outer}: area {}", mesh.measure().area); + } + } + } +} diff --git a/docs/step-import.md b/docs/step-import.md index 3e7ef7dc1..c87d84b39 100644 --- a/docs/step-import.md +++ b/docs/step-import.md @@ -36,6 +36,11 @@ Designs saved before imports made components keep their Import where it is (no c Read: manifold solid breps (with voids), faceted breps, shell-based surface models; planes, cylinders, cones, spheres, tori, B-spline and rational B-spline surfaces (with knots, Bézier, uniform, quasi-uniform), surfaces of revolution and linear extrusion, trimmed surfaces; lines, circles, ellipses, hyperbolas and parabolas (as exact rational curves, which export writes back as exact rational B-splines), B-spline curves, polylines, trimmed/surface/seam curves; length and angle units (SI prefixes, conversion-based units such as inches); product names (body names come from the solid's name, else the product's; `names` below chooses); surface colours; assemblies (next-assembly-usage occurrences placed by item-defined transformations, and mapped items) — the product tree is kept in the feature and becomes the import's components (above). Files are untrusted: sizes, entity counts and nesting are capped and every kernel call is guarded. What cannot be read becomes a warning on the feature (a face left out gives an open body); not read yet: offset surfaces, pcurve-only edges. +Extruded circular and elliptical faces keep their profile's parameter seam outside +the face trim, or on an explicit seam edge. Narrow faces such as rounded gear tooth +tips therefore stay narrow through the full thickness instead of wrapping around +the supporting ellipse in the middle. + ## Files from other CAD systems Files from different systems differ in which way is up and in what they call their solids. `doc.open`, `file.insert_step` and the MCP `open` tool take two options for STEP and IGES files; without them a file comes in as written.