ethercat-linux/scripts/prune-tsproj.ps1

50 lines
1.8 KiB
PowerShell
Raw Permalink Normal View History

#Requires -Version 5.1
<#
.SYNOPSIS
Prune TwinCAT reference tsproj: keep only EK1100 + EL6695 Primary, remove extra slaves/mappings.
#>
param(
[string]$SourceTsproj = 'C:\Users\tonycao\work\free\TwinCATProject1_DC_Reference\TwinCATProject1.tsproj',
[string]$DestTsproj = 'C:\Users\tonycao\work\twincat3-auto-cli\TwinCAT_EL6695_Primary\TwinCATProject1.tsproj'
)
[xml]$xml = Get-Content $SourceTsproj -Encoding Default
# Find EK1100 box by ProductCode (EK1100 = 0x044c2c52)
$ek1100 = $xml.SelectSingleNode("//Box[EtherCAT[@ProductCode='#x044c2c52']]")
if (-not $ek1100) { throw "EK1100 box not found in $SourceTsproj" }
# Remove all child boxes except the EL6695 (ProductCode 0x1a273052)
$children = @($ek1100.SelectNodes("./Box"))
foreach ($b in $children) {
$ec = $b.SelectSingleNode("./EtherCAT")
if (-not $ec) { continue }
if ($ec.GetAttribute('ProductCode') -ne '#x1a273052') {
$ek1100.RemoveChild($b) | Out-Null
}
}
# Fix remaining EL6695 attributes for simple topology
$el6695 = $ek1100.SelectSingleNode("./Box[EtherCAT[@ProductCode='#x1a273052']]")
if (-not $el6695) { throw "EL6695 box not found under EK1100" }
$ecat = $el6695.SelectSingleNode("./EtherCAT")
$ecat.SetAttribute('PortABoxInfo', '#x01000003')
$ecat.SetAttribute('AdsServerAddress', 'a9feb0d90203ea03')
# Prune Mappings: keep only OwnerB entries that contain EL6695
$mappings = $xml.SelectSingleNode("//Mappings")
if ($mappings) {
$ownerAs = @($mappings.SelectNodes("./OwnerA"))
foreach ($oa in $ownerAs) {
$obs = @($oa.SelectNodes("./OwnerB"))
foreach ($ob in $obs) {
if ($ob.GetAttribute('Name') -notmatch 'EL6695') {
$oa.RemoveChild($ob) | Out-Null
}
}
}
}
$xml.Save($DestTsproj)
Write-Host "Pruned tsproj saved to $DestTsproj"