Microsoft Visio 2007 Professional and 2010 Professional and Premium includes the Network / Rack Diagram template, which opens the Rack-mounted equipment stencil. This stencil includes the Patch Panel master which can be stretched from 2U to 25U high. However, I have often required a 1U patch Panel, so I decided to look into the Microsoft shape to see if it could be modified to allow it to be reduced to 1U without looking ridiculous.
VBA
Displaying the RGB values of Shapes
Everyone knows that you can change the colour of Visio shapes. Most people know that you can also change the fill pattern too.
Automating Area and Perimeter Length Shape Data
[UPDATE: Microsoft introduced a new function into Visio 2010 called PATHLENGTH(…), so some of the following is now not required. See Automatic Line and Segment Lengths in Visio )
Another newsgroup question has asked about automating the update of shape area and perimeter lengths. Now, this is an area (no pun intended) that I am most interested in because I used to do a lot of space planning. Visio Professional does include Space and Boundary shapes on the Resources stencil that use an add-in to update the area, but does nothing about the perimeter length. These shapes do highlight some of the issues to be considered though…
- Visio pages can be scaled, and indeed the various floor plan and site layout templates in Visio are pre-scaled. Most templates are not scaled, and therefore default to 1:1 scale.
- Visio measures everything internally in inches, although you can display in almost whatever units you choose.
- The Visio Application object has a handy ConvertResult (StringOrNumber, UnitsIn, UnitsOut) method, which can be used for linear and area measurements … and can also be used for date and times.
- Visio can store decimal numbers to a very high degree of precision (I counted 14 decimal places), but you almost always want to format the display.
- Visio can call a method in a VBA project when the values in specified cells are changed.
- It is easy to display Shape Data in a shape, either by using Data Graphics or by Insert Field
[Read more…] about Automating Area and Perimeter Length Shape Data
Listing Connections in Visio 2010
One of the best bits of Visio is the connections between shapes, but it has always been difficult to understand these connections in code. There are connections to and from shapes, which require you to understand where you are and which way to look … at the beginning or the end of a 1D connector. Visio 2010 has added some useful extra methods to the shape object which makes understanding connections much easier. This post will show you how you can use the new GluedShapes() and ConnectedShapes() methods.
Take, for example, a network diagram where there are connections between servers and routers:
You may wish to list the connections at the start and end of each cable. Previously, this would have meant inspecting the connected cell to see if was at the beginning or end of the line, but now you can use the GluedShapes() method of a shape to retrieve an array of the 2D shapes connected at one end or another with the relevant arguments, visGluedShapesIncoming2D or visGluedShapesOutgoing2D. The ListGluedConnections macro below displays the following in the immediate window:
Connector Dynamic connector
> Router.45 router-02
< Server server-01 Connector Dynamic connector.107 > Router.45 router-02
< Server.30 server-02 Connector Dynamic connector.108 > Router router-01
< Server.75 server-03 Connector Dynamic connector.109 > Router.91 router-03
< Server.30 server-02 Connector Dynamic connector.110 > Router.91 router-03
< Server server-01
Public Sub ListGluedConnections() Dim shp As Visio.Shape Dim connectorShape As Visio.Shape Dim sourceShape As Visio.Shape Dim targetShape As Visio.Shape Dim aryTargetIDs() As Long Dim arySourceIDs() As Long Dim targetID As Long Dim sourceID As Long Dim i As Integer For Each shp In Visio.ActivePage.Shapes If shp.OneD Then Debug.Print "Connector", shp.Name arySourceIDs = shp.GluedShapes(visGluedShapesIncoming2D, "") For i = 0 To UBound(arySourceIDs) Set sourceShape = Visio.ActivePage.Shapes.ItemFromID(arySourceIDs(i)) If sourceShape.CellExists("Prop.NetworkName", Visio.visExistsAnywhere) Then Debug.Print , ">", sourceShape.Name, sourceShape.Cells("Prop.NetworkName").ResultStr("") End If Next aryTargetIDs = shp.GluedShapes(visGluedShapesOutgoing2D, "") For i = 0 To UBound(aryTargetIDs) Set targetShape = Visio.ActivePage.Shapes.ItemFromID(aryTargetIDs(i)) If targetShape.CellExists("Prop.NetworkName", Visio.visExistsAnywhere) Then Debug.Print , "<", targetShape.Name, targetShape.Cells("Prop.NetworkName").ResultStr("") End If Next End If Next End Sub
Similarly, you may want to simply list the next connected 2D shape, effectively ignoring the cable. In this case, you can use the new ConnectedShapes() method, with the relevant arguments visGluedShapesIncoming2D or visGluedShapesOutgoing2D, to produce an output like the following ( using the ListNextConnections macro below):
Shape Server server-01
> Router.45 router-02
> Router.91 router-03
Shape Router router-01
< Server.75 server-03 Shape Server.30 server-02 > Router.45 router-02
> Router.91 router-03
Shape Router.45 router-02
< Server server-01
< Server.30 server-02 Shape Server.75 server-03 > Router router-01
Shape Router.91 router-03
< Server server-01
< Server.30 server-02
Public Sub ListNextConnections() Dim shp As Visio.Shape Dim connectorShape As Visio.Shape Dim sourceShape As Visio.Shape Dim targetShape As Visio.Shape Dim aryTargetIDs() As Long Dim arySourceIDs() As Long Dim targetID As Long Dim sourceID As Long Dim i As Integer For Each shp In Visio.ActivePage.Shapes If Not shp.OneD Then If shp.CellExists("Prop.NetworkName", Visio.visExistsAnywhere) Then Debug.Print "Shape", shp.Name, shp.Cells("Prop.NetworkName").ResultStr("") arySourceIDs = shp.ConnectedShapes(visConnectedShapesOutgoingNodes, "") For i = 0 To UBound(arySourceIDs) Set sourceShape = Visio.ActivePage.Shapes.ItemFromID(arySourceIDs(i)) If sourceShape.CellExists("Prop.NetworkName", Visio.visExistsAnywhere) Then Debug.Print , "<", sourceShape.Name, sourceShape.Cells("Prop.NetworkName").ResultStr("") End If Next aryTargetIDs = shp.ConnectedShapes(visConnectedShapesIncomingNodes, "") For i = 0 To UBound(aryTargetIDs) Set targetShape = Visio.ActivePage.Shapes.ItemFromID(aryTargetIDs(i)) If targetShape.CellExists("Prop.NetworkName", Visio.visExistsAnywhere) Then Debug.Print , ">", targetShape.Name, targetShape.Cells("Prop.NetworkName").ResultStr("") End If Next End If End If Next End Sub
I think this makes interrogation of connected diagrams, of all flavours, much simpler, though I would love to have similar ShapeSheet functions too!
Visio 2010 : Containment and Cross-Functional Flowcharts
One of the templates to get a revision in Visio 2010 is the Cross Functional Flowchart template because of the new list and containment functionality that has been added into the core application. I had to write a small bit of code in earlier versions of Visio for each flowchart shape to automatically understand which swimlane and phase it belongs to, but now there are ShapeSheet functions available, so a slight modification of a flowchart master enables it to inherit values from the swimlane that it is in. This article demonstrates who you can do this to, for example, synchronize the fill color of each flowchart shape to that of the swimlane that it belongs to.
[Read more…] about Visio 2010 : Containment and Cross-Functional Flowcharts
Copying Data from one Shape to Another
A recent newsgroup question asked for example code to demonstrate how shape data can be copied from one shape to another via a connector between the two. This is something that others might want to do also, and not just by connecting shapes, but also by selection since you might change your mind about which shape to use, but you have already entered a lot of information on the original shape. A good example of this might be when diagramming a network and needing to change between one type of server and another. So, in this blog, I will demonstrate how shape data can be transferred by connection or by selection, and how to limit the transfer to rows that match by name or by label.
Note that Shape Data is the new name for Custom Properties in Visio 2007.
Firstly, I should explain why it may be necessary to match by name or by label: In the following screenshot, you can see the Shape Data window, Define Shape Data dialog, and the ShapeSheet for a Server shape. I have ticked “Run in developer mode” in Tools / Options / Advanced, otherwise I would not be able to see the Name, Sort key, Ask on drop or Hidden in the dialog.