diff --git a/README.md b/README.md index 80fd77b..c24a3bf 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,10 @@ Legend: | --- | ---: | ---: | ---: | | Common (base) | 6 | 2 | 33% | | Common editor/document | 23 | 9 | 39% | -| Project manager | 5 | 0 | 0% | +| Project manager | 5 | 1 | 20% | | Board editor (PCB) | 22 | 13 | 59% | | Schematic editor (dedicated proto commands) | 0 | 0 | n/a | -| **Total** | **56** | **24** | **43%** | +| **Total** | **56** | **25** | **45%** | ### Common (base) @@ -88,7 +88,7 @@ Legend: | KiCad Command | Status | Rust API | | --- | --- | --- | -| `GetNetClasses` | Not yet | - | +| `GetNetClasses` | Implemented | `KiCadClient::get_net_classes_raw`, `KiCadClient::get_net_classes` | | `SetNetClasses` | Not yet | - | | `ExpandTextVariables` | Not yet | - | | `GetTextVariables` | Not yet | - | diff --git a/docs/TEST_CLI.md b/docs/TEST_CLI.md index 631d52a..a661336 100644 --- a/docs/TEST_CLI.md +++ b/docs/TEST_CLI.md @@ -47,6 +47,12 @@ List nets: cargo run --bin kicad-ipc-cli -- nets ``` +List project net classes: + +```bash +cargo run --bin kicad-ipc-cli -- net-classes +``` + List enabled board layers: ```bash diff --git a/src/client.rs b/src/client.rs index bb2d1e3..2b8f5d7 100644 --- a/src/client.rs +++ b/src/client.rs @@ -35,6 +35,7 @@ const KICAD_API_TOKEN_ENV: &str = "KICAD_API_TOKEN"; const CMD_PING: &str = "kiapi.common.commands.Ping"; const CMD_GET_VERSION: &str = "kiapi.common.commands.GetVersion"; +const CMD_GET_NET_CLASSES: &str = "kiapi.common.commands.GetNetClasses"; const CMD_GET_OPEN_DOCUMENTS: &str = "kiapi.common.commands.GetOpenDocuments"; const CMD_GET_NETS: &str = "kiapi.board.commands.GetNets"; const CMD_GET_BOARD_ENABLED_LAYERS: &str = "kiapi.board.commands.GetBoardEnabledLayers"; @@ -61,6 +62,7 @@ const CMD_SAVE_DOCUMENT_TO_STRING: &str = "kiapi.common.commands.SaveDocumentToS const CMD_SAVE_SELECTION_TO_STRING: &str = "kiapi.common.commands.SaveSelectionToString"; const RES_GET_VERSION: &str = "kiapi.common.commands.GetVersionResponse"; +const RES_NET_CLASSES_RESPONSE: &str = "kiapi.common.commands.NetClassesResponse"; const RES_GET_OPEN_DOCUMENTS: &str = "kiapi.common.commands.GetOpenDocumentsResponse"; const RES_GET_NETS: &str = "kiapi.board.commands.NetsResponse"; const RES_GET_BOARD_ENABLED_LAYERS: &str = "kiapi.board.commands.BoardEnabledLayersResponse"; @@ -317,6 +319,28 @@ impl KiCadClient { .collect()) } + pub async fn get_net_classes_raw(&self) -> Result { + let command = common_commands::GetNetClasses {}; + let response = self + .send_command(envelope::pack_any(&command, CMD_GET_NET_CLASSES)) + .await?; + response_payload_as_any(response, RES_NET_CLASSES_RESPONSE) + } + + pub async fn get_net_classes(&self) -> Result, KiCadError> { + let payload = self.get_net_classes_raw().await?; + let response: common_commands::NetClassesResponse = + decode_any(&payload, RES_NET_CLASSES_RESPONSE)?; + + let mut classes: Vec = response + .net_classes + .into_iter() + .map(map_net_class_info) + .collect(); + classes.sort_by(|left, right| left.name.cmp(&right.name)); + Ok(classes) + } + pub async fn get_current_project_path(&self) -> Result { let docs = self.get_open_documents(DocumentType::Pcb).await?; select_single_project_path(&docs) diff --git a/test-scripts/kicad-ipc-cli.rs b/test-scripts/kicad-ipc-cli.rs index 363eb72..6d6db49 100644 --- a/test-scripts/kicad-ipc-cli.rs +++ b/test-scripts/kicad-ipc-cli.rs @@ -32,6 +32,7 @@ enum Command { }, ProjectPath, BoardOpen, + NetClasses, Nets, EnabledLayers, ActiveLayer, @@ -179,6 +180,22 @@ async fn run() -> Result<(), KiCadError> { return Err(KiCadError::BoardNotOpen); } } + Command::NetClasses => { + let classes = client.get_net_classes().await?; + println!("net_class_count={}", classes.len()); + for class in classes { + println!( + "name={} type={:?} priority={} constituents={}", + class.name, + class.class_type, + class + .priority + .map(|value| value.to_string()) + .unwrap_or_else(|| "-".to_string()), + class.constituents.join(",") + ); + } + } Command::Nets => { let nets = client.get_nets().await?; if nets.is_empty() { @@ -571,6 +588,7 @@ fn parse_args() -> Result<(CliConfig, Command), KiCadError> { "version" => Command::Version, "project-path" => Command::ProjectPath, "board-open" => Command::BoardOpen, + "net-classes" => Command::NetClasses, "nets" => Command::Nets, "enabled-layers" => Command::EnabledLayers, "active-layer" => Command::ActiveLayer, @@ -906,7 +924,7 @@ fn default_config() -> CliConfig { fn print_help() { println!( - "kicad-ipc-cli\n\nUSAGE:\n cargo run --bin kicad-ipc-cli -- [--socket URI] [--token TOKEN] [--timeout-ms N] [command options]\n\nCOMMANDS:\n ping Check IPC connectivity\n version Fetch KiCad version\n open-docs [--type ] List open docs (default type: pcb)\n project-path Get current project path from open PCB docs\n board-open Exit non-zero if no PCB doc is open\n nets List board nets (requires one open PCB)\n netlist-pads Emit pad-level netlist data (with footprint context)\n items-by-id --id ... Show parsed details for specific item IDs\n item-bbox --id ... Show bounding boxes for item IDs\n hit-test --id --x-nm --y-nm [--tolerance-nm ]\n Hit-test one item at a point\n types-pcb List PCB KiCad object type IDs from proto enum\n items-raw --type-id ... Dump raw Any payloads for requested item type IDs\n items-raw-all-pcb [--debug] Dump all PCB item payloads across all PCB object types\n pad-shape-polygon --pad-id ... --layer-id [--debug]\n Dump pad polygons on a target layer\n padstack-presence --item-id ... --layer-id ... [--debug]\n Check padstack shape presence matrix across layers\n title-block Show title block fields\n board-as-string Dump board as KiCad s-expression text\n selection-as-string Dump current selection as KiCad s-expression text\n stackup Show typed board stackup\n graphics-defaults Show typed graphics defaults\n appearance Show typed editor appearance settings\n netclass Show typed netclass map for current board nets\n proto-coverage-board-read Print board-read command coverage vs proto\n board-read-report [--out P] Write markdown board reconstruction report\n enabled-layers List enabled board layers\n active-layer Show active board layer\n visible-layers Show currently visible board layers\n board-origin [--type ] Show board origin (`grid` default, or `drill`)\n selection-summary Show current selection item type counts\n selection-details Show parsed details for selected items\n selection-raw Show raw Any payload bytes for selected items\n smoke ping + version + board-open summary\n help Show help\n\nTYPES:\n schematic | symbol | pcb | footprint | drawing-sheet | project\n" + "kicad-ipc-cli\n\nUSAGE:\n cargo run --bin kicad-ipc-cli -- [--socket URI] [--token TOKEN] [--timeout-ms N] [command options]\n\nCOMMANDS:\n ping Check IPC connectivity\n version Fetch KiCad version\n open-docs [--type ] List open docs (default type: pcb)\n project-path Get current project path from open PCB docs\n board-open Exit non-zero if no PCB doc is open\n net-classes List project netclass definitions\n nets List board nets (requires one open PCB)\n netlist-pads Emit pad-level netlist data (with footprint context)\n items-by-id --id ... Show parsed details for specific item IDs\n item-bbox --id ... Show bounding boxes for item IDs\n hit-test --id --x-nm --y-nm [--tolerance-nm ]\n Hit-test one item at a point\n types-pcb List PCB KiCad object type IDs from proto enum\n items-raw --type-id ... Dump raw Any payloads for requested item type IDs\n items-raw-all-pcb [--debug] Dump all PCB item payloads across all PCB object types\n pad-shape-polygon --pad-id ... --layer-id [--debug]\n Dump pad polygons on a target layer\n padstack-presence --item-id ... --layer-id ... [--debug]\n Check padstack shape presence matrix across layers\n title-block Show title block fields\n board-as-string Dump board as KiCad s-expression text\n selection-as-string Dump current selection as KiCad s-expression text\n stackup Show typed board stackup\n graphics-defaults Show typed graphics defaults\n appearance Show typed editor appearance settings\n netclass Show typed netclass map for current board nets\n proto-coverage-board-read Print board-read command coverage vs proto\n board-read-report [--out P] Write markdown board reconstruction report\n enabled-layers List enabled board layers\n active-layer Show active board layer\n visible-layers Show currently visible board layers\n board-origin [--type ] Show board origin (`grid` default, or `drill`)\n selection-summary Show current selection item type counts\n selection-details Show parsed details for selected items\n selection-raw Show raw Any payload bytes for selected items\n smoke ping + version + board-open summary\n help Show help\n\nTYPES:\n schematic | symbol | pcb | footprint | drawing-sheet | project\n" ); } @@ -1309,6 +1327,11 @@ fn proto_coverage_board_read_rows() -> Vec<(&'static str, &'static str, &'static "implemented", "get_open_documents", ), + ( + "kiapi.common.commands.GetNetClasses", + "implemented", + "get_net_classes_raw/get_net_classes", + ), ( "kiapi.common.commands.GetItems", "implemented",