Visio 2003 UML Class Export Tool
I spent the last week designing the architecture with UML for the rewrite of our CMS and arrived at a very workable version of our class model. I used Visio 2003 and once I had created the ColdFusion datatypes I was able to make considerable progress.
However, when I came to export my class definitions to some sort of file to begin implementing my CFCs...
...no export facility. So I dug around and found a Microsoft article on how to export UML diagrams to XMI format. Wonderful, I thought, but soon discovered that we don't have Visual C++ in house and can't compile the code. Bugger.
Back to the drawing board. I knew there had to be a way to access the model through VBA but couldn't locate the objects I needed so after I found a snippet for exporting custom properties to Excel and a little rework I came up with a script to suit my needs.
Select a number of classes in a Visio 2003 page and then run the script to get an XML file similar to this:
<class name="Security::Role">
<properties>
<property>-RoleId[1] : guid</property>
<property>-Name[1] : string</property>
<property>-Description[1] : string</property>
<property>-Messages[0..*] : Message</property>
</properties>
<methods>
<method>+init(in name, in description) : guid</method>
<method>+addMessage(in message : Message) : guid</method>
<method>+deleteMessage(in messageId : guid) : boolean</method>
</methods>
</class>
</classes>
- Open your class diagram at a page with a class you wish to export.
- Control+click each class you wish to export
- Open the Visual Basic Editor (Alt+F11)
- Double click on "ThisDocument" if no script is currently loaded
- Paste the script below here into the empty page
- Edit the "LogFileLocation" string to tell it where you wish to save your file. (Note: this must exist before the script runs)
- Hit F5 to run the script
- Enter a name for the file in the popup box
- Hit OK
- You should see a message box with the XML document. Click OK
- Open the directory you gave for the export location
- You should see an XML export file!
Sub ExportShapes()
Const LogFileLocation As String = "C:\visioExportFiles\"
Dim selObj As Visio.Selection 'Shapes selection collection
Dim shpObj As Visio.Shape 'A shape instance
Dim i As Integer
Dim myClassDef As String
Dim LogFileName As String
Dim FileNum As Integer
myClassDef = "<?xml version=""1.0"" encoding=""UTF-8""?><classes>"
Set selObj = Visio.ActiveWindow.Selection
For i = 1 To selObj.Count
Set shpObj = selObj(i)
myClassDef = myClassDef & "<class name=""" & shpObj.Shapes.Item(7).Text & """>"
myClassDef = myClassDef & "<properties><property>" & Replace(shpObj.Shapes.Item(6).Text, Chr(10), "</property><property>") & "</property></properties>"
myClassDef = myClassDef & "<methods><method>" & Replace(shpObj.Shapes.Item(5).Text, Chr(10), "</method><method>") & "</method></methods>"
myClassDef = myClassDef & "</class>"
Next i
myClassDef = myClassDef & "</classes>"
'Begin export
LogFileName = InputBox("What is the file name you wish to use (exclude the .xml extension)?", "Export Classes")
MsgBox (myClassDef)
If Len(LogFileName) Then
FileNum = FreeFile ' next file number
Open LogFileLocation & LogFileName & ".xml" For Output As #FileNum ' creates the file if it doesn't exist
Print #FileNum, myClassDef ' write information at the end of the text file
Close #FileNum ' close the file
Else
MsgBox ("Bad file name")
End If
End Sub