dc38349cbf
* Kernel: the B-rep record and the native body file come from solidcore-record SolidCraft's portable B-rep record (`solidcraft_geom::brep`: brep.rs, topology, history, envelope, part, curve2) and the native body file (`solidcraft_kernel::native`: the tagged binary codec and the container) move to solidcore's `solidcore-record` crate: geometry as data, with structural validation and no maths (solidcore's docs/layers.md, stage 1). Every caller uses `solidcore_record::brep` and `solidcore_record::native`; the old copies are deleted. What computes from coordinates stays with the kernel, in geom's `brep`: - `brep::check`: the record's geometric checks (orthonormal frames, non-degenerate planes, unit revolution axes, non-singular placements). `check::validate` and `check::validate_built` are the record's validation and these together, and every caller of `Brep::validate` uses them, so what is accepted is unchanged; - `brep::label`: a surface's natural ranges (a cone's apex), `curve_interval`, `edge_curve_interval` and `form_turns`; - `brep::pcurve::eval`: a pcurve's point; - `geometry::check`, `placement::moved` and `split_edges` become functions on the record. The kernel's native module keeps the conversion between a body and its saved record (metadata.rs) and checks the decoded record's geometry. `native/analytic.rs`, which no module declared, is deleted. solidcore-record is pinned to its rev on solidcore's `layer-record` branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NvoxKMbMFSQU8tzRtoJhtT * Geometry: Vec2, Vec3, Plane and the profile types come from solidcore-record The plain data of vectors and points, sketch planes and planar profiles (`Seg2`, `Loop2`, `Region2`) moves to `solidcore-record`, with its constants and component-wise operators only (owner decision, solidcore layer split). Their geometry stays in geom as traits: `Vec2Geom`, `Vec3Geom`, `PlaneGeom`, `Seg2Geom`, `Loop2Geom`, `Region2Geom`, `Seg2ArcFit`, `Loop2ArcFit` and `Region2ArcFit`, in scope with `use solidcraft_geom::prelude::*`. Every caller names the types from `solidcore_record`; geom no longer defines or re-exports them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NvoxKMbMFSQU8tzRtoJhtT * Kernel: pin solidcore-record to solidcore main f5e8e9a29 solidcore-record is merged on solidcore main (tangency-labs/solidcore#2519); its crate there is identical to the rev pinned before but for its clippy configuration. The lock is regenerated against the merged rev. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NvoxKMbMFSQU8tzRtoJhtT --------- Co-authored-by: jyviko <2501648+jyviko@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
5.8 KiB
Rust
129 lines
5.8 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 solidcraft_geom::prelude::*;
|
|
|
|
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);
|
|
}
|
|
if let Ok(world) = after.world_state() {
|
|
world.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;
|