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

kicad_monkey_core/pcb/
padstacks.rs

1//! Sparse authored pad/via records and bounded effective pad copper facts.
2//! Source declarations remain unchanged by the separate borrowed resolver.
3
4use super::*;
5mod resolve;
6pub use resolve::{PcbResolvedPadCopperLayer, resolve_pad_copper_layer};
7
8#[derive(Clone, Debug, PartialEq)]
9pub struct PcbPadstack {
10    pub mode: Option<String>,
11    pub layers: Vec<PcbPadstackLayer>,
12    pub source_range: Range<usize>,
13}
14
15#[derive(Clone, Debug, PartialEq)]
16pub struct PcbPadstackLayer {
17    /// `Inner` is the source front/inner/back selector, not a board layer name.
18    pub layer: String,
19    pub shape: Option<String>,
20    pub size: Option<PcbPoint>,
21    pub offset: Option<PcbPoint>,
22    pub rect_delta: Option<PcbPoint>,
23    pub roundrect_rratio: Option<f64>,
24    pub chamfer_ratio: Option<f64>,
25    pub chamfer_corners: Vec<String>,
26    pub clearance: Option<f64>,
27    pub thermal_bridge_width: Option<f64>,
28    pub thermal_bridge_angle: Option<f64>,
29    pub thermal_gap: Option<f64>,
30    /// Explicit -1 (inherited) remains distinct from an absent declaration.
31    pub zone_connect: Option<i64>,
32    pub custom_options: Option<PcbPadCustomOptions>,
33    pub custom_primitives: Vec<PcbPadCustomPrimitive>,
34    pub source_range: Range<usize>,
35}
36
37#[derive(Clone, Debug, PartialEq)]
38pub struct PcbViaStack {
39    pub mode: Option<String>,
40    pub layers: Vec<PcbViaStackLayer>,
41    pub source_range: Range<usize>,
42}
43
44/// KiCad via-stack rows support only a circular diameter, not pad shapes/policies.
45#[derive(Clone, Debug, PartialEq)]
46pub struct PcbViaStackLayer {
47    pub layer: String,
48    pub size: Option<f64>,
49    pub source_range: Range<usize>,
50}
51
52pub(super) fn padstack(
53    source: &str,
54    children: &[FormSpan],
55    limits: PcbLimits,
56    budget: &mut pads::CustomPadBudget,
57) -> Result<Option<PcbPadstack>, Error> {
58    let Some(span) = child(children, "padstack") else {
59        return Ok(None);
60    };
61    let fields = direct_children(source, span, limits.max_pad_children, limits)?;
62    let mut layers = Vec::new();
63    for layer in fields
64        .iter()
65        .filter(|field| field.head.as_deref() == Some("layer"))
66    {
67        if layers.len() >= limits.max_layers {
68            return Err(limit_error());
69        }
70        let values = direct_children(source, layer, limits.max_pad_children, limits)?;
71        layers.push(PcbPadstackLayer {
72            layer: first_string(source, layer)?.unwrap_or_default(),
73            shape: optional_child_string(source, &values, "shape")?,
74            size: optional_child_point(source, &values, "size")?,
75            offset: optional_child_point(source, &values, "offset")?,
76            rect_delta: optional_child_point(source, &values, "rect_delta")?,
77            roundrect_rratio: optional_child_f64(source, &values, "roundrect_rratio")?,
78            chamfer_ratio: optional_child_f64(source, &values, "chamfer_ratio")?,
79            chamfer_corners: child_strings(
80                source,
81                &values,
82                "chamfer",
83                limits.max_pad_chamfer_corners,
84            )?,
85            clearance: optional_child_f64(source, &values, "clearance")?,
86            thermal_bridge_width: optional_child_f64(source, &values, "thermal_bridge_width")?,
87            thermal_bridge_angle: optional_child_f64(source, &values, "thermal_bridge_angle")?,
88            thermal_gap: optional_child_f64(source, &values, "thermal_gap")?,
89            zone_connect: optional_child_i64(source, &values, "zone_connect")?,
90            custom_options: pads::custom_options_from_children(source, &values, limits)?,
91            custom_primitives: pads::custom_primitives_from_children(
92                source, &values, limits, budget,
93            )?,
94            source_range: layer.range.clone(),
95        });
96    }
97    Ok(Some(PcbPadstack {
98        mode: optional_child_string(source, &fields, "mode")?,
99        layers,
100        source_range: span.range.clone(),
101    }))
102}
103
104pub(super) fn viastack(
105    source: &str,
106    children: &[FormSpan],
107    limits: PcbLimits,
108) -> Result<Option<PcbViaStack>, Error> {
109    let Some(span) = child(children, "padstack") else {
110        return Ok(None);
111    };
112    let fields = direct_children(source, span, limits.max_via_children, limits)?;
113    let mut layers = Vec::new();
114    for layer in fields
115        .iter()
116        .filter(|field| field.head.as_deref() == Some("layer"))
117    {
118        if layers.len() >= limits.max_layers {
119            return Err(limit_error());
120        }
121        let values = direct_children(source, layer, limits.max_via_children, limits)?;
122        layers.push(PcbViaStackLayer {
123            layer: first_string(source, layer)?.unwrap_or_default(),
124            size: optional_child_f64(source, &values, "size")?,
125            source_range: layer.range.clone(),
126        });
127    }
128    Ok(Some(PcbViaStack {
129        mode: optional_child_string(source, &fields, "mode")?,
130        layers,
131        source_range: span.range.clone(),
132    }))
133}