0
mirror of https://github.com/bbenchoff/OrthoRoute.git synced 2026-08-27 15:35:27 +00:00
Files
OrthoRoute/scripts/launch_kicad_debug.ps1
2026-07-29 22:58:54 -07:00

150 lines
5.6 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<#
.SYNOPSIS
Launch KiCad with OrthoRoute debug logging enabled.
.DESCRIPTION
Sets ORTHO_DEBUG and optional screenshot env vars in the current process,
then starts KiCad so it inherits them. Env vars are scoped to this session
only — they are removed after KiCad exits.
.PARAMETER KiCadVersion
KiCad major version to launch. Default: 10.0
.PARAMETER ScreenshotFreq
Save a PCB screenshot every N routing iterations. Default: 5 (0 = every iteration).
.PARAMETER ScreenshotScale
PNG resolution multiplier (e.g. 8 = 8× native). Default: 8
.PARAMETER NoScreenshots
Skip screenshots entirely (log only, no PNG output).
.PARAMETER RunValidation
Run automated smoke test validation after KiCad exits.
Calls scripts/optimize_and_validate.ps1 with smoke test.
.EXAMPLE
.\scripts\launch_kicad_debug.ps1
.\scripts\launch_kicad_debug.ps1 -KiCadVersion 10.0
.\scripts\launch_kicad_debug.ps1 -ScreenshotFreq 10 -ScreenshotScale 4
.\scripts\launch_kicad_debug.ps1 -NoScreenshots
.\scripts\launch_kicad_debug.ps1 -RunValidation
#>
param(
[string]$KiCadVersion = "10.0",
[int] $ScreenshotFreq = 5,
[int] $ScreenshotScale = 8,
[switch]$NoScreenshots,
[switch]$RunValidation
)
$kicadRoots = @(
(Join-Path $env:LOCALAPPDATA "Programs\KiCad"),
(Join-Path $env:ProgramFiles "KiCad")
)
$kicadExe = $kicadRoots |
ForEach-Object { Join-Path $_ "$KiCadVersion\bin\kicad.exe" } |
Where-Object { Test-Path -LiteralPath $_ } |
Select-Object -First 1
if (-not $kicadExe) {
Write-Error "KiCad $KiCadVersion was not found in a per-user or machine installation."
Write-Host "Available versions:"
$kicadRoots |
Where-Object { Test-Path -LiteralPath $_ } |
ForEach-Object { Get-ChildItem -LiteralPath $_ -Directory } |
Select-Object -ExpandProperty Name -Unique
exit 1
}
# --- Set env vars (process scope only) ---
$env:ORTHO_DEBUG = '1'
if ($NoScreenshots) {
Remove-Item Env:ORTHO_SCREENSHOT_FREQ -ErrorAction SilentlyContinue
Remove-Item Env:ORTHO_SCREENSHOT_SCALE -ErrorAction SilentlyContinue
} else {
$env:ORTHO_SCREENSHOT_FREQ = "$ScreenshotFreq"
$env:ORTHO_SCREENSHOT_SCALE = "$ScreenshotScale"
}
# --- Print config ---
Write-Host ""
Write-Host "=== OrthoRoute Debug Launch ===" -ForegroundColor Cyan
Write-Host " KiCad : $kicadExe"
Write-Host " ORTHO_DEBUG : $env:ORTHO_DEBUG (full DEBUG log + [ITER N] timing)"
if ($NoScreenshots) {
Write-Host " Screenshots : disabled"
} else {
Write-Host " SCREENSHOT_FREQ : every $ScreenshotFreq iteration(s)"
Write-Host " SCREENSHOT_SCALE : $ScreenshotScale x"
}
Write-Host ""
$docs = [System.Environment]::GetFolderPath('MyDocuments')
$pluginDir = Join-Path $docs "KiCad\$KiCadVersion\3rdparty\plugins\com_github_bbenchoff_orthoroute"
Write-Host " Plugin folder : $pluginDir"
Write-Host " Plugin log : $pluginDir\logs\latest.log"
Write-Host ""
Write-Host " IPC pre-flight checklist:" -ForegroundColor Yellow
Write-Host " 1. Open a .kicad_pcb file in KiCad BEFORE running the plugin"
Write-Host " 2. KiCad menu: Preferences -> Plugins -> Enable Python API (must be checked)"
Write-Host " 3. Restart KiCad after changing the Python API setting"
Write-Host ""
Write-Host " Press Ctrl+C here to kill KiCad and clean up env vars." -ForegroundColor Yellow
Write-Host ""
# --- Sync plugin sources before launching ---
Write-Host "Syncing plugin sources..." -ForegroundColor DarkCyan
& "$PSScriptRoot\copy_to_kicad.ps1" -KiCadVersion $KiCadVersion
Write-Host ""
try {
# Start KiCad — it inherits env vars from this process
& $kicadExe
} finally {
# Clean up regardless of how KiCad exits
Remove-Item Env:ORTHO_DEBUG, Env:ORTHO_SCREENSHOT_FREQ, Env:ORTHO_SCREENSHOT_SCALE -ErrorAction SilentlyContinue
Write-Host ""
Write-Host "KiCad exited. Debug env vars cleared." -ForegroundColor Green
# Show log tail for convenience
$pluginLog = Join-Path ([System.Environment]::GetFolderPath('MyDocuments')) `
"KiCad\$KiCadVersion\3rdparty\plugins\com_github_bbenchoff_orthoroute\logs\latest.log"
if (Test-Path $pluginLog) {
Write-Host ""
Write-Host "--- Last 40 lines of latest.log ---" -ForegroundColor Cyan
Get-Content $pluginLog | Select-Object -Last 40
} else {
Write-Host " No log found at: $pluginLog" -ForegroundColor Yellow
}
# --- Optional validation step ---
if ($RunValidation) {
Write-Host ""
Write-Host "=== Running Automated Validation ===" -ForegroundColor Cyan
Write-Host " Smoke test (100 nets, <30s) to verify routing still works..."
Write-Host ""
$validateScript = Join-Path $PSScriptRoot "optimize_and_validate.ps1"
if (Test-Path $validateScript) {
try {
& $validateScript -SkipDeploy -TestBoard smoke
$validationExit = $LASTEXITCODE
Write-Host ""
if ($validationExit -eq 0) {
Write-Host "✅ VALIDATION PASSED: Routing successful" -ForegroundColor Green
} elseif ($validationExit -eq 2) {
Write-Host "⚠️ VALIDATION WARNING: Performance regression detected" -ForegroundColor Yellow
} else {
Write-Host "❌ VALIDATION FAILED: Routing errors detected (exit code $validationExit)" -ForegroundColor Red
}
} catch {
Write-Host "❌ Validation script error: $_" -ForegroundColor Red
}
} else {
Write-Warning "Validation script not found: $validateScript"
}
}
}