02ac56d12f
* claim: #6901 * Feature windows choose Join, New Body or Cut from what the material touches (#6901) Extrude, Revolve, Loft, Sweep, Coil and the Box, Cylinder, Sphere and Torus choose their operation by running the Join on a copy of the design off the window thread and counting the pieces: Join when the material makes one body with what it touches, New Body when it would be a separate piece, Cut when it lies inside a body (Revolve and the four primitives; Extrude keeps its own Cut). The choice changes only when a check is in, a pick in the Operation row (the shown option included) stops it for good, and OK waits for a running check. Hidden bodies take no part: with some hidden, the shown ones become the feature's targets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Operation row: a pick in Extrude stops its own choice at once; ja/zh strings, gallery entry Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Operation check: contact test first, mesh volumes for Cut, OK takes the evaluated Join Material touching no body is a New Body without running the Join (the contact test costs 0-240 ms where the Join costs seconds on a large body). Inside is told from display-mesh volumes instead of exact ones (8 s on a 400-hole plate). The check evaluates the Join through Session::ahead, and OK of the same commands takes it (Session::prime) rather than computing it again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Preference: Choose the operation automatically (on by default) Preferences > Design gets "Choose the operation automatically" (auto_operation, on by default). Off, feature windows don't choose Join, New Body or Cut from what their material touches: each keeps the operation it opens with (Extrude its own Join or Cut), no check runs and OK doesn't wait for one, as before Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Arabic, Hebrew, Vietnamese and Turkish strings for the operation choice and its preference Arabic, Hebrew and Vietnamese are complete catalogs now, and Turkish has a full catalog, so this change's three strings get them too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Operation check: a closed window's check stops; the icon audit doesn't choose A window's operation check kept running after the window closed, a Join on the design for nothing, and the test harness waited for it after every step: the icon audit, which opens every window, took ~30 min instead of ~1. The check now runs under a Progress, and closing the window (or one that doesn't choose) abandons it: its evaluation stops at the next feature and nothing waits for it. The icon audit turns auto_operation off, as it only looks at icons (75 s again). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Box placement test: the window keeps its New Body The test #6248 added places a box that overlaps Base and expects a second body; with #6901 the window would join or cut it. The test is about where the box goes, so it turns auto_operation off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Join pieces: read the target lists leniently after the rebase Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
126 lines
5.7 KiB
Rust
126 lines
5.7 KiB
Rust
//! What a feature's material does when it is joined: the test the feature windows use to choose
|
|
//! Join, New Body or Cut by themselves (#6901). It runs the Join itself, so it agrees with the
|
|
//! result OK makes, and with the warning a Join apart gets (#4434).
|
|
|
|
use serde_json::Value;
|
|
|
|
use crate::Session;
|
|
|
|
/// The most commands a feature window previews at once.
|
|
const MAX_COMMANDS: usize = 64;
|
|
|
|
/// What a feature's material makes, joined to the bodies it may join.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum Addition {
|
|
/// It would be a separate piece (or there is no body to join): a New Body.
|
|
Apart,
|
|
/// It makes one body with what it touches: a Join.
|
|
Joins,
|
|
/// It adds no material: it lies inside a body (a Cut, where the window offers one).
|
|
Inside,
|
|
}
|
|
|
|
/// What `cmds` (a feature window's commands, with operation New Body or Join) make when run with
|
|
/// Join (see [`crate::Ahead::addition`]).
|
|
pub fn addition(s: &Session, cmds: &[(String, Value)], inside: bool) -> Option<Addition> {
|
|
s.ahead().addition(cmds, inside).0
|
|
}
|
|
|
|
impl crate::Ahead {
|
|
/// What `cmds` (a feature window's commands, with operation New Body or Join) make when run
|
|
/// with Join, and, when they join, the Join evaluated (OK takes it rather than computing it
|
|
/// again, [`crate::Session::prime`]). `inside`: tell material lying inside a body
|
|
/// ([`Addition::Inside`]) from material that joins. None when they have no such operation
|
|
/// (Combine, a Cut chosen by hand, a feature without one), the model has no body, or the Join
|
|
/// fails.
|
|
///
|
|
/// Material that touches no body can't be fused to one: that is found from the material alone
|
|
/// and a contact test, without the Join (a boolean that takes seconds on a large body).
|
|
pub fn addition(self, cmds: &[(String, Value)], inside: bool) -> (Option<Addition>, Option<crate::Evaluated>) {
|
|
let s = self.0;
|
|
let before = s.model.state();
|
|
if cmds.is_empty() || cmds.len() > MAX_COMMANDS || before.bodies.is_empty() {
|
|
return (None, None);
|
|
}
|
|
let (Some(alone), Some(joined)) = (with_operation(cmds, "new"), with_operation(cmds, "join")) else { return (None, None) };
|
|
let Ok(tools) = s.preview(&alone) else { return (None, None) };
|
|
let tools: Vec<&solidcraft_doc::ModelBody> = tools.after.bodies.iter().filter(|b| tools.before.body(&b.name).is_none()).collect();
|
|
// Every body the Join could take part in (its scope is narrower): none touched, none joined.
|
|
let named: Vec<String> = joined
|
|
.iter()
|
|
.flat_map(|(_, p)| {
|
|
let p = p.get("params").filter(|_| p.get("command").is_some()).unwrap_or(p);
|
|
crate::params::string_list_or_default(p, "targets").into_iter().chain(crate::params::string_list_or_default(p, "participants"))
|
|
})
|
|
.collect();
|
|
let candidates: Vec<&solidcraft_doc::ModelBody> = before
|
|
.bodies
|
|
.iter()
|
|
.filter(|b| named.is_empty() || named.iter().any(|n| n == "*" || *n == b.name))
|
|
.filter(|b| !b.body.is_mesh())
|
|
.collect();
|
|
if !tools.is_empty() && !tools.iter().any(|t| candidates.iter().any(|b| t.body.meets(&b.body).unwrap_or(true))) {
|
|
return (Some(Addition::Apart), None);
|
|
}
|
|
let Ok((after, _)) = s.preview_in_place(&joined, None) else { return (None, None) };
|
|
let state = after.model.state();
|
|
// A Join with nothing to join makes a new body.
|
|
if state.bodies.iter().any(|b| before.body(&b.name).is_none()) || !solidcraft_doc::join_pieces_added(&before, &state).is_empty() {
|
|
return (Some(Addition::Apart), None);
|
|
}
|
|
if inside && adds_nothing(&before, &state, &tools) {
|
|
return (Some(Addition::Inside), None);
|
|
}
|
|
after.world_state().warm_display();
|
|
(Some(Addition::Joins), Some(crate::Evaluated(after.model)))
|
|
}
|
|
}
|
|
|
|
/// Whether the Join from `before` to `after` added (next to) no volume: its material lies inside
|
|
/// the bodies. Display meshes are close enough for that, against the material's own volume;
|
|
/// measuring a large body exactly takes seconds.
|
|
fn adds_nothing(before: &solidcraft_doc::ModelState, after: &solidcraft_doc::ModelState, tools: &[&solidcraft_doc::ModelBody]) -> bool {
|
|
let (mut had, mut have) = (0.0, 0.0);
|
|
for was in &before.bodies {
|
|
match after.body(&was.name) {
|
|
Some(now) if now.body.same_shape(&was.body) => {}
|
|
Some(now) => {
|
|
had += mesh_volume(&was.mesh());
|
|
have += mesh_volume(&now.mesh());
|
|
}
|
|
None => had += mesh_volume(&was.mesh()),
|
|
}
|
|
}
|
|
let material: f64 = tools.iter().map(|t| mesh_volume(&t.mesh())).sum();
|
|
material > 0.0 && (have - had) < material * 0.01
|
|
}
|
|
|
|
/// The volume a closed mesh encloses.
|
|
fn mesh_volume(m: &solidcraft_geom::Mesh) -> f64 {
|
|
m.triangles.iter().filter_map(|t| m.tri(t)).map(|[a, b, c]| a.dot(b.cross(c)) / 6.0).sum::<f64>().abs()
|
|
}
|
|
|
|
/// `cmds` with their New Body or Join operation set to `op`; None when none has one.
|
|
fn with_operation(cmds: &[(String, Value)], op: &str) -> Option<Vec<(String, Value)>> {
|
|
let mut out = cmds.to_vec();
|
|
let mut any = false;
|
|
for (command, p) in &mut out {
|
|
let inner = p.get("command").and_then(Value::as_str).unwrap_or(command.as_str());
|
|
if inner == "solid.combine" {
|
|
return None;
|
|
}
|
|
let fields = if p.get("command").is_some() { p.get_mut("params")? } else { p };
|
|
if let Some(o) = fields.get_mut("operation")
|
|
&& o.as_str().is_some_and(|o| o == "new" || o == "join")
|
|
{
|
|
*o = Value::from(op);
|
|
any = true;
|
|
}
|
|
}
|
|
any.then_some(out)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "join_pieces_tests.rs"]
|
|
mod tests;
|