Question

I am trying to use/understand Interop.EA and EA class in C# and how to create diagrams programmatically.

I'm working with user data (.XML file (not XMI)) from another web application.

I have tried CSAddinDemo from Sparx and it works fine, but does not show how to add/create new.

My goal is to create EA class diagram form XML data, in C#.

Something like this??

public void EA_create()
{
    EA.DiagramObject d = new DiagramObject();
    EA.Element e = new Element();

    EA.Element elementEa = EA.Element.AddNew("Requirement", "non-functional");
    elementEa.Update();
    elements.Refresh();
    //MessageBox.Show("Class created");
}
Was it helpful?

Solution

First off, you need an EA project (aka "repository") to work in. In order to create a new EA project (.EAP file), use Repository.CreateModel(). In order to open an existing one, use Repository.OpenFile().

Other than the Repository class, which is the top-level class for all interactions with EA, you don't create objects yourself. Instead, you call AddNew() on the various Collections you traverse -- this goes for packages, elements, diagrams, diagram objects, connectors, attributes, tagged values, etc etc.

Repository.Models is such a collection (of Packages).

A Package then contains additional collections, such as Packages, Elements and Diagrams. A Diagram has a collection of DiagramObjects.

A DiagramObject is the graphical representation of an element within one (1) diagram (remember that an element is stored in exactly one package but can be shown in any number of diagrams).

EA does not allow you to create any diagrams or elements at the top level of the package tree (called the root node); you must first create a child package. So at the very least you need to create one Package and one Diagram, a number of Elements and one DiagramObject for each element.

So in the code you've got, AddNew() doesn't work because you're trying to call it on an Element and you need to call it on a Collection.

Your use of Update() and Refresh() are correct, assuming that elements is a Collection. You must always call Update after any changes to an object, and Refresh after any changes to a collection.

There's some good getting-started stuff in the help file. Look in Automation and Scripting -- Enterprise Architect Object Model, and read the section titled Using the Automation Interface.

Then, look at Reference -- Code Samples, especially Open the Repository and Add and Manage Diagrams.

Finally, note that an Add-In is a piece of code which extends EA and is run from within an EA process. This isn't necessary if you want to create a new model or make changes to an existing one; you only need to write an Add-In if you want to integrate with EA's GUI or respond to various events triggered by EA.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top