Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: qwerty on May 10, 2012, 10:38:33 pm

Title: Selected elements in diagram
Post by: qwerty on May 10, 2012, 10:38:33 pm
When you right click any element or connector in a diagram then EA offers the addin-menu in the context. I faintly remember a method to retrieve the selected element(s) in a diagram. But I can't find it now. How can I find out which element(s)/connector is/are selected in the diagram from an add-in?

q.
Title: Re: Selected elements in diagram
Post by: Makulik on May 10, 2012, 11:16:43 pm
The Diagram class contains a read only collection property SelectedObjects, I guess that's what you're looking for (see http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/automation/diagram2.html).

HTH
Günther
Title: Re: Selected elements in diagram
Post by: qwerty on May 10, 2012, 11:44:59 pm
Doh! I like EA. GetTreeSelectedObject is in Repository. Of course this one is in Diagram. I was looking for GetDiagramSelectedObject or the like...

Thanks!

q.
Title: Re: Selected elements in diagram
Post by: qwerty on May 11, 2012, 12:38:12 am
Interestingly there is also a property SelectedConnector (which I did not expect...). The documentation tells this is r/w. For curiosity sake I tried setting this to zero (thus deselecting the connector). Well, that did not work. I guess this is an error in the documentation and it's meant to be r/o -> Roy?

q.
Title: Re: Selected elements in diagram
Post by: Geert Bellekens on May 11, 2012, 03:38:58 pm
I always use getContextItem:
Here's the code I use for getting and setting the selected element from https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/Model.cs
Code: [Select]
/// the Element currently selected in EA
    public UML.Classes.Kernel.Element selectedElement {
      get {
        Object selectedItem;
        try
        {
            this.wrappedModel.GetContextItem(out selectedItem);
            return this.factory.createElement(selectedItem);
        }
        catch (COMException)
        {
            //something went wrong
            return null;
        }

        
      }
          set
          {
                if (value is Package)
                {
                      this.wrappedModel.ShowInProjectView(((Package)value).wrappedPackage);
                }
                else if (value is ElementWrapper)
                {
                      this.wrappedModel.ShowInProjectView(((ElementWrapper)value).wrappedElement);
                }
              else if (value is Operation)
              {
                  this.wrappedModel.ShowInProjectView(((Operation)value).wrappedOperation);
              }
              else if (value is Attribute)
              {
                  this.wrappedModel.ShowInProjectView(((Attribute)value).wrappedAttribute);
              }
              else if (value is Parameter)
              {
                    Operation operation = (Operation)((Parameter)value).operation;
                    this.wrappedModel.ShowInProjectView(operation.wrappedOperation);
              }
          }
    }

But that only returns one object, not a collection.

Geert
Title: Re: Selected elements in diagram
Post by: qwerty on May 11, 2012, 07:22:14 pm
Truly Konzistend. Multiple elements selected are not in a "real" context. Neither in a diagram nor in the browser.

However, thanks for the note :-) Might be useful, though.

q.