Skip to content

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

STEP import: a surface model of open shells, some closed, beside a turned sheet imports again (#13226)

bherbruck1e6b1ec20fc7
diff --git a/crates/kernel/src/step_in/parallel.rs b/crates/kernel/src/step_in/parallel.rs
index 378324e921..3b0e74c0d0 100644
--- a/crates/kernel/src/step_in/parallel.rs
+++ b/crates/kernel/src/step_in/parallel.rs
@@ -161,7 +161,16 @@ fn smlib_body(record: &solidcore_record::brep::Brep) -> std::result::Result<(Bod
let (body, body_order) = crate::native::body_from_smlib_ordered(&brep).map_err(failed)?;
// SMLib's face i is the record's `smlib_order` face and the body's `body_order` face (an
// empty order is the same one).
- let (Some(smlib_order), Some(body_order)) = (smlib_order, body_order) else { return Err(renumbered()) };
+ let Some(body_order) = body_order else { return Err(renumbered()) };
+ // SMLib's export may number the edges and vertices otherwise than the record (a surface model
+ // whose open shell uses faces against their surfaces beside closed shells: SMLib walks those
+ // faces as their surfaces face). An import still lists SMLib's faces in the record's order;
+ // that is taken where each face is checked to be the record's face it would be.
+ let smlib_order = match smlib_order {
+ Some(order) => order,
+ None if faces_in_record_order(record, body.record(), &body_order.faces) => Default::default(),
+ None => return Err(renumbered()),
+ };
let at = |order: &[usize], i: usize| if order.is_empty() { Some(i) } else { order.get(i).copied() };
let faces = brep.counts().map_err(|e| failed(KernelError::Failed(format!("SMLib: {e}"))))?[0];
let mut of_record = vec![usize::MAX; body.face_count()];
@@ -177,6 +186,44 @@ fn smlib_body(record: &solidcore_record::brep::Brep) -> std::result::Result<(Bod
Ok((body, of_record))
}
+/// Whether SMLib's face `i` of a body made from `record` (the body's face `body_order[i]`, or `i`
+/// for an empty order) is the record's face `i`, face by face: the same kind of surface, the same
+/// loops of the same lengths, each loop through the same corners (within the vertices' and the
+/// records' tolerances, whichever way and from wherever SMLib walks it).
+fn faces_in_record_order(record: &solidcore_record::brep::Brep, body: &solidcore_record::brep::Brep, body_order: &[usize]) -> bool {
+ use solidcore_record::brep::{Brep, Loop};
+ if record.faces.len() != body.faces.len() || !(body_order.is_empty() || body_order.len() == body.faces.len()) {
+ return false;
+ }
+ // Each loop's corners: the start of each of its edges as the loop walks them, with its tolerance.
+ let corners = |b: &Brep, l: &Loop| -> Option<Vec<([f64; 3], f64)>> {
+ let vertex = |v: u32| b.vertices.get(usize::try_from(v).ok()?).map(|x| (x.point, x.tolerance));
+ match l {
+ Loop::Vertex { vertex: v } => Some(vec![vertex(*v)?]),
+ Loop::Edges { edges } => edges
+ .iter()
+ .map(|e| {
+ let edge = b.edges.get(usize::try_from(e.edge).ok()?)?;
+ vertex(if e.forward { edge.vertices[0] } else { edge.vertices[1] })
+ })
+ .collect(),
+ }
+ };
+ let slack = record.tolerance + body.tolerance;
+ (0..record.faces.len()).all(|i| {
+ let at = if body_order.is_empty() { Some(i) } else { body_order.get(i).copied() };
+ let (Some(a), Some(b)) = (record.faces.get(i), at.and_then(|k| body.faces.get(k))) else { return false };
+ if std::mem::discriminant(&a.surface) != std::mem::discriminant(&b.surface) || a.loops.len() != b.loops.len() {
+ return false;
+ }
+ a.loops.iter().zip(&b.loops).all(|(x, y)| {
+ let (Some(p), Some(q)) = (corners(record, x), corners(body, y)) else { return false };
+ p.len() == q.len()
+ && p.iter().all(|(u, tu)| q.iter().any(|(w, tw)| (0..3).map(|k| (u[k] - w[k]).powi(2)).sum::<f64>().sqrt() <= tu + tw + slack))
+ })
+ })
+}
+
/// The face of its one operand each face of `body` is, from its history.
fn kept_faces(body: &Body) -> Option<Vec<usize>> {
let h = body.history()?;
diff --git a/crates/kernel/src/step_in/tests.rs b/crates/kernel/src/step_in/tests.rs
index e0972f14d2..8c09572a00 100644
--- a/crates/kernel/src/step_in/tests.rs
+++ b/crates/kernel/src/step_in/tests.rs
@@ -662,6 +662,69 @@ fn a_surface_models_closed_shell_beside_a_sheet_is_a_solid_of_its_own() {
assert!(imp.bodies.iter().all(|b| b.color == Some([1.0, 0.5, 0.0])));
}
+/// A surface model whose shells are all OPEN_SHELLs, one of them closed all the same (a unit
+/// cube) beside a sheet of two faces, the first used against its plane (as SolidWorks 2011 wrote
+/// a vendor part): SMLib's export walks that face as its plane faces and numbers the edges and
+/// vertices otherwise than the reader, its faces still in the reader's order. The model comes in
+/// as one body of all its faces, each the file's face it was; it once came in as nothing ("SMLib
+/// renumbered the solid's faces"), and the file with it.
+#[test]
+fn a_surface_model_of_open_shells_one_closed_beside_a_turned_sheet_imports() {
+ let mut t = String::from(HEAD);
+ t += &cube_entities().replace("#100=CLOSED_SHELL", "#100=OPEN_SHELL");
+ // Two unit squares at z = 5 sharing the edge x = 1, both facing +z: the first on a plane
+ // facing -z used against it, the second on a plane facing +z.
+ let corners = [(0, 0), (1, 0), (2, 0), (2, 1), (1, 1), (0, 1)];
+ for (i, (x, y)) in corners.iter().enumerate() {
+ t += &format!("#{}=CARTESIAN_POINT('',({x}.,{y}.,5.));\n#{}=VERTEX_POINT('',#{});\n", 800 + i, 810 + i, 800 + i);
+ }
+ let edges = [(0, 1), (1, 4), (4, 5), (5, 0), (1, 2), (2, 3), (3, 4)];
+ for (k, (a, b)) in edges.iter().enumerate() {
+ let (p, q) = (corners[*a], corners[*b]);
+ t += &format!("#{}=DIRECTION('',({}.,{}.,0.));\n#{}=VECTOR('',#{},1.);\n", 900 + 2 * k, q.0 - p.0, q.1 - p.1, 901 + 2 * k, 900 + 2 * k);
+ t += &format!(
+ "#{}=LINE('',#{},#{});\n#{}=EDGE_CURVE('',#{},#{},#{},.T.);\n",
+ 820 + k,
+ 800 + a,
+ 901 + 2 * k,
+ 830 + k,
+ 810 + a,
+ 810 + b,
+ 820 + k
+ );
+ }
+ let faces: [(&[(usize, bool)], i32, &str); 2] =
+ [(&[(0, true), (1, true), (2, true), (3, true)], -1, ".F."), (&[(4, true), (5, true), (6, true), (1, false)], 1, ".T.")];
+ for (f, (lp, nz, sense)) in faces.iter().enumerate() {
+ let base = 940 + 20 * f;
+ let oes: Vec<String> = lp
+ .iter()
+ .enumerate()
+ .map(|(j, (e, fwd))| {
+ t += &format!("#{}=ORIENTED_EDGE('',*,*,#{},{});\n", base + j, 830 + e, if *fwd { ".T." } else { ".F." });
+ format!("#{}", base + j)
+ })
+ .collect();
+ t += &format!("#{}=EDGE_LOOP('',({}));\n#{}=FACE_OUTER_BOUND('',#{},.T.);\n", base + 5, oes.join(","), base + 6, base + 5);
+ t += &format!("#{}=CARTESIAN_POINT('',(0.,0.,5.));\n#{}=DIRECTION('',(0.,0.,{nz}.));\n", base + 7, base + 8);
+ t += &format!("#{}=AXIS2_PLACEMENT_3D('',#{},#{},$);\n#{}=PLANE('',#{});\n", base + 9, base + 7, base + 8, base + 10, base + 9);
+ t += &format!("#{}=ADVANCED_FACE('',(#{}),#{},{sense});\n", base + 11, base + 6, base + 10);
+ }
+ t += "#130=OPEN_SHELL('',(#951,#971));\n#110=SHELL_BASED_SURFACE_MODEL('Bracket',(#130,#100));\n";
+ t += &context(20, "(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.))");
+ t += "#60=MANIFOLD_SURFACE_SHAPE_REPRESENTATION('',(#110),#20);\n";
+ t += TAIL;
+ let imp = step_import(&t).unwrap_or_else(|e| panic!("{e}"));
+ assert_eq!(imp.bodies.len(), 1, "{:?}", imp.warnings);
+ let b = &imp.bodies[0];
+ assert_eq!((b.source_item, b.file_faces, b.body.face_count()), (110, 8, 8), "{:?}", imp.warnings);
+ assert!(b.refused.is_none(), "{:?}", b.refused);
+ // Each face is the file's face it was: the sheet's first, then the cube's.
+ assert_eq!(b.face_origin, [951, 971, 411, 431, 451, 471, 491, 511]);
+ let m = measure(&b.body).unwrap();
+ assert!(rel(m.area, 8.0) < 1e-9, "area {}", m.area);
+}
+
#[test]
fn hand_written_part_in_inches_with_colour() {
let mut t = String::from(HEAD);