by adom
Let an AI (Claude, ChatGPT, Codex or any agent) run Altium Designer for you: create symbols and footprints, place parts (also from Altium 365), wire and annotate schematics, Update PCB, set the stack-up and rules, route, pour copper, add mounting holes and stitching vias, run ERC and DRC, and make Gerbers, BOMs and STEP. Free in Hydrogen; bring your own AI and Altium licence.
main
Colby Knox
0.2.112: footprint reads see AD 24+ typed component layers (#41); agent lint for use-before-declaration and reserved na…
2533a43
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
//! Footprint layers as the reads report them (#41). Since Altium Designer 24 a footprint can be drawn on typed
//! component layers (Top Assembly, Top Courtyard, Top Designator, Top 3D Body, ...): mechanical layers, usually above
//! Mechanical 16, that carry a kind. The agent sends each layer a footprint uses as `[id, name, kind]`, with the name
//! the user sees in Altium and the kind number (-1 for a layer that is not mechanical, 0 for a mechanical layer with
//! no kind). Pure.
use serde_json::{json, Value};
/// A mechanical layer's kind by Altium's number, in the words of its layer type (`CourtyardTop`, `3DBodyTop`).
/// 1, 2, 7 to 14, 16, 17 and 26 to 28 were read from a real library on AD 26.11; the rest follow Altium's file format.
pub fn kind_name(k: i64) -> Option<&'static str> {
Some(match k {
1 => "AssemblyTop", 2 => "AssemblyBottom", 3 => "AssemblyNotes", 4 => "Board",
5 => "CoatingTop", 6 => "CoatingBottom", 7 => "ComponentCenterTop", 8 => "ComponentCenterBottom",
9 => "ComponentOutlineTop", 10 => "ComponentOutlineBottom", 11 => "CourtyardTop", 12 => "CourtyardBottom",
13 => "DesignatorTop", 14 => "DesignatorBottom", 15 => "Dimensions", 16 => "DimensionsTop", 17 => "DimensionsBottom",
18 => "FabNotes", 19 => "GluePointsTop", 20 => "GluePointsBottom", 21 => "GoldPlatingTop", 22 => "GoldPlatingBottom",
23 => "ValueTop", 24 => "ValueBottom", 25 => "VCut", 26 => "3DBodyTop", 27 => "3DBodyBottom", 28 => "RouteToolPath",
29 => "Sheet", 30 => "BoardShape",
_ => return None,
})
}
/// One layer the footprint uses.
#[derive(Clone, Debug, PartialEq)]
pub struct Layer {
pub id: i64,
pub name: String,
pub kind: Option<&'static str>,
}
/// The agent's `[[id, name, kind], ...]` rows.
pub fn table(rows: &Value) -> Vec<Layer> {
rows.as_array().map(|a| a.iter().filter_map(|r| Some(Layer {
id: r.get(0)?.as_i64()?,
name: r.get(1).and_then(|v| v.as_str()).unwrap_or("").to_string(),
kind: kind_name(r.get(2).and_then(|v| v.as_i64()).unwrap_or(-1)),
})).collect()).unwrap_or_default()
}
/// The name a footprint read gives a layer: the standard's name for copper, silkscreen, paste and mask (as
/// altium_edit_footprint takes them), else the name the user sees in Altium ("Top Courtyard", "Mechanical 15"),
/// else the board model's name.
pub fn display_name(id: i64, layers: &[Layer]) -> String {
let std = match id {
1 => "F.Cu", 32 => "B.Cu", 33 => "F.SilkS", 34 => "B.SilkS", 35 => "F.Paste", 36 => "B.Paste", 37 => "F.Mask", 38 => "B.Mask", 74 => "*.Cu",
_ => "",
};
if !std.is_empty() { return std.to_string(); }
layers.iter().find(|l| l.id == id && !l.name.is_empty()).map(|l| l.name.clone()).unwrap_or_else(|| crate::routing::layer_name(id))
}
/// The layers that hold the courtyard, best first: a Courtyard Top kind, then Courtyard Bottom, then Mechanical 15
/// (id 71, where this bridge and the IPC wizards put it), then a layer named for a courtyard.
pub fn courtyard_layers(layers: &[Layer]) -> Vec<i64> {
for want in [Some("CourtyardTop"), Some("CourtyardBottom")] {
let ids: Vec<i64> = layers.iter().filter(|l| l.kind == want).map(|l| l.id).collect();
if !ids.is_empty() { return ids; }
}
if layers.iter().any(|l| l.id == 71) || layers.is_empty() { return vec![71]; }
layers.iter().filter(|l| l.name.to_lowercase().contains("courtyard")).map(|l| l.id).collect()
}
/// The reply's layer list: each layer's id, name, kind and how many graphics are on it.
pub fn details(layers: &[Layer], graphics_on: impl Fn(i64) -> usize) -> Vec<Value> {
layers.iter().map(|l| {
let mut v = json!({"id": l.id, "name": display_name(l.id, layers), "altiumName": l.name, "graphics": graphics_on(l.id)});
if let Some(k) = l.kind { v["kind"] = json!(k); }
v
}).collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn template() -> Vec<Layer> {
// as the agent read MartyPM's "Default Footprint" (#41) on AD 26.11
table(&json!([[1, "Top Layer", -1], [33, "Top Overlay", -1], [67108917, "Top Assembly", 1], [67108921, "Top Component Center", 7],
[67108923, "Top Component Outline", 9], [67108925, "Top Courtyard", 11], [67108927, "Top Designator", 13]]))
}
#[test]
fn typed_layers_keep_their_altium_names_and_kinds() {
let l = template();
assert_eq!(display_name(33, &l), "F.SilkS");
assert_eq!(display_name(67108925, &l), "Top Courtyard");
assert_eq!(l.iter().find(|x| x.id == 67108925).unwrap().kind, Some("CourtyardTop"));
assert_eq!(l.iter().find(|x| x.id == 33).unwrap().kind, None, "a layer that is not mechanical has no kind");
}
#[test]
fn the_courtyard_is_the_typed_courtyard_else_mechanical_15() {
assert_eq!(courtyard_layers(&template()), vec![67108925]);
let old = table(&json!([[1, "Top Layer", -1], [69, "Fab", 0], [71, "Courtyard", 0]]));
assert_eq!(courtyard_layers(&old), vec![71]);
assert_eq!(courtyard_layers(&[]), vec![71], "an old agent sends no layers: Mechanical 15 as before");
}
#[test]
fn details_count_the_graphics_on_each_layer() {
let d = details(&template(), |id| if id == 67108925 { 4 } else { 0 });
let c = d.iter().find(|x| x["id"] == 67108925).unwrap();
assert_eq!(c["name"], "Top Courtyard");
assert_eq!(c["kind"], "CourtyardTop");
assert_eq!(c["graphics"], 4);
assert!(d.iter().find(|x| x["id"] == 33).unwrap().get("kind").is_none());
}
}