Engine: keep every placed body's frame with the world state (#7411)

Each command that names a body (appearance, material, move, ...) mapped it to
its component's own name by working out every placed body's world name and
frame, so a command on a large assembly cost a walk of the whole design. The
list is now kept with the world-placement cache while it is current.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jackson OurslandandClaude Opus 5.5 authored and GitHub committed 2026-10-27 03:33:53 +00:00
1 parent 4e07b6c4b7
commit 614e006eac
3 files changed
+48 -5

No files matched your search

+2 -5
View File
@@ -101,7 +101,7 @@ fn table(id: &str) -> &'static [(&'static str, K)] {
struct Frames {
/// Worked out on first use: most commands pick no body, and a large assembly's frames cost
/// a walk of every body on every command.
bodies: std::cell::OnceCell<Vec<(String, String, Mat)>>,
bodies: std::cell::OnceCell<crate::BodyFrames>,
active: Mat,
/// The frame of the sketch being edited (its component), else the active component's.
sketch: Mat,
@@ -117,10 +117,7 @@ fn frames(s: &Session) -> Frames {
impl Frames {
/// World name, the component's own body name, and its frame, for every body.
fn bodies(&self, s: &Session) -> &[(String, String, Mat)] {
self.bodies.get_or_init(|| {
let st = s.model.state();
s.placed_document().body_frames_with_source(&st).into_iter().map(|(world, own, _, _, m)| (world, own, m)).collect()
})
self.bodies.get_or_init(|| s.body_frames())
}
}
+21
View File
@@ -264,8 +264,14 @@ struct WorldCache {
world: Arc<solidcraft_doc::ModelState>,
/// The moved components' bodies as placed for `world`, reused by the next world state.
placed: Arc<std::sync::Mutex<solidcraft_doc::PlacedBodies>>,
/// Every placed body's world name, own name and frame for `world` ([`Session::body_frames`]),
/// worked out when a command first maps a body name.
frames: Arc<std::sync::OnceLock<BodyFrames>>,
}
/// Every placed body's world name, its component's own name for it, and its frame.
pub(crate) type BodyFrames = Arc<Vec<(String, String, solidcraft_doc::Mat)>>;
impl WorldCache {
fn matches(&self, session: &Session, model: &Arc<solidcraft_doc::ModelState>) -> bool {
Arc::ptr_eq(&self.doc, &session.doc)
@@ -795,11 +801,26 @@ impl Session {
active_occurrence_path: self.active_occurrence_path.clone(),
world: w.clone(),
placed,
frames: Arc::default(),
});
}
w
}
/// Every placed body's world name, own name and frame. Kept with the world state while it is
/// current, so commands naming a body in a large assembly do not each name every body again.
pub(crate) fn body_frames(&self) -> BodyFrames {
let st = self.model.state();
let compute = || -> BodyFrames {
Arc::new(self.placed_document().body_frames_with_source(&st).into_iter().map(|(world, own, _, _, m)| (world, own, m)).collect())
};
let cell = self.world_cache.lock().ok().and_then(|c| c.as_ref().filter(|c| c.matches(self, &st)).map(|c| c.frames.clone()));
match cell {
Some(cell) => cell.get_or_init(compute).clone(),
None => compute(),
}
}
pub fn refresh(&mut self) {
self.model.evaluate(&self.doc);
#[cfg(test)]
@@ -262,3 +262,28 @@ fn joint_drive_reuses_reference_tessellation_across_rigid_poses() {
}
assert_eq!(local.bounds(), bounds, "old snapshots stay immutable");
}
/// Commands map world body names through every body's frame: worked out once while the world
/// state is current, and again when a placement moves or a body is added.
#[test]
fn body_frames_are_kept_with_the_world_state() {
let mut session = Session::default();
let first = run(&mut session, "component.create", json!({"name":"Part"}))["occurrence"].as_u64().unwrap();
run(&mut session, "solid.box", json!({"length":2,"width":3,"height":4,"body_name":"Block","operation":"new"}));
let second = run(&mut session, "occurrence.copy", json!({"occurrence":first}))["occurrence"].as_u64().unwrap();
let _ = session.world_state();
let frames = session.body_frames();
assert!(Arc::ptr_eq(&frames, &session.body_frames()), "kept while the world state is current");
let copy = session.occurrence_placement(&[second]).unwrap().bodies[0].world.clone();
assert!(frames.iter().any(|(world, own, _)| *world == copy && own == "Block"), "a copy maps back to its definition");
run(&mut session, "occurrence.move", json!({"occurrence":second,"translate":[10,0,0],"capture":false}));
let _ = session.world_state();
let moved = session.body_frames();
assert!(!Arc::ptr_eq(&frames, &moved), "a moved placement works them out again");
let at = |frames: &crate::BodyFrames| frames.iter().find(|(world, ..)| *world == copy).map(|(_, _, m)| m[3][0]);
assert_eq!(at(&moved), Some(10.0), "with the placement's new frame");
assert_eq!(at(&frames), Some(0.0));
run(&mut session, "solid.box", json!({"length":1,"width":1,"height":1,"body_name":"Other","operation":"new"}));
let _ = session.world_state();
assert!(session.body_frames().iter().any(|(world, ..)| world.starts_with("Other")), "a new body is in them");
}