Powershell and Visio: Move objects and add text

Lets take our look a Visio a bit further and move objects around and change the text labels

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
034
035
036
037
038
039
040
041
## 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"))

## move objects
$shape2.SetCenter(4,9)
$shape1.SetCenter(4,4)

## Add text
$shape1.Text = "File Server"
$shape2.Text = "My Workstation"

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

 

To move an object we can use the SetCenter method.  The numbers are X & Y co-ordinates.  The origin is at the bottom left corner (0,0).  Depending on what units and paper size you are using – using A4 I get 8,11 as an approximation to the top right corner so I’m guessing the co-ordinates represent inches. Check on your implementation.

The text is simply changed by setting the Text property as shown.

Now we have the basics of working with Visio – its time to think what we can do with it.

Published Fri, Jun 11 2010 19:43 by RichardSiddaway

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: