Book a Demo

Author Topic: Adding Connector using API and Manually  (Read 6956 times)

Eamonn John Casey

  • EA User
  • **
  • Posts: 110
  • Karma: +0/-1
    • View Profile
Adding Connector using API and Manually
« on: February 02, 2017, 10:58:39 pm »
I got a problem where I create a Connector through the API and create the same Connector manually. The API Version shows the stereotype in the diagram. The manual one shows the text I want to show. I have create my own MDG.

The code looks like this:
Code: [Select]
public EA.Connector FindorCreateConnector(string fromElementGUID, string toElementGUID, string connectorType, string connectorDirection, string connectorStereoType)
{
    EA.Element fromElement = eaRepository.GetElementByGuid(fromElementGUID);
    EA.Element toElement = eaRepository.GetElementByGuid(toElementGUID);
    EA.Connector connectorFound = null;

    foreach (EA.Connector connector in fromElement.Connectors)
    {
        if (connector.SupplierID == toElement.ElementID)
        {
            connectorFound = connector;
        }
    }
    if (null == connectorFound)
    {
    connectorFound = (EA.Connector)fromElement.Connectors.AddNew("", "Connector");
        connectorFound.Type = connectorType;
        connectorFound.Stereotype = connectorStereoType;
        connectorFound.SupplierID = toElement.ElementID;
        connectorFound.Direction = connectorDirection;
        connectorFound.Update();
    }
    return connectorFound;
}

Calling it like this:
Code: [Select]
FindorCreateConnector(fromGUID, toGUID, "association", "", "NAV_Begrep::relatertTil");
Any ideas what is missing?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Adding Connector using API and Manually
« Reply #1 on: February 03, 2017, 02:30:52 am »
No idea, but your creation of the connector is strange, since the 2nd parameter from the addNew is supposed to take the connector type.

Code: [Select]
connectorFound = (EA.Connector)fromElement.Connectors.AddNew("", connectorType);
q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Adding Connector using API and Manually
« Reply #2 on: February 03, 2017, 04:45:11 am »
Try StereotypeEx instead of Stereotype. I believe that usually works a lot better.

Geert

Nabil

  • EA User
  • **
  • Posts: 149
  • Karma: +5/-2
    • View Profile
    • View My LinkedIn Profile Here
Re: Adding Connector using API and Manually
« Reply #3 on: February 03, 2017, 04:32:22 pm »

Code: [Select]
        connectorFound.Stereotype = connectorStereoType;
 

No need to change Stereotype,
Try Adding this line after update, should work

fromElement.Connectors.Refresh();

BR,
Nabil
« Last Edit: February 03, 2017, 04:33:53 pm by nabil_EA_consultant »
Nabil

Eamonn John Casey

  • EA User
  • **
  • Posts: 110
  • Karma: +0/-1
    • View Profile
Re: Adding Connector using API and Manually
« Reply #4 on: February 03, 2017, 06:36:04 pm »
Thanks. Let you know what the solution is.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Adding Connector using API and Manually
« Reply #5 on: February 03, 2017, 08:12:07 pm »
@nabil The Refresh operation just updates the collection after changes and it is needed only if you want to iterate over it in the later program text.

q.

Eamonn John Casey

  • EA User
  • **
  • Posts: 110
  • Karma: +0/-1
    • View Profile
Re: Adding Connector using API and Manually
« Reply #6 on: February 08, 2017, 08:29:21 pm »
Turns out I was over complication Things as usual. Here is the corrected code that does work:

Code: [Select]
public EA.Connector FindorCreateConnector(string fromElementGUID, string toElementGUID, string connectorDirection, string connectorStereoType)
{
EA.Element fromElement = eaRepository.GetElementByGuid(fromElementGUID);
EA.Element toElement = eaRepository.GetElementByGuid(toElementGUID);
EA.Connector connectorFound = null;

foreach (EA.Connector connector in fromElement.Connectors) {
if (connector.SupplierID == toElement.ElementID) {
connectorFound = connector;
}
}
if (null == connectorFound) {
connectorFound = (EA.Connector)fromElement.Connectors.AddNew("", connectorStereoType);
connectorFound.SupplierID = toElement.ElementID;
connectorFound.Direction = connectorDirection;
connectorFound.Update();
}
return connectorFound;
}