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.114: placed parts keep their models (library placement), set_models replaces only footprints, edit_footprint/librar…
500e445
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
//! generate_body: a part's 3D body from its package family and datasheet dimensions, as a STEP file. The standard's
//! shared writer makes it (vendor/adom-eda-checks `writers::body3d`, standard 0.8.0, from this bridge's own body3d.rs):
//! prisms to IPC-7351 proportions, AP214, coloured body, leads and marking. This bridge writes the file.
use serde_json::{json, Value};
/// altium_generate_body {family, dimensionsMm?, marking?, markingStyle?, outputPath}
pub fn generate_body(args: &Value) -> Value {
let Some(path) = crate::support::s(args, "outputPath") else { return json!({"success": false, "errorCode": "bad_args", "error": "outputPath (.step) is required"}) };
// the STEP product is named after the file, as before
let stem = std::path::Path::new(&path).file_stem().map(|s| s.to_string_lossy().to_string()).unwrap_or("body".into());
let mut eargs = args.clone();
eargs["name"] = json!(stem);
// a dimension given beside family, not in dimensionsMm, was ignored without a word: pins: 2 left a SOT-23 at three
// leads (#42). Such keys are read as dimensions, and the reply says so.
let (lifted, dims) = lift_dimensions(args);
if !lifted.is_empty() { eargs["dimensionsMm"] = dims; }
// the engine directly, not checks::engine: verb prefixing would walk the STEP text, and the hint is this bridge's own
let mut r = adom_eda_checks::run("generate_body", &json!({"args": eargs}));
let Some(text) = r.get("text").and_then(|t| t.as_str()).map(str::to_string) else { return r };
if let Some(p) = std::path::Path::new(&path).parent() { let _ = std::fs::create_dir_all(p); }
if let Err(e) = std::fs::write(&path, text) { return json!({"success": false, "errorCode": "write_failed", "error": format!("{path}: {e}")}); }
if let Some(o) = r.as_object_mut() { o.remove("text"); }
r["stepPath"] = json!(path);
if !lifted.is_empty() { r["dimensionsFromTopLevel"] = json!(lifted); }
r["_hint"] = json!("The body is in the package's own frame (IPC-7351 zero orientation, pin 1 top-left, seated on z = 0), ready for altium_add_body {libraryPath, footprintName, step}. Compare bboxMm with the datasheet's body size before attaching it.");
r
}
/// Dimension keys the engine reads (the JEDEC letters, pins, rows, cols) given at the top level instead of in
/// dimensionsMm: their names, and dimensionsMm with them added (one already in dimensionsMm wins).
fn lift_dimensions(args: &Value) -> (Vec<String>, Value) {
const KEYS: &[&str] = &["pins", "rows", "cols", "A", "A1", "b", "c", "D", "D1", "D2", "E", "E1", "E2", "e", "L", "L1", "H", "W"];
let mut dims = args.get("dimensionsMm").cloned().filter(|v| v.is_object()).unwrap_or_else(|| json!({}));
let mut lifted = Vec::new();
for k in KEYS {
if let Some(v) = args.get(*k).filter(|v| v.is_number()) {
if dims.get(*k).is_none() { dims[*k] = v.clone(); lifted.push(k.to_string()); }
}
}
(lifted, dims)
}
#[cfg(test)]
mod tests {
#[test]
fn a_pin_count_beside_the_family_is_read() {
let (l, d) = super::lift_dimensions(&serde_json::json!({"family": "sot23", "pins": 2, "dimensionsMm": {"e": 1.9}}));
assert_eq!(l, vec!["pins"]);
assert_eq!(d, serde_json::json!({"e": 1.9, "pins": 2}));
let (l, _) = super::lift_dimensions(&serde_json::json!({"family": "soic", "pins": 2, "dimensionsMm": {"pins": 14}}));
assert!(l.is_empty(), "dimensionsMm wins");
let path = std::env::temp_dir().join("altium-body3d-2pin").join("D1.step");
let r = super::generate_body(&serde_json::json!({"family": "sot23", "pins": 2, "outputPath": path.to_string_lossy()}));
assert_eq!(r["solids"], serde_json::json!(3), "a body and two leads: {r}");
assert_eq!(r["dimensionsFromTopLevel"], serde_json::json!(["pins"]));
let _ = std::fs::remove_dir_all(path.parent().unwrap());
}
#[test]
fn the_engine_writes_the_body_this_bridge_saves() {
let dir = std::env::temp_dir().join("altium-body3d-test");
let path = dir.join("U1.step");
let r = super::generate_body(&serde_json::json!({"family": "soic", "marking": "NE555", "outputPath": path.to_string_lossy()}));
assert_eq!(r["success"], serde_json::json!(true), "{r}");
assert!(r.get("text").is_none() && r["engine"].as_str().unwrap().starts_with("adom-eda-checks"));
let t = std::fs::read_to_string(&path).unwrap();
assert!(t.starts_with("ISO-10303-21;") && t.contains("'U1'"));
assert_eq!(r["bboxMm"]["z"], serde_json::json!(1.75));
let _ = std::fs::remove_dir_all(dir);
}
}