Skip to content

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

Eval: To / From Object reads its face's part, not every part's placement (#12866)

Kai Evans1c03f2be062c
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 322e25c555..cb2531f325 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -1647,6 +1647,21 @@ impl PlacementReads {
}
}
+ /// The bodies To / From Object's stored face names find, each where it is placed: evaluation
+ /// finds a named face by its name ([`with_named_face_points`] fails a feature whose name finds
+ /// none). Everything when a name is missing or empty, finds no one body, or `st` is `None`:
+ /// the face is then found by its point, on whatever body lies there.
+ fn object_faces(&mut self, st: Option<&ModelState>, names: Option<&[String]>) {
+ let bodies: Option<Vec<&str>> = st.zip(names).and_then(|(st, names)| {
+ let first_of_its_name = |b: &&ModelBody| st.body(&b.name).is_some_and(|first| std::ptr::eq(first, *b));
+ names.iter().map(|n| crate::name_lookup::face_body(st, n).filter(first_of_its_name).map(|b| b.name.as_str())).collect()
+ });
+ match bodies {
+ Some(bodies) => bodies.into_iter().for_each(|b| self.placed_body(st, b)),
+ None => self.all = true,
+ }
+ }
+
/// The same target selection as `apply_traced`, expanded only over the timeline input.
fn drawn_targets(&mut self, st: Option<&ModelState>, f: &Feature) {
let Some(st) = st else {
@@ -1826,11 +1841,22 @@ fn placement_digest(doc: &Document, f: &Feature, st: Option<&ModelState>) -> u64
}
}
}
+ // The faces To / From Object names, in `face_points` order.
+ let object_names = match &f.kind {
+ FeatureKind::Extrude { extent, .. } => usize::from(extent.to.is_some()) + usize::from(extent.from.is_some()),
+ FeatureKind::Revolve { to, .. } => usize::from(to.is_some()),
+ _ => 0,
+ };
match &f.kind {
- // To / From Object finds its target where that target's component is placed (#928),
- // over every body.
- FeatureKind::Extrude { extent, .. } if extent.to.is_some() || extent.from.is_some() => reads.all = true,
- FeatureKind::Revolve { to: Some(_), .. } => reads.all = true,
+ // To / From Object finds its target where the target's body is placed (#928): only that
+ // body's placement, not every part's (#6325). Its tools meet their targets as other
+ // joins and cuts do.
+ kind if object_names > 0 => {
+ reads.object_faces(st, f.face_names.get(..object_names));
+ if drawn_tools_change_bodies(kind) {
+ reads.drawn_targets(st, f);
+ }
+ }
// A mirror of placed bodies follows the placements it was picked at and its own component's
// frame, not every occurrence: moving an unrelated one leaves it (and its cache) alone.
FeatureKind::Mirror { placements, bodies, .. } if !placements.is_empty() => return mirror_placements_digest(doc, f, placements, bodies),
diff --git a/crates/doc/src/eval_cost_tests.rs b/crates/doc/src/eval_cost_tests.rs
index 9e9c3df0f1..b03431e61c 100644
--- a/crates/doc/src/eval_cost_tests.rs
+++ b/crates/doc/src/eval_cost_tests.rs
@@ -656,6 +656,126 @@ fn a_tool_cutting_another_part_cuts_again_when_that_part_moves() {
assert!((volume(&model, &body) - (400.0 - 16.0)).abs() < 1e-6, "{}", volume(&model, &body));
}
+/// A root design with two 10 x 10 x 4 mm plates, "Plate" at the origin and "Other" 30 mm along Y,
+/// and a sketch on origin plane `plane` of the rectangle `profile` spans (opposite corners, in mm):
+/// (the plate part, the other part, the sketch).
+fn two_plates_and_a_sketch(doc: &mut Document, plane: &str, profile: [Vec2; 2]) -> (u64, u64, u64) {
+ let plate = doc.add_component(Some("Plate"), 0).unwrap();
+ let other = doc.add_component(Some("Other"), 0).unwrap();
+ place(doc, other, rigid(Vec3::new(0.0, 30.0, 0.0), Vec3::ZERO, Vec3::Z, 0.0));
+ plate_with_holes(doc, other, other, 0);
+ plate_with_holes(doc, plate, plate, 0);
+ let mut sk = Sketch::new();
+ let [a, c] = profile;
+ let p = [a, Vec2::new(c.x, a.y), c, Vec2::new(a.x, c.y)].map(|q| sk.add_point(q, None).unwrap());
+ for k in 0..4 {
+ sk.add_line_pts(p[k], p[(k + 1) % 4], None).unwrap();
+ }
+ let sketch = doc.add_feature(FeatureKind::Sketch { plane: PlaneRef::Origin { name: plane.into() }, sketch: sk }, None).unwrap();
+ (plate, other, sketch)
+}
+
+/// `kind` added to the root with its face points named after the faces under them, as the
+/// command that makes it names them.
+fn add_named(doc: &mut Document, kind: FeatureKind) -> u64 {
+ let mut model = Model::new();
+ model.evaluate(doc);
+ let names = face_point_names(doc, &model.state(), &kind, 0).unwrap();
+ assert!(names.iter().all(|n| !n.is_empty()), "{names:?}");
+ let id = doc.add_feature(kind, None).unwrap();
+ doc.feature_mut(id).unwrap().face_names = names;
+ id
+}
+
+/// An extrude in the root from the top of the plate (z 4) 6 mm up, over 2 to 8 mm on X and Y.
+fn extrude_from_the_plate(doc: &mut Document) -> (u64, u64, u64) {
+ let (plate, other, sketch) = two_plates_and_a_sketch(doc, "XY", [Vec2::new(2.0, 2.0), Vec2::new(8.0, 8.0)]);
+ let kind = FeatureKind::Extrude {
+ thin: None,
+ sketch: SketchId(sketch),
+ profiles: ProfileSel::All,
+ extent: serde_json::from_value(serde_json::json!({"distance": "6", "from": [5.0, 5.0, 4.0]})).unwrap(),
+ operation: Operation::NewBody,
+ targets: vec![],
+ };
+ (plate, other, add_named(doc, kind))
+}
+
+/// Where the body feature `id` made has its centre.
+fn centroid_of(model: &Model, id: u64) -> Vec3 {
+ let st = model.state();
+ kernel::measure(&st.bodies.iter().find(|b| b.feature == id).unwrap().body).unwrap().centroid
+}
+
+/// An extrude from a face of the plate reads where the plate is, and nothing else (#6325): a
+/// joint moving another part leaves its result, and every result after it, as it was.
+#[test]
+fn an_extrude_from_its_own_part_keeps_its_result_when_another_part_moves() {
+ let mut doc = Document::new("from object");
+ let (plate, other, extrude) = extrude_from_the_plate(&mut doc);
+ let mut model = Model::new();
+ model.evaluate(&doc);
+ assert!(model.results.iter().all(|r| r.error.is_none()), "{:?}", model.results.iter().find(|r| r.error.is_some()));
+ assert!((centroid_of(&model, extrude).z - 7.0).abs() < 1e-6, "{:?}", centroid_of(&model, extrude));
+ place(&mut doc, other, rigid(Vec3::new(0.0, 40.0, 3.0), Vec3::ZERO, Vec3::Z, 0.0));
+ model.evaluate(&doc);
+ assert_eq!(model.last_recomputed, 0);
+ // The plate lifted 5 mm: the extrude starts on its top where it is now (#928).
+ place(&mut doc, plate, rigid(Vec3::new(0.0, 0.0, 5.0), Vec3::ZERO, Vec3::Z, 0.0));
+ model.evaluate(&doc);
+ assert_eq!(model.last_recomputed, 1);
+ assert!((centroid_of(&model, extrude).z - 12.0).abs() < 1e-6, "{:?}", centroid_of(&model, extrude));
+}
+
+/// A revolve To a face of the plate, as the extrude from it: it turns again only when the plate
+/// moves.
+#[test]
+fn a_revolve_to_its_own_part_keeps_its_result_when_another_part_moves() {
+ let mut doc = Document::new("revolve to object");
+ let (plate, other, sketch) = two_plates_and_a_sketch(&mut doc, "XZ", [Vec2::new(12.0, 0.0), Vec2::new(15.0, 2.0)]);
+ let kind = FeatureKind::Revolve {
+ sketch: SketchId(sketch),
+ profiles: ProfileSel::All,
+ axis: AxisRef::SketchAxis { axis: "y".into() },
+ angle: "90 deg".into(),
+ operation: Operation::NewBody,
+ targets: vec![],
+ angle2: None,
+ symmetric: false,
+ convention: Default::default(),
+ to: Some(serde_json::from_value(serde_json::json!([5.0, 5.0, 4.0])).unwrap()),
+ };
+ let revolve = add_named(&mut doc, kind);
+ let mut model = Model::new();
+ model.evaluate(&doc);
+ assert!(model.results.iter().all(|r| r.error.is_none()), "{:?}", model.results.iter().find(|r| r.error.is_some()));
+ let turned = volume(&model, &model.state().bodies.iter().find(|b| b.feature == revolve).unwrap().name.clone());
+ place(&mut doc, other, rigid(Vec3::new(0.0, 40.0, 3.0), Vec3::ZERO, Vec3::Z, 0.0));
+ model.evaluate(&doc);
+ assert_eq!(model.last_recomputed, 0);
+ // The plate turned a quarter about Z: the revolve turns to its top where it is now.
+ place(&mut doc, plate, rigid(Vec3::ZERO, Vec3::ZERO, Vec3::Z, std::f64::consts::FRAC_PI_2));
+ model.evaluate(&doc);
+ assert_eq!(model.last_recomputed, 1);
+ let now = volume(&model, &model.state().bodies.iter().find(|b| b.feature == revolve).unwrap().name.clone());
+ assert!((now - turned).abs() > 1.0, "{turned} then {now}");
+}
+
+/// A From face without a name is found by its point, on whatever body lies there: moving any
+/// part extrudes again.
+#[test]
+fn an_extrude_from_a_face_without_a_name_extrudes_again_when_any_part_moves() {
+ let mut doc = Document::new("from a point");
+ let (_, other, extrude) = extrude_from_the_plate(&mut doc);
+ doc.feature_mut(extrude).unwrap().face_names.clear();
+ let mut model = Model::new();
+ model.evaluate(&doc);
+ assert!((centroid_of(&model, extrude).z - 7.0).abs() < 1e-6, "{:?}", centroid_of(&model, extrude));
+ place(&mut doc, other, rigid(Vec3::new(0.0, 40.0, 3.0), Vec3::ZERO, Vec3::Z, 0.0));
+ model.evaluate(&doc);
+ assert_eq!(model.last_recomputed, 1);
+}
+
#[test]
fn a_body_is_measured_once_while_it_stays_the_same() {
let mut doc = Document::new("measured once");
diff --git a/crates/doc/src/name_lookup.rs b/crates/doc/src/name_lookup.rs
index db61ecd373..a25f27b27d 100644
--- a/crates/doc/src/name_lookup.rs
+++ b/crates/doc/src/name_lookup.rs
@@ -339,6 +339,27 @@ pub fn locate_face(st: &ModelState, name: &str, p: Vec3) -> Option<FaceHit> {
locate_in(st.bodies.iter().enumerate().filter(|(_, b)| !b.body.is_mesh()), name, p).ok()
}
+/// The one body of `st` with a face the stored face name finds, as [`locate_face`] looks for it.
+/// `None` when no body has one, several do, or the name is one saved before format 14.
+pub(crate) fn face_body<'a>(st: &'a ModelState, name: &str) -> Option<&'a ModelBody> {
+ if let Some((body, _)) = crate::sheet::split_sheet_face_name(name) {
+ return st.bodies.iter().find(|b| b.name == body && !b.body.is_mesh());
+ }
+ if name.is_empty() || legacy_name(name).is_some() {
+ return None;
+ }
+ let solids = || st.bodies.iter().filter(|b| !b.body.is_mesh());
+ naming::warm_names(solids());
+ let mut holders = solids().filter(|b| {
+ let names = naming::names(b);
+ (0..names.faces.len()).any(|face| crate::history_names::matches(&names, face, name).is_some_and(exactly))
+ });
+ match (holders.next(), holders.next()) {
+ (Some(b), None) => Some(b),
+ _ => None,
+ }
+}
+
/// [`locate_face`] for a feature that works on the faces of the body called `body` (a shell, a
/// draft…): the face of that body the name finds, found without working out the names of every
/// other body (a body named from geometry is meshed for it: seconds in a large model). When