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.90 (source, not released; final shapes not run in Altium yet): the eleven verbs of standard 0.30.14 in the standard…
78b01ce
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
//! cancel_run (EDA Bridge Standard #29, app tier): stop a long job the bridge started, between its steps. A process
//! running inside Altium cannot be interrupted from outside, so this covers the bridge's own multi-step verbs (sch_batch,
//! import_design's wizard): each one registers here and asks `cancelled()` between steps. Every request has its own
//! thread, so a cancel arrives while the job runs.
use serde_json::{json, Value};
use std::cell::Cell;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;
#[derive(Clone)]
struct Run { id: u64, verb: String, stage: String, cancel: bool, started: std::time::Instant }
static NEXT: AtomicU64 = AtomicU64::new(1);
static RUNS: Mutex<Vec<Run>> = Mutex::new(Vec::new());
thread_local! { static MINE: Cell<u64> = const { Cell::new(0) }; }
/// A running job; dropping it ends the run.
pub struct Guard(u64);
impl Drop for Guard {
fn drop(&mut self) {
if let Ok(mut r) = RUNS.lock() { r.retain(|x| x.id != self.0); }
MINE.with(|m| m.set(0));
}
}
pub fn begin(verb: &str) -> Guard {
let id = NEXT.fetch_add(1, Ordering::SeqCst);
if let Ok(mut r) = RUNS.lock() { r.push(Run { id, verb: verb.to_string(), stage: "started".into(), cancel: false, started: std::time::Instant::now() }); }
MINE.with(|m| m.set(id));
Guard(id)
}
/// Say where this thread's job has got to.
pub fn stage(what: &str) {
let id = MINE.with(|m| m.get());
if let Ok(mut r) = RUNS.lock() { if let Some(x) = r.iter_mut().find(|x| x.id == id) { x.stage = what.to_string(); } }
}
/// Has this thread's job been told to stop?
pub fn cancelled() -> bool {
let id = MINE.with(|m| m.get());
RUNS.lock().map(|r| r.iter().any(|x| x.id == id && x.cancel)).unwrap_or(false)
}
fn row(x: &Run) -> Value { json!({"runId": x.id.to_string(), "verb": x.verb, "stage": x.stage, "runningSec": x.started.elapsed().as_secs()}) }
/// altium_cancel_run {runId?}
pub fn cancel_run(args: &Value) -> Value {
let Ok(mut r) = RUNS.lock() else { return json!({"success": false, "errorCode": "internal", "error": "the run list is locked"}) };
let want = args.get("runId").map(|v| v.as_str().map(str::to_string).unwrap_or(v.to_string()));
let running: Vec<Value> = r.iter().map(row).collect();
let hit = match &want {
Some(id) => r.iter_mut().find(|x| x.id.to_string() == *id),
None => r.iter_mut().max_by_key(|x| x.id),
};
match hit {
Some(x) => {
x.cancel = true;
json!({"success": true, "cancelRequested": true, "runId": x.id.to_string(), "verb": x.verb, "stage": x.stage, "running": running,
"_hint": "The job stops at its next step and answers cancelled in its own reply, with what it took back. A step already inside Altium runs to its end."})
}
None => json!({"success": true, "cancelRequested": false, "runId": want, "running": running,
"reason": if want.is_some() { "no run with that id is going" } else { "nothing the bridge can stop is running" },
"_hint": "cancel_run stops the bridge's own multi-step verbs (sch_batch, import_design) between steps. A process inside Altium cannot be interrupted from outside: altium_agent_status says whether one is busy."}),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_run_is_told_to_stop_and_ends() {
let g = begin("sch_batch");
stage("op 1");
assert!(!cancelled());
let id = RUNS.lock().unwrap().iter().find(|x| x.verb == "sch_batch").unwrap().id.to_string();
let r = cancel_run(&json!({"runId": id}));
assert_eq!(r["cancelRequested"], true);
assert_eq!(r["stage"], "op 1");
assert!(cancelled());
drop(g);
assert!(!cancelled());
assert_eq!(cancel_run(&json!({"runId": "999999"}))["cancelRequested"], false);
}
}