Powershell + Visio 创建一个乌鸦巢图?
Powershell + Visio Creating a Crow's nest diagram?
我正在尝试使用 powershell 在 Visio 2013 中以编程方式生成鱼尾纹数据库图表。
我对如何在将实体形状添加到绘图后向实体形状添加属性感到困惑。
这就是我到目前为止的想法。它打开 Visio 并将一个实体添加到绘图中,然后为该实体命名。这一切都有效。
clear-host
$visio = New-Object -ComObject Visio.Application
$docs = $visio.Documents
$doc = $docs.Add("Basic Diagram.vst") # use basic template
$pages = $visio.ActiveDocument.Pages # set active page
$page = $pages.Item(1)
$DBCrowStencil = "C:\Program Files (x86)\Microsoft Office\Office15\Visio Content33\DBCROW_U.vssx" # Add the crow's foot notation stencils.
$stencil = $visio.Documents.Add($DBCrowStencil)
$EntityShape = $stencil.Masters.Item("Entity")
$AttributeShape = $stencil.Masters.Item("Attribute")
$shape1 = $page.Drop($EntityShape, 2, 8)
$shape1.Text = "Table Name"
问题是实体添加了 0 个属性。我已经尝试了一些非常基本的东西,例如:
$x = 1
$y = 0.3
$Increment = 0.5
For ($i=1; $i -lt 5; $i++) {
$shape1.Drop($AttributeShape, $x, $y)
$shape1.Text = ("Col Name " + $i)
$y = $y - $Increment
}
这在技术上可行。属性已添加并绑定到实体,但实体不会调整大小以封装所有属性。我无法调整实体的大小,因为它受到保护,当我通过 Visio GUI 手动向实体添加属性时,当我 drag/drop 将属性添加到实体上时,实体会自动调整大小。这让我相信 .Drop() 不是将属性添加到实体的合适方法,但我不知道这样做的正确方法是什么。
如何在 Visio 中以编程方式向实体添加属性?
注意: "Attribute" 和 "Entity" 指的是 "Crow's foot database notation" 模具中包含的 Visio 形状。
这是最终代码,供其他人参考:
$visio = New-Object -ComObject Visio.Application
$docs = $visio.Documents
$doc = $docs.Add("Basic Diagram.vst") # use basic template
$pages = $visio.ActiveDocument.Pages # set active page
$page = $pages.Item(1)
$DBCrowStencil = "C:\Program Files (x86)\Microsoft Office\Office15\Visio Content33\DBCROW_U.vssx" # Add the crow's foot notation stencils.
$stencil = $visio.Documents.Add($DBCrowStencil)
$EntityShape = $stencil.Masters.Item("Entity")
$AttributeShape = $stencil.Masters.Item("Attribute")
$shape1 = $page.Drop($EntityShape, 2, 8)
$shape1.Text = "Table Name"
For ($i=1; $i -lt 5; $i++) {
$attr = $page.DropIntoList($AttributeShape, $shape1, $i)
$attr.Text = ("Col Name " + $i)
}
我没有弄乱实体和属性,但根据宏(这是了解如何在 Visio 中以编程方式执行操作的最佳方式),这似乎是方法:
$x = 1
$y = 0.3
$Increment = 0.5
For ($i=1; $i -lt 5; $i++) {
$attr= $page.DropIntoList($AttributeShape,$shape1,1)
$attr.Text = ("Col Name " + $i)
$y = $y - $Increment
}
对我来说效果很好,但属性是从下到上编号的。我想这应该很容易修复。
我正在尝试使用 powershell 在 Visio 2013 中以编程方式生成鱼尾纹数据库图表。
我对如何在将实体形状添加到绘图后向实体形状添加属性感到困惑。
这就是我到目前为止的想法。它打开 Visio 并将一个实体添加到绘图中,然后为该实体命名。这一切都有效。
clear-host
$visio = New-Object -ComObject Visio.Application
$docs = $visio.Documents
$doc = $docs.Add("Basic Diagram.vst") # use basic template
$pages = $visio.ActiveDocument.Pages # set active page
$page = $pages.Item(1)
$DBCrowStencil = "C:\Program Files (x86)\Microsoft Office\Office15\Visio Content33\DBCROW_U.vssx" # Add the crow's foot notation stencils.
$stencil = $visio.Documents.Add($DBCrowStencil)
$EntityShape = $stencil.Masters.Item("Entity")
$AttributeShape = $stencil.Masters.Item("Attribute")
$shape1 = $page.Drop($EntityShape, 2, 8)
$shape1.Text = "Table Name"
问题是实体添加了 0 个属性。我已经尝试了一些非常基本的东西,例如:
$x = 1
$y = 0.3
$Increment = 0.5
For ($i=1; $i -lt 5; $i++) {
$shape1.Drop($AttributeShape, $x, $y)
$shape1.Text = ("Col Name " + $i)
$y = $y - $Increment
}
这在技术上可行。属性已添加并绑定到实体,但实体不会调整大小以封装所有属性。我无法调整实体的大小,因为它受到保护,当我通过 Visio GUI 手动向实体添加属性时,当我 drag/drop 将属性添加到实体上时,实体会自动调整大小。这让我相信 .Drop() 不是将属性添加到实体的合适方法,但我不知道这样做的正确方法是什么。
如何在 Visio 中以编程方式向实体添加属性?
注意: "Attribute" 和 "Entity" 指的是 "Crow's foot database notation" 模具中包含的 Visio 形状。
这是最终代码,供其他人参考:
$visio = New-Object -ComObject Visio.Application
$docs = $visio.Documents
$doc = $docs.Add("Basic Diagram.vst") # use basic template
$pages = $visio.ActiveDocument.Pages # set active page
$page = $pages.Item(1)
$DBCrowStencil = "C:\Program Files (x86)\Microsoft Office\Office15\Visio Content33\DBCROW_U.vssx" # Add the crow's foot notation stencils.
$stencil = $visio.Documents.Add($DBCrowStencil)
$EntityShape = $stencil.Masters.Item("Entity")
$AttributeShape = $stencil.Masters.Item("Attribute")
$shape1 = $page.Drop($EntityShape, 2, 8)
$shape1.Text = "Table Name"
For ($i=1; $i -lt 5; $i++) {
$attr = $page.DropIntoList($AttributeShape, $shape1, $i)
$attr.Text = ("Col Name " + $i)
}
我没有弄乱实体和属性,但根据宏(这是了解如何在 Visio 中以编程方式执行操作的最佳方式),这似乎是方法:
$x = 1
$y = 0.3
$Increment = 0.5
For ($i=1; $i -lt 5; $i++) {
$attr= $page.DropIntoList($AttributeShape,$shape1,1)
$attr.Text = ("Col Name " + $i)
$y = $y - $Increment
}
对我来说效果很好,但属性是从下到上编号的。我想这应该很容易修复。