kicad_monkey_core/pcb/pads/
custom.rs1use super::*;
2
3pub type PcbPadPolygonPoint = PcbPolygonPoint;
5
6#[derive(Clone, Debug, PartialEq)]
8pub enum PcbPadPrimitiveGeometry {
9 Line {
10 start: PcbPoint,
11 end: PcbPoint,
12 },
13 Arc {
14 start: PcbPoint,
15 mid: PcbPoint,
16 end: PcbPoint,
17 },
18 Circle {
19 center: PcbPoint,
20 end: PcbPoint,
21 },
22 Rect {
23 start: PcbPoint,
24 end: PcbPoint,
25 radius: Option<f64>,
26 },
27 Polygon {
28 points: Vec<PcbPadPolygonPoint>,
29 },
30 Curve {
31 points: [PcbPoint; 4],
32 },
33 BoundingBoxProxy {
34 start: PcbPoint,
35 end: PcbPoint,
36 },
37 VectorProxy {
38 start: PcbPoint,
39 end: PcbPoint,
40 },
41}
42
43pub(super) fn primitive_geometry(
44 source: &str,
45 primitive: &FormSpan,
46 fields: &[FormSpan],
47 limits: PcbLimits,
48 count: &mut usize,
49) -> Result<(Vec<PcbPoint>, Option<PcbPadPrimitiveGeometry>), Error> {
50 let (legacy_points, outline, complete) = outline_points(source, fields, limits, count)?;
51 let geometry = match primitive.head.as_deref() {
52 Some("gr_poly") if complete => Some(PcbPadPrimitiveGeometry::Polygon { points: outline }),
53 Some("gr_curve") if complete && outline.len() == 4 && legacy_points.len() == 4 => {
54 Some(PcbPadPrimitiveGeometry::Curve {
55 points: legacy_points.as_slice().try_into().expect("four XY points"),
56 })
57 }
58 _ => endpoint_geometry(source, primitive, fields, limits, count)?,
59 };
60 Ok((legacy_points, geometry))
61}
62
63fn outline_points(
64 source: &str,
65 fields: &[FormSpan],
66 limits: PcbLimits,
67 count: &mut usize,
68) -> Result<(Vec<PcbPoint>, Vec<PcbPadPolygonPoint>, bool), Error> {
69 let mut legacy_points = Vec::new();
70 let mut outline = Vec::new();
71 let mut complete = child(fields, "pts").is_some();
72 if let Some(container) = child(fields, "pts") {
73 for point in direct_children(source, container, limits.max_pad_custom_point_forms, limits)?
74 {
75 match point.head.as_deref() {
76 Some("xy") => {
77 let values = first_two_scalar_values(source, &point)?;
78 let [x, y] = values.as_slice() else {
79 complete = false;
80 continue;
81 };
82 charge(count, 1, limits)?;
83 let xy = PcbPoint {
84 x: parse_f64(x, &point)?,
85 y: parse_f64(y, &point)?,
86 };
87 legacy_points.push(xy);
88 outline.push(PcbPadPolygonPoint::Xy(xy));
89 }
90 Some("arc") => {
91 let fields = direct_children(source, &point, limits.max_pad_children, limits)?;
92 let (Some(start), Some(mid), Some(end)) = (
93 optional_child_point(source, &fields, "start")?,
94 optional_child_point(source, &fields, "mid")?,
95 optional_child_point(source, &fields, "end")?,
96 ) else {
97 complete = false;
98 continue;
99 };
100 charge(count, 3, limits)?;
101 outline.push(PcbPadPolygonPoint::Arc { start, mid, end });
102 }
103 _ => complete = false,
104 }
105 }
106 }
107 Ok((legacy_points, outline, complete))
108}
109
110fn endpoint_geometry(
111 source: &str,
112 primitive: &FormSpan,
113 fields: &[FormSpan],
114 limits: PcbLimits,
115 count: &mut usize,
116) -> Result<Option<PcbPadPrimitiveGeometry>, Error> {
117 Ok(match primitive.head.as_deref() {
118 Some("gr_line" | "gr_rect" | "gr_arc" | "gr_circle" | "gr_bbox" | "gr_vector") => {
119 let start_name = if primitive.head.as_deref() == Some("gr_circle") {
120 "center"
121 } else {
122 "start"
123 };
124 let start = optional_child_point(source, fields, start_name)?;
125 let end = optional_child_point(source, fields, "end")?;
126 let mid = optional_child_point(source, fields, "mid")?;
127 charge(
129 count,
130 usize::from(start.is_some())
131 + usize::from(end.is_some())
132 + usize::from(mid.is_some()),
133 limits,
134 )?;
135 match (primitive.head.as_deref(), start, mid, end) {
136 (Some("gr_line"), Some(start), _, Some(end)) => {
137 Some(PcbPadPrimitiveGeometry::Line { start, end })
138 }
139 (Some("gr_arc"), Some(start), Some(mid), Some(end)) => {
140 Some(PcbPadPrimitiveGeometry::Arc { start, mid, end })
141 }
142 (Some("gr_circle"), Some(center), _, Some(end)) => {
143 Some(PcbPadPrimitiveGeometry::Circle { center, end })
144 }
145 (Some("gr_rect"), Some(start), _, Some(end)) => {
146 Some(PcbPadPrimitiveGeometry::Rect {
147 start,
148 end,
149 radius: optional_child_f64(source, fields, "radius")?,
150 })
151 }
152 (Some("gr_bbox"), Some(start), _, Some(end)) => {
153 Some(PcbPadPrimitiveGeometry::BoundingBoxProxy { start, end })
154 }
155 (Some("gr_vector"), Some(start), _, Some(end)) => {
156 Some(PcbPadPrimitiveGeometry::VectorProxy { start, end })
157 }
158 _ => None,
159 }
160 }
161 _ => None,
162 })
163}
164
165fn charge(count: &mut usize, added: usize, limits: PcbLimits) -> Result<(), Error> {
166 if added > limits.max_pad_custom_points.saturating_sub(*count) {
167 return Err(limit_error());
168 }
169 *count += added;
170 Ok(())
171}