Click here to Skip to main content
13,772,614 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

18.4K views
2 bookmarked
Posted 2 Aug 2013
Licenced CPOL

Enterprise Architect Automation – Save/Export All Diagrams as Pictures

, 2 Aug 2013
Rate this:
Please Sign up or sign in to vote.
Enterprise architect automation - save/export all diagrams as pictures

I’ve recently used Sparx’s Enterprise Architect (EA) tool for one of my projects to create various design diagrams (use-case diagrams, activity diagrams, sequence diagrams and class diagrams, etc.). While the tools like Visio are able to produce data models and class structures for conventional programming languages, in addition to that, EA can be used to create data models and diagrams for frameworks like SharePoint too.

In EA, user can create new data types for their frameworks and the data types can be mapped against the conventional SQL data types. I was able to generate the SharePoint list logical data models using that feature.

  • The important feature of Sparx is, It’s able to import the code from the .NET source files and generates the class structures.
  • The graphical representations of elements (objects) and connectors are beautiful and it supports different output formats like BMP, PNG, JPG and PDF, etc.
  • It has powerful automation functionality which are exposed via COM. Programmers can automate the repetitive tasks of Sparx via VBScript, JavaScript, VB6, .NET, etc.

Here is the script to automate the creation of images from the diagrams created with single mouse click. This VBScript code is based on the sample script mentioned in the following link:

option explicit
!INC Local Scripts.EAConstants-VBScript

‘ Global reference to the current project interface
dim projectInterface as EA.Project
dim foldername
‘ Show the script output window
Repository.EnsureOutputVisible "Script"
foldername = "c:\\temp\\"
ExportAllDiagrams()
sub ExportAllDiagrams()
    ‘ Show the script output window
    Repository.EnsureOutputVisible "Script"
    set projectInterface = Repository.GetProjectInterface()
    
    ‘ Iterate through all model nodes
    dim currentModel as EA.Package
    for each currentModel in Repository.Models
        ‘ Iterate through all child packages and save out their diagrams
        dim childPackage
        for each childPackage in currentModel.Packages
            DumpDiagrams childPackage, foldername
        next
        
    next
    Session.Output "Done!"  
end sub
‘
‘ Recursively saves all diagrams under the provided package and its children
‘
sub DumpDiagrams ( thePackage, byval subfoldername )

    'Cast thePackage to EA.Package so we get intellisense
    dim currentPackage as EA.Package
    set currentPackage = thePackage
    
    dim newfoldername
    newfoldername = trim(replace( replace_
    ( thePackage.Name, ":", ""), ".", ""))
    if newfoldername <> "" then
        ‘ Save and close the diagram
        subfoldername =  subfoldername + newfoldername  + "\\"
        
        CreateFolder subfoldername
    end if
    
    'Iterate through all diagrams in the current package
    dim currentDiagram
    dim filename
    for each currentDiagram in currentPackage.Diagrams
        filename = trim(replace( replace_
        ( currentDiagram.Name, ":", ""), ".", ""))
        if filename <> "" then
            ‘ Open the diagram
            Repository.OpenDiagram( currentDiagram.DiagramID )
            projectInterface.SaveDiagramImageToFile GetFilename(subfoldername ,filename + ".png", 1)
            Repository.CloseDiagram( currentDiagram.DiagramID )
        end if
    next
    ‘ Iterate through all diagrams in the sub package
    dim childPackage ‘as EA.Package
    for each childPackage in currentPackage.Packages
        DumpDiagrams childPackage, subfoldername
    next
    
end sub
Function GetFilename(thefoldername, theFilename, byval thesequence )
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   
   dim nametocheck
   if thesequence = 1 then
       nametocheck = thefoldername + theFilename
   else
       nametocheck = thefoldername + cstr(thesequence) + theFilename
   end if
        
   If fso.FileExists(nametocheck) Then
        GetFilename = GetFilename(thefoldername, theFilename, thesequence + 1 ) 
   else
        if thesequence = 1 then
            GetFilename = thefoldername + theFilename
        else
            GetFilename = thefoldername + cstr(thesequence) + theFilename
        end if
   End if
   
End Function
Function CreateFolder( theFolder )
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   If not fso.FolderExists(theFolder) Then
    Set f = fso.CreateFolder(theFolder)
  End if
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sabarinathan A
Architect
India India
I have been programming for last 20 years on various platforms including .NET, Visual Basic 6, Oracle and SQL server.

I decided to write articles when I have free time hoping to share my thoughts with you.

To know more about me visit http://sabarinathanarthanari.com

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.181119.1 | Last Updated 2 Aug 2013
Article Copyright 2013 by Sabarinathan A
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid