Categories

Powershell and Visio: connectors

Last time we looked at creating a Visio drawing and adding a couple of objects.  This time we will build on that and resize the objects and add a connector.

The Resize method on a shape is ONLY AVAIALBLE ON VISIO 2010

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
## create visio document
$visio = New-Object -ComObject Visio.Application
$docs = $visio.Documents

## use basic template
$doc = $docs.Add("Basic Diagram.vst")

## set active page
$pages = $visio.ActiveDocument.Pages
$page = $pages.Item(1)

## Add a stencil
$mysten = "C:\Program Files\Microsoft Office\Office14\Visio Content\1033\EntApp_M.vss"
$stencil = $visio.Documents.Add($mysten)

## Add objects
$server = $stencil.Masters.Item("Server")
$workstn = $stencil.Masters.Item("Workstation")

$shape1 = $page.Drop($server, 2, 2)
$shape2 = $page.Drop($workstn, 5, 5)

## Resize Objects
$shape1.Resize(1, 5, 70)
$shape2.Resize(1, 5, 70)

## Connect Objects
$connect = $page.Drop($page.Application.ConnectorToolDataObject,0,0)
$start = $connect.CellsU("BeginX").GlueTo($shape1.CellsU("PinX"))
$end = $connect.CellsU("EndX").GlueTo($shape2.CellsU("PinX"))

$doc.SaveAs("c:\scripts\visio\draw1.vsd")
$visio.Quit()

 

As seems to be the case with the other Office applications once you have worked out how to create the document the rest is relatively straightforward.

This is the same code as last time except I use the Resize method to change the size – the parameters denote the top right hand corner (values 0-7 with 0 being middle top and proceeding clockwise), the amount to drag the corner (5) and the units – in this case millimeters

The connection is made using the connectortooldataobject and defining the start and end objects.

 

Thanks to Alan Renouf for his powershell script to document  VMware environments into Visio which forms the basis of these scripts so far.

Leave a Reply