Writing Code to Extract Data from a Visio Drawing

3 4

The shapes in Microsoft Visio drawings can contain a rich variety of information. Using Automation, you can write programs that extract information from a Visio drawing to use in another application. For example, you might want to use the shapes in a drawing to automatically generate sales orders. Or, you might extract information from the shapes in a flowchart to a spreadsheet to estimate the costs associated with a particular manufacturing process. You can also extract information about data types used in a solution, and protect data in a drawing from unplanned changes by saving a copy of the data in an external file.

No matter how you plan to use the data you extract from a drawing, the basic process for gathering it is the same. The code introduced in this sample outlines gathering data from a Visio drawing; what you do with the information you gather is dependent upon how your program interacts with other applications.

Extracting Data from a Drawing: an Example

Suppose you want to write an application that gathers information about the network components in a drawing to generate sales orders. A salesperson and a client might collaborate on a drawing that represents a new network system, as in the following illustration.

figure 20-1. a visio drawing with shape information that can be extracted.

Figure 20-1 A Visio drawing with shape information that can be extracted.

You can write an Automation program that runs when the drawing is saved and that collects information about the shapes in the drawing and places it into an array. You could then write additional code that uses the data in the array to automatically generate an order for the specified components.

figure 20-2. data about the shapes on the drawing page.

Figure 20-2 Data about the shapes on the drawing page.

Using Microsoft Visual Basic for Applications (VBA), you could write code much like the following example that executes when the Visio drawing is saved. After defining variables and getting the Shapes collection associated with the first page in the drawing, this code defines the OrderInfo array to hold the data that will be gathered from the shapes in the collection. Each shape's name is identified and added to the array, along with information gathered about three custom properties for each shape: the part number, manufacturer, and cost. After this information is gathered for each shape on the page, the results are displayed in the Immediate window for verification. Finally, the data in the array is passed to another application or saved to disk as a text file.

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

 Private Sub Document_DocumentSaved(ByVal doc As IVDocument)       'Visio Page object       Dim pagObj As Visio.Page       'Visio Shapes collection       Dim shpsObj As Visio.Shapes       'Visio Shape object       Dim shpObj As Visio.Shape       'Visio Cell object       Dim celObj As Visio.Cell       'Array to hold purchase order info       Dim OrderInfo() As String       'Counter       Dim iShapeCount As Integer       'Counter       Dim i As Integer       'Get the active page.       Set pagObj = ActivePage       'Get the Shapes collection of the page.       Set shpsObj = pagObj.Shapes     'Total number of shapes.     iShapeCount = shpsObj.Count     'Set the array size to hold all of the shape information.     '0-based array, 4 by total number of shapes.     ReDim OrderInfo(3, iShapeCount - 1)            'For each shape on the page, collect the Name, Part Number,     'Manufacturer, and Cost.     For i = 1 To iShapeCount         'Get the i'th shape.         Set shpObj = shpsObj(i)         'Get the shape name.         OrderInfo(0, i - 1) = shpObj.Name         'Get the Part Number property, then get the value _         'as a string.         If shpObj.CellExists("Prop.Part_Number", _                 visExistsLocally)Then             Set celObj = shpObj.Cells("Prop.Part_Number")             OrderInfo(1, i - 1) = celObj.ResultStr("")         End If         'Get the Manufacturer property, then get the value _         'as a string.         If shpObj.CellExists("Prop.Manufacturer", _                  visExistsLocally) Then             Set celObj = shpObj.Cells("Prop.Manufacturer")             OrderInfo(2, i - 1) = celObj.ResultStr("")         End If         'Get the Cost property, then get the value.         If shpObj.CellExists("Prop.Cost", visExistsLocally) Then             Set celObj = shpObj.Cells("Prop.Cost")             OrderInfo(3, i - 1) = celObj.ResultIU         End If         'Release Shape object.         Set shpObj = Nothing     Next            'Print to Immediate window to verify data collection.     For i = 0 To pagObj.Shapes.Count - 1         Debug.Print OrderInfo(0, i) & "," _             & OrderInfo(1, i) & "," _             & OrderInfo(2, i) & "," _             & OrderInfo(3, i)     Next            'Call a function to write data out to any external     'data storage, and pass the array of collected data to     'ExportData OrderInfo. End Sub 

Examining the Code for Extracting Data from a Drawing

The previous code sample can be broken into several distinct parts:

  • Defining the variables and array that will contain the data.
  • Looping through the shapes in the page's Shapes collection to gather data and place it into an array.
  • Viewing the resulting array on the screen to verify data collection.
  • Exporting the data to another application.

Defining variables

In the first section of the code, the Dim statement is used to define variables for the Page object, a Shapes collection, a Shape object, and a Cell object. In addition, an array called OrderInfo is defined, along with an integer for counting through the loop that follows.

Once the variables are defined, the page variable is set to the document's active page. The shpsObj variable defines a collection made up of all shapes on the active page, and iShapeCount tallies the number of shapes for which information will be gathered. Finally, the OrderInfo array defines an array to hold data about four properties for each shape contained in the Shapes collection for the page. If you were working in a document containing several pages, you could define a loop to increment through each page in the document. In that case, you could either expand the array to contain multiple Shapes collections, or create a different array for each page.

Using a loop to gather information about shapes

The next section of the code populates the OrderInfo array with information about all of the shapes on the page. The iShapeCount variable stores the number of shapes on the page; the counting integer is defined relative to this range. Using a For.Next statement, the integer starts with the first shape in the collection, gathers information about it, releases the shape, and then gathers information about the next shape, continuing until it reaches the last shape in the collection.

For each shape in the collection, this code adds four pieces of information to the array. The shpObj.Name property simply collects the name of the shape. The next three fields are drawn from custom property fields. The code first checks to see if the Prop.Part_Number, Prop.Manufacturer, and Prop.Cost cells exist; if they do, the celObj variable is assigned to each cell value in turn, and the resulting strings are added to the array. In this code, if a cell does not exist, the field is simply skipped. In a more complete solution, you might want the code to respond differently if the cells do not exist.

Verifying data collection

Using another For.Next statement, the next section of the code simply displays the information in the array for each shape in the Immediate window, separated by commas. Depending on the way you plan to use the information in the array, this can be a useful troubleshooting technique.

Exporting data to another application

This code sample doesn't actually include any code for exporting the data in the array to another application, as the approach will vary considerably depending on how the information will be used and which application you plan to work with.



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