Skip to content

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

Opening: a design saved by 0.24 opens with its sketches in later versions (#8151) (#10379)

kaltenbrunnermarkus686056e4dec7
diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs
index 978d7254b7..b3ff5de430 100644
--- a/crates/doc/src/document.rs
+++ b/crates/doc/src/document.rs
@@ -323,6 +323,11 @@ pub struct Extent {
/// `start_offset`).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from: Option<FacePick>,
+ /// A Join To Object leaves the body it runs to as its own body, joining only its targets.
+ /// Designs saved before a Join also took that body (format 39 and older, #8151) open with
+ /// it set, so they keep the bodies they were built with.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub to_body_apart: bool,
}
/// Thin Extrude: a wall of constant thickness along sketch curves instead of the profiles.
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 9daa63b38d..ca37066ded 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -5397,8 +5397,9 @@ fn feature_tools_with_constructor(
let name = |k: usize| f.face_names.get(k).map(String::as_str);
let (to_name, from_name) = if extent.to.is_some() { (name(0), name(1)) } else { (None, name(0)) };
let to_face = extent.to.and_then(|p| object_face(&tst, to_name, p.0, Some(n)));
- // The body the extent runs to: a Join to it joins that body too (#6424).
- *to_body = to_face.and_then(|(bi, _)| tst.bodies.get(bi)).map(|b| b.name.clone());
+ // The body the extent runs to: a Join to it joins that body too (#6424), unless the
+ // design was built before it did (#8151).
+ *to_body = to_face.filter(|_| !extent.to_body_apart).and_then(|(bi, _)| tst.bodies.get(bi)).map(|b| b.name.clone());
let centre = {
let pts: Vec<Vec2> = regions.iter().flat_map(|r| r.outer.polyline(1e-2)).collect();
let c = pts.iter().fold(Vec2::default(), |a, p| a + *p) * (1.0 / pts.len().max(1) as f64);
diff --git a/crates/doc/src/format.rs b/crates/doc/src/format.rs
index da9560f8dd..30adc6763a 100644
--- a/crates/doc/src/format.rs
+++ b/crates/doc/src/format.rs
@@ -1913,6 +1913,7 @@ mod tests {
to: Some(crate::FacePick(solidcraft_geom::Vec3::new(0.0, 0.0, 10.0))),
to_offset: Some("offset + 1 mm".into()),
from: Some(crate::FacePick(solidcraft_geom::Vec3::new(0.0, 0.0, 5.0))),
+ to_body_apart: false,
},
operation: Operation::NewBody,
targets: vec![],
diff --git a/crates/doc/src/format/pending/join-to-body-apart.rs b/crates/doc/src/format/pending/join-to-body-apart.rs
new file mode 100644
index 0000000000..4bfca98512
--- /dev/null
+++ b/crates/doc/src/format/pending/join-to-body-apart.rs
@@ -0,0 +1,70 @@
+//! A Join extrude To Object may save `to_body_apart: true` in its `extent`: the body it runs to
+//! stays its own body and only the Join's targets take the tool. Since 0.24.1 a Join To Object
+//! also joins the body it runs to (#6424); designs saved by 0.24.0 and earlier (format 39 and
+//! older) were built without that, so their later features name the body it would swallow and
+//! the faces the union would merge (#8151). Such a design gets the field on every Join extrude
+//! To Object as it opens, and keeps its bodies; designs of format 40 and later are left as they
+//! are. The last release refuses a design that has the field (it refuses the newer format
+//! number).
+
+use serde_json::Value;
+
+use crate::Result;
+
+/// The first format whose Join extrudes To Object also joined the body they run to.
+const JOINS_TO_BODY: u32 = 40;
+
+pub fn step(v: &mut Value) -> Result<()> {
+ // The file's own format is still in it: upgrades never rewrite `format`, and a file without
+ // one is format 1.
+ let version = match v.get("format").and_then(Value::as_str) {
+ Some(format) => crate::format::version_of(format),
+ None => Some(1),
+ };
+ if version.is_none_or(|n| n >= JOINS_TO_BODY) {
+ return Ok(());
+ }
+ for feature in v.get_mut("features").and_then(Value::as_array_mut).into_iter().flatten() {
+ let join_extrude = feature.get("type").and_then(Value::as_str) == Some("extrude")
+ && feature.get("operation").and_then(Value::as_str) == Some("join");
+ if !join_extrude {
+ continue;
+ }
+ if let Some(extent) = feature.get_mut("extent").and_then(Value::as_object_mut)
+ && extent.get("to").is_some_and(|to| !to.is_null())
+ {
+ extent.insert("to_body_apart".into(), Value::Bool(true));
+ }
+ }
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ #![allow(clippy::unwrap_used)]
+ use serde_json::json;
+
+ fn design(format: &str) -> serde_json::Value {
+ json!({"format": format, "features": [
+ {"type": "extrude", "operation": "join", "extent": {"distance": "0", "to": [0.0, 0.0, 30.0]}},
+ {"type": "extrude", "operation": "join", "extent": {"distance": "10 mm"}},
+ {"type": "extrude", "operation": "cut", "extent": {"distance": "0", "to": [0.0, 0.0, 30.0]}},
+ ]})
+ }
+
+ fn apart(v: &serde_json::Value) -> Vec<bool> {
+ v["features"].as_array().unwrap().iter().map(|f| f["extent"]["to_body_apart"] == json!(true)).collect()
+ }
+
+ #[test]
+ fn only_joins_to_an_object_saved_before_format_40_keep_the_body_apart() {
+ let mut old = design("solidcraft/39");
+ super::step(&mut old).unwrap();
+ assert_eq!(apart(&old), [true, false, false]);
+ for newer in ["solidcraft/40", "solidcraft/46"] {
+ let mut v = design(newer);
+ super::step(&mut v).unwrap();
+ assert_eq!(apart(&v), [false, false, false], "{newer}");
+ }
+ }
+}
diff --git a/crates/doc/src/format_offsets.rs b/crates/doc/src/format_offsets.rs
index d1ce0a38e9..83ae3d1d09 100644
--- a/crates/doc/src/format_offsets.rs
+++ b/crates/doc/src/format_offsets.rs
@@ -467,6 +467,7 @@ mod tests {
to: None,
to_offset: None,
from: None,
+ to_body_apart: false,
},
operation: crate::Operation::NewBody,
targets: vec![],
diff --git a/crates/doc/src/placement_reuse.rs b/crates/doc/src/placement_reuse.rs
index 511efb503c..5d4ab8a028 100644
--- a/crates/doc/src/placement_reuse.rs
+++ b/crates/doc/src/placement_reuse.rs
@@ -110,6 +110,7 @@ pub(super) fn local_geometry(f: &Feature, components: &std::collections::HashMap
to,
to_offset: _,
from,
+ to_body_apart: _,
} = extent;
to.is_none() && from.is_none() && !through_all && components.get(&sketch.0) == Some(&f.component)
}
diff --git a/crates/doc/src/profile_refs.rs b/crates/doc/src/profile_refs.rs
index eb11c7cdbb..13164703b3 100644
--- a/crates/doc/src/profile_refs.rs
+++ b/crates/doc/src/profile_refs.rs
@@ -456,6 +456,7 @@ mod tests {
to: None,
to_offset: None,
from: None,
+ to_body_apart: false,
},
operation: crate::Operation::NewBody,
targets: vec![],
@@ -638,6 +639,7 @@ mod tests {
to: None,
to_offset: None,
from: None,
+ to_body_apart: false,
},
operation: crate::Operation::NewBody,
targets: vec![],
diff --git a/crates/doc/src/tests.rs b/crates/doc/src/tests.rs
index 3a7b2594ce..593ce4329d 100644
--- a/crates/doc/src/tests.rs
+++ b/crates/doc/src/tests.rs
@@ -43,6 +43,7 @@ fn plate_doc() -> (Document, u64) {
to: None,
to_offset: None,
from: None,
+ to_body_apart: false,
},
operation: Operation::NewBody,
targets: vec![],
@@ -315,6 +316,7 @@ fn box_fillet_cut_timeline() {
to: None,
to_offset: None,
from: None,
+ to_body_apart: false,
},
operation: Operation::Cut,
targets: vec![],
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index d18833add0..cbbd433f10 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -1776,6 +1776,12 @@ fn redefine(s: &mut Session, p: &Value) -> Result<Value> {
*sketch = Some(*before);
*profiles = picked.clone();
}
+ // An old design's Join To Object keeps the body it runs to apart through an edit (#8151).
+ if let (FeatureKind::Extrude { extent: before, .. }, FeatureKind::Extrude { extent: after, .. }) = (&old.kind, &mut new.kind)
+ && after.to.is_some()
+ {
+ after.to_body_apart = before.to_body_apart;
+ }
// A boundary surface's loop of sketch curves and its curve network are one command's two
// forms: an edit may turn one into the other.
let boundary = |k: &FeatureKind| matches!(k, FeatureKind::SurfaceBoundary { .. } | FeatureKind::SurfaceNetwork { .. });
diff --git a/crates/engine/src/cmd/face_join_tests.rs b/crates/engine/src/cmd/face_join_tests.rs
index ca5f61430c..f272fcc2ad 100644
--- a/crates/engine/src/cmd/face_join_tests.rs
+++ b/crates/engine/src/cmd/face_join_tests.rs
@@ -230,3 +230,50 @@ fn join_extrude_to_another_body_joins_that_body() {
assert!((volume("Base") - 60_000.0).abs() < 1e-6, "the target took the tool: {}", volume("Base"));
assert_eq!(state.body("Base").unwrap().body.lumps().map_or(1, |l| l.len()), 1, "one piece");
}
+
+/// #8151: a design saved before a Join To Object also joined the body it runs to (format 39 and
+/// older) keeps that body apart when it opens, so its later features still find it, and an edit
+/// of the extrude keeps it apart too.
+#[test]
+fn join_extrude_to_another_body_saved_before_it_joined_keeps_that_body() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length":40,"width":40,"height":10,"body_name":"Base"}));
+ run(&mut s, "solid.box", json!({"corner":[0,0,40],"length":40,"width":40,"height":20,"body_name":"Target"}));
+ let sketch = run(&mut s, "sketch.create", json!({"plane":{"face":[20,20,10]}}))["sketch"].as_u64().unwrap();
+ let plane = s.model.state().sketch(sketch).unwrap().plane;
+ let at = |x: f64, y: f64| {
+ let p = plane.to_local(Vec3::new(x, y, 10.0));
+ json!([p.x, p.y])
+ };
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": at(10.0,10.0), "p1": at(30.0,30.0)}));
+ run(&mut s, "sketch.finish", json!({}));
+ // A feature after the extrude names the body it runs to, as the lid's sketch and moves did.
+ // Today's join leaves no such body to name, so the move is made first and put last below.
+ let moved = run(&mut s, "solid.move", json!({"bodies":["Target"],"translate":[0,0,5]}))["feature"].clone();
+ let extrude = run(&mut s, "solid.extrude", json!({"sketch":sketch,"to":[20,20,40],"operation":"join","targets":["Base"]}))["feature"].clone();
+ let mut saved: Value = serde_json::from_str(&s.doc.to_json()).unwrap();
+ let features = saved["features"].as_array_mut().unwrap();
+ let at_move = features.iter().position(|f| f["id"] == moved).unwrap();
+ let feature = features.remove(at_move);
+ features.push(feature);
+ saved["format"] = json!("solidcraft/39");
+ saved.as_object_mut().unwrap().remove("format_pending");
+ let mut old = Session::new(solidcraft_doc::format::read(&saved.to_string()).unwrap());
+ let check = |s: &Session| {
+ assert!(s.model.results.iter().all(|r| r.error.is_none()), "{:?}", s.model.results);
+ let state = s.model.state();
+ let volume = |name: &str| solidcraft_kernel::measure(&state.body(name).unwrap().body).unwrap().volume;
+ assert_eq!(state.bodies.len(), 2, "the body the extrude runs to stays its own body");
+ assert!((volume("Base") - 28_000.0).abs() < 1e-6, "the base took the tool: {}", volume("Base"));
+ assert!((volume("Target") - 32_000.0).abs() < 1e-6, "the target is untouched: {}", volume("Target"));
+ };
+ check(&old);
+ run(
+ &mut old,
+ "timeline.redefine",
+ json!({"feature":extrude,"command":"solid.extrude","params":{"sketch":sketch,"to":[20,20,40],"operation":"join","targets":["Base"]}}),
+ );
+ check(&old);
+ // The design saves the choice and opens with it again.
+ check(&Session::new(solidcraft_doc::format::read(&old.doc.to_json()).unwrap()));
+}
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index b1c34042ba..b0ea55f68a 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -1209,6 +1209,7 @@ pub(super) fn extent_param(s: &Session, p: &Value, cmd: &str) -> Result<Extent>
to: p.get("to").and_then(vec3).map(FacePick),
to_offset: expr(p, "to_offset"),
from: p.get("from").and_then(vec3).map(FacePick),
+ to_body_apart: false,
})
}