Creating Add-In for Sparx Enterprise Architect

This article explains how to create add-in for Sparx Systems Enterprise Architect - the best UML modeling tool I know.

**Enterprise Architect is an excellent software, with good object model. Really, **the company that develops UML modeling software is expected to have **outstanding object model, because if not they, then who?

Extending EA is very simple, especially with Visual Studio 2008. It takes only 7 quick steps and less than 5 minutes to create your own add-in. Let’s start.

  • Open Visual Studio and create class library with name that corresponds to what it expect to do.
  • Change the name of the Class1 class to something more reliable, for example, Main or AddIn.
  • Add reference to the “c:\Program Files\Sparx Systems\EA\Interop.EA.dll” library. You may also add reference to the System.Windows.Forms namespace.
  • Add the ComVisible attribute to the main class.
  • In the project properties check the “Register for COM interop” checkbox on the “Build” tab, add the “c:\Program Files\Sparx Systems\EA” folder on the “Reference Paths” tab, and set the “Start external program” option to the “c:\Program Files\Sparx Systems\EA\EA.exe” executable on the Debug tab.
  • Register add-in in EA by adding new registry key (not value) to “HKEY_CURRENT_USER\Software\Sparx Systems\EAAddIns” (create it if this key does not exist) with name of your add-in project and [project_name].[class_name] as default value.
  • Add the code. Basically, the add-in may contain only one main method - EA_Connect, but you can also modify menu items in EA_GetMenuItems and handle clicks in EA_MenuClick. More information about event handlers and object model is available in the EA SDK

Note: on 64-bit operating systems the EA folder may be “c:\Program Files (x86)\Sparx Systems\EA”.

For example, this simple add-in adds the new menu item that shows number of classes in the project:

using System.Runtime.InteropServices;
using System.Windows.Forms;
using EA;

namespace EaProjectStats
{
    [ComVisible(true)]
    public class AddIn
    {

        // Called Before EA starts to check Add-In Exists
        public string EA_Connect(Repository repository)
        {
            // nothing special
            return "EaProjectStats.AddIn - connected";
        }

        // Called when user Click Add-Ins Menu item.
        public object EA_GetMenuItems(Repository repository,
            string location, string menuName)
        {
            switch (menuName)
            {
                case "":
                    return "&Project statistic...";
            }
            return "";
        }

        // Sets the state of the menu depending if there is
        // an active project or not
        static bool IsProjectOpen(Repository repository)
        {
            try
            {
                return null != repository.Models;
            }
            catch
            {
                return false;
            }
        }

        // Called once Menu has been opened to see what menu
        // items are active.
        public void EA_GetMenuState(Repository repository,
            string location, string menuName, string itemName,
            ref bool isEnabled, ref bool isChecked)
        {
            if (IsProjectOpen(repository))
            {
                if (itemName == "Project statistic...")
                    isEnabled = true;
            }
            else
                // If no open project, disable all menu options
                isEnabled = false;
        }

        // Called when user makes a selection in the menu.
        // This is your main exit point to the rest of your Add-in
        public void EA_MenuClick(Repository repository,
            string location, string menuName, string itemName)
        {
            switch (itemName)
            {
                case "&Project statistic...":
                    var count = 0;
                    foreach (Package model in repository.Models)
                        foreach (Package package in model.Packages)
                            count += CountClasses(package);
                    MessageBox.Show("This project contains "
                        + count + " " + (count == 1 ? "class" : "classes"));
                    break;
            }
        }

        private static int CountClasses(Package package)
        {
            var count = 0;
            foreach (Element e in package.Elements)
                if (e.Type == "Class")
                    count++;
            foreach (Package p in package.Packages)
                count += CountClasses(p);
            return count;
        }

    }
} 

Registry export file for this add-in:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Sparx Systems\EAAddIns\EaProjectStats]
@="EaProjectStats.AddIn"

To run it just copy and paste the code below to the project you have created and run the project. In the EA application window open the project that contains class diagrams or create a new one. After that the menu item in Add-Ins menu becomes enabled. Selecting “Show project statistic…” shows the message box with total number of classes in your project.