Generate UML Design Documents from Code





Generate UML Design Documents from Code

Use Visual Studio to generate design documents in Visio.

Like all good developers, you keep your design documentation up-to-date, right? Most likely, that's a no. After all, who wants to constantly go back and continuously update design documents while coding? Most developers I know usually don't have the time because they are too busy trying to get the application implemented.

To help with this task, you can use Visual Studio to generate those pesky design documents, in particular the class diagrams, by having it reverse engineer your code. Although it won't replace and update all of your design documents, it can cut down on the amount of time it takes to maintain them manually. Essentially, this hack is the opposite of [Hack #81].

Reverse Engineer Your Code

To get started, you first need to create a Visual Studio Project. This can be any type of project, and most likely it's one that you've been working on or have already created. In this hack, I'm going to use the VehicleLibrary example found in [Hack #81]. To refresh your memory, the Solution Explorer of that project can be seen in Figure.

VehicleLibrary project in the Solution Explorer


To reverse engineer the code for this project, do the following:

  1. Click Project Visio UML Reverse Engineer.

  2. Browse to the location where you want your Visio file to be saved, give it a name, and click Save.

  3. Depending on the size and complexity of your project, the reverse engineering process may take a moment or two.

  4. Once your code has been successfully reverse engineered, an instance of Visio is automatically started. You can see in the Model Explorer that your classes have been automatically created (see Figure).

Classes automatically generated in Visio


  1. You can also see in your Visual Studio instance that the generated Visio file has been added to your project under the Solution Items folder.

Create Your Class Diagram

Unfortunately, that's as far as the reverse engineering will take you. You'll have to create the class diagram manually. Depending on the size of your class hierarchy, this may or may not be a big deal. The positive spin to this is that Visio is smart enough to understand the relationships between the generated classes.

For instance, when you drag your classes onto a diagram, Visio automatically determines their relationships and draws the generalization lines for you. An example is shown in Figure.

Dragging classes onto a Visio diagram


As I was dragging classes from the Model Explorer onto a static structure diagram in Visio (see Figure), all the arrowed lines were drawn for me. I didn't have to remember all the relationships between the classes, which in this case happen to be inheritance lines.

So as you can see, Visual Studio can be used to reverse engineer your classes from code into Visio for use on class diagrams, sequence diagrams, and other related design documents.

Dave Donaldson


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows