M.S. Visio Automation

Introduction

A Microsoft Visio solution combines shapes and automation to solve various business and technical problems, as well as improve productivity. The off-the-shelf Visio product contains numerous drawing templates that meet the basic needs of most users for creating organizational charts, space plans, flow charts, network diagrams, and more.

One of the main strengths of Visio products is that the platform enables developers to create customized Visio environments. This article demonstrates the flexibility of Visio products as a development platform by describing the following customized business solutions:

Using the native functionality in Visio as a launch pad for a custom solution, you can develop your application in less time-and with potentially more feature-than if you started from scratch with a traditional programming language. The Visio environment includes the intelligent and powerful functionality of Smart Shapes symbols, the Shape Sheet, and templates. You can customize menu commands and toolbars, publish data to the Web, print properly formatted drawings, and integrate your solution with other applications. You can integrate customized Visio solutions with the following applications: Microsoft Word, Microsoft Excel, and ODBC (Open Database Connectivity) or LDAP (Lightweight Directory Access Protocol) databases like Microsoft Access, Microsoft SQL Server, Oracle's database server, and Microsoft Active Directory. You can develop applications using any programming language that supports Automation such as Microsoft Visual Basic or C/C++. Visio solutions can also integrate with custom programs such as engineering analysis tools and rules-based product configuration applications.

Customizing Visio

For the purpose of this article, the term Automation refers to the extension of Visio functionality through custom development and integration. Through Automation, you can add new forms to Visio, connect externally to a database, create Smart Shapes symbols enforcing rules and bounds on drawings, and integrate with other applications. You can use the Visio graphics engine, the core of Visio, for your own program's graphics, custom user interfaces, data management, and file management environments. The graphics engine also exposes Visio objects (for example, shapes, masters, menus, and toolbars), properties, methods, and events. Figure 1 shows the Visio development environment.

visio.gif
 
Figure 1. A diagram of the Visio platform and its components

As a developer, you need to determine how much native Visio functionality can be used as the basis for your custom solution and how much new functionality you must develop. Typically, you'll get superior results in less time by leveraging the built-in functionality in Visio. If your solution is complex, however, you should consider developing a custom interface using Visual Basic (.DLL), VBA, or C++ (.VSL), to better control the look, feel, and functionality of the application and shapes. Also custom interface can be developed on .NET platform for Visio 2003.

Modifying Visio Native Functionality

This section briefly describes some of the functionality native to Visio, such as Smart Shapes symbols, the Shape Sheet  window, templates, custom properties, and database connectivity that you modify or build on to create your own customized solutions. There are many other Visio features in addition to those described within this article that you can use to enhance your application. For more information on Visio development capabilities, see the Recommended Reading section.

SmartShapes

Shapes are drawing components used to construct or assemble a diagram. The end user simply drags the shape to a drawing page. A shape can represent a real-world object, or an easily recognized symbol, so that a user quickly understands what object to select and how an object should be used. Visio Smart Shapes symbols, generally referred to as shapes, may contain programmed intelligence that enables them to behave on a drawing page as you expect the real-world object to behave. For example, a shape representing a computer system or a piece of office furniture can be built to represent standard industry dimensions, and they can be locked against resizing so that a user cannot inadvertently change the shape's dimensions.

Use the following methods to customize a shape and its behavior, defining how the shape appears on the screen, how the shape responds when you select it, and how its text behaves:

  • Define Custom Properties, available from the shape's shortcut menu. 
  • Modify formulas in a shape's Shape Sheet spread sheet. 
  • Add a command to a shape's or drawing page's shortcut menu. 
  • Associate an event with a shape. For example, you can define an action to occur when the user double-clicks the shape. 
  • Write a program that uses Automation to control your shapes. For example, create a Visio drawing automatically or populate a form that is displayed to the user from data in an ODBC compliant database.

Consider the following tips when you create customized Smart Shapes symbols:

  • Depending on the complexity of your application, shape design and creation can be a significant part of your development effort. You can build a more flexible solution that requires less coding and maintenance in the long run if you carefully design your shapes. Put as much functionality as possible into your shapes but do not make them unnecessarily complex or layer them too deeply. Decide how the shape is to be used in the application and design as much intelligence into the shape as possible. However, very complex shapes can take longer to display when dragged onto the drawing page.

  • Avoid adding behaviors to a shape that are inconsistent with the shape's expected role because doing so may confuse your user. For example, in a manufacturing scenario, some equipment may only be installed in a vertical orientation, and enabling the user to rotate that shape may result in drawings that don't make sense. In some cases, a simpler shape is better, and may actually perform better in Visio than a more complicated shape.

  • Ensure that the shape is correctly sized and oriented with regard to other shapes on the drawing page. Make sure the shape connects correctly to other shapes and that you prevent shapes that have no relationship from being inadvertently connected by the user. Consider whether one shape can fit inside another shape on the drawing page, and if so, establish rules restricting the user from resizing the smaller shape. 
  • Ask the user how the shape will be used in the application. Will the shape portray an asset (a patrol car, for example)? Do you want informational shapes, and if so, can the information be represented in a Text box instead? How many different ways will the shape be used within the product?

  • You can create new shapes in several ways. You can draw the new shape freehand using the Visio drawing tools, or you can import graphics from other programs, convert metafiles into shapes, or scan images to use as shapes. You can also use Shape Operations to create more advanced shapes not easily drawn with the arc and line drawing tools found on the toolbar in Visio products. Shape Operations lets you merge together multiple simple shapes, like squares and triangles, into a single more complex shape using commands like Combine, Intersect, Join, Trim and Subtract.

