Book a Demo

Author Topic: Select an item inside the diagram by guid  (Read 4276 times)

TheMintRubber

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Select an item inside the diagram by guid
« on: September 11, 2017, 07:00:35 pm »
Hi,

I am creating an EA addin written in WPF.

I have to select an item in the EA diagram when the user clicks on a item in my plugin. I have the item's guid, but I can't find a way to select the element in EA.

I have found the Diagram.SelectedObjects.AddNew(string name, string type), but I don't know how to get the name and type by my guid.

So, is there a way to select an item in the opened Diagram using the item's guid?

Thanks.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Select an item inside the diagram by guid
« Reply #1 on: September 11, 2017, 09:10:30 pm »
Yes you can.
You can select an element using EA.Diagram.SelectedObjects.AddNew(elementID, type) or you can select a connector using EA.Diagram.SelectedConnector

If you have the GUID then you can get the EA.Element using Repository.GetElementByGUID() or the connector using Repository.GetConnectorByGUID()

Here the code I use in my framework:

Code: [Select]
public void selectItem(UML.Extended.UMLItem itemToSelect)
{
if (itemToSelect is Operation)
{
bool found = false;
//if the item is a relation or an operation then search through the links first
foreach (DiagramLinkWrapper diagramLinkWrapper in this.diagramLinkWrappers)
{
if (itemToSelect is Operation
   && diagramLinkWrapper.relation is Message)
{
Message message = (Message)diagramLinkWrapper.relation;
if (itemToSelect.Equals(message.calledOperation))
{
this.wrappedDiagram.SelectedConnector = message.wrappedConnector;
found = true;
//done, no need to loop further
break;
}
}
}
//The operation could also be called in an Action.
if (!found)
{
List<UML.Actions.BasicActions.CallOperationAction> actions = ((Operation)itemToSelect).getDependentCallOperationActions().ToList();
List<UML.Diagrams.DiagramElement> diagramObjects = this.diagramObjectWrappers.ToList();

foreach (Action  action in actions)
{
//try to find an diagramObjectwrapper that refrences the action
UML.Diagrams.DiagramElement diagramObject = diagramObjects.Find(
x => x.element.Equals(action));
if (diagramObject != null)
{
//found it, select the action and break out of for loop
this.selectItem(action);
found = true;
break;
}
}
}
if (!found)
{
//can't find a message on this diagram that calls the operation.
//then we try it with the operations parent
this.selectItem(((Operation)itemToSelect).owner);

}
}
else if (itemToSelect is ConnectorWrapper)
{
this.wrappedDiagram.SelectedConnector = ((ConnectorWrapper)itemToSelect).wrappedConnector;
//check if it worked
if (wrappedDiagram.SelectedConnector == null
   && itemToSelect is Message)
{
this.selectItem(((Message)itemToSelect).calledOperation);
}
}
else if (itemToSelect is ElementWrapper)
{
ElementWrapper elementToSelect = (ElementWrapper)itemToSelect;
this.wrappedDiagram.SelectedObjects.AddNew(elementToSelect.wrappedElement.ElementID.ToString(),
   elementToSelect.wrappedElement.Type);
}
}