Skip to main content
← Back to kicad-monkey Rust Docs kicad-monkey Rust Crate API Reference

kicad_monkey_core/pcb/
fabrication.rs

1//! Observed fabrication dimensions over current source records, not design-rule minima.
2
3use super::*;
4
5/// Scalar pad/via envelope statistic in millimetres; not a copper-union clearance proof.
6#[derive(Clone, Debug, Default, PartialEq)]
7pub struct PcbAnnularRingStats {
8    /// Minimum eligible source envelope ring, rounded to six decimal millimetres.
9    pub minimum_mm: Option<f64>,
10    /// Eligible nonnegative pad/via rings, including zero and overlapping authored pads.
11    pub sample_count: usize,
12}
13
14impl PcbView<'_> {
15    /// Observe through-hole pad and via rings using authored size/drill dimensions.
16    /// Pads are visited before vias. NPTH/SMD pads and negative isolated helper-pad
17    /// rings are excluded; zero survives. Drill offsets, custom copper unions and
18    /// layer-dependent padstacks do not alter this source-envelope statistic.
19    /// The existing reader limits apply; both selected families are required.
20    pub fn annular_ring_stats(&self) -> Result<PcbAnnularRingStats, Error> {
21        if !self.selection.contains(PcbFamily::Pads) || !self.selection.contains(PcbFamily::Vias) {
22            return Err(Error::build(
23                ErrorKind::InvalidBuildValue,
24                "Annular-ring observation requires selected pad and via families",
25            ));
26        }
27        let mut stats = PcbAnnularRingStats::default();
28        for pad in self.pads() {
29            let pad = pad?;
30            if let Some(ring) = pad_ring(&pad) {
31                stats.include(ring);
32            }
33        }
34        for via in self.vias() {
35            let via = via?;
36            if via.size > 0.0 && via.drill > 0.0 {
37                stats.include((via.size - via.drill) / 2.0);
38            }
39        }
40        Ok(stats)
41    }
42}
43
44fn pad_ring(pad: &PcbPad) -> Option<f64> {
45    if !pad.kind.eq_ignore_ascii_case("thru_hole") || pad.size_x <= 0.0 || pad.size_y <= 0.0 {
46        return None;
47    }
48    let drill = pad.drill.as_ref()?;
49    let height = drill.height.unwrap_or(drill.width);
50    if drill.width <= 0.0 || height <= 0.0 {
51        return None;
52    }
53    Some((pad.size_x - drill.width).min(pad.size_y - height) / 2.0)
54}
55
56impl PcbAnnularRingStats {
57    fn include(&mut self, ring: f64) {
58        if ring < 0.0 {
59            return;
60        }
61        // Correctly rounded decimal conversion matches Python round(value, 6),
62        // without a scale/multiply step that can move binary half boundaries.
63        let rounded = format!("{ring:.6}").parse::<f64>().unwrap_or(ring);
64        self.minimum_mm = Some(self.minimum_mm.map_or(rounded, |value| value.min(rounded)));
65        self.sample_count += 1;
66    }
67}