diff --git a/NetDiagram-PS/NetDiagram-PS.psm1 b/NetDiagram-PS/NetDiagram-PS.psm1
index e84ea15..f0e4170 100644
--- a/NetDiagram-PS/NetDiagram-PS.psm1
+++ b/NetDiagram-PS/NetDiagram-PS.psm1
@@ -792,19 +792,46 @@ function Export-DrawIO {
$null = $xml.AppendLine(' ')
$null = $xml.AppendLine(' ')
- # Add subnet containers (best practice: group by network segment)
+ # Assign each node to its first matching subnet before sizing containers.
+ $nodeSubnet = @{}
+ $subnetNodeCount = @{}
+ foreach ($subnet in $Topology.Subnets) {
+ $subnetNodeCount[$subnet.CIDR] = 0
+ }
+ foreach ($node in $uniqueNodes) {
+ foreach ($subnet in $Topology.Subnets) {
+ if (Test-IPInSubnet -IP $node.IP -CIDR $subnet.CIDR) {
+ $nodeSubnet[$node.IP] = $subnet.CIDR
+ $subnetNodeCount[$subnet.CIDR]++
+ break
+ }
+ }
+ }
+
+ # Add subnet containers. Each row starts below the tallest container in the
+ # previous row, so dense subnets cannot overlap the row beneath them.
$containerID = $nextID
$subnetContainers = @{}
+ $containerIndex = 0
+ $containerRowY = 20
+ $containerRowHeight = 0
foreach ($subnet in $Topology.Subnets) {
$subnetLabel = [System.Security.SecurityElement]::Escape("$($subnet.Label)`n$($subnet.CIDR)")
$containerStyle = 'swimlane;fontSize=14;fontStyle=1;fillColor=#f5f5f5;strokeColor=#666666;rounded=1;'
- # Calculate container size based on nodes in subnet
+ $column = $containerIndex % 2
+ if ($column -eq 0 -and $containerIndex -gt 0) {
+ $containerRowY += $containerRowHeight + 50
+ $containerRowHeight = 0
+ }
+
+ $nodeRows = [Math]::Ceiling($subnetNodeCount[$subnet.CIDR] / 4.0)
$containerWidth = 800
- $containerHeight = 550
- $containerX = 20 + (($subnetContainers.Count % 2) * 850)
- $containerY = 20 + ([Math]::Floor($subnetContainers.Count / 2) * 600)
+ $containerHeight = [Math]::Max(140, 40 + ([int]$nodeRows * 110))
+ $containerX = 20 + ($column * 850)
+ $containerY = $containerRowY
+ $containerRowHeight = [Math]::Max($containerRowHeight, $containerHeight)
$null = $xml.AppendLine(" ")
$null = $xml.AppendLine(" ")
@@ -812,27 +839,28 @@ function Export-DrawIO {
$subnetContainers[$subnet.CIDR] = $containerID
$containerID++
+ $containerIndex++
}
$nextID = $containerID
- # Map each node to the first subnet container whose CIDR contains it, so nodes
- # are parented/positioned inside the right swimlane instead of always parent="1".
+ # Map assigned subnet CIDRs to their emitted container IDs.
$nodeContainer = @{}
foreach ($node in $uniqueNodes) {
- foreach ($subnet in $Topology.Subnets) {
- if ($subnetContainers.ContainsKey($subnet.CIDR) -and (Test-IPInSubnet -IP $node.IP -CIDR $subnet.CIDR)) {
- $nodeContainer[$node.IP] = $subnetContainers[$subnet.CIDR]
- break
- }
+ if ($nodeSubnet.ContainsKey($node.IP)) {
+ $nodeContainer[$node.IP] = $subnetContainers[$nodeSubnet[$node.IP]]
}
}
# Layout bookkeeping: per-container child index, and a base Y for unmatched
# (canvas-level) nodes placed below the container grid so nothing overlaps.
$containerChildCount = @{}
- $containerRows = [Math]::Ceiling($subnetContainers.Count / 2.0)
- $unmatchedBaseY = 20 + ([int]$containerRows * 600) + 40
+ $unmatchedBaseY = if ($subnetContainers.Count -gt 0) {
+ $containerRowY + $containerRowHeight + 40
+ }
+ else {
+ 20
+ }
# Add nodes with enhanced styles and metadata
foreach ($layer in $layerOrder) {
diff --git a/tests/NetDiagram.Tests.ps1 b/tests/NetDiagram.Tests.ps1
index e321402..b39e7df 100644
--- a/tests/NetDiagram.Tests.ps1
+++ b/tests/NetDiagram.Tests.ps1
@@ -583,6 +583,90 @@ Describe 'Export-DrawIO subnet container parenting (#9 regression)' {
}
}
+Describe 'Export-DrawIO dynamic container layout (#19 regression)' {
+ It 'Contains every node for a -node subnet' -TestCases @(
+ @{ Count = 0 }
+ @{ Count = 1 }
+ @{ Count = 16 }
+ @{ Count = 17 }
+ @{ Count = 50 }
+ ) {
+ param($Count)
+
+ $nodes = @(
+ for ($index = 1; $index -le $Count; $index++) {
+ [pscustomobject]@{
+ IP = "10.0.0.$index"; Hostname = "node-$index"; Role = 'switch'
+ Vendor = 'Test'; OS = 'Test'; Layer = 'Access'; Reachable = $null
+ }
+ }
+ )
+ $topology = [pscustomobject]@{
+ Nodes = $nodes
+ Edges = @()
+ Subnets = @([pscustomobject]@{ CIDR = '10.0.0.0/24'; Label = 'Test'; VLAN = 1 })
+ }
+ $drawioPath = Join-Path $script:TestDataPath "layout-$Count.drawio"
+
+ $topology | Export-DrawIO -OutFile $drawioPath
+ $xml = [xml](Get-Content -Path $drawioPath -Raw)
+ $container = $xml.SelectSingleNode("//mxCell[contains(@style,'swimlane')]")
+ $containerHeight = [double]$container.mxGeometry.height
+ $nodeCells = @($xml.SelectNodes("//mxCell[@vertex='1' and not(contains(@style,'swimlane'))]"))
+
+ $nodeCells | Should -HaveCount $Count
+ foreach ($node in $nodeCells) {
+ $node.parent | Should -Be $container.id
+ ([double]$node.mxGeometry.y + [double]$node.mxGeometry.height) |
+ Should -BeLessOrEqual $containerHeight
+ }
+ }
+
+ It 'Keeps multiple variable-height containers and unmatched nodes separated' {
+ $nodes = @()
+ $subnets = @()
+ $counts = @(50, 1, 17, 16)
+ for ($subnetIndex = 0; $subnetIndex -lt $counts.Count; $subnetIndex++) {
+ $octet = $subnetIndex + 1
+ $subnets += [pscustomobject]@{ CIDR = "10.0.$octet.0/24"; Label = "Subnet $octet"; VLAN = $octet }
+ for ($hostIndex = 1; $hostIndex -le $counts[$subnetIndex]; $hostIndex++) {
+ $nodes += [pscustomobject]@{
+ IP = "10.0.$octet.$hostIndex"; Hostname = "node-$octet-$hostIndex"; Role = 'switch'
+ Vendor = 'Test'; OS = 'Test'; Layer = 'Access'; Reachable = $null
+ }
+ }
+ }
+ $nodes += [pscustomobject]@{
+ IP = '203.0.113.10'; Hostname = 'unmatched'; Role = 'server'
+ Vendor = 'Test'; OS = 'Test'; Layer = 'Servers'; Reachable = $null
+ }
+ $topology = [pscustomobject]@{ Nodes = $nodes; Edges = @(); Subnets = $subnets }
+ $drawioPath = Join-Path $script:TestDataPath 'layout-multiple.drawio'
+
+ $topology | Export-DrawIO -OutFile $drawioPath
+ $xml = [xml](Get-Content -Path $drawioPath -Raw)
+ $containers = @($xml.SelectNodes("//mxCell[contains(@style,'swimlane')]"))
+
+ for ($left = 0; $left -lt $containers.Count; $left++) {
+ for ($right = $left + 1; $right -lt $containers.Count; $right++) {
+ $a = $containers[$left].mxGeometry
+ $b = $containers[$right].mxGeometry
+ $overlaps = ([double]$a.x -lt ([double]$b.x + [double]$b.width)) -and
+ (([double]$a.x + [double]$a.width) -gt [double]$b.x) -and
+ ([double]$a.y -lt ([double]$b.y + [double]$b.height)) -and
+ (([double]$a.y + [double]$a.height) -gt [double]$b.y)
+ $overlaps | Should -Be $false
+ }
+ }
+
+ $containerBottom = ($containers | ForEach-Object {
+ [double]$_.mxGeometry.y + [double]$_.mxGeometry.height
+ } | Measure-Object -Maximum).Maximum
+ $unmatched = $xml.SelectSingleNode("//mxCell[@vertex='1' and contains(@value,'unmatched')]")
+ [double]$unmatched.mxGeometry.y | Should -BeGreaterThan $containerBottom
+ }
+}
+
Describe 'Quick-start wizard CIDR math (#4 regression)' {
BeforeAll {
# Dot-source the example; the dot-source guard returns before the wizard body