use super::bosses::{Boss, Section, not_dimensioned, read_polygon};
use super::envelope::profile_views;
use super::{AXES, Context, Measurement, Planner, Problem, Shape, Want, end_on, fmt, want_id};
use crate::bridge::Feature;
use crate::lint::polygonal::stock_wanted;
pub(super) const PLANNER: Planner = Planner {
kinds: &["polygonal_stock"],
wants,
};
fn read<'a>(cx: &Context<'a>) -> Vec<(&'a Feature, Result<Boss<'a>, String>)> {
let wanted = stock_wanted(cx.analysis);
cx.analysis
.features
.iter()
.filter(|f| f.kind == "polygonal_stock")
.map(|f| {
let broken = wanted
.iter()
.find_map(|w| (w.feature.id == f.id).then(|| w.values.clone().err())?);
let read = match broken {
Some(why) => Err(why),
None => read_polygon(f, &cx.frame),
};
(f, read)
})
.collect()
}
pub(super) fn whole(cx: &Context) -> bool {
read(cx).iter().any(|(_, r)| r.is_ok())
}
fn wants(cx: &Context) -> (Vec<Want>, Vec<Problem>) {
let mut out = Vec::new();
let mut problems = Vec::new();
for (f, read) in read(cx) {
let b = match read {
Ok(b) => b,
Err(why) => {
problems.push(not_dimensioned(f, &why));
continue;
}
};
let Section::Polygon {
sides,
across_flats,
flats,
..
} = &b.section
else {
unreachable!("a stock record is read as a prism")
};
let (Some(length), Some(base)) = (b.height, b.base) else {
unreachable!("the contract gives a stock a positive length")
};
let k = b.axis;
let across: Vec<&str> = (0..3).filter(|&j| j != k).map(|j| AXES[j]).collect();
let view = end_on(k);
let parameter = "polygon_across_flats.length";
out.push(Want {
id: want_id(&f.id, parameter),
views: vec![view],
value: *across_flats,
terms: vec![
if *sides == 6 {
"HEX".into()
} else {
format!("{sides}-SIDED")
},
fmt(*across_flats),
"A/F".into(),
],
measurements: vec![Measurement {
feature: f.id.clone(),
parameter: parameter.into(),
value: *across_flats,
}],
turning: None,
basis: format!(
"the part is {sides}-sided stock along {}: its size across flats is called out where its outline is seen, in place of the overall {} and {} extents, which depend on how the stock is turned",
AXES[k], across[0], across[1]
),
shape: Shape::Callout { arcs: Vec::new(),
sites: flats.iter().map(|c| (*c, 0.0)).collect(),
},
});
let views = profile_views(k);
let j = (0..3)
.find(|&j| j != k && profile_views(j).contains(&views[0]))
.expect("a profile view shows a radial axis");
let rim = |p: [f64; 3]| {
let mut p = p;
p[j] += b.half(j);
p
};
let parameter = "stock_length.length";
out.push(Want {
id: want_id(&f.id, parameter),
views,
value: length,
terms: vec![fmt(length)],
measurements: vec![Measurement {
feature: f.id.clone(),
parameter: parameter.into(),
value: length,
}],
turning: None,
basis: format!(
"the stock's length, end to end along its axis, in a view that shows its profile, in place of the overall {} extent (the same span)",
AXES[k]
),
shape: Shape::Linear {
a: rim(base),
b: rim(b.top),
axis: k,
a_edge: true,
b_edge: true,
order: 2,
},
});
}
(out, problems)
}