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.