Book a Demo

Author Topic: Add Tagged Value to Connector  (Read 3931 times)

Kai H.

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • Where is my mind?
    • View Profile
Add Tagged Value to Connector
« on: March 25, 2011, 04:05:54 am »
Hi,

here is some working code for adding a new TaggedValue to an Element:

Code: [Select]
EA.TaggedValue b = (EA.TaggedValue)portElem.TaggedValues.AddNew("myRate", "TaggedValue");
b.Value = "0.1";
b.Update();

But the same code is not working for Connectors:

Code: [Select]
EA.TaggedValue tv = (EA.TaggedValue)connector.TaggedValues.AddNew("myName", "TaggedValue");
tv.Value = "myValue";
tv.Update();

Surprisingly, this results in the following exception:

Code: [Select]
System.InvalidCastException was unhandled by user code
  Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'EA.TaggedValue'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{EF08950B-949E-435F-84A3-A59E3106C3BF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
  Source=Anonymously Hosted DynamicMethods Assembly

Any ideas?

Cheers

Kai

stao

  • EA User
  • **
  • Posts: 137
  • Karma: +0/-0
    • View Profile
Re: Add Tagged Value to Connector
« Reply #1 on: March 25, 2011, 04:32:58 am »
for some reason the tags of connectors and methods are handled different than the tags of other elements.

this code should work:

Code: [Select]
EA.ConnectorTag tv = (EA.ConnectorTag)connector.TaggedValues.AddNew("myName", "TaggedValue");
tv.Value = "myValue";
tv.Update();


PS. if you plan to use taggedValues more often i would highly recommend to use a helper method.


heres mine:

Code: [Select]

public static EA.ConnectorTag setTaggedValue(EA.Connector connector, String name, String value)
        {
            connector.TaggedValues.Refresh();
            EA.ConnectorTag aTag = findTaggedValue(connector, name);
            if (aTag == null) aTag = (EA.ConnectorTag)connector.TaggedValues.AddNew(name, value);
            aTag.Value = value;
            aTag.Update();
            return aTag;
        }


the method only creates a new TaggedValue if there isnt already a tag with the same name.
else only the value of the taggedValue will be changed.

(This is for avoiding multiplie TaggedValues with the same name)

Stao

Kai H.

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
  • Where is my mind?
    • View Profile
Re: Add Tagged Value to Connector
« Reply #2 on: March 25, 2011, 06:09:21 pm »
Thank you stao, it works fine!

Another hint: if you want to add a TaggedValue after you created the connector, remember to call the Update() function on the connector before you add the TaggedValue, otherwise it will not appear in EA. A second Update() of the connector after the TaggedValue is inserted is not neccessary:

Code: [Select]
EA.Connector connector = (EA.Connector)sourceElement.Connectors.AddNew("Connector", "Connector");
connector.ClientID = sourceElement.ElementID;
connector.SupplierID = destinationElement.ElementID;
connector.Update();
EA.ConnectorTag tv = (EA.ConnectorTag)connector.TaggedValues.AddNew("myName", "TaggedValue");
tv.Value = "myValue";
tv.Update();

Kai
« Last Edit: March 25, 2011, 06:10:41 pm by kai.hoefig »