Skip to content

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

Wave 315/509: Bodies: an operation's result is its record, not the SMLib body it was exported from (and 2 more)

jyviko70beda47b1b3
diff --git a/apps/solidcraft-cli/tests/run_strict.rs b/apps/solidcraft-cli/tests/run_strict.rs
index f762994634..590ae6cb6c 100644
--- a/apps/solidcraft-cli/tests/run_strict.rs
+++ b/apps/solidcraft-cli/tests/run_strict.rs
@@ -65,7 +65,10 @@ fn allow_errors_writes_anyway_and_a_saved_design_keeps_failing() {
let output = cli().arg("run").arg(dir.broken_script()).arg("--allow-errors").arg("--out").arg(&step).arg("--save").arg(&saved).output().unwrap();
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
let measured: Value = serde_json::from_slice(&output.stdout).unwrap();
- assert_eq!(measured["total"]["volume_mm3"], json!(4000.0), "the box without its fillet");
+ // SMLib integrates the volume (a relative accuracy of 1e-4 asked for; a box comes out within
+ // a few units in the last place), so the box's 4000 mm³ is checked to 1e-9, not to the bit.
+ let volume = measured["total"]["volume_mm3"].as_f64().unwrap();
+ assert!((volume - 4000.0).abs() < 4000.0 * 1e-9, "the box without its fillet: {volume}");
assert!(std::fs::metadata(&step).unwrap().len() > 0);
// The saved design still holds the failing feature: running it fails too, `eval` reports it.
let again = dir.0.join("again.step");
diff --git a/crates/engine/src/frames_tests.rs b/crates/engine/src/frames_tests.rs
index 8047a3ed1f..e67f940a07 100644
--- a/crates/engine/src/frames_tests.rs
+++ b/crates/engine/src/frames_tests.rs
@@ -377,7 +377,8 @@ fn an_explicit_sketch_plane_is_read_in_the_frame_asked_for() {
#[test]
fn a_combine_tool_stays_where_it_is_placed() {
let mut s = Session::default();
- run(&mut s, "component.create", json!({"name": "Part"}));
+ // The design's first part is grounded when it is made (#2804); this one is moved.
+ run(&mut s, "component.create", json!({"name": "Part", "grounded": false}));
run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 10, "corner": [-20, -20, 0], "body_name": "Plate"}));
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "occurrence.move", json!({"occurrence": "Part:1", "axis": [1, 0, 0], "angle": "90 deg"}));
diff --git a/crates/kernel/src/body.rs b/crates/kernel/src/body.rs
index d987204142..c75b3ad73e 100644
--- a/crates/kernel/src/body.rs
+++ b/crates/kernel/src/body.rs
@@ -441,15 +441,11 @@ impl SurfaceKind {
impl Body {
/// A body with `record` and nothing else set.
- fn bare(record: Brep, smlib: Option<solidcraft_smlib::Brep>, mesh: Option<std::sync::Arc<crate::meshbody::TriMesh>>) -> Body {
+ fn bare(record: Brep, mesh: Option<std::sync::Arc<crate::meshbody::TriMesh>>) -> Body {
let size = span_of(&record);
- let cell = std::sync::OnceLock::new();
- if let Some(handle) = smlib {
- let _ = cell.set(Ok(handle));
- }
Body {
record: std::sync::Arc::new(record),
- smlib: std::sync::Arc::new(cell),
+ smlib: std::sync::Arc::new(std::sync::OnceLock::new()),
size,
volume: Some(std::sync::Arc::new(std::sync::Mutex::new(None))),
mesh,
@@ -468,18 +464,21 @@ impl Body {
/// structure (budgets, references, loops, regions, canonical order). Its faces face as the
/// record says.
pub(crate) fn from_record(record: Brep) -> Result<Body> {
- Ok(Body::bare(solid_record(record)?, None, None))
+ Ok(Body::bare(solid_record(record)?, None))
}
- /// The body of an SMLib B-rep and its record (SMLib's export, in canonical order), keeping
- /// the SMLib body so later operations need not build it again.
- pub(crate) fn from_smlib_record(handle: solidcraft_smlib::Brep, record: Brep) -> Result<Body> {
- Ok(Body::bare(solid_record(record)?, Some(handle), None))
+ /// The body of SMLib's export of a B-rep (its record, in canonical order). The SMLib body
+ /// the export came from is not kept: the export is not exact to the bit (its parameters,
+ /// radians, pcurves), so that body measures and operates a little differently from the
+ /// record's import, and a body evaluated afresh would then differ from the same body read
+ /// back from a file. Every operation starts from the record's import ([`Body::smlib`]).
+ pub(crate) fn from_smlib_record(record: Brep) -> Result<Body> {
+ Ok(Body::bare(solid_record(record)?, None))
}
/// A mesh body of `mesh`.
pub(crate) fn of_mesh(mesh: crate::meshbody::TriMesh) -> Body {
- Body::bare(empty_record(), None, Some(std::sync::Arc::new(mesh)))
+ Body::bare(empty_record(), Some(std::sync::Arc::new(mesh)))
}
/// The body's record: faces, edges and vertices in the body's own order. Empty for a mesh body.
diff --git a/crates/kernel/src/native.rs b/crates/kernel/src/native.rs
index cef1920e42..4a4d67fe44 100644
--- a/crates/kernel/src/native.rs
+++ b/crates/kernel/src/native.rs
@@ -126,7 +126,7 @@ pub fn decode_native_body(bytes: &[u8]) -> Result<Body> {
}
/// An SMLib body as a kernel body: SMLib's export mapped by `solidcraft_smlib` (canonical order,
-/// radians, pcurves) is the body's record, and the SMLib body is kept with it.
+/// radians, pcurves) is the body's record ([`Body::from_smlib_record`]).
pub fn body_from_smlib(brep: &solidcraft_smlib::Brep) -> Result<Body> {
let _span = solidcraft_profile::span("kernel", "body_from_smlib");
body_from_smlib_ordered(brep).map(|(body, _)| body)
@@ -137,8 +137,7 @@ pub fn body_from_smlib(brep: &solidcraft_smlib::Brep) -> Result<Body> {
pub(crate) fn body_from_smlib_ordered(brep: &solidcraft_smlib::Brep) -> Result<(Body, Option<solidcraft_smlib::history::Order>)> {
let _span = solidcraft_profile::span("kernel", "body_from_smlib_ordered");
let (record, order) = brep.to_native_ordered().map_err(|e| KernelError::Failed(e.to_string()))?;
- let handle = brep.try_clone().map_err(|e| KernelError::Failed(format!("SMLib: {e}")))?;
- Ok((Body::from_smlib_record(handle, record)?, Some(order)))
+ Ok((Body::from_smlib_record(record)?, Some(order)))
}
/// `body` in SMLib, with where each of SMLib's faces and edges is in the body's face and edge
diff --git a/crates/kernel/src/surfaces.rs b/crates/kernel/src/surfaces.rs
index 6fb52f9e6b..ec40d6a63c 100644
--- a/crates/kernel/src/surfaces.rs
+++ b/crates/kernel/src/surfaces.rs
@@ -1255,7 +1255,7 @@ mod imp {
checked(&sewn, "stitch")?;
let (record, kept) = sewn.to_native_ordered().map_err(|e| failed(format!("SMLib stitch: {e}")))?;
let h = if same_numbering(r, &back) { sewn.history().ok().and_then(|raw| raw.history(&[order], &kept).ok()) } else { None };
- Ok((Body::from_smlib_record(sewn, record)?, h))
+ Ok((Body::from_smlib_record(record)?, h))
}
pub(super) fn thicken(body: &Body, thickness: f64, symmetric: bool) -> Result<Body> {