Skip to content

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

Wave 304/509: STEP import: a circle edge between two vertices is the arc between them, however short (and 3 more)

jyviko01aaf872b036
diff --git a/crates/kernel/src/step_in/tests.rs b/crates/kernel/src/step_in/tests.rs
index 38395eb397..a79238ea8b 100644
--- a/crates/kernel/src/step_in/tests.rs
+++ b/crates/kernel/src/step_in/tests.rs
@@ -497,17 +497,18 @@ fn cube_entities() -> String {
for (i, (x, y, z)) in pts.iter().enumerate() {
s += &format!("#{}=CARTESIAN_POINT('',({x}.,{y}.,{z}.));\n#{}=VERTEX_POINT('',#{});\n", 200 + i, 210 + i, 200 + i);
}
- // 12 edges, each on the line through its two vertices (trimmed by them).
+ // 12 edges, each on the line through its two vertices (trimmed by them). Their directions
+ // and vectors are #340 to #363, clear of the ids the assembly fixtures add (#600 on).
let edges = [(0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7)];
for (k, (a, b)) in edges.iter().enumerate() {
let (p, q) = (pts[*a], pts[*b]);
let d = (q.0 - p.0, q.1 - p.1, q.2 - p.2);
- s += &format!("#{}=DIRECTION('',({}.,{}.,{}.));\n#{}=VECTOR('',#{},1.);\n", 600 + 2 * k, d.0, d.1, d.2, 601 + 2 * k, 600 + 2 * k);
+ s += &format!("#{}=DIRECTION('',({}.,{}.,{}.));\n#{}=VECTOR('',#{},1.);\n", 340 + 2 * k, d.0, d.1, d.2, 341 + 2 * k, 340 + 2 * k);
s += &format!(
"#{}=LINE('',#{},#{});\n#{}=EDGE_CURVE('',#{},#{},#{},.T.);\n",
300 + k,
200 + a,
- 601 + 2 * k,
+ 341 + 2 * k,
320 + k,
210 + a,
210 + b,
@@ -897,10 +898,13 @@ fn repeated_items_are_shared_and_one_use_items_are_not() {
assert!(boxes[1].bbox.min.dist(Vec3::new(90.0, 0.0, 0.0)) < 1e-6, "{:?}", boxes[1].bbox);
}
+/// A face on a surface the reader does not know is left out and named; the rest of the solid
+/// comes in as a surface body of the faces that read, with no "open body" warning on top (as
+/// `a_refused_solid_comes_in_as_a_surface_body_of_the_faces_that_read`).
#[test]
fn unsupported_face_is_left_out_and_reported() {
let mut t = String::from(HEAD);
- let cube = cube_entities().replace("#410=PLANE('',#409);", "#410=OFFSET_SURFACE('',#409,1.,.F.);");
+ let cube = cube_entities().replace("#410=PLANE('',#409);", "#410=CURVE_BOUNDED_SURFACE('',#409,(),.F.);");
t += &cube;
t += "#60=ADVANCED_BREP_SHAPE_REPRESENTATION('',(#101),#20);\n";
t += &context(20, "(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.))");
@@ -909,8 +913,11 @@ fn unsupported_face_is_left_out_and_reported() {
assert_eq!(imp.bodies.len(), 1);
assert!(!imp.bodies[0].closed);
assert_eq!(imp.bodies[0].body.face_count(), 5);
- assert!(imp.warnings.iter().any(|w| w.contains("OFFSET_SURFACE")), "{:?}", imp.warnings);
- assert!(imp.warnings.iter().any(|w| w.contains("open body")), "{:?}", imp.warnings);
+ assert!(imp.bodies[0].body.is_surface() && !imp.bodies[0].body.is_mesh());
+ let unsupported = "face #411: not supported yet: surface CURVE_BOUNDED_SURFACE";
+ assert!(imp.warnings.iter().any(|w| w.contains("'Cube': imported as surfaces: ") && w.contains(unsupported)), "{:?}", imp.warnings);
+ assert!(imp.warnings.iter().any(|w| w.contains("'Cube': left out: ") && w.contains(unsupported)), "{:?}", imp.warnings);
+ assert!(!imp.warnings.iter().any(|w| w.contains("open body")), "{:?}", imp.warnings);
}
#[test]
@@ -2477,3 +2484,82 @@ fn a_round_face_off_its_frames_quarter_lines_is_formed_within_one_turn() {
assert!(rel(m.area, area) < 1e-6, "{start}: area {} vs {area}", m.area);
}
}
+
+/// A circle edge between two vertices its file's uncertainty does not tell apart is the short arc
+/// between them, not a full turn (the engine assembly's water pump housing has a 20.4 um arc
+/// between two vertices in a part whose uncertainty is 20.5 um). A can whose wall is
+/// split by two rulings 0.6 um apart, under the default 1 um: read as turns, the sliver of wall
+/// between them wound once round the can with no second bound and the can came in as surfaces.
+#[test]
+fn a_circle_edge_between_two_vertices_closer_than_the_tolerance_is_the_arc_between_them() {
+ let (r, h) = (5.0f64, 10.0f64);
+ let delta = 0.6e-3 / r;
+ let (c, s) = (r * delta.cos(), r * delta.sin());
+ let text = format!(
+ "{HEAD}
+#1=CARTESIAN_POINT('',(0.,0.,0.));
+#2=DIRECTION('',(0.,0.,1.));
+#3=DIRECTION('',(1.,0.,0.));
+#5=AXIS2_PLACEMENT_3D('',#1,#2,#3);
+#6=CARTESIAN_POINT('',(0.,0.,{h:?}));
+#7=AXIS2_PLACEMENT_3D('',#6,#2,#3);
+#10=CARTESIAN_POINT('',({r:?},0.,0.));
+#11=CARTESIAN_POINT('',({c:?},{s:?},0.));
+#12=CARTESIAN_POINT('',({r:?},0.,{h:?}));
+#13=CARTESIAN_POINT('',({c:?},{s:?},{h:?}));
+#14=VERTEX_POINT('',#10);
+#15=VERTEX_POINT('',#11);
+#16=VERTEX_POINT('',#12);
+#17=VERTEX_POINT('',#13);
+#20=CIRCLE('',#5,{r:?});
+#21=CIRCLE('',#7,{r:?});
+#22=VECTOR('',#2,1.);
+#23=LINE('',#10,#22);
+#24=LINE('',#11,#22);
+#30=EDGE_CURVE('',#14,#15,#20,.T.);
+#31=EDGE_CURVE('',#15,#14,#20,.T.);
+#32=EDGE_CURVE('',#16,#17,#21,.T.);
+#33=EDGE_CURVE('',#17,#16,#21,.T.);
+#34=EDGE_CURVE('',#14,#16,#23,.T.);
+#35=EDGE_CURVE('',#15,#17,#24,.T.);
+#40=ORIENTED_EDGE('',*,*,#31,.F.);
+#41=ORIENTED_EDGE('',*,*,#30,.F.);
+#42=EDGE_LOOP('',(#40,#41));
+#43=FACE_OUTER_BOUND('',#42,.T.);
+#44=PLANE('',#5);
+#45=ADVANCED_FACE('Bottom',(#43),#44,.F.);
+#50=ORIENTED_EDGE('',*,*,#32,.T.);
+#51=ORIENTED_EDGE('',*,*,#33,.T.);
+#52=EDGE_LOOP('',(#50,#51));
+#53=FACE_OUTER_BOUND('',#52,.T.);
+#54=PLANE('',#7);
+#55=ADVANCED_FACE('Top',(#53),#54,.T.);
+#60=ORIENTED_EDGE('',*,*,#30,.T.);
+#61=ORIENTED_EDGE('',*,*,#35,.T.);
+#62=ORIENTED_EDGE('',*,*,#32,.F.);
+#63=ORIENTED_EDGE('',*,*,#34,.F.);
+#64=EDGE_LOOP('',(#60,#61,#62,#63));
+#65=FACE_OUTER_BOUND('',#64,.T.);
+#66=CYLINDRICAL_SURFACE('',#5,{r:?});
+#67=ADVANCED_FACE('Sliver',(#65),#66,.T.);
+#70=ORIENTED_EDGE('',*,*,#31,.T.);
+#71=ORIENTED_EDGE('',*,*,#34,.T.);
+#72=ORIENTED_EDGE('',*,*,#33,.F.);
+#73=ORIENTED_EDGE('',*,*,#35,.F.);
+#74=EDGE_LOOP('',(#70,#71,#72,#73));
+#75=FACE_OUTER_BOUND('',#74,.T.);
+#77=ADVANCED_FACE('Wall',(#75),#66,.T.);
+#80=CLOSED_SHELL('',(#45,#55,#67,#77));
+#81=MANIFOLD_SOLID_BREP('Can',#80);
+#82=SHAPE_REPRESENTATION('',(#81),$);
+{TAIL}"
+ );
+ let imp = step_import(&text).unwrap();
+ assert_eq!(imp.bodies.len(), 1, "{:?}", imp.warnings);
+ let can = &imp.bodies[0];
+ assert!(can.closed && can.refused.is_none() && !can.body.is_surface(), "{:?} {:?}", can.refused, imp.warnings);
+ let pi = std::f64::consts::PI;
+ let m = measure(&can.body).unwrap();
+ assert!(rel(m.volume, pi * r * r * h) < 1e-6, "{}", m.volume);
+ assert!(rel(m.area, 2.0 * pi * r * r + 2.0 * pi * r * h) < 1e-6, "{}", m.area);
+}
diff --git a/crates/smlib/src/pcurves.rs b/crates/smlib/src/pcurves.rs
index 720d36f6b6..3fd64a8f75 100644
--- a/crates/smlib/src/pcurves.rs
+++ b/crates/smlib/src/pcurves.rs
@@ -46,6 +46,8 @@ fn trims(face: &TrimFace<'_>) -> Result<Vec<Vec<Curve2>>, String> {
let once = ours.clone().find(|u| ours.clone().filter(|v| v.edge == u.edge).count() == 1).ok_or("a face of seam edges only")?;
let at = edge_of(once.edge)?;
let reversed = made_uses.iter().find(|m| m.edge as usize == at).ok_or("a use SMLib did not keep")?.forward != once.forward;
+ // Each edge's tolerance as SMLib held it (raised where it lies off the surface).
+ let held = arrays.reals.get(&crate::flat::EDGE_TOLERANCE).ok_or("edge tolerances")?;
let mut result = Vec::with_capacity(face.loops.len());
for l in &face.loops {
let mut pcs = Vec::with_capacity(l.len());
@@ -55,7 +57,8 @@ fn trims(face: &TrimFace<'_>) -> Result<Vec<Vec<Curve2>>, String> {
let ours = face.edges.get(u.edge as usize).ok_or("edge reference")?;
let found = made_uses.iter().find(|m| m.edge as usize == e && m.forward == (u.forward != reversed));
let pc = found.ok_or("a use SMLib did not keep")?.pcurve.clone();
- pcs.push(on_our_interval(pc, ours, edge.interval)?);
+ let tolerance = held.get(u.edge as usize).copied().unwrap_or(ours.tolerance).max(ours.tolerance);
+ pcs.push(on_our_interval(pc, ours, edge.interval, tolerance)?);
}
result.push(pcs);
}
@@ -97,12 +100,19 @@ fn cover_gaps(body: &crate::Brep, face: &TrimFace<'_>, bare: &HashSet<(u32, u32,
}
/// A pcurve SMLib made on its edge's interval `theirs`, on ours. SMLib may set an edge's end to
-/// a parameter of its own (a knot its vertex lies at, the end of the part on the face): where
-/// each end it chose is within the edge's tolerance of ours on the edge's curve, the pcurve is
-/// mapped onto our interval by the affine change of parameter (exact for a B-spline: its knots
-/// move), and the record's check measures it against the edge as any pcurve. Farther is SMLib
-/// trimming the edge, refused with how far.
-pub(crate) fn on_our_interval(pc: Curve2, ours: &solidcraft_geom::brep::pcurve::TrimEdge<'_>, theirs: [f64; 2]) -> Result<Curve2, String> {
+/// a parameter of its own (a knot its vertex lies at, the end of the part on the face: where an
+/// edge runs past its face's patch, SMLib ends it where its drop onto the patch ends, when that
+/// is within the edge's cross-section tolerance of its vertex): where each end it chose is
+/// within `tolerance` of ours on the edge's curve (the edge's tolerance as SMLib held it,
+/// [`cover_gaps`]), the pcurve is mapped onto our interval by the affine change of parameter
+/// (exact for a B-spline: its knots move), and the record's check measures it against the edge
+/// as any pcurve. Farther is SMLib trimming the edge, refused with how far.
+pub(crate) fn on_our_interval(
+ pc: Curve2,
+ ours: &solidcraft_geom::brep::pcurve::TrimEdge<'_>,
+ theirs: [f64; 2],
+ tolerance: f64,
+) -> Result<Curve2, String> {
let [a, b] = ours.interval;
let [c, d] = theirs;
if a == c && b == d {
@@ -111,10 +121,10 @@ pub(crate) fn on_our_interval(pc: Curve2, ours: &solidcraft_geom::brep::pcurve::
let curve = solidcraft_geom::brep::eval::CurveEval::new(ours.nurbs.unwrap_or(ours.curve)).map_err(|e| e.to_string())?;
let at = |t: f64| curve.point(t).ok_or_else(|| format!("the edge's curve has no point at {t}"));
let shift = at(a)?.dist(at(c)?).max(at(b)?.dist(at(d)?));
- if !(shift <= ours.tolerance) || !(d > c) {
+ if !(shift <= tolerance) || !(d > c) {
return Err(format!(
- "SMLib moved an edge's interval from {:?} to {:?}, its ends {shift} mm along its curve (tolerance {} mm)",
- ours.interval, theirs, ours.tolerance
+ "SMLib moved an edge's interval from {:?} to {:?}, its ends {shift} mm along its curve (tolerance {tolerance} mm)",
+ ours.interval, theirs
));
}
let map = |k: f64| {
diff --git a/crates/smlib/src/tests.rs b/crates/smlib/src/tests.rs
index c3251961ba..5125cc58e9 100644
--- a/crates/smlib/src/tests.rs
+++ b/crates/smlib/src/tests.rs
@@ -91,9 +91,11 @@ fn every_call_is_a_profiler_span_timed_by_the_c_api() {
assert!(names.contains(&"smlib_make_box") && names.contains(&"smlib_brep_volume"), "{r:?}");
}
-/// SMLib may end an edge at a parameter of its own (a knot its vertex lies at): within the
-/// edge's tolerance on its curve the pcurve is mapped onto our interval exactly; farther is SMLib
-/// trimming the edge, refused with how far.
+/// SMLib may end an edge at a parameter of its own (a knot its vertex lies at, where its drop onto
+/// the patch ends): within the edge's tolerance as SMLib held it, on its curve, the pcurve is
+/// mapped onto our interval exactly; farther is SMLib trimming the edge, refused with how far. An
+/// edge whose gap from its surface raised its tolerance (two of Poppy Humanoid's left thigh, SMLib
+/// ending them 0.1 um short) is held at that tolerance.
#[test]
fn a_pcurve_smlib_ends_within_the_edges_tolerance_is_mapped_onto_our_interval() {
use solidcraft_geom::brep::pcurve::TrimEdge;
@@ -102,12 +104,17 @@ fn a_pcurve_smlib_ends_within_the_edges_tolerance_is_mapped_onto_our_interval()
let edge = TrimEdge { curve: &line, nurbs: None, interval: [0.0, 1.0], vertices: [0, 1], tolerance: 1e-5 };
let theirs = [0.0, 1.0 - 2e-8];
let pc = Curve2::Spline { knots: vec![0.0, 0.0, theirs[1], theirs[1]], points: vec![[0.0, 0.0], [1.0, 0.0]] };
- let mapped = crate::pcurves::on_our_interval(pc.clone(), &edge, theirs).unwrap();
+ let mapped = crate::pcurves::on_our_interval(pc.clone(), &edge, theirs, edge.tolerance).unwrap();
assert_eq!(mapped.interval(), Some([0.0, 1.0]));
assert_eq!(mapped.eval(1.0), Some([1.0, 0.0]));
// 0.02 mm along the edge is past its 1e-5 mm.
- let err = crate::pcurves::on_our_interval(pc, &edge, [0.0, 0.99]).unwrap_err();
+ let err = crate::pcurves::on_our_interval(pc, &edge, [0.0, 0.99], edge.tolerance).unwrap_err();
assert!(err.contains("SMLib moved an edge's interval") && err.contains("tolerance"), "{err}");
+ // Held at 0.03 mm, as an edge 0.01 mm off its surface is, the same end is within it.
+ let short = Curve2::Spline { knots: vec![0.0, 0.0, 0.99, 0.99], points: vec![[0.0, 0.0], [1.0, 0.0]] };
+ let held = crate::pcurves::on_our_interval(short, &edge, [0.0, 0.99], 0.03).unwrap();
+ assert_eq!(held.interval(), Some([0.0, 1.0]));
+ assert_eq!(held.eval(1.0), Some([1.0, 0.0]));
}
/// An edge whose curve runs 2.5 um past its vertex and its face's patch (as Poppy Humanoid's
diff --git a/crates/step/src/topology.rs b/crates/step/src/topology.rs
index 6ec5da657d..ce440d6585 100644
--- a/crates/step/src/topology.rs
+++ b/crates/step/src/topology.rs
@@ -699,8 +699,11 @@ impl<'b, 'a> Builder<'b, 'a> {
let curve = self.edge_geometry(rid(p.get(3), "edge geometry")?, 0)?;
let (ps, pe) = (self.point(vs)?, self.point(ve)?);
let closed = vs == ve;
- let near = closed || v3(ps).dist(v3(pe)) <= self.tol;
- let mapped = map_edge(&curve, ps, pe, same_sense, closed, near, self.tol).map_err(|m| format!("edge #{id}: {m}"))?;
+ // A full turn when the edge has one vertex, or two at one point. Two vertices apart bound
+ // the arc between them, however short: a 20.4 um arc between vertices a 20.5 um uncertainty
+ // does not tell apart is that arc, not a turn around its face.
+ let full = closed || ps == pe;
+ let mapped = map_edge(&curve, ps, pe, same_sense, closed, full, self.tol).map_err(|m| format!("edge #{id}: {m}"))?;
if mapped.sense_mismatch {
self.sense_mismatches += 1;
}
@@ -1654,9 +1657,9 @@ fn turn(x: f64) -> f64 {
}
/// The edge from `ps` to `pe` on `curve`: its schema curve and interval (min → max) and which
-/// way it runs. `closed`: the same vertex at both ends; `near`: the ends coincide within the
-/// tolerance (a full turn on a periodic curve).
-fn map_edge(curve: &Curve, ps: P3, pe: P3, same_sense: bool, closed: bool, near: bool, tol: f64) -> R<Mapped> {
+/// way it runs. `closed`: the same vertex at both ends; `full`: the ends are one point (a full
+/// turn on a periodic curve).
+fn map_edge(curve: &Curve, ps: P3, pe: P3, same_sense: bool, closed: bool, full: bool, tol: f64) -> R<Mapped> {
match curve {
Curve::Line { origin, direction, magnitude } => {
if closed {
@@ -1675,11 +1678,11 @@ fn map_edge(curve: &Curve, ps: P3, pe: P3, same_sense: bool, closed: bool, near:
Ok(Mapped { curve: BCurve::Line { start, end }, interval: [0.0, hi - lo], with_step, sense_mismatch: with_step != same_sense })
}
Curve::Circle { position, radius } => {
- conic(BCurve::Circle { frame: frame(position), radius: *radius }, position, [*radius; 2], ps, pe, same_sense, near)
+ conic(BCurve::Circle { frame: frame(position), radius: *radius }, position, [*radius; 2], ps, pe, same_sense, full)
}
Curve::Ellipse { position, semi_axis_1, semi_axis_2 } => {
let radii = [*semi_axis_1, *semi_axis_2];
- conic(BCurve::Ellipse { frame: frame(position), radii }, position, radii, ps, pe, same_sense, near)
+ conic(BCurve::Ellipse { frame: frame(position), radii }, position, radii, ps, pe, same_sense, full)
}
Curve::BSpline(b) => open_or_closed(map_bspline_curve(b)?, ps, pe, same_sense, closed, tol),
Curve::Polyline(points) => {
@@ -1722,7 +1725,7 @@ fn open_conic(curve: &Curve, ps: P3, pe: P3, same_sense: bool, closed: bool, spl
/// A circle or ellipse: the vertices' angles, the arc between them in the edge's sense (a full
/// turn when they coincide).
-fn conic(curve: BCurve, position: &Placement, radii: [f64; 2], ps: P3, pe: P3, same_sense: bool, near: bool) -> R<Mapped> {
+fn conic(curve: BCurve, position: &Placement, radii: [f64; 2], ps: P3, pe: P3, same_sense: bool, full: bool) -> R<Mapped> {
let y = position.y();
let param = |p: P3| {
let q = sub(p, position.origin);
@@ -1730,7 +1733,7 @@ fn conic(curve: BCurve, position: &Placement, radii: [f64; 2], ps: P3, pe: P3, s
};
let (ts, te) = (param(ps), param(pe));
// Along the curve from `from` to `to`, increasing.
- let span = |from: f64, to: f64| if near { TAU } else { turn(to - from) };
+ let span = |from: f64, to: f64| if full { TAU } else { turn(to - from) };
let (lo, d) = if same_sense { (ts, span(ts, te)) } else { (te, span(te, ts)) };
if !(d > 0.0) {
return Err("a degenerate edge on a conic".into());