feat: add export_sch_dxf tool (kicad-cli, schematic DXF)
New MCP tool `export_sch_dxf` wrapping `kicad-cli sch export dxf`: outputs one DXF per page into a directory. Exposes drawing-sheet override, theme, B&W, exclude-drawing-sheet, default-font, and page selection. schematicPath required; directory output uses outputDir. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -602,6 +602,7 @@ class KiCADInterface:
|
|||||||
"export_sch_bom": self._handle_export_sch_bom,
|
"export_sch_bom": self._handle_export_sch_bom,
|
||||||
"export_sch_pdf": self._handle_export_sch_pdf,
|
"export_sch_pdf": self._handle_export_sch_pdf,
|
||||||
"export_sch_svg": self._handle_export_sch_svg,
|
"export_sch_svg": self._handle_export_sch_svg,
|
||||||
|
"export_sch_dxf": self._handle_export_sch_dxf,
|
||||||
"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,
|
||||||
@@ -5829,6 +5830,84 @@ class KiCADInterface:
|
|||||||
logger.error(f"Error exporting schematic SVG: {e}")
|
logger.error(f"Error exporting schematic SVG: {e}")
|
||||||
return {"success": False, "message": str(e)}
|
return {"success": False, "message": str(e)}
|
||||||
|
|
||||||
|
def _handle_export_sch_dxf(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Export a schematic to DXF via kicad-cli (`sch export dxf`).
|
||||||
|
|
||||||
|
Output is a directory (one DXF per page). Exposes drawing-sheet override,
|
||||||
|
theme, B&W, exclude-drawing-sheet, default-font, and page selection.
|
||||||
|
schematicPath is required.
|
||||||
|
"""
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
logger.info("Exporting schematic DXF via kicad-cli")
|
||||||
|
try:
|
||||||
|
schematic_path = params.get("schematicPath")
|
||||||
|
output_dir = params.get("outputDir")
|
||||||
|
|
||||||
|
if not schematic_path:
|
||||||
|
return {"success": False, "message": "schematicPath is required"}
|
||||||
|
if not os.path.exists(schematic_path):
|
||||||
|
return {"success": False, "message": f"Schematic not found: {schematic_path}"}
|
||||||
|
if not output_dir:
|
||||||
|
return {"success": False, "message": "outputDir is required"}
|
||||||
|
|
||||||
|
kicad_cli = self._find_kicad_cli_static()
|
||||||
|
if not kicad_cli:
|
||||||
|
return {"success": False, "message": "kicad-cli not found in PATH"}
|
||||||
|
|
||||||
|
output_dir = os.path.abspath(os.path.expanduser(output_dir))
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
cmd = [kicad_cli, "sch", "export", "dxf", "--output", output_dir]
|
||||||
|
|
||||||
|
if params.get("drawingSheet"):
|
||||||
|
cmd += ["--drawing-sheet", params["drawingSheet"]]
|
||||||
|
for kv in params.get("defineVar", []) or []:
|
||||||
|
cmd += ["--define-var", kv]
|
||||||
|
|
||||||
|
value_map = {
|
||||||
|
"theme": "--theme",
|
||||||
|
"defaultFont": "--default-font",
|
||||||
|
"pages": "--pages",
|
||||||
|
}
|
||||||
|
for key, flag in value_map.items():
|
||||||
|
val = params.get(key)
|
||||||
|
if val is not None and val != "":
|
||||||
|
cmd += [flag, str(val)]
|
||||||
|
|
||||||
|
flag_map = {
|
||||||
|
"blackAndWhite": "--black-and-white",
|
||||||
|
"excludeDrawingSheet": "--exclude-drawing-sheet",
|
||||||
|
}
|
||||||
|
for key, flag in flag_map.items():
|
||||||
|
if params.get(key):
|
||||||
|
cmd.append(flag)
|
||||||
|
|
||||||
|
cmd.append(schematic_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()}",
|
||||||
|
}
|
||||||
|
|
||||||
|
files = sorted(
|
||||||
|
f for f in os.listdir(output_dir) if os.path.isfile(os.path.join(output_dir, f))
|
||||||
|
)
|
||||||
|
return {"success": True, "outputDir": output_dir, "files": files}
|
||||||
|
|
||||||
|
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 schematic DXF: {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.
|
||||||
|
|
||||||
|
|||||||
@@ -1200,5 +1200,42 @@ export function registerExportTools(server: McpServer, callKicadScript: CommandF
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Export Schematic DXF Tool (kicad-cli, full option set)
|
||||||
|
// ------------------------------------------------------
|
||||||
|
server.tool(
|
||||||
|
"export_sch_dxf",
|
||||||
|
"Export a schematic to DXF via kicad-cli (`sch export dxf`), one DXF per page into a directory. Exposes drawing-sheet override, theme, black-and-white, exclude-drawing-sheet, default-font, and page selection. schematicPath is REQUIRED.",
|
||||||
|
{
|
||||||
|
schematicPath: z.string().describe("Path to the .kicad_sch (required)"),
|
||||||
|
outputDir: z.string().describe("Output directory for the DXF files"),
|
||||||
|
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"),
|
||||||
|
theme: z.string().optional().describe("Color theme to use (default: schematic settings)"),
|
||||||
|
blackAndWhite: z.boolean().optional().describe("Black and white only"),
|
||||||
|
excludeDrawingSheet: z.boolean().optional().describe("No drawing sheet"),
|
||||||
|
defaultFont: z.string().optional().describe("Default font name"),
|
||||||
|
pages: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.describe("Comma list of page numbers to print (blank = all pages)"),
|
||||||
|
},
|
||||||
|
async (args) => {
|
||||||
|
logger.debug(`Exporting schematic DXF to: ${args.outputDir}`);
|
||||||
|
const result = await callKicadScript("export_sch_dxf", args);
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: JSON.stringify(result),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
logger.info("Export tools registered");
|
logger.info("Export tools registered");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ export const toolCategories: ToolCategory[] = [
|
|||||||
"export_sch_bom",
|
"export_sch_bom",
|
||||||
"export_sch_pdf",
|
"export_sch_pdf",
|
||||||
"export_sch_svg",
|
"export_sch_svg",
|
||||||
|
"export_sch_dxf",
|
||||||
"export_pdf",
|
"export_pdf",
|
||||||
"export_svg",
|
"export_svg",
|
||||||
"export_3d",
|
"export_3d",
|
||||||
|
|||||||
Reference in New Issue
Block a user