0Day Forums
Access to nested elements by script in Enterprise Architect - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: JScript (https://0day.red/Forum-JScript)
+--- Thread: Access to nested elements by script in Enterprise Architect (/Thread-Access-to-nested-elements-by-script-in-Enterprise-Architect)



Access to nested elements by script in Enterprise Architect - winzler314 - 07-24-2023

I'm a beginner with Enterprise Architect and I need to write a little program to select all "Activity" type in a Enterprise Architect project to set a particular value of "Alias" property (not the same but following a particular logic).
I think to do this with the Scripting tool, but I had some difficulties to access to any activity object by scripting.

I found the link [1] "Enterprise Architect Object Model" but I could not solve my question.
Below is attached the project tree.

Here there is some javascript code i tried:

var elem as EA.Element;
elem = Repository.GetTreeSelectedObject();
elem.Alias = "Hi at all";

But this code has 2 problems:

1) it needs to select the activity with the mouse pointer;

2) after running the code the "Alias" field of the activity is empty.

[![enter image description here][2]][2]


[1]:
[2]:



RE: Access to nested elements by script in Enterprise Architect - idolatressjejvtezgnl - 07-24-2023

1. How to process all the Activities
--
There are different ways to deal with this. One option would be to start at the selected **Package** and iterate over each of the owned elements; and their owned elements.
That would be something like this (C#, but you get the gist of it):

public void main()
{
var selectedPackage = Repository.GetTreeSelectedPackage();
processPackage(selectedPackage);
}

private void processPackage(EA.Package package)
{
//process owned elements
foreach (EA.Element element in package.Elements)
{
processElement(element);
}
//process SubPackages
foreach(EA.Package subPackage in package.Packages)
{
processPackage(subPackage);
}
}
private void processElement(EA.Element element)
{
//test type and steroetype to make sure we only treat Activities
if (element.Type == "Activity" && element.Stereotype == "Activity")
{
element.Alias = "newAlias";
element.Update();
}
//process owned Elements
foreach(EA.Element subElement in element.Elements)
{
processElement(subElement);
}
}

This works OK for a limited number of elements, but it get real slow if you wanted to process larger numbers of elements scattered over a large model.

In that case it is better to use `Repository.GetElementSet(MySQLSelectQuery,2)`. Use this operation with an SQL query (second parameter needs to be `2`) to select exactly the elements you need.
This will be an order of magnitude faster then iterating over the whole model.

Examples of this approach (and much more) in my github repositories:

- C# [Enterprise Architect Add-in Framework][1]
- VBScript [Enterprise-Architect-VBScript-Library][2]

2. How to save updates
--
That is simple enough. Make sure to call `Update()` after changing any property of the API object.


[1]:

[To see links please register here]

[2]:

[To see links please register here]