diff --git a/README.md b/README.md index b98d88a..1cc8360 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,9 @@ Legend: | Common (base) | 6 | 4 | 67% | | Common editor/document | 23 | 12 | 52% | | Project manager | 5 | 3 | 60% | -| Board editor (PCB) | 22 | 14 | 64% | +| Board editor (PCB) | 22 | 15 | 68% | | Schematic editor (dedicated proto commands) | 0 | 0 | n/a | -| **Total** | **56** | **33** | **59%** | +| **Total** | **56** | **34** | **61%** | ### Common (base) @@ -114,7 +114,7 @@ Legend: | `CheckPadstackPresenceOnLayers` | Implemented | `KiCadClient::check_padstack_presence_on_layers_raw`, `KiCadClient::check_padstack_presence_on_layers` | | `InjectDrcError` | Not yet | - | | `GetVisibleLayers` | Implemented | `KiCadClient::get_visible_layers` | -| `SetVisibleLayers` | Not yet | - | +| `SetVisibleLayers` | Implemented | `KiCadClient::set_visible_layers` | | `GetActiveLayer` | Implemented | `KiCadClient::get_active_layer` | | `SetActiveLayer` | Implemented | `KiCadClient::set_active_layer` | | `GetBoardEditorAppearanceSettings` | Implemented | `KiCadClient::get_board_editor_appearance_settings_raw`, `KiCadClient::get_board_editor_appearance_settings` | diff --git a/docs/TEST_CLI.md b/docs/TEST_CLI.md index 8f727ac..5d3c052 100644 --- a/docs/TEST_CLI.md +++ b/docs/TEST_CLI.md @@ -101,6 +101,12 @@ Show visible layers: cargo run --bin kicad-ipc-cli -- visible-layers ``` +Set visible layers: + +```bash +cargo run --bin kicad-ipc-cli -- set-visible-layers --layer-id 0 --layer-id 31 +``` + Show board origin (grid origin by default): ```bash diff --git a/src/client.rs b/src/client.rs index b46a478..a247035 100644 --- a/src/client.rs +++ b/src/client.rs @@ -49,6 +49,7 @@ const CMD_GET_BOARD_ENABLED_LAYERS: &str = "kiapi.board.commands.GetBoardEnabled const CMD_GET_ACTIVE_LAYER: &str = "kiapi.board.commands.GetActiveLayer"; const CMD_SET_ACTIVE_LAYER: &str = "kiapi.board.commands.SetActiveLayer"; const CMD_GET_VISIBLE_LAYERS: &str = "kiapi.board.commands.GetVisibleLayers"; +const CMD_SET_VISIBLE_LAYERS: &str = "kiapi.board.commands.SetVisibleLayers"; const CMD_GET_BOARD_ORIGIN: &str = "kiapi.board.commands.GetBoardOrigin"; const CMD_GET_BOARD_STACKUP: &str = "kiapi.board.commands.GetBoardStackup"; const CMD_GET_GRAPHICS_DEFAULTS: &str = "kiapi.board.commands.GetGraphicsDefaults"; @@ -608,6 +609,18 @@ impl KiCadClient { Ok(payload.layers.into_iter().map(layer_to_model).collect()) } + pub async fn set_visible_layers(&self, layer_ids: Vec) -> Result<(), KiCadError> { + let board = self.current_board_document_proto().await?; + let command = board_commands::SetVisibleLayers { + board: Some(board), + layers: layer_ids, + }; + + self.send_command(envelope::pack_any(&command, CMD_SET_VISIBLE_LAYERS)) + .await?; + Ok(()) + } + pub async fn get_board_origin(&self, kind: BoardOriginKind) -> Result { let board = self.current_board_document_proto().await?; let command = board_commands::GetBoardOrigin { diff --git a/test-scripts/kicad-ipc-cli.rs b/test-scripts/kicad-ipc-cli.rs index 233870b..36db293 100644 --- a/test-scripts/kicad-ipc-cli.rs +++ b/test-scripts/kicad-ipc-cli.rs @@ -52,6 +52,9 @@ enum Command { layer_id: i32, }, VisibleLayers, + SetVisibleLayers { + layer_ids: Vec, + }, BoardOrigin { kind: BoardOriginKind, }, @@ -323,6 +326,10 @@ async fn run() -> Result<(), KiCadError> { } } } + Command::SetVisibleLayers { layer_ids } => { + client.set_visible_layers(layer_ids.clone()).await?; + println!("set_visible_layer_count={}", layer_ids.len()); + } Command::BoardOrigin { kind } => { let origin = client.get_board_origin(kind).await?; println!( @@ -814,6 +821,32 @@ fn parse_args_from(mut args: Vec) -> Result<(CliConfig, Command), KiCadE } } "visible-layers" => Command::VisibleLayers, + "set-visible-layers" => { + let mut layer_ids = Vec::new(); + let mut i = 1; + while i < args.len() { + if args[i] == "--layer-id" { + let value = args.get(i + 1).ok_or_else(|| KiCadError::Config { + reason: "missing value for set-visible-layers --layer-id".to_string(), + })?; + layer_ids.push(value.parse::().map_err(|err| KiCadError::Config { + reason: format!("invalid set-visible-layers --layer-id `{value}`: {err}"), + })?); + i += 2; + continue; + } + i += 1; + } + + if layer_ids.is_empty() { + return Err(KiCadError::Config { + reason: "set-visible-layers requires one or more `--layer-id ` arguments" + .to_string(), + }); + } + + Command::SetVisibleLayers { layer_ids } + } "board-origin" => { let mut kind = BoardOriginKind::Grid; let mut i = 1; @@ -1207,7 +1240,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] [--client-name NAME] [--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 expand-text-variables Expand variables in provided text values\n Options: --text (repeatable)\n text-extents Measure text bounding box\n Options: --text \n text-as-shapes Convert text to rendered shapes\n Options: --text (repeatable)\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 set-active-layer --layer-id \n Set active board layer\n visible-layers Show currently visible board layers\n board-origin [--type ] Show board origin (`grid` default, or `drill`)\n refresh-editor [--frame ] Refresh a specific editor frame (default: pcb)\n begin-commit Start staged commit and print commit ID\n end-commit --id [--action ] [--message ]\n End staged commit with commit/drop action\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] [--client-name NAME] [--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 expand-text-variables Expand variables in provided text values\n Options: --text (repeatable)\n text-extents Measure text bounding box\n Options: --text \n text-as-shapes Convert text to rendered shapes\n Options: --text (repeatable)\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 set-active-layer --layer-id \n Set active board layer\n visible-layers Show currently visible board layers\n set-visible-layers --layer-id ...\n Set visible board layers\n board-origin [--type ] Show board origin (`grid` default, or `drill`)\n refresh-editor [--frame ] Refresh a specific editor frame (default: pcb)\n begin-commit Start staged commit and print commit ID\n end-commit --id [--action ] [--message ]\n End staged commit with commit/drop action\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" ); } @@ -1833,4 +1866,21 @@ mod tests { other => panic!("unexpected command variant: {other:?}"), } } + + #[test] + fn parse_args_parses_set_visible_layers() { + let (_, command) = parse_args_from(vec![ + "set-visible-layers".to_string(), + "--layer-id".to_string(), + "3".to_string(), + "--layer-id".to_string(), + "47".to_string(), + ]) + .expect("set-visible-layers args should parse"); + + match command { + Command::SetVisibleLayers { layer_ids } => assert_eq!(layer_ids, vec![3, 47]), + other => panic!("unexpected command variant: {other:?}"), + } + } }