use serde_json::Value;
use super::envelope::profile_views;
use super::{
AXES, Context, Measurement, P3, Planner, Problem, Shape, Want, end_on, point, want_id,
};
use crate::bridge::Feature;
use crate::bridge::holes::{dir_to_part, principal};
pub(super) const PLANNER: Planner = Planner {
kinds: &["groove", "annular_groove", "face_groove"],
wants,
};
pub(super) fn not_dimensioned(f: &Feature, why: &str) -> Problem {
Problem {
id: format!("p:feature_not_dimensioned:{}", f.id),
code: "feature_not_dimensioned".into(),
severity: "warning".into(),
message: format!(
"{} is not dimensioned: {why}.",
crate::wording::capital(&crate::wording::feature(f))
),
subjects: vec![f.id.clone()],
..Default::default()
}
}
pub(super) fn size(f: &Feature, name: &str) -> Result<f64, String> {
f.parameters
.get(name)
.and_then(Value::as_f64)
.filter(|x| *x > 0.0)
.ok_or_else(|| format!("its record gives no positive {name}"))
}
fn want(cx: &Context, f: &Feature) -> Result<Want, String> {
let fmt = |v| crate::dims::fmt_in(v, cx.spec.units);
let p = &f.parameters;
let width = size(f, "width")?;
let diameter = size(f, "diameter")?;
let at = cx
.frame
.point(point(p.get("at")).ok_or("its record gives no at")?);
let axis = p
.get("axis")
.and_then(|a| cx.frame.letter(a))
.ok_or("its record gives no axis")?;
let k = principal(axis)
.ok_or("its axis is not along x, y or z, so no standard view shows its profile")?;
let r = 0.5 * diameter;
let sites: Vec<(P3, f64)> = (0..3)
.filter(|&j| j != k)
.flat_map(|j| {
[-r, r].map(|d| {
let mut s = at;
s[j] += d;
(s, 0.0)
})
})
.collect();
let views = profile_views(k);
Ok(Want {
units: cx.spec.units,
id: want_id(&f.id, "groove.length"),
views,
value: width,
terms: vec![
fmt(width),
"WIDE".into(),
"×".into(),
format!("ø{}", fmt(diameter)),
],
measurements: vec![
Measurement {
feature: f.id.clone(),
parameter: "groove.length".into(),
value: width,
},
Measurement {
feature: f.id.clone(),
parameter: "groove.diameter".into(),
value: diameter,
},
],
turning: None,
basis: format!(
"a groove's axial width and floor diameter are called out together, in a view that shows the profile about {}",
AXES[k]
),
shape: Shape::Callout {
sites,
arcs: Vec::new(),
},
})
}
fn owner(cx: &Context, f: &Feature) -> Result<(), String> {
let mut profiles: Vec<&Value> = cx
.analysis
.features
.iter()
.filter(|s| s.kind == "turned_step")
.filter_map(|s| s.parameters.get("profile"))
.collect();
profiles.sort_by_key(|p| p.to_string());
profiles.dedup();
if profiles.len() < 2
|| f.parameters
.get("profile")
.is_some_and(|p| profiles.contains(&p))
{
return Ok(());
}
Err(format!(
"the part has {} turned profiles and its record names none of them, so it has no one owner",
profiles.len()
))
}
type Line<'a> = (usize, P3, Option<&'a Value>);
fn line<'a>(cx: &Context, f: &'a Feature) -> Option<Line<'a>> {
let p = &f.parameters;
let at = cx.frame.point(point(p.get("at"))?);
let k = principal(cx.frame.letter(p.get("axis")?)?)?;
Some((k, at, p.get("profile")))
}
pub(super) fn annular(cx: &Context, f: &Feature) -> Result<Vec<Want>, String> {
let fmt = |v| crate::dims::fmt_in(v, cx.spec.units);
let p = &f.parameters;
let face = f.kind == "face_groove";
let outer = if face {
size(f, "outer_diameter")?
} else {
2.0 * size(f, "outer_radius")?
};
let inner = if face {
size(f, "inner_diameter")?
} else {
2.0 * size(f, "inner_radius")?
};
let depth = size(f, "depth")?;
let axis = dir_to_part(
&cx.frame.0,
point(p.get("axis")).ok_or("its record gives no axis")?,
);
let k = principal(axis)
.ok_or("its axis is not along x, y or z, so no standard view shows it end-on")?;
let floor = cx.frame.point(
point(p.get(if face { "axis_point" } else { "floor_center" }))
.ok_or("its record gives no floor centre")?,
);
let mut mouth = floor;
if !face {
mouth[k] += axis[k].signum() * depth;
}
let view = end_on(k);
let m = |parameter: &str, value: f64| Measurement {
feature: f.id.clone(),
parameter: parameter.into(),
value,
};
let mut out = vec![Want { units: cx.spec.units,
id: want_id(&f.id, "annular_groove_outer.diameter"),
views: vec![view],
value: outer,
terms: vec![
format!("⌀{}", fmt(outer)),
"×".into(),
format!("⌀{}", fmt(inner)),
"↧".into(),
fmt(depth),
],
measurements: vec![
m("annular_groove_outer.diameter", outer),
m("annular_groove_inner.diameter", inner),
m("annular_groove_depth.length", depth),
],
turning: None,
basis: "an annular groove's outer and inner diameters and depth are called out together, in the view down its axis".into(),
shape: Shape::Callout {
sites: vec![(mouth, 0.5 * outer)],
arcs: Vec::new(),
},
}];
if face {
for (name, word) in [("outer_wall_angle", "OUTER"), ("inner_wall_angle", "INNER")] {
let angle = size(f, name)?;
if (angle - 90.0).abs() > 1e-3 {
out[0]
.terms
.extend([word.into(), format!("{}°", crate::dims::fmt(angle))]);
out[0]
.measurements
.push(m(&format!("face_groove_{name}.angle"), angle));
}
}
}
let concentric = face
&& crate::bridge::turned_body(cx.analysis).is_some_and(|(a, on, _)| {
a == k && (0..3).all(|j| j == k || (mouth[j] - on[j]).abs() <= 1e-3)
});
let lo = cx.analysis.envelope[0];
for j in (0..3).filter(|&j| j != k) {
if concentric && !cx.spec.origin.contains_key(AXES[j]) {
continue;
}
let value = mouth[j] - lo[j];
if value.abs() < 1e-6 {
continue;
}
let parameter = format!("location.length.{}", AXES[j]);
let mut a = mouth;
a[j] = lo[j];
out.push(Want { units: cx.spec.units,
id: want_id(&f.id, ¶meter),
views: vec![view],
value,
terms: vec![fmt(value)],
measurements: vec![m(¶meter, value)],
turning: None,
basis: format!(
"an annular groove's centre located in {} from the part's {} face, in the view down its axis",
AXES[j],
["left", "front", "bottom"][j]
),
shape: Shape::Linear {
a,
b: mouth,
axis: j,
a_edge: true,
b_edge: false,
order: 1,
},
});
}
Ok(out)
}
fn wants(cx: &Context) -> (Vec<Want>, Vec<Problem>) {
let mut out: Vec<(Want, Option<Line>)> = Vec::new();
let mut problems = Vec::new();
for f in cx.analysis.features.iter().filter(|f| {
(f.kind == "annular_groove"
&& crate::lint::grooves::annular_owners(cx.analysis, f).is_none())
|| f.kind == "face_groove"
}) {
match annular(cx, f) {
Ok(ws) => {
let on = if f.kind == "face_groove" {
point(f.parameters.get("axis"))
.and_then(|d| principal(dir_to_part(&cx.frame.0, d)))
.zip(point(f.parameters.get("axis_point")).map(|p| cx.frame.point(p)))
.map(|(k, p)| (k, p, None))
} else {
None
};
for w in ws {
let same =
if f.kind == "face_groove" && matches!(w.shape, Shape::Callout { .. }) {
out.iter().position(|(o, l)| {
on.zip(*l).is_some_and(|((k, p, _), (j, q, _))| {
k == j && (0..3).all(|a| a == k || (p[a] - q[a]).abs() <= 1e-3)
}) && o.terms.ends_with(&w.terms)
&& o.measurements
.iter()
.any(|m| m.feature.starts_with("f:face_groove:"))
})
} else {
None
};
if let Some(i) = same {
let o = &mut out[i].0;
o.measurements.extend(w.measurements);
if let (Shape::Callout { sites, .. }, Shape::Callout { sites: more, .. }) =
(&mut o.shape, w.shape)
{
sites.extend(more);
}
let n = o
.measurements
.iter()
.map(|m| &m.feature)
.collect::<std::collections::BTreeSet<_>>()
.len();
if n == 2 {
o.terms.insert(0, String::new());
}
o.terms[0] = format!("{n}×");
o.basis = "equal annular face recesses share one counted callout, retaining each rim diameter, depth and sloping wall angle".into();
} else {
out.push((w, on));
}
}
}
Err(why) => problems.push(not_dimensioned(f, &why)),
}
}
for f in cx.analysis.features.iter().filter(|f| f.kind == "groove") {
let w = match owner(cx, f).and_then(|()| want(cx, f)) {
Ok(w) => w,
Err(why) => {
problems.push(not_dimensioned(f, &why));
continue;
}
};
let on = line(cx, f);
let same = on.and_then(|(k, at, profile)| {
out.iter().position(|(o, l)| {
o.terms.ends_with(&w.terms)
&& l.is_some_and(|(k2, at2, profile2)| {
k2 == k
&& profile2 == profile
&& (0..3).all(|j| j == k || (at[j] - at2[j]).abs() <= 1e-3)
})
})
});
let (Some(i), Some((k, ..))) = (same, on) else {
out.push((w, on));
continue;
};
let o = &mut out[i].0;
o.measurements.extend(w.measurements);
if let (Shape::Callout { sites, .. }, Shape::Callout { sites: more, .. }) =
(&mut o.shape, w.shape)
{
sites.extend(more);
}
let n = o.measurements.len() / 2;
if n == 2 {
o.terms.insert(0, String::new());
}
o.terms[0] = format!("{n}×");
o.basis = format!(
"{n} grooves of one turned profile have one width and floor diameter, so one callout counts them, in a view that shows the profile about {}",
AXES[k]
);
}
(out.into_iter().map(|(w, _)| w).collect(), problems)
}