use serde_json::Value;
use super::levels::{Through, ownership};
use super::{AXES, Context, Measurement, Planner, Problem, QFrame, Shape, Want, end_on, point};
use crate::bridge::Feature;
use crate::bridge::holes::principal;
pub(super) const PLANNER: Planner = Planner {
kinds: &["through_step", "circular_blind_step", "paired_ramp_step"],
wants,
};
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()
}
}
fn size(f: &Feature, name: &str) -> Result<f64, String> {
match f.parameters.get(name).and_then(Value::as_f64) {
Some(v) if v.is_finite() && v > 0.0 => Ok(v),
Some(v) => Err(format!("its {name} {v} is not a positive size")),
None => Err(format!("its record gives no {name}")),
}
}
fn run(f: &Feature, frame: &QFrame) -> Result<usize, String> {
let letter = f.parameters.get("axis").ok_or("its record gives no axis")?;
frame
.letter(letter)
.and_then(principal)
.ok_or_else(|| "its run lies along no axis of the part".into())
}
fn through_wants(s: &Through, units: crate::units::Units) -> Vec<Want> {
let fmt = |v| crate::dims::fmt_in(v, units);
let view = end_on(s.run);
let id = &s.feature.id;
s.legs()
.into_iter()
.map(|(k, a, b)| {
let value = (b[k] - a[k]).abs();
let parameter = format!("through_step_leg.length.{}", AXES[k]);
Want { units,
id: super::want_id(id, ¶meter),
views: vec![view],
value,
terms: vec![fmt(value)],
measurements: vec![Measurement {
feature: id.clone(),
parameter,
value,
}],
basis: format!(
"a rectangular through step states both legs of its open section, in the view that looks along its run: this leg runs along {}",
AXES[k]
),
shape: Shape::Linear {
a,
b,
axis: k,
a_edge: false,
b_edge: false,
order: 0,
},
turning: None,
}
})
.collect()
}
fn circular(
f: &Feature,
frame: &QFrame,
analysis: &crate::model::Analysis,
units: crate::units::Units,
) -> Result<Want, String> {
let fmt = |v| crate::dims::fmt_in(v, units);
let r = size(f, "radius")?;
let depth = size(f, "length")?;
let k = run(f, frame)?;
let ends: Vec<_> = f
.parameters
.get("centreline")
.and_then(Value::as_array)
.ok_or("its record gives no centreline")?
.iter()
.map(|p| point(Some(p)))
.collect::<Option<_>>()
.ok_or("its centreline is not points")?;
let [a, b] = ends[..] else {
return Err("its centreline is not two points".into());
};
let centre = frame.point([0, 1, 2].map(|j| 0.5 * (a[j] + b[j])));
let view = end_on(k);
let terms = vec![
format!("R{}", fmt(r)),
"×".into(),
fmt(depth),
"DEEP".into(),
];
Ok(Want { units,
id: super::want_id(&f.id, "circular_step_radius.radius"),
views: vec![view],
value: r,
terms,
measurements: vec![
Measurement {
feature: f.id.clone(),
parameter: "circular_step_radius.radius".into(),
value: r,
},
Measurement {
feature: f.id.clone(),
parameter: "circular_step_depth.length".into(),
value: depth,
},
],
basis: "a circular blind step states its radius and blind depth in one callout, in the view that sees its arc".into(),
shape: Shape::Callout {
arcs: analysis.profile_arcs.get(&f.id).cloned().ok_or("its trimmed circular boundary is unavailable")?,
sites: vec![(centre, r)],
},
turning: None,
})
}
fn ramp(f: &Feature, frame: &QFrame, units: crate::units::Units) -> Result<Want, String> {
let fmt = |v| crate::dims::fmt_in(v, units);
let angle = size(f, "angle")?;
if angle >= 90.0 {
return Err(format!("its angle {angle} is not acute"));
}
let length = size(f, "length")?;
let k = run(f, frame)?;
let at = point(f.parameters.get("at")).ok_or("its record gives no at")?;
let view = end_on(k);
let terms = vec![
"2×".into(),
format!("{}°", crate::dims::fmt(angle)),
"×".into(),
fmt(length),
"RUN".into(),
];
Ok(Want { units,
id: super::want_id(&f.id, "ramp_angle.angle"),
views: vec![view],
value: angle,
terms,
measurements: vec![
Measurement {
feature: f.id.clone(),
parameter: "ramp_angle.angle".into(),
value: angle,
},
Measurement {
feature: f.id.clone(),
parameter: "ramp_run.length".into(),
value: length,
},
],
basis: "a paired-ramp step states its two equal ramp angles and their run in one callout, in the view that sees the ramps' section".into(),
shape: Shape::Callout { arcs: Vec::new(),
sites: vec![(frame.point(at), 0.0)],
},
turning: None,
})
}
fn wants(cx: &Context) -> (Vec<Want>, Vec<Problem>) {
let fmt = |v| crate::dims::fmt_in(v, cx.spec.units);
let mut out = Vec::new();
let mut problems = Vec::new();
let o = ownership(cx);
for s in &o.lowered {
out.extend(through_wants(s, cx.spec.units));
}
for (s, owners) in &o.legacy {
let f = s.feature;
let owners: Vec<String> = owners
.iter()
.map(|o| match o.strip_suffix("'s thickness") {
Some(plate) => {
let p = cx.analysis.features.iter().find(|f| f.id == plate);
let size = |name: &str| p.and_then(|p| p.parameters.get(name)?.as_f64());
match size("hi").zip(size("lo")) {
Some((hi, lo)) => format!("the {} plate thickness", fmt(hi - lo)),
None => "a plate's thickness".into(),
}
}
None => o.clone(),
})
.collect();
let named = match owners.split_last() {
Some((last, [])) => last.clone(),
Some((last, rest)) => format!("{} and {last}", rest.join(", ")),
None => String::new(),
};
problems.push(Problem {
id: format!("p:dimensions_not_ported:{}", f.id),
code: "dimensions_not_ported".into(),
severity: "info".into(),
message: format!(
"{} is not dimensioned as a through step: as in draftwright, its legs are stated by {named}, but those dimensions do not claim it.",
crate::wording::capital(&crate::wording::feature(f))
),
subjects: vec![f.id.clone()],
..Default::default()
});
}
for f in &cx.analysis.features {
let want = match f.kind.as_str() {
"circular_blind_step" => circular(f, &cx.frame, cx.analysis, cx.spec.units),
"paired_ramp_step" => ramp(f, &cx.frame, cx.spec.units),
_ => continue,
};
match want {
Ok(w) => out.push(w),
Err(why) => problems.push(not_dimensioned(f, &why)),
}
}
(out, problems)
}