Extrude to Object: a Join extrude also joins the target body (#6424) (#6471)

* claim: #6424

* fix: a join extrude also joins the body it targets (#6424)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Homero Thompson
2026-10-27 00:53:25 +00:00
committed by GitHub
parent 5622963b91
commit 622a392a83
2 changed files with 62 additions and 4 deletions
+36 -4
View File
@@ -2914,13 +2914,27 @@ fn empty_boolean_targets(doc: &Document, state: &ModelState, f: &Feature, tools:
/// [`apply_op`] with tools whose lineage is known: the bodies it makes or changes keep theirs.
fn apply_traced(doc: &Document, state: &mut ModelState, f: &Feature, tools: Vec<Traced>, op: Operation, targets: &[String]) -> Result<()> {
apply_traced_with(doc, state, f, tools, op, targets, &[])
}
/// [`apply_traced`] with bodies that take part in the boolean besides the feature's targets:
/// the body a Join extrude runs to (#6424), which joins with the tool.
fn apply_traced_with(
doc: &Document,
state: &mut ModelState,
f: &Feature,
tools: Vec<Traced>,
op: Operation,
targets: &[String],
extra: &[String],
) -> Result<()> {
let tools = if op == Operation::Intersect { intersection_tool_union(tools)? } else { tools };
// Participants: the feature's own targets, else its explicit participants, else the bodies
// of its component (and what it made itself): other components are left alone.
let targets: &[String] = if targets.is_empty() { &f.participants } else { targets };
let all = targets.len() == 1 && targets.first().is_some_and(|t| t == "*");
let named = !all && !targets.is_empty();
let target_idx: Vec<usize> = if all {
let mut target_idx: Vec<usize> = if all {
(0..state.bodies.len()).collect()
} else if targets.is_empty() {
(0..state.bodies.len()).filter(|i| state.bodies.get(*i).is_some_and(|b| b.component == f.component || b.feature == f.id)).collect()
@@ -2931,6 +2945,15 @@ fn apply_traced(doc: &Document, state: &mut ModelState, f: &Feature, tools: Vec<
}
v
};
// Bodies that take part besides the feature's own targets: the body a Join extrude runs to
// joins with the tool (#6424).
for name in extra {
if let Some(i) = state.bodies.iter().position(|b| &b.name == name)
&& !target_idx.contains(&i)
{
target_idx.push(i);
}
}
if op == Operation::NewBody || target_idx.is_empty() {
if op == Operation::Cut || op == Operation::Intersect {
return Err(empty_boolean_targets(doc, state, f, &tools, targets.is_empty()));
@@ -4990,13 +5013,16 @@ pub(crate) fn thin_regions(vals: &BTreeMap<String, Value>, ss: &SolvedSketch, th
const APART_GAP: f64 = 1e-3;
/// The tool bodies a feature adds or removes (extrude, revolve, primitives), in the current state.
/// `to_body` is the body a To Object extent runs to (an extrude), when it found one.
fn feature_tools_with_constructor(
doc: &Document,
vals: &BTreeMap<String, Value>,
f: &Feature,
st: &ModelState,
constructor: &mut Option<ExtrudeConstructor>,
to_body: &mut Option<String>,
) -> Result<Vec<Traced>> {
*to_body = None;
let unnamed = |bodies: Vec<Body>| -> Vec<Traced> { bodies.into_iter().map(Traced::unnamed).collect() };
match &f.kind {
FeatureKind::Extrude { sketch, profiles, extent, operation, targets, thin } => {
@@ -5030,6 +5056,8 @@ 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());
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);
@@ -6599,7 +6627,7 @@ fn replay(
// points follow their named faces, as the feature's own did (#2121): a To Object
// extrude's copies reach the face where it is now, not where it was picked.
let moved = with_named_face_points(doc, st, src).map(|(g, _)| g);
let tools = feature_tools_with_constructor(doc, vals, moved.as_ref().unwrap_or(src), st, &mut None)?;
let tools = feature_tools_with_constructor(doc, vals, moved.as_ref().unwrap_or(src), st, &mut None, &mut None)?;
for (k, m) in mats.iter().enumerate() {
let start = copies.len();
for t in &tools {
@@ -7124,7 +7152,8 @@ fn eval_feature(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature, st:
| FeatureKind::Coil { operation, targets, .. }
| FeatureKind::Sweep { operation, targets, .. } => {
let mut constructor = None;
let tools = feature_tools_with_constructor(doc, vals, f, st, &mut constructor)?;
let mut to_body = None;
let tools = feature_tools_with_constructor(doc, vals, f, st, &mut constructor, &mut to_body)?;
// Cuts through sheet metal become cut-outs of its flat pattern.
if matches!(operation, Operation::Cut | Operation::Intersect) && matches!(f.kind, FeatureKind::Extrude { .. }) && !st.sheets.is_empty() {
let bodies: Vec<Body> = tools.iter().map(|t| t.body.clone()).collect();
@@ -7133,7 +7162,10 @@ fn eval_feature(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature, st:
return apply_beside_sheet_cut(doc, st, f, tools, *operation, &crate::plain(targets), &handled);
}
}
apply_traced(doc, st, f, tools, *operation, &crate::plain(targets))
// A Join extrude to an object joins that object too (#6424): the tool and the body
// it runs to are one body, not only the body the profile was sketched on.
let extra: Vec<String> = if *operation == Operation::Join { to_body.into_iter().collect() } else { Vec::new() };
apply_traced_with(doc, st, f, tools, *operation, &crate::plain(targets), &extra)
}
FeatureKind::Hole { thread, thread_depth, diameter, handedness, .. } => {
let thread_depth = thread_depth.as_ref().map(|d| val(vals, d, Kind::Length)).transpose()?;
+26
View File
@@ -166,3 +166,29 @@ fn join_of_a_profile_sketched_on_a_face_leaves_a_neighbour_it_lies_on() {
assert_eq!(targets, &["Bracket"]);
}
}
/// #6424: an extrude that runs to another body and joins it makes one body of the tool, the
/// body it was sketched on and the body it runs to.
#[test]
fn join_extrude_to_another_body_joins_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"}));
// A profile on the base's top face, extruded up to the target's bottom face.
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!({}));
// The join names the body the profile was sketched on; the body it runs to joins too.
run(&mut s, "solid.extrude", json!({"sketch":sketch,"to":[20,20,40],"operation":"join","targets":["Base"]}));
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(), 1, "the tool, the base and the target are one 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");
}