feat: add export_odb tool (kicad-cli, ODB++ archive)
New MCP tool `export_odb` wrapping `kicad-cli pcb export odb`: precision, compression mode (zip/tgz/none), and units. Single ODB++ job archive for CAM/MES/assembly. Self-contained interface handler reading the saved .kicad_pcb. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -590,6 +590,7 @@ class KiCADInterface:
|
|||||||
"export_gerbers": self._handle_export_gerbers,
|
"export_gerbers": self._handle_export_gerbers,
|
||||||
"export_drill": self._handle_export_drill,
|
"export_drill": self._handle_export_drill,
|
||||||
"export_ipc2581": self._handle_export_ipc2581,
|
"export_ipc2581": self._handle_export_ipc2581,
|
||||||
|
"export_odb": self._handle_export_odb,
|
||||||
"generate_netlist": self._handle_generate_netlist,
|
"generate_netlist": self._handle_generate_netlist,
|
||||||
"sync_schematic_to_board": self._handle_sync_schematic_to_board,
|
"sync_schematic_to_board": self._handle_sync_schematic_to_board,
|
||||||
"list_schematic_libraries": self._handle_list_schematic_libraries,
|
"list_schematic_libraries": self._handle_list_schematic_libraries,
|
||||||
@@ -4763,6 +4764,74 @@ class KiCADInterface:
|
|||||||
logger.error(f"Error exporting IPC-2581: {e}")
|
logger.error(f"Error exporting IPC-2581: {e}")
|
||||||
return {"success": False, "message": str(e)}
|
return {"success": False, "message": str(e)}
|
||||||
|
|
||||||
|
def _handle_export_odb(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Export the PCB in ODB++ format via kicad-cli (`pcb export odb`).
|
||||||
|
|
||||||
|
ODB++ is a fab/assembly job archive. ``compression`` selects the output
|
||||||
|
container (zip / tgz / none). Reads the saved .kicad_pcb.
|
||||||
|
"""
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
logger.info("Exporting ODB++ via kicad-cli")
|
||||||
|
try:
|
||||||
|
board_path = params.get("boardPath") or self._current_board_path()
|
||||||
|
output_path = params.get("outputPath")
|
||||||
|
|
||||||
|
if not board_path:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": "boardPath is required (no current board could be resolved)",
|
||||||
|
}
|
||||||
|
if not os.path.exists(board_path):
|
||||||
|
return {"success": False, "message": f"Board not found: {board_path}"}
|
||||||
|
if not output_path:
|
||||||
|
return {"success": False, "message": "outputPath is required"}
|
||||||
|
|
||||||
|
kicad_cli = self._find_kicad_cli_static()
|
||||||
|
if not kicad_cli:
|
||||||
|
return {"success": False, "message": "kicad-cli not found in PATH"}
|
||||||
|
|
||||||
|
output_path = os.path.abspath(os.path.expanduser(output_path))
|
||||||
|
parent = os.path.dirname(output_path)
|
||||||
|
if parent:
|
||||||
|
os.makedirs(parent, exist_ok=True)
|
||||||
|
|
||||||
|
cmd = [kicad_cli, "pcb", "export", "odb", "--output", output_path]
|
||||||
|
|
||||||
|
if params.get("drawingSheet"):
|
||||||
|
cmd += ["--drawing-sheet", params["drawingSheet"]]
|
||||||
|
for kv in params.get("defineVar", []) or []:
|
||||||
|
cmd += ["--define-var", kv]
|
||||||
|
for key, flag in {
|
||||||
|
"precision": "--precision",
|
||||||
|
"compression": "--compression",
|
||||||
|
"units": "--units",
|
||||||
|
}.items():
|
||||||
|
val = params.get(key)
|
||||||
|
if val is not None and val != "":
|
||||||
|
cmd += [flag, str(val)]
|
||||||
|
|
||||||
|
cmd.append(board_path)
|
||||||
|
|
||||||
|
logger.info(f"Running: {' '.join(cmd)}")
|
||||||
|
result = subprocess.run(cmd, capture_output=True, text=True, timeout=180)
|
||||||
|
|
||||||
|
if result.returncode != 0:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"message": f"kicad-cli failed (exit {result.returncode}): "
|
||||||
|
f"{result.stderr.strip()}",
|
||||||
|
}
|
||||||
|
return {"success": True, "outputPath": output_path}
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
return {"success": False, "message": "kicad-cli not found in PATH"}
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
return {"success": False, "message": "kicad-cli timed out after 180 seconds"}
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error exporting ODB++: {e}")
|
||||||
|
return {"success": False, "message": str(e)}
|
||||||
|
|
||||||
def _handle_generate_netlist(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
def _handle_generate_netlist(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
"""Generate netlist from schematic and return structured JSON.
|
"""Generate netlist from schematic and return structured JSON.
|
||||||
|
|
||||||
|
|||||||
@@ -499,5 +499,40 @@ export function registerExportTools(server: McpServer, callKicadScript: CommandF
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Export ODB++ Tool (kicad-cli)
|
||||||
|
// ------------------------------------------------------
|
||||||
|
server.tool(
|
||||||
|
"export_odb",
|
||||||
|
"Export the PCB in ODB++ format via kicad-cli. Single job archive (copper, drill, placement, components, nets, outline) widely used by CAM/MES/assembly. Reads the last SAVED state of the .kicad_pcb.",
|
||||||
|
{
|
||||||
|
outputPath: z.string().describe("Output file path (archive or directory per compression)"),
|
||||||
|
boardPath: z.string().optional().describe("Path to the .kicad_pcb (default: current board)"),
|
||||||
|
drawingSheet: z.string().optional().describe("Path to a drawing sheet override"),
|
||||||
|
defineVar: z
|
||||||
|
.array(z.string())
|
||||||
|
.optional()
|
||||||
|
.describe("Project variable overrides as 'KEY=VALUE' strings"),
|
||||||
|
precision: z.number().optional().describe("Coordinate precision (default 2)"),
|
||||||
|
compression: z
|
||||||
|
.enum(["zip", "tgz", "none"])
|
||||||
|
.optional()
|
||||||
|
.describe("Output container/compression mode (default zip)"),
|
||||||
|
units: z.enum(["mm", "in"]).optional().describe("Units (default mm)"),
|
||||||
|
},
|
||||||
|
async (args) => {
|
||||||
|
logger.debug(`Exporting ODB++ to: ${args.outputPath}`);
|
||||||
|
const result = await callKicadScript("export_odb", args);
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: JSON.stringify(result),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
logger.info("Export tools registered");
|
logger.info("Export tools registered");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ export const toolCategories: ToolCategory[] = [
|
|||||||
"export_gerbers",
|
"export_gerbers",
|
||||||
"export_drill",
|
"export_drill",
|
||||||
"export_ipc2581",
|
"export_ipc2581",
|
||||||
|
"export_odb",
|
||||||
"export_pdf",
|
"export_pdf",
|
||||||
"export_svg",
|
"export_svg",
|
||||||
"export_3d",
|
"export_3d",
|
||||||
|
|||||||
Reference in New Issue
Block a user