Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Hurra on June 09, 2017, 11:33:08 pm
-
Hello!
Where is my thought process off?
I have an on Diagram-script.
currentDiagram = Repository.GetCurrentDiagram(); (as EA.Diagram)
allObjects = currentDiagram.DiagramObjects; (as EA.Collection)
var listOfObjectNames = new Array();
var listOfElementIDs = new Array();
for (var i = 0; i < allObjects.Count; i++)
{
thisObject = allObjects.GetAt(i);
thisElement = Repository.GetElementByID(thisObject.ElementID);
listOfObjectNames.push(thisElement.Name);
listOfElementIDs.push(thisElement.ElementID);
}
allElements = Repository.GetElementSet(listOfElementIDs, 1);
listOfObjectNames.sort();
for (var j = 0; j < allObjects.Count; j++)
{
Session.Output(listOfObjectNames[j]);
allElements.GetByName(listOfObjectNames[j]);
thisObject = thisElement;
// Do stuff
thisObject.left = thisObject.left - 10;
thisObject.Update;
}
currentDiagram.Update;
My problem is the Collection.GetByName. At first I tried it on a collection with DiagramObjects, but I found in the documentation that:
GetByName(string Name)
Object
Notes: Gets an item in the current collection by name. Supported for Model, Package, Element, Diagram and element TaggedValue collections.
If the collection does not contain any items (or, for the Tagged Value collection, if the collection contains items but the method cannot locate an object with the specified name) the method returns a null value. For other collections, if the method is unable to find an object with the specified name, it raises an exception.
Parameters:
Name: String
Therefore I created a collection with Elements, through allElements = Repository.GetElementSet(listOfElementIDs, 1);. But I get the same error message. The end of this code snippet is kind of pseudo code.
The main problem is that I can't get Collection.GetByName to work.
Help?
-
I think that GetElementSet does not return what it's supposed to return (merely I guess it will work with child element sets though). Anyhow, just write your own wrapper for GetByName since you can handle duplicate names in a proper fashion. EA's operation will only return an arbitrary element if your name set is not unique.
q.