Book a Demo

Author Topic: Create a new element using automation, C#  (Read 4190 times)

Chris Griffis

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Create a new element using automation, C#
« on: February 20, 2009, 08:24:42 am »
I can't seem to find in the documentation how to create a new element within a package through the automation interface (using C#)

I am trying to synchronize a package within the repository to an external database. when i encounter records in the external databse that do not correspond to exisitng elements within the model, i would like to create them in the current package so that these new elements match what is in the database.

so for example, let's say for a given repository, I would like to do something like
Code: [Select]
private void method1(EA.Repository r, string params)
{
   EA.Package package = r.GetTreeSelectedPackage();
   package.addNewElement(params);
}
or
Code: [Select]
private void method1(EA.Repository r, string constructorParams)
{
   EA.Package package = r.GetTreeSelectedPackage();
   EA.Element e = new EA.Element(constructorParams);
   package.add(e);
}

etc.

How do I do this?  

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Create a new element using automation, C#
« Reply #1 on: February 20, 2009, 09:06:20 am »
There are some VB.NET code samples in the documentation that may help:
http://www.sparxsystems.com/uml_tool_guide/sdk_for_enterprise_architect/addandmanageelements.html

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Create a new element using automation, C#
« Reply #2 on: February 20, 2009, 09:15:27 am »
Hi Chris,

It takes a bit of getting used to...

First create the element by adding it to a parent collection - there is no other way to do this.

Then commit (for lack of a better word) the element to the model with the Update method.

In between you can set some properties of the element, but only a few. You cannot set reference the element from anything else (connectors, other elements, the parent element or package) until you have performed the Update.

So, you would end up with something like this:
Code: [Select]
...
EA.Package pack = r.GetPackageByID(packID); // Returns a package, no need to cast
EA.Element elem = (EA.Element) pack.Elements.AddNew("My Name", "Class"); // Returns an object, you need to cast the result
elem.Stereotype = "special"; // No delimiters, EA will handle them
elem.Update(); // You can also test the boolean return value
...
pack.Elements.Refresh() // Required before you retrieve the element from the parent collection
...

HTH, David
No, you can't have it!