use crate::board::{Footprint, Pad, PadShape, Shape};
use crate::Point;
pub const SEAT_TOLERANCE: i32 = 50;
pub const TOP_SLAB: i32 = 50;
pub use crate::board::SUBSTRATE_THICKNESS;
#[derive(Clone, Debug, PartialEq)]
pub enum SeatContact {
Seat(Point),
Body {
top: Vec<Point>,
through: Vec<Point>,
lowest: i32,
thickness: i32,
},
}
#[derive(Clone, Debug, PartialEq)]
pub enum SeatVerdict {
OnPad,
NoPad,
OffPad { by: i32 },
OffDrill { by: i32 },
NoReach { lowest: i32 },
NotThrough,
SpansPads { pads: Vec<String> },
}
fn copper(pad: &Pad) -> Shape {
match pad.corners() {
Some(corners) => Shape::polygon(corners, pad.effective_corner_radius()),
None => Shape::rounded_pad(pad.at, pad.size, pad.shape, pad.effective_corner_radius()),
}
}
fn hole(pad: &Pad) -> Option<Shape> {
let drill = pad.drill?;
Some(match pad.drill_height {
Some(height) => Shape::rounded_pad(pad.at, Point::new(drill, height), PadShape::Oval, 0),
None => Shape::circle(pad.at, drill / 2),
})
}
fn excursion(shape: &Shape, points: &[Point]) -> f64 {
points.iter().map(|p| shape.distance_to_point(*p)).fold(0., f64::max)
}
pub fn pad_seat_check(footprint: &Footprint, pin: &str, contact: &SeatContact) -> SeatVerdict {
let pin = pin.trim();
let pads: Vec<&Pad> = footprint.pads.iter().filter(|pad| pad.number.trim() == pin).collect();
if pads.is_empty() {
return SeatVerdict::NoPad;
}
let tolerance = f64::from(SEAT_TOLERANCE);
let round = |by: f64| by.round() as i32;
match contact {
SeatContact::Seat(at) => {
let by = pads.iter().map(|pad| copper(pad).distance_to_point(*at)).fold(f64::INFINITY, f64::min);
if by <= tolerance { SeatVerdict::OnPad } else { SeatVerdict::OffPad { by: round(by) } }
}
SeatContact::Body { top, through, lowest, .. } => {
let mut touched: Vec<String> = footprint
.pads
.iter()
.filter(|pad| !pad.number.trim().is_empty())
.filter(|pad| top.iter().chain(through).any(|p| copper(pad).distance_to_point(*p) <= 0.))
.map(|pad| pad.number.trim().to_owned())
.collect();
touched.sort();
touched.dedup();
if touched.len() > 1 {
return SeatVerdict::SpansPads { pads: touched };
}
let drilled: Vec<(&Pad, Shape)> = pads.iter().filter_map(|pad| Some((*pad, hole(pad)?))).collect();
if !drilled.is_empty() {
if through.is_empty() {
return if top.is_empty() && *lowest > TOP_SLAB {
SeatVerdict::NoReach { lowest: *lowest }
} else {
SeatVerdict::NotThrough
};
}
let by = drilled.iter().map(|(_, hole)| excursion(hole, through)).fold(f64::INFINITY, f64::min);
return if by <= tolerance { SeatVerdict::OnPad } else { SeatVerdict::OffDrill { by: round(by) } };
}
if top.is_empty() {
return if *lowest > TOP_SLAB {
SeatVerdict::NoReach { lowest: *lowest }
} else {
let by = pads.iter().map(|pad| excursion(&copper(pad), through)).fold(f64::INFINITY, f64::min);
SeatVerdict::OffPad { by: round(by.max(tolerance + 1.)) }
};
}
let by = pads.iter().map(|pad| excursion(&copper(pad), top)).fold(f64::INFINITY, f64::min);
if by <= tolerance { SeatVerdict::OnPad } else { SeatVerdict::OffPad { by: round(by) } }
}
}
}
fn mm(um: i32) -> String {
format!("{:.2}", f64::from(um) / 1_000.)
}
impl SeatVerdict {
pub fn passes(&self) -> bool {
*self == SeatVerdict::OnPad
}
pub fn message(&self, pin: &str, contact: &SeatContact, assumed: bool) -> Option<String> {
let thickness = match contact {
SeatContact::Body { thickness, .. } if assumed => format!(" (read through the {} mm board every board is built with)", mm(*thickness)),
_ => String::new(),
};
Some(match self {
SeatVerdict::OnPad => return None,
SeatVerdict::NoPad => format!("Pin {pin} has no pad: the footprint has no pad numbered {pin}"),
SeatVerdict::OffPad { by } => format!("Pin {pin} seats {} mm outside pad {pin}", mm(*by)),
SeatVerdict::OffDrill { by } => format!("Pin {pin} passes {} mm outside the drill of pad {pin}{thickness}", mm(*by)),
SeatVerdict::NoReach { lowest } => {
format!("Pin {pin} does not reach the board: its contact body ends {} mm above it", mm(*lowest))
}
SeatVerdict::NotThrough => format!("Pin {pin} does not go into the drill of pad {pin}{thickness}"),
SeatVerdict::SpansPads { pads } => format!(
"Pin {pin}'s contact body lands on pads {}: name the pin's own solid, or seat the point on a face",
pads.join(", ")
),
})
}
}