Adam Howitt's Blog

Jun 27
2005

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:

<classes>
    <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>
To get started

 

  1. Open your class diagram at a page with a class you wish to export.
  2. Control+click each class you wish to export
  3. Open the Visual Basic Editor (Alt+F11)
  4. Double click on "ThisDocument" if no script is currently loaded
  5. Paste the script below here into  the empty page
  6. Edit the "LogFileLocation" string to tell it where you wish to save your file.  (Note: this must exist before the script runs)
  7. Hit F5 to run the script
  8. Enter a name for the file in the popup box
  9. Hit OK
  10. You should see a message box with the XML document.  Click OK
  11. Open the directory you gave for the export location
  12. You should see an XML export file!
So here is the script to perform the magic.
Option Explicit
 
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

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
[Add Comment] [Subscribe to Comments]
  1. I am writing a tool to go from XMI to CFCs and supporting SQL DDL. I am using Poseidon for my modelling. I have to say, I am not getting XMI that looks remotely as clean as your export from Visio. I wonder if this export is XMI compliant? Would you be interested in playing around with this XMI->CFC concept with me?

  2. I was trying to get the properties to input into XML, just wondering, did you install XMIExport.dll? what else did you have?

  3. Fantastic! Thanks for the script!

  4. Carolynne, you are welcome! Ingrid - I completely missed your message. I didn't install anything special as far as I can remember. Chip - shoot me an email about the XMI > CFC concept!

  5. Excellent Stuff. Thanks a lot.

  6. how would you adapt your code to work with the UML sequence diagram entities? The objects I'm using apparently do not support that property.

    is there some documentation that I can download that might describe the properties of the UML objects used in Visio?

    Thanks Eden

[Add Comment]