Skip to content

Instantly share code, notes, and snippets.

@aschmidt75
Created November 5, 2023 14:46
Show Gist options
  • Save aschmidt75/c67270224e88f74be1c6121c87af6822 to your computer and use it in GitHub Desktop.
Save aschmidt75/c67270224e88f74be1c6121c87af6822 to your computer and use it in GitHub Desktop.
drawio
[CmdletBinding()]
class DrawIOStyle {
[Hashtable]$elems
[bool]$IsImage
DrawIOStyle() {
$this.elems = New-Object -TypeName Hashtable
}
[void]Set([string]$key, [string]$value) {
$this.elems.Add($key, $value)
if ($key -eq "image") {
$this.IsImage = $true
}
}
[string]Render() {
$res = ""
if ($this.IsImage) {
$res += "image;"
}
$this.elems.GetEnumerator() | foreach {
$res += "{0}={1};" -f $_.key, $_.value
}
return $res
}
}
function New-DrawIOStyle {
[CmdletBinding()]
$res = (New-Object -TypeName DrawIOStyle)
$res
}
function Update-DrawIOStyle {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, Mandatory=$True)]
[DrawIOStyle]$Style,
[string]$width,
[string]$html,
[string]$whitespace,
[string]$rounded,
[string]$fillColor,
[string]$strokeColor,
[string]$dashed,
[string]$dashPattern,
[string]$align,
[string]$verticalAlign,
[string]$image,
[string]$fontSize,
[string]$aspect
)
begin {
$validParams = @(
"width",
"html",
"whitespace",
"rounded",
"fillColor",
"strokeColor",
"dashed",
"dashPattern",
"align",
"verticalAlign",
"image",
"fontSize",
"aspect"
)
}
process {
foreach ($v in $validParams) {
if ($PSBoundParameters[$v]) {
$Style.Set($v, $PSBoundParameters[$v])
}
}
}
end {
$Style
}
}
function Format-DrawIOStyle {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline,Mandatory=$True)]
[DrawIOStyle]$Style
)
$Style.Render()
}
function Get-DrawIORandomID {
return [Convert]::ToBase64String((1..9| % {Get-Random -Minimum 0 -Maximum 256}))
}
class MXCell {
[string]$Id
[string]$ParentId
[string]$Value
[string]$Vertex
[string]$Style
[MXGeometry]$Geometry
MXCell($Id) {
$this.Id = $Id
$this.ParentId = "1"
$this.Geometry = $Null
$this.Vertex = "1"
}
MXCell($Id, $ParentId) {
$this.Id = $Id
$this.ParentId = $ParentId
$this.Geometry = $Null
$this.Vertex = "1"
}
[void]Write($xmlWriter) {
$xmlWriter.WriteStartElement('mxCell')
$xmlWriter.WriteAttributeString('id', $this.Id)
$xmlWriter.WriteAttributeString('parent', $this.ParentId)
if ($this.Value) {
$xmlWriter.WriteAttributeString('value', $this.Value)
}
if ($this.Vertex) {
$xmlWriter.WriteAttributeString('vertex', $this.Vertex)
}
if ($this.Style) {
$xmlWriter.WriteAttributeString('style', $this.Style)
}
if ($this.Geometry) {
$this.Geometry.Write($xmlWriter)
}
$xmlWriter.WriteEndElement()
}
}
class MXGeometry {
[string]$As
[string]$X
[string]$Y
[string]$Width
[string]$Height
MXGeometry() {
$this.As = "Geometry"
}
MXGeometry($x, $y, $w, $h) {
$this.As = "geometry"
$this.X = $x
$this.Y = $y
$this.Width = $w
$this.Height = $h
}
[void]Write($xmlWriter) {
$xmlWriter.WriteStartElement('mxGeometry')
$xmlWriter.WriteAttributeString('as', $this.As)
if ($this.X) {
$xmlWriter.WriteAttributeString('x', $this.X)
}
if ($this.Y) {
$xmlWriter.WriteAttributeString('y', $this.Y)
}
if ($this.Width) {
$xmlWriter.WriteAttributeString('width', $this.Width)
}
if ($this.Height) {
$xmlWriter.WriteAttributeString('height', $this.Height)
}
$xmlWriter.WriteEndElement()
}
}
function Write-DrawIODocument {
<#
.SYNOPSIS
Takes MXCells from a pipeline and writes them to an xml document suitable for DrawIO to read.
.DESCRIPTION
This function creates an XMLWriter, writes a DrawIO conformant XML Document with headers
and embeds all MXCells from a pipeline into it.
#>
[CmdletBinding()]
param(
[string]$Path,
[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
[MXCell]$cell,
[System.Xml.XmlWriterSettings]$Settings
)
begin {
if (-not $Settings) {
$Settings = [System.Xml.XmlWriterSettings]::new()
$Settings.Indent = $true
$Settings.OmitXmlDeclaration = $true
$Settings.NewLineOnAttributes = $false
}
$sb = [System.Text.StringBuilder]::new()
$writer = [System.Xml.XmlWriter]::Create($sb, $settings)
if ($Path) {
$writer = [System.Xml.XmlWriter]::Create($Path, $settings)
}
$writer.WriteStartDocument()
$writer.WriteStartElement('mxfile')
$writer.WriteAttributeString('host', 'app.diagrams.net')
$writer.WriteStartElement('diagram')
$writer.WriteAttributeString('name', 'ZoneNetworking')
$id = Get-DrawIORandomID
$writer.WriteAttributeString('id', $id)
$writer.WriteStartElement('mxGraphModel')
$graphDefaults = @{
dx="1114"
dy="797"
grid="1"
gridSize="10"
guides="1"
tooltips="1"
connect="1"
arrows="1"
fold="1"
page="1"
pageScale="1"
pageWidth="827"
pageHeight="1169"
math="0"
shadow="0"
}
foreach ($gd in $graphDefaults.GetEnumerator()) {
$writer.WriteAttributeString($gd.Name, $gd.Value)
}
$writer.WriteStartElement('root')
# write the 0 and 1 cells
$writer.WriteStartElement('mxCell')
$writer.WriteAttributeString('id', '0')
$writer.WriteEndElement()
$writer.WriteStartElement('mxCell')
$writer.WriteAttributeString('id', '1')
$writer.WriteAttributeString('parent', '0')
$writer.WriteEndElement()
}
process {
$cell.Write($writer)
}
end {
$writer.WriteEndElement()
$writer.WriteEndElement()
$writer.WriteEndElement()
$writer.WriteEndElement()
$writer.Flush()
$writer.Close()
if (-not $Path) {
return $sb.ToString()
}
}
}
$DrawIODefaultStyle_Vnet_Details = @{
html = 1
rounded = 0
whitespace = "wrap"
fillColor = "none"
strokeColor = "#101010"
dashed = "1"
align = "left"
verticalAlign = "top"
}
$DrawIODefaultStyle_Vnet = New-DrawIOStyle | Update-DrawIOStyle @DrawIODefaultStyle_Vnet_Details
$DrawIODefaultStyle_Vnet_Icon_Details = @{
fontSize = "12"
image ="img/lib/azure2/networking/Virtual_Networks.svg"
align = "right"
aspect = "fixed"
}
$DrawIODefaultStyle_Vnet_Icon = New-DrawIOStyle | Update-DrawIOStyle @DrawIODefaultStyle_Vnet_Icon_Details
function Create-DrawIOVirtualNetwork {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[string]$NetworkAddress,
[string]$cellId,
[int]$x,
[int]$y,
[int]$width = 400,
[int]$height = 100
)
if (-not $cellId) {
$cellId = Get-DrawIORandomID
}
$cell = [MXCell]::New(("{0}-box" -f $cellId))
$cell.Value = "<b>{0}</b>&nbsp;{1}<br/>[Virtual Network]" -f $Name, $NetworkAddress
$cell.Vertex = "1"
$cell.Style = $DrawIODefaultStyle_Vnet | Format-DrawIOStyle
$cell.Geometry = [MXGeometry]::New($x, $y, $width, $height)
$icon = [MXCell]::New(("{0}-icon" -f $cellId))
$icon.Value = ""
$icon.Vertex = "1"
$icon.ParentId = $cell.Id
$icon.Style = $DrawIODefaultStyle_Vnet_Icon | Format-DrawIOStyle
$icon.Geometry = [MXGeometry]::New($width-32, 0, 32, 32)
@(
$cell
$icon
)
}
$s = New-DrawIOStyle | Update-DrawIOStyle -html 1 -rounded 0 -whitespace Wrap -fillColor "#765454"
$id = Get-DrawIORandomID
$cell = [MXCell]::New(("{0}-1" -f $id),'1')
$cell.Value = "abc"
$cell.Vertex = "1"
$cell.Style = $s | Format-DrawIOStyle
$cell.Geometry = [MXGeometry]::New(50,50,400,100)
$n1 = Create-DrawIOVirtualNetwork -Name "myvnet1" -NetworkAddress "10.28.0.0/16" -X 100 -Y 300
$n2 = Create-DrawIOVirtualNetwork -Name "myvnet2" -NetworkAddress "10.42.0.0/16" -X 100 -Y 500
@(
$cell
$n1
$n2
) | Write-DrawIODocument #-Path "/tmp/t1.drawio"
# styles
#
# html=1;whitespace=wrap;rounded=0
# fillColor=none oder #rrggbb
# strokeColor
# dashed=1;dashPattern=8 4;
# align=left;verticalAlign=top
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment