user3246693
user3246693

Reputation: 793

Powershell + Visio Creating a Crow's nest diagram?

I am trying to pro grammatically generate a Crow's foot database diagram in Visio 2013 using powershell.

I'm getting hung up on how I add attributes to the entity shape after adding the entity shape to the drawing.

This is what I've come up with so far. It opens Visio and adds an entity to the drawing and then names the entity. This all works.

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 Content\1033\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"

The problem is the entity has 0 attributes added to it. I've tried something very rudimentary, such as this:

$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
}

Which technically works. The attributes are added and bound to the entity, but the entity doesn't re-size to encapsulate all the attributes. I can't resize the entity as it's protected, and when I manually add an attribute to the entity via the Visio GUI, the entity automatically resizes when I drag/drop the attribute onto it. This leads me to believe that .Drop() is not the appropriate method for adding the attributes to the entity, but I can't figure out what the correct method for doing this is.

How do you pro grammatically add attributes to an entity in Visio?

Note: "Attribute" and "Entity" are referring to the Visio shapes contained within the "Crow's foot database notation" stencil.

Here's the final code that is working for reference for anyone else looking at this:

$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 Content\1033\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)
}

Upvotes: 2

Views: 712

Answers (1)

Mike Shepard
Mike Shepard

Reputation: 18166

I haven't messed with entities and attributes, but according to the macro (which is the best way to figure out how to do stuff programmatically in Visio), this seems to be the approach:

$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
}

It worked fine for me, but the attributes were numbered from the bottom to the top. That should be easy to fix, I think.

Upvotes: 1

Related Questions