Skip to content

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

Saved model: kept only when every body decodes to itself (#6760) (#6761)

semmelrinde28b27aac8f96
diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs
index 02c6e4e279..17d383a008 100644
--- a/crates/engine/src/cmd/file.rs
+++ b/crates/engine/src/cmd/file.rs
@@ -1146,6 +1146,56 @@ mod tests {
assert!(d.rejected.is_empty());
}
+ /// A design whose saved model would give a body back with other topology opens with the
+ /// bodies it was saved with. A pattern copy cut by an automatic extrude came back from the
+ /// native codec with two loose vertices more (10 faces, 18 edges, 12 vertices instead of 10);
+ /// such a body is not kept, the design is saved without its model and evaluated on opening.
+ #[test]
+ fn a_design_opens_with_the_bodies_it_was_saved_with_when_one_does_not_decode_to_itself() {
+ let mut session = Session::new(Document::new("Roundtrip"));
+ for (cmd, p) in [
+ ("solid.cylinder", json!({"axis": "Z", "base": [-2, -17, -2], "body_name": "B2", "height": 11, "radius": 6})),
+ ("sketch.create", json!({"plane": "YZ"})),
+ (
+ "sketch.line",
+ json!({"closed": true, "ids": ["w3a", "w3b", "w3c", "w3d"], "infer": true, "points": [[-3, -22], [1, -22], [1, -17], [-3, -17]]}),
+ ),
+ ("sketch.dimension", json!({"entities": ["w3a"], "type": "length", "value": 9})),
+ ("solid.extrude", json!({"distance": -1, "operation": "cut"})),
+ ("solid.box", json!({"corner": [27, -22, 0], "height": 12, "length": "d3", "operation": "new", "width": 3})),
+ ("sketch.create", json!({"plane": "YZ"})),
+ (
+ "sketch.line",
+ json!({"closed": true, "ids": ["w6a", "w6b", "w6c", "w6d"], "infer": true, "points": [[-16, -23], [2, -23], [2, -15], [-16, -15]]}),
+ ),
+ ("sketch.dimension", json!({"entities": ["w6a"], "type": "length", "value": 11})),
+ ("solid.extrude", json!({"distance": 18, "operation": "new"})),
+ ("solid.extrude", json!({"distance": -5, "operation": "new"})),
+ ("sketch.create", json!({"plane": "YZ"})),
+ ("sketch.circle.center", json!({"center": [15, -8], "radius": 9})),
+ ("solid.extrude", json!({"distance": "d8", "operation": "new"})),
+ ("solid.pattern.circular", json!({"axis": "Y", "bodies": ["Body4"], "count": 3})),
+ ("solid.extrude", json!({"distance": -16, "operation": "auto"})),
+ ] {
+ session.execute(cmd, &p).unwrap();
+ }
+ let counts = |s: &Session| {
+ s.model
+ .state()
+ .bodies
+ .iter()
+ .map(|b| (b.name.clone(), b.body.face_count(), b.body.edge_count(), b.body.vertex_count()))
+ .collect::<Vec<_>>()
+ };
+ let bytes = session.design_container(solidcraft_io::DesignCompression::Fast, false, true).unwrap();
+ let path = std::env::temp_dir().join(format!("solidcraft-roundtrip-{}.solidcraft", std::process::id()));
+ std::fs::write(&path, &bytes).unwrap();
+ let mut reopened = Session::default();
+ reopened.execute("doc.open", &json!({"path": path})).unwrap();
+ std::fs::remove_file(&path).unwrap();
+ assert_eq!(counts(&reopened), counts(&session));
+ }
+
#[test]
fn saved_model_needs_no_meshes_and_is_adopted_only_for_its_document() {
let mut session = Session::new(Document::new("Saved model"));
diff --git a/crates/io/src/design/native_cache.rs b/crates/io/src/design/native_cache.rs
index 109856ff86..fc9d288221 100644
--- a/crates/io/src/design/native_cache.rs
+++ b/crates/io/src/design/native_cache.rs
@@ -101,6 +101,12 @@ struct Encoded {
parts: Vec<usize>,
}
+/// What a decoded body must share with the one encoded: its faces, edges, vertices and shells.
+fn same_topology(a: &solidcraft_kernel::Body, b: &solidcraft_kernel::Body) -> bool {
+ let counts = |x: &solidcraft_kernel::Body| (x.face_count(), x.edge_count(), x.vertex_count(), x.shell_count());
+ counts(a) == counts(b)
+}
+
impl Encoded {
fn of(model: &SavedModel) -> Result<Self> {
// What to encode: every body, and the part every placed copy shows.
@@ -117,7 +123,14 @@ impl Encoded {
}));
}
let bodies: Vec<&solidcraft_kernel::Body> = model.bodies.iter().chain(shares.iter().map(|s| s.original())).collect();
- let encoded = solidcraft_kernel::map_on_threads(&bodies, 16, |b| solidcraft_kernel::encode_native_body(b).ok());
+ // A body is kept only when it decodes to itself: one the codec gave back with other
+ // topology (two loose vertices more on a pattern copy cut by an extrude) would open as
+ // another body. Then the model isn't kept, and opening the design evaluates it.
+ let encoded = solidcraft_kernel::map_on_threads(&bodies, 16, |b| {
+ let bytes = solidcraft_kernel::encode_native_body(b).ok()?;
+ let back = solidcraft_kernel::decode_native_body(&bytes).ok()?;
+ same_topology(b, &back).then_some(bytes)
+ });
let mut slices = Vec::new();
let mut by_digest = std::collections::HashMap::new();
let mut index = Vec::with_capacity(encoded.len());