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.75 source (not yet released, needs signing): schematic_review, pin_pad_check, route_pair, length_tune, thermal_vias…
289c671
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
//! Net classes from the names a designer already gave the nets: ground, power, clocks, the fast interfaces (USB,
//! Ethernet, HDMI, PCIe, DDR, MIPI, LVDS, SATA), and the differential pairs their _P/_N (+/-, DP/DN) suffixes spell;
//! proposed first, written as Altium net classes and pairs when asked.
use serde_json::{json, Value};
use std::collections::BTreeMap;
/// The class a net's name puts it in, if any. Pure.
pub fn classify(net: &str) -> Option<&'static str> {
let n = net.to_ascii_uppercase();
let core = n.trim_start_matches(['+', '-', '/', '\\']);
if crate::schreview::is_ground(&n) || core.starts_with("GND") { return Some("GND"); }
let power = core.contains("VBUS") || core.starts_with("VCC") || core.starts_with("VDD") || core.starts_with("VBAT") || core.starts_with("VIN") || core.starts_with("VBUS")
|| core.starts_with("VSYS") || core.starts_with("VREF") || core.starts_with("PWR") || core.starts_with("VCORE") || core.starts_with("VOUT")
|| (core.starts_with('V') && core.len() > 1 && core.as_bytes()[1].is_ascii_digit())
|| core.chars().next().map(|c| c.is_ascii_digit()).unwrap_or(false) && (core.contains('V'));
// a supply's name wins over what it feeds (VDD_XTAL is the crystal's supply, not a clock)
if power { return Some("Power"); }
let fast = ["USB", "ETH", "RGMII", "RMII", "SGMII", "HDMI", "PCIE", "DDR", "MIPI", "CSI", "DSI", "LVDS", "SATA", "SERDES", "QSPI", "SDIO", "EMMC"];
if fast.iter().any(|f| core.contains(f)) { return Some("HighSpeed"); }
if core.contains("CLK") || core.contains("XTAL") || core.contains("OSC") || core.ends_with("SCK") { return Some("Clock"); }
None
}
/// Differential pairs by name: X_P/X_N, XP/XN, X+/X-, X_DP/X_DM. Pure.
pub fn pairs(nets: &[String]) -> Vec<(String, String, String)> {
let set: BTreeMap<String, &String> = nets.iter().map(|n| (n.to_ascii_uppercase(), n)).collect();
let mut out = Vec::new();
for (u, orig) in &set {
// longest suffixes first: USB_DP must pair as USB (DP/DN), not as USB_D (P/N)
for (p, q) in [("_DP", "_DN"), ("_DP", "_DM"), ("DP", "DN"), ("DP", "DM"), ("_P", "_N"), ("+", "-"), ("P", "N")] {
if let Some(stem) = u.strip_suffix(p) {
if stem.is_empty() { continue; }
if let Some(neg) = set.get(&format!("{stem}{q}")) {
let name = stem.trim_end_matches('_').to_string();
if !out.iter().any(|(n, _, _): &(String, String, String)| *n == name) { out.push((name, (*orig).clone(), (*neg).clone())); }
break;
}
}
}
}
out
}
/// altium_auto_classes {filePath?, apply?, powerWidthMm?}
pub fn auto_classes(args: &Value) -> Value {
if let Some(g) = crate::support::need_licensed() { return g; }
if let Some(p) = crate::support::s(args, "filePath") { if let Some(e) = crate::support::must_open(&p, 60) { return e; } }
let st = crate::routing::routing_state(&json!({}));
if st.get("success").and_then(|b| b.as_bool()) != Some(true) { return st; }
let nets: Vec<String> = st["nets"].as_array().cloned().unwrap_or_default().iter().filter_map(|n| n.as_str().map(String::from)).collect();
let mut classes: BTreeMap<&str, Vec<String>> = BTreeMap::new();
for n in &nets { if let Some(c) = classify(n) { classes.entry(c).or_default().push(n.clone()); } }
let prs = pairs(&nets);
let proposal = json!({"classes": classes, "pairs": prs.iter().map(|(n, p, q)| json!({"name": n, "pos": p, "neg": q})).collect::<Vec<_>>(),
"unclassified": nets.iter().filter(|n| classify(n).is_none()).count()});
if args.get("apply").and_then(|b| b.as_bool()) != Some(true) {
return json!({"success": true, "applied": false, "proposal": proposal, "_hint": "A proposal from the net names. Check it, then call again with apply:true to write the classes (Power gets a width rule, powerWidthMm, default 0.5) and the pairs."});
}
let mut done = Vec::new();
for (c, members) in &classes {
let mut a = json!({"name": c, "nets": members});
if *c == "Power" { a["width"] = json!(args.get("powerWidthMm").and_then(|v| v.as_f64()).unwrap_or(0.5)); }
let r = crate::design::net_classes(&a);
done.push(json!({"class": c, "nets": members.len(), "success": r.get("success").cloned().unwrap_or(json!(false)), "error": r.get("error").cloned().unwrap_or(Value::Null)}));
}
for (n, p, q) in &prs {
let r = crate::design::diff_pairs(&json!({"name": n, "pos": p, "neg": q}));
done.push(json!({"pair": n, "success": r.get("success").cloned().unwrap_or(json!(false)), "error": r.get("error").cloned().unwrap_or(Value::Null)}));
}
json!({"success": done.iter().all(|d| d["success"] == json!(true)), "applied": true, "proposal": proposal, "written": done,
"_hint": "Scope rules to them: InNetClass('Power'), InNetClass('HighSpeed'), InDifferentialPair('<name>'). altium_impedance {apply} sizes the pairs; altium_current_capacity checks the power nets."})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_sort_into_classes_and_pairs() {
for (n, c) in [("GND", Some("GND")), ("AGND", Some("GND")), ("+3V3", Some("Power")), ("VDD_CORE", Some("Power")), ("5V", Some("Power")), ("VBUS", Some("Power")),
("USB_DP", Some("HighSpeed")), ("ETH_TX_P", Some("HighSpeed")), ("MCLK", Some("Clock")), ("VDD_XTAL", Some("Power")), ("USB_VBUS", Some("Power")), ("XTAL_IN", Some("Clock")), ("SPI_SCK", Some("Clock")), ("N1", None), ("LED_RED", None)] {
assert_eq!(classify(n), c, "{n}");
}
let nets: Vec<String> = ["USB_DP", "USB_DN", "ETH_TX_P", "ETH_TX_N", "LVDS0+", "LVDS0-", "SDA"].iter().map(|s| s.to_string()).collect();
let p = pairs(&nets);
assert_eq!(p.len(), 3);
assert!(p.iter().any(|(n, a, b)| n == "USB" && a == "USB_DP" && b == "USB_DN"));
assert!(p.iter().any(|(n, a, b)| n == "LVDS0" && a == "LVDS0+" && b == "LVDS0-"));
}
}