You are here
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
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.
* On 64-bit operating systems the EA folder may be "c:\Program Files (x86)\Sparx Systems\EA".
Please note that all comments that look like "Thank you! This is exactly what I've looked for! You are THE GREAT! My site with flash games" will be immediately deleted without any compunction and your IP will be reported to mollom and added to the spamlists. Thank you for understanding.



Comments
GenerateSourceCode EA
First to congratulate the excellent article on the EA Interop.
You know how to generate C # code through Interop.EA.dll?
Please help me!
A hug,
Alexandre.
Could you please clarify what
Could you please clarify what you want to do?
Do you want to call EA code generation module from the extension?
In this case you should use the project interface:
http://www.sparxsystems.com/uml_tool_guide/sdk_for_enterprise_architect/... (search for Generate)
Regards,
Alex
Interop EA.
Hello Alex, thanks for answering my question.
What I do is to generate C # code using the method GenerateSourceCode Interface Package.
I've managed to generate through this interface.
Thanks for the link you sent, greatly help in my project.
All the best to you!
Where do I install the addin?
Interesting post.
I´ve build an addin in C# called myaddin.dll and now I want to install it in other computer. Where have I copy my dll? what folder?
Thanks.
You can copy the assembly to
You can copy the assembly to any folder you like. I prefer "C:\Program Files (x86)\Sparx Systems\EAAddIns" + subfolder for your addin. However, if you use installer, it may be a good idea to install it in "Program Files" + subfolder.
Regards,
Alex
Add new comment