Shape Sheets and Custom Properties

Everything in Visio-every document, page, style, shape, group, master, and program object-has an associated Shape Sheet spreadsheet where information about the object is stored. The information in this spreadsheet defines the object's unique behavior, characteristics, and capabilities such as geometry, orientation, text, height, width, angle, color, and more. You can enter mathematical formulas into Shape Sheet cells to control the way an object behaves in certain situations. Shape Sheet cells contain formulas or values that are recalculated dynamically as the user manipulates the object. For example, when you draw a shape on a drawing page, the geometry of the shape is stored within the shape's Shape Sheet as a collection of horizontal and vertical locations (called vertices) connected by lines. If you move or resize the shape, the vertex information is updated in the Shape Sheet.

As a shape developer, you have precise control over the appearance and behavior of the shapes you create. You can change a shape's default behavior and enhance the behavior by editing the shape's Shape Sheet either manually from within the Shape Sheet   window or programmatically from within your application. You can also develop forms that enable a user to manipulate Shape Sheet information through an interface that they can understand. The Custom Properties form is an example of a native Visio form that enables editing of the Custom Properties cells in the Shape Sheet, without the need to view the Shape Sheet itself.

Tip  To add the Show Shape Sheet command to an object's right-click shortcut menu, click Options on the Tools menu. In the Options dialog box, click the Advanced tab, and then select Run in Developer Mode. Click OK.

All Shape Sheet cells have a unique name that you can reference within other Shape Sheet cells or your Automation code. This eliminates the need to programmatically navigate through an entire Shape Sheet section searching for a particular reference. You can also create user-defined cells within a Shape Sheet, creating a working area where you can enter formulas for reference by other cells or add-ons.

You may add custom properties to an individual shape, a master, or a page. The custom property data is stored in the object's Shape Sheet and you can add or edit these Custom Properties with the Define Custom Properties dialog box or programmatically through the Shape Sheet. Custom property data is used to supplement the graphical data in your drawing, to generate reports, and to associate data with an external data source, like a database or .XML file.

Templates, Stencils, and Masters

In many (but not all) custom Visio applications, you typically begin a drawing by opening a template. Templates provide a designated set of predefined Visio settings and stencils, and may also define a custom user interface with modified menus and toolbars. Templates are particularly effective when you are creating multiple drawing files that need a consistent look, to streamline a drawing process, or when creating a custom solution that will be distributed to users. Templates contain pointers to associated stencils that hold your master shapes. When a user opens a template, the stencils linked to that template also open.

Stencils are predefined containers holding master shapes. A master shape, commonly called a master, is a shape that you use repeatedly to create or modify drawings by dragging the master from the stencil onto a drawing page. The shape on the drawing page is an instance of the master. The advantage of masters is that they enable complex, predefined shapes to be used repeatedly without redrawing the shape each time it is needed. You can create customized stencils and add custom masters to the stencil by dragging the shape on to an open stencil. For example, you can manipulate the stencils and masters through your application programming, dragging shapes from a stencil to a drawing page, or retrieving the names of all docked stencils opened within a template.

Tailoring the User Interface: Menus, Toolbars, and Accelerators

You can tailor the Visio user interface by adding menu items and toolbar buttons that are needed for a special application, or by removing existing items and buttons that you don't want to be available while your application is running. To add or remove existing menu items and toolbar buttons, use the Toolbars Customize command available from the View menu. Or, add custom menu items, tools and accelerators (shortcut keys) to your solution by writing code that accesses the UIObject objects or by creating a custom user-interface file, a .VSU file, which contains the menus and tools you need for your solution. You can also create shortcut menus that contain context-sensitive commands, displayed when a user right-clicks a shape.

Business Scenario

The business scenario described in this section shows how Microsoft Visio enables you to link graphics with data to model real-world conditions to solve diverse business and technical problems.

Generating the Business Process XML File

The problem

A workflow automation frame work requires a well defined XML file for modeling the process. The process contains different activities and relationship between the activities. Simple XML editor to define the process will not sufficient to see the conceptual view of the process. Visual objects are necessary for realisig the process definition.

The solution

The solution was to create a custom Visio application which should generate the XML file along with the Visio document.  Perhaps the biggest benefit of this solution is that it provides meaningful drawings for the Process. Custom shapes, stencil and Templates are created to draw the workflow process. Each activity will have property pages thru which properties can be set. This information gets saved with the shape objects. When user saves the document custom add-in application will generate the required XML file.

Below snap shot shows the customized application.

visio1.gif
 
Enhancements like simulation, process monitoring for the deployed application can be done.

Features of the Application

  • Pictorial notation for the process definition.
  • XML file generation based on the drawing.
  • Simulating the cost and time before implementing the business process.
  • After deploying the process definition monitoring and reporting functionality is also included in the application.

Advantages over building separate application

  • Less development cost.
  • Reuse of drag and drop functionality. 
  • Reducing the training effort for our application as end user knows the Visio functionality.

Conclusion

Simple add-in/add-on application for Microsoft Visio can be used for various types of requirements.  This customization can be done in a less time with fewer resources.

Up Next
    Ebook Download
    View all
    Learn
    View all