3 4
The easiest kind of solution to implement is a standardized drawing that uses the content supplied with Microsoft Visio, along with shapes, stencils, and templates that you create. If you want to provide more assistance to users for creating specific types of drawings, you might want to add programming with the solution in the form of Microsoft Visual Basic for Applications (VBA) code, a COM (Component Object Model) add-in, or a Visio add-on. If the drawings to be created follow a strict set of rules, a solution might include an application that uses data from another source to generate drawings that the user can modify. If a solution is to provide more than just drawings, it will involve integration with an external database and possibly with external applications (either off-the-shelf or developed in-house).
One developer can often create a simple Visio solution that consists of custom shapes, a template, and a small amount of code. More elaborate solutions, however, can require a team of developers, each with particular skills. For example, the team could consist of the following members:
Once the team is assembled, the following steps are suggested for the development process:
Ask users what steps they follow to accomplish their tasks. Collect examples of current drawings that your solution will automate or improve. In a large project, consider documenting requirements so that other users and developers can review them and understand what is needed.
Incremental development and integration of Visio capabilities in a solution usually leads to a better result. It allows the user to use the solution sooner, and is a good way to get feedback to improve the solution during development.
The usability of a solution starts with how usable and relevant the shapes are going to be. For example, should you provide the user with many individual shapes, or schedule the time required to create multishapes (shapes that are designed to change appearance based on a setting)? Some users might find a few versatile shapes that can be manipulated into many different configurations more functional and less overwhelming than a stencil with many shapes to choose from; others might prefer a large assortment of single-purpose shapes.
Standardized drawings alone can deliver much of the benefit users are looking for. Beware of over-engineering a solution; just because Visio is programmable doesn't mean a solution must include programming.
The approach to this step will depend on the kind of solution you are developing. For details, see Integrating a Visio Solution with a Database and Choices for Implementing Automation later in this section.
Start developing your solution by building the shapes it will need, and put as much shape functionality as possible in formulas. There are two important reasons to start with shapes:
If the shape behavior you want is predictable and can be accomplished with formulas (for example, automatic sizing or scaling), put it in the shape using the ShapeSheet. If the behavior changes dynamically at run time—for example, the text in a shape or the arrangement of shapes in a drawing may change—handle that behavior in a program such as in VBA code in a document, an ActiveX control, a Visio add-on, or a COM add-in. You can control the appearance and behavior of shapes with great precision by setting shape formulas. If you can create a stencil of masters to accompany your solution, users might not need to draw anything with the Visio drawing tools.
As you build masters for a program, test them in a Visio instance by manually creating the kinds of drawings you intend the program to automate. This will give you a good idea of the code you'll need to write and the data you'll need to provide. It will also show you if your shape is working the way you expect.
Finally, remember that the stencil that contains your masters is an important part of your solution's user interface. As such, you'll want to:
For details about creating masters and stencils, see Chapter 3, Visio Masters, Stencils, Templates, and Documents. For details about distributing stencils in a Visio solution, see Chapter 13, Packaging Stencils and Templates.
A template provides a common workspace for users. It facilitates standardization by making it easy for the user to create standardized drawings by choosing from sets of shapes.
A template can include styles and set up drawing pages using a uniform grid and measurement system. A template can set up drawing pages with shapes already on them and open one or more stencils so a user can add more shapes. A template can also provide drawings with their own user interface by including ActiveX controls, such as command buttons and text boxes, custom controls that perform special tasks, and VBA code that allows a user to interact with the drawing through the controls. Providing a template with a Visio solution can yield the following benefits:
Note
After you develop your solution's masters and template (if any), you can use Automation to implement the rest of your solutions. Exactly what this entails depends on the purpose of your solution and the context in which it will run. However, you'll typically use Automation to do the following:
Most stand-alone programs will need a dialog box or wizard page to advise the user what to do and prompt for any information that the program needs to execute.
Shapes can have custom properties, which can be configured to prompt the user to enter data or shape properties when, for example, a master is dropped on the drawing page. However, to preserve data types and protect data from unplanned changes, you might want your solution to store data in and retrieve it from an external database.
If your solution creates a drawing, it will need to determine which masters to drop and where to drop them, set the shapes' text and apply styles, and connect shapes. If your solution reads drawings or works with existing shapes, it will need to find the shapes, make sure they're appropriate for the program, and get and set shape properties and formulas.
Remember that a shape can have formulas that resize or reorient it appropriately when your program moves or resizes it—just as if you moved or resized the shape yourself, using the mouse in a Visio drawing window. If you find yourself writing a lot of complex code that manipulates shapes, take a step back and think about whether that functionality can be handled by shape formulas.
For special considerations of distributing a Visio solution that includes Automation, see Chapter 26, Packaging a Visio Automation Solution.
Integrating a Visio solution with a database requires some planning to synchronize the drawings with the database. It's important to decide which database should be used, what should be changed and how, and when the changes should occur.
For example, in the security system solution described earlier in this chapter, each component (camera, sensor, control unit, and so on) is a numbered part in the manufacturer's catalog. By storing a part number as a custom property of each master, it is easy to look up part information in a version of the parts catalog stored as a database.
After designing the interactions between a solution and a database, a solution can make changes by:
The Database Wizard provided with Visio can define user-defined cells and link custom property cells to database fields for simple solutions or for prototyping more complex solutions. You can use the DAO (Data Access Object) library provided by Microsoft to access databases through ODBC (Open Database Connectivity) or use the Jet database engine. Or, your Visio solution might call an Automation server that actually updates and synchronizes the database, which provides more control over the integrity of the database.
For details about Visio solutions and databases, see Chapter 20, Integrating Data with a Visio Solution.
The kind of program you write depends on what you're trying to do. You could write a VBA macro in a Visio document or another Automation controller application, or a stand-alone program in Visual Basic or C/C++. You could write a dynamic-link library (DLL) for a COM add-in, or another special kind of DLL that runs with Visio, called a Visio library (VSL). Users might run your program from the Microsoft Windows desktop or Windows Explorer, or they might run it from Visio, by choosing a command added to a Visio menu, a button added to its toolbar, or even by double-clicking or right-clicking a shape in a drawing. Or you could design your program to run automatically when a certain event happens, such as when a document is opened or created.
There are four basic ways to implement Automation in a Visio solution. You can implement the following:
An EXE file is easy to build, can be written in many different languages, and is fairly robust. However, an EXE file must execute in a different process from the Visio instance it is controlling and must be loaded each time it is executed, which can affect performance.
A VSL is much faster than an EXE file because it executes in the same process as a Visio instance and is loaded once, and then executed from memory. You can also determine, within context, when the VSL is visible to the user. However, a VSL is not as robust as an EXE file—if it crashes, the Visio instance usually does too—and it must be written in C++. Also, a VSL is faster than an EXE file only if most of its processing time is spent controlling the Visio instance; if the VSL spends half or more of its time controlling another application, then a program that executes in the process of the other application might be a better choice.
Like a VSL, VBA macros execute in the same process as a Visio instance and are easier to write, so programmers can accomplish more in less time. VBA macros are easy to distribute, which is both an advantage and a disadvantage. Every drawing contains a copy of the VBA code, which complicates fixing bugs and adding new features.
When you use VBA macros in Microsoft Visio 2002, be sure to consider security issues. You should plan on obtaining a digital certificate and signing your VBA project with it. For details on digital certificates and digital signatures, see Using Digital Certificates to Produce Trusted Solutions in Chapter 15, Programming Visio with VBA.
You can build COM add-ins with any of the Office applications in Microsoft Office Developer (version 2000 or later). If you have a Microsoft Office Developer license, you can also build COM add-ins using VBA in Visio. In addition, you can create COM add-ins with Visual Basic or C++. Like VSLs and VBA macros, COM add-ins execute in the same process as a Visio instance and are fairly easy to write.
The hybrid approach supports in-process execution if the Automation server is created as a DLL, and it supports a wide range of architectures. However, a hybrid approach tends to require more careful system design.
Programming Visio with VBA is discussed in chapters 14 through 26 of this guide. For details about using Visual Basic with Visio, see Chapter 27, Programming Visio with Visual Basic. For details about programming Visio with C++ and writing VSLs, see Chapter 28, Programming Visio with C++.