Writing Code to Create a Visio Drawing from Data

3 4

Just as you can extract data from a Microsoft Visio drawing to use in another application, you can use data from other sources to create a diagram. For example, you might use information from a sales order for network components to generate an installation diagram, or you might extract data from an employee database to generate an organization chart.

However you plan to create the drawing, you need to make some important design decisions before you start coding:

  • Determine whether your code creates a new drawing or assumes the user has already created one.
  • Determine how the data source is selected: Is the data always drawn from the same location, or should it be selectable by a user?
  • Decide how to choose which stencil contains the masters you plan to use in your drawing and how they map to the data you plan to use; you can design shapes for a new stencil as part of your solution.

The code sample in this section shows how you might create a simple diagram from the contents of a database.

Creating a Drawing from Data: an Example

Suppose you want to write an application that generates a network installation drawing from data imported from a sales order. When the sale is complete and the order is entered into a database (like the following illustration), an Automation program can extract records from the database and use them to create a drawing for an installation crew to use.

figure 20-3. records in a database.

Figure 20-3 Records in a database.

In the example, the code associates a field in the database record with a shape on a particular stencil, and then drops instances of the shapes on a drawing page and adds information from other fields as custom properties of the shapes. The result is a drawing that configures the new items in a customer's network based on data entered by the salesperson.

figure 20-4. an installation diagram generated from database records.

Figure 20-4 An installation diagram generated from database records.

Using Microsoft Visual Basic for Applications (VBA), you could write code much like the following example that creates a new drawing from a template that includes the stencil you want to use, and then populates it with shapes based on data read from the database. In this example, a hub shape is placed at the center of the page, and then nodes are connected to the hub in a circle. Each shape displays text, which in this example might include notes made by the salesperson about how the client wants a component to be configured.

For a detailed explanation of this code, see Examining the Code for Creating a Drawing from Data later in this section.

 Public Sub CreateDrawing()              Dim shpObjHUB As Visio.Shape       Dim shpObjNodes As Visio.Shape       Dim shpObjConnector As Visio.Shape       Dim mstObjConnector As Visio.Master       Dim mstObj As Visio.Master       Dim stnObj As Visio.Document       Dim dX, dY As Double       Dim dDegreeInc As Double       Dim dRad As Double       Dim dPageWidth, dPageHeight As Double       Dim i As Integer              Const PI = 3.1415       Const CircleRadius = 2              Dim arrNetData() As String              'Read data.       InitData arrNetData              'To place shapes in even increments around the circle,       'divide 360 by the total number of items in the array.       dDegreeInc = 360 / UBound(arrNetData)              'Read the PageWidth and PageHeight properties.       dPageWidth = ActivePage.PageSheet.Cells("PageWidth").ResultIU       dPageHeight = ActivePage.PageSheet.Cells("PageHeight").ResultIU              'Open the Basic Network Shapes 3D Stencil. _        Set stnObj = Application.Documents.OpenEx _             ("Basic Network Shapes 3D.vss", visOpenDocked)              'Process the hub shape.       Set mstObj = stnObj.Masters(arrNetData(0, 0))       Set shpObjHUB = ActivePage.Drop(mstObj, dPageWidth / 2, _             dPageHeight / 2)              'Set the text of the hub shape.       shpObjHUB.Text = arrNetData(0, 1)              'Get the Connector master.       Set mstObjConnector = stnObj.Masters("Bottom to Top Angled")              'Process the nodes.       For i = 1 To UBound(arrNetData)             Set mstObj = stnObj.Masters(arrNetData(i, 0))             'Determine X, Y location for placement (in circle _             'around hub).             dRad = (dDegreeInc * i) * PI / 180             dX = CircleRadius * Cos(dRad) + (dPageWidth / 2)             dY = CircleRadius * Sin(dRad) + (dPageHeight / 2)             'Add shape to drawing in proper location.             Set shpObj = ActivePage.Drop(mstObj, dX, dY)             'Set shape text.             shpObj.Text = arrNetData(i, 1)                    'Connect the current node to the hub.             Set shpObjConnector = ActivePage.Drop _                   (mstObjConnector, 0, 0)             shpObjConnector.SendToBack             'Glue the begin point to the hub shape.             shpObjConnector.Cells("BeginX"). _                   GlueTo shpObjHUB.Cells("Connections.X1")             'Glue the end point to the node that was just added.             shpObjConnector.Cells("EndX"). _                   GlueTo shpObj.Cells("Connections.X1")       Next End Sub 

Examining the Code for Creating a Drawing from Data

The code sample in the previous section (Creating a Drawing from Data: an Example) can be broken into three distinct parts:

  • Setting up the program
  • Adding the hub shape to the page
  • Adding the node shapes to the page and connecting them to the hub

Setting up the program

The first section of code defines the constants and variables the program will use. Next, the array containing the data that the drawing will be created from is defined and initialized. Finally, the page dimensions are determined (this information will be used later as the basis for centering the hub shape), and the stencil used by this drawing is opened. In a more sophisticated solution, you might design the code in such a way as to allow the user to choose which stencil to open. Alternatively, stencil information might be imported as part of the data, rather than hard-coded as it is here.

Adding the hub shape to the page

In this program, nodes are connected to a hub, which is centered on the document page. The next section of the code reads the imported array to identify which master to use for the hub shape, drops an instance of that master at the center of the page, and sets the text for the shape. Next, the connector shape is identified, though instances of it are not dropped on the page until the node shapes are added to the drawing.

Adding node shapes to the page and connecting them to the hub

The final part of this program contains a loop that matches a field in each record in the imported array with a master on the open stencil. Once the master has been identified, the placement of the shape instance is determined; shapes are added to the page in a circle around the hub. After the shape is dropped at the correct location, the connector shape identified above is added to the page. Its begin point is connected to the hub shape, and its end point to the new node shape. This process is repeated for each record included in the array, and results in the finished drawing.



Developing Microsoft Visio Solutions 2001
Developing Microsoft Visio Solutions (Pro-Documentation)
ISBN: 0735613532
EAN: 2147483647
Year: 2004
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net