From 94581a7c6b32c1cf2f8518a961c2b642686061cd Mon Sep 17 00:00:00 2001 From: Milind Sharma Date: Fri, 20 Feb 2026 14:15:26 +0800 Subject: [PATCH] feat(project): add GetTextVariables API bindings --- README.md | 6 +++--- docs/TEST_CLI.md | 6 ++++++ src/client.rs | 18 ++++++++++++++++++ test-scripts/kicad-ipc-cli.rs | 16 +++++++++++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c24a3bf..9355335 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 | 1 | 20% | +| Project manager | 5 | 2 | 40% | | Board editor (PCB) | 22 | 13 | 59% | | Schematic editor (dedicated proto commands) | 0 | 0 | n/a | -| **Total** | **56** | **25** | **45%** | +| **Total** | **56** | **26** | **46%** | ### Common (base) @@ -91,7 +91,7 @@ Legend: | `GetNetClasses` | Implemented | `KiCadClient::get_net_classes_raw`, `KiCadClient::get_net_classes` | | `SetNetClasses` | Not yet | - | | `ExpandTextVariables` | Not yet | - | -| `GetTextVariables` | Not yet | - | +| `GetTextVariables` | Implemented | `KiCadClient::get_text_variables_raw`, `KiCadClient::get_text_variables` | | `SetTextVariables` | Not yet | - | ### Board editor (PCB) diff --git a/docs/TEST_CLI.md b/docs/TEST_CLI.md index a661336..66736ed 100644 --- a/docs/TEST_CLI.md +++ b/docs/TEST_CLI.md @@ -53,6 +53,12 @@ List project net classes: cargo run --bin kicad-ipc-cli -- net-classes ``` +List text variables for current board document: + +```bash +cargo run --bin kicad-ipc-cli -- text-variables +``` + List enabled board layers: ```bash diff --git a/src/client.rs b/src/client.rs index 2b8f5d7..3193d48 100644 --- a/src/client.rs +++ b/src/client.rs @@ -36,6 +36,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_TEXT_VARIABLES: &str = "kiapi.common.commands.GetTextVariables"; 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"; @@ -63,6 +64,7 @@ const CMD_SAVE_SELECTION_TO_STRING: &str = "kiapi.common.commands.SaveSelectionT const RES_GET_VERSION: &str = "kiapi.common.commands.GetVersionResponse"; const RES_NET_CLASSES_RESPONSE: &str = "kiapi.common.commands.NetClassesResponse"; +const RES_TEXT_VARIABLES: &str = "kiapi.common.project.TextVariables"; 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"; @@ -341,6 +343,22 @@ impl KiCadClient { Ok(classes) } + pub async fn get_text_variables_raw(&self) -> Result { + let command = common_commands::GetTextVariables { + document: Some(self.current_board_document_proto().await?), + }; + let response = self + .send_command(envelope::pack_any(&command, CMD_GET_TEXT_VARIABLES)) + .await?; + response_payload_as_any(response, RES_TEXT_VARIABLES) + } + + pub async fn get_text_variables(&self) -> Result, KiCadError> { + let payload = self.get_text_variables_raw().await?; + let response: common_project::TextVariables = decode_any(&payload, RES_TEXT_VARIABLES)?; + Ok(response.variables.into_iter().collect()) + } + 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 6d6db49..6c9b39b 100644 --- a/test-scripts/kicad-ipc-cli.rs +++ b/test-scripts/kicad-ipc-cli.rs @@ -33,6 +33,7 @@ enum Command { ProjectPath, BoardOpen, NetClasses, + TextVariables, Nets, EnabledLayers, ActiveLayer, @@ -196,6 +197,13 @@ async fn run() -> Result<(), KiCadError> { ); } } + Command::TextVariables => { + let variables = client.get_text_variables().await?; + println!("text_variable_count={}", variables.len()); + for (name, value) in variables { + println!("name={} value={}", name, value); + } + } Command::Nets => { let nets = client.get_nets().await?; if nets.is_empty() { @@ -589,6 +597,7 @@ fn parse_args() -> Result<(CliConfig, Command), KiCadError> { "project-path" => Command::ProjectPath, "board-open" => Command::BoardOpen, "net-classes" => Command::NetClasses, + "text-variables" => Command::TextVariables, "nets" => Command::Nets, "enabled-layers" => Command::EnabledLayers, "active-layer" => Command::ActiveLayer, @@ -924,7 +933,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 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" + "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 text-variables List text variables for current board document\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" ); } @@ -1332,6 +1341,11 @@ fn proto_coverage_board_read_rows() -> Vec<(&'static str, &'static str, &'static "implemented", "get_net_classes_raw/get_net_classes", ), + ( + "kiapi.common.commands.GetTextVariables", + "implemented", + "get_text_variables_raw/get_text_variables", + ), ( "kiapi.common.commands.GetItems", "implemented",