|
| 1 | +use super::types::{mm_to_mil_10, MaskPaths}; |
| 2 | +use anyhow::{anyhow, Result}; |
| 3 | +use gerber_parser::{gerber_types::*, parse}; |
| 4 | +use std::io::{BufReader, Cursor}; |
| 5 | + |
| 6 | +fn coords_to_mm(coords: &Option<Coordinates>, last: (f64, f64), units: Unit) -> (f64, f64) { |
| 7 | + let mut x = last.0; |
| 8 | + let mut y = last.1; |
| 9 | + if let Some(coords) = coords { |
| 10 | + if let Some(cx) = coords.x { |
| 11 | + let mut val: f64 = cx.into(); |
| 12 | + if matches!(units, Unit::Inches) { |
| 13 | + val *= 25.4; |
| 14 | + } |
| 15 | + x = val; |
| 16 | + } |
| 17 | + if let Some(cy) = coords.y { |
| 18 | + let mut val: f64 = cy.into(); |
| 19 | + if matches!(units, Unit::Inches) { |
| 20 | + val *= 25.4; |
| 21 | + } |
| 22 | + y = val; |
| 23 | + } |
| 24 | + } |
| 25 | + (x, y) |
| 26 | +} |
| 27 | + |
| 28 | +fn to_svg_space(x_mm: f64, y_mm: f64) -> (f64, f64) { |
| 29 | + (mm_to_mil_10(x_mm), -mm_to_mil_10(y_mm)) |
| 30 | +} |
| 31 | + |
| 32 | +fn rect_path(center: (f64, f64), w_mm: f64, h_mm: f64) -> String { |
| 33 | + let (cx, cy) = center; |
| 34 | + let hw = mm_to_mil_10(w_mm) / 2.0; |
| 35 | + let hh = mm_to_mil_10(h_mm) / 2.0; |
| 36 | + let x0 = cx - hw; |
| 37 | + let x1 = cx + hw; |
| 38 | + let y0 = cy - hh; |
| 39 | + let y1 = cy + hh; |
| 40 | + format!("M {x0} {y0} L {x1} {y0} {x1} {y1} {x0} {y1} {x0} {y0} ") |
| 41 | +} |
| 42 | + |
| 43 | +fn path_from_region(points: &[(f64, f64)]) -> Option<String> { |
| 44 | + if points.is_empty() { |
| 45 | + return None; |
| 46 | + } |
| 47 | + let mut d = String::new(); |
| 48 | + for (idx, (x, y)) in points.iter().enumerate() { |
| 49 | + if idx == 0 { |
| 50 | + d.push_str(&format!("M {} {} ", x, y)); |
| 51 | + } else { |
| 52 | + d.push_str(&format!("L {} {} ", x, y)); |
| 53 | + } |
| 54 | + } |
| 55 | + d.push('Z'); |
| 56 | + Some(d) |
| 57 | +} |
| 58 | + |
| 59 | +fn aperture_bbox(ap: &Aperture) -> Option<(f64, f64)> { |
| 60 | + match ap { |
| 61 | + Aperture::Circle(c) => Some((c.diameter, c.diameter)), |
| 62 | + Aperture::Rectangle(r) | Aperture::Obround(r) => Some((r.x, r.y)), |
| 63 | + Aperture::Macro(name, args) if name == "RoundRect" => { |
| 64 | + parse_round_rect_macro(args.as_deref()) |
| 65 | + } |
| 66 | + _ => None, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn parse_round_rect_macro(args: Option<&[MacroDecimal]>) -> Option<(f64, f64)> { |
| 71 | + let args = args?; |
| 72 | + if args.len() < 3 { |
| 73 | + return None; |
| 74 | + } |
| 75 | + // Skip radius (first value) |
| 76 | + let coords = &args[1..]; |
| 77 | + let mut xs = Vec::new(); |
| 78 | + let mut ys = Vec::new(); |
| 79 | + for pair in coords.chunks(2) { |
| 80 | + if pair.len() == 2 { |
| 81 | + match (&pair[0], &pair[1]) { |
| 82 | + (MacroDecimal::Value(x), MacroDecimal::Value(y)) => { |
| 83 | + xs.push(x); |
| 84 | + ys.push(y); |
| 85 | + } |
| 86 | + _ => {} |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + if xs.is_empty() || ys.is_empty() { |
| 91 | + return None; |
| 92 | + } |
| 93 | + let w = xs.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(*b)) |
| 94 | + - xs.iter().fold(f64::INFINITY, |a, &b| a.min(*b)); |
| 95 | + let h = ys.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(*b)) |
| 96 | + - ys.iter().fold(f64::INFINITY, |a, &b| a.min(*b)); |
| 97 | + Some((w.abs(), h.abs())) |
| 98 | +} |
| 99 | + |
| 100 | +pub fn parse_solder_mask(content: &str) -> Result<MaskPaths> { |
| 101 | + let reader = BufReader::new(Cursor::new(content)); |
| 102 | + let doc = match parse(reader) { |
| 103 | + Ok(doc) => doc, |
| 104 | + Err((partial, err)) => { |
| 105 | + if partial.commands().is_empty() { |
| 106 | + return Err(anyhow!("Failed to parse solder mask: {err}")); |
| 107 | + } |
| 108 | + partial |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + let mut units = doc.units.unwrap_or(Unit::Millimeters); |
| 113 | + let mut current_aperture: Option<&Aperture> = None; |
| 114 | + let mut current_pos: (f64, f64) = (0.0, 0.0); |
| 115 | + let mut region_active = false; |
| 116 | + let mut region_points: Vec<(f64, f64)> = Vec::new(); |
| 117 | + let mut shapes: MaskPaths = Vec::new(); |
| 118 | + let mut interp_mode = InterpolationMode::Linear; |
| 119 | + |
| 120 | + let apertures = &doc.apertures; |
| 121 | + |
| 122 | + for command in doc.commands() { |
| 123 | + match command { |
| 124 | + Command::ExtendedCode(ExtendedCode::Unit(u)) => units = *u, |
| 125 | + Command::FunctionCode(FunctionCode::GCode(g)) => match g { |
| 126 | + GCode::InterpolationMode(m) => interp_mode = *m, |
| 127 | + GCode::RegionMode(on) => { |
| 128 | + if !on && region_active && region_points.len() > 1 { |
| 129 | + if let Some(d) = path_from_region(®ion_points) { |
| 130 | + shapes.push(d); |
| 131 | + } |
| 132 | + region_points.clear(); |
| 133 | + } |
| 134 | + region_active = *on; |
| 135 | + } |
| 136 | + _ => {} |
| 137 | + }, |
| 138 | + Command::FunctionCode(FunctionCode::DCode(d)) => match d { |
| 139 | + DCode::SelectAperture(code) => current_aperture = apertures.get(code), |
| 140 | + DCode::Operation(op) => match op { |
| 141 | + Operation::Move(coords) => { |
| 142 | + current_pos = coords_to_mm(coords, current_pos, units); |
| 143 | + } |
| 144 | + Operation::Interpolate(coords, _) => { |
| 145 | + let next = coords_to_mm(coords, current_pos, units); |
| 146 | + if region_active { |
| 147 | + if region_points.is_empty() { |
| 148 | + let (sx, sy) = to_svg_space(current_pos.0, current_pos.1); |
| 149 | + region_points.push((sx, sy)); |
| 150 | + } |
| 151 | + let (nx, ny) = to_svg_space(next.0, next.1); |
| 152 | + region_points.push((nx, ny)); |
| 153 | + } |
| 154 | + current_pos = next; |
| 155 | + let _ = interp_mode; // currently unused (linearized) |
| 156 | + } |
| 157 | + Operation::Flash(coords) => { |
| 158 | + let pos_mm = coords_to_mm(coords, current_pos, units); |
| 159 | + let (cx, cy) = to_svg_space(pos_mm.0, pos_mm.1); |
| 160 | + let ap_ref = current_aperture.or_else(|| { |
| 161 | + apertures |
| 162 | + .iter() |
| 163 | + .max_by_key(|(code, _)| *code) |
| 164 | + .map(|(_, ap)| ap) |
| 165 | + }); |
| 166 | + if let Some(ap) = ap_ref { |
| 167 | + if let Some((w, h)) = aperture_bbox(ap) { |
| 168 | + shapes.push(rect_path((cx, cy), w, h)); |
| 169 | + } |
| 170 | + } |
| 171 | + current_pos = pos_mm; |
| 172 | + } |
| 173 | + }, |
| 174 | + }, |
| 175 | + _ => {} |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // Close any pending region |
| 180 | + if region_active && region_points.len() > 1 { |
| 181 | + if let Some(d) = path_from_region(®ion_points) { |
| 182 | + shapes.push(d); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + Ok(shapes) |
| 187 | +} |
0 commit comments