Book a Demo

Author Topic: Change connector types in a diagram  (Read 4972 times)

Gomathi

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Change connector types in a diagram
« on: December 22, 2018, 12:50:00 am »
I have a class diagram with several 'Association' connectors between the classes. I would like to change all the connector types to 'Realization'.
Is it possible to change all the connector types at once using any scripts?
Thanks in advance.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Change connector types in a diagram
« Reply #1 on: December 22, 2018, 01:07:59 am »
Yes, it is.

Write a script that acts on the current diagram,

And then
- loop through all Diagram.diagramLinks,
- select the associated Connector
- Change the Type field
- Update() the connector

Reload the diagram to see the changes.

Geert

Gomathi

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Re: Change connector types in a diagram
« Reply #2 on: December 22, 2018, 01:38:18 am »
Thanks for the suggestion, Geert.

I wrote a script as suggested. But, the type is always read as 'undefined'.
Could you please let me know what mistake I'm doing?
Thanks in advance.

Code: [Select]
function main()
{
    Repository.EnsureOutputVisible( "Script" );
    Repository.ClearOutput( "Script" );

    // Get the type of element selected in the Project Browser
    var treeSelectedType = Repository.GetTreeSelectedItemType();

    switch ( treeSelectedType )
    {
        case otPackage :
        {
            // Code for when a package is selected
            var pkg as EA.Package;
            pkg = Repository.GetTreeSelectedObject();

            Session.Output("----------------------------------------");
            Session.Output("Processing... " + pkg.Name);

            for (var i = 0 ; i < pkg.Diagrams.Count; i++)
            {
                var element as EA.Diagram;
                element = pkg.Diagrams.GetAt(i);

                Session.Output("Analyzing : " + element.Name);

                for (var j = 0; j < element.DiagramLinks.Count; j++)
                {

                    var attrib as EA.DiagramLink;
                    attrib = element.DiagramLinks.GetAt(j);
    Session.Output("Connector Type :" + attrib.Type);

                    if (attrib.Type == "Generalization")
    {
attrib.Type = "Realization";
  attrib.Update();
Session.Output("- Changed type :");
    }

                }
            }

            Session.Output("----------------------------------------");
            break;
        }

        default:
        {
            // Error message
            Session.Prompt( "This script does not support items of this type.", promptOK );
        }
    }
}

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Change connector types in a diagram
« Reply #3 on: December 22, 2018, 01:50:27 am »
You forgot the step

- select the associated Connector from the diagramLink

DiagramLink and Connector are different objects (of different types)

Geert

Gomathi

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Re: Change connector types in a diagram
« Reply #4 on: December 22, 2018, 04:40:46 am »
Hi Geert,

Thanks a lot. I found the way to access a connector from diagramLink.
This is the final code which works.

Code: [Select]
function main()
{
    Repository.EnsureOutputVisible( "Script" );
    Repository.ClearOutput( "Script" );

    // Get the type of element selected in the Project Browser
    var treeSelectedType = Repository.GetTreeSelectedItemType();

    switch ( treeSelectedType )
    {
        case otPackage :
        {
            // Code for when a package is selected
            var pkg as EA.Package;
            pkg = Repository.GetTreeSelectedObject();

            Session.Output("----------------------------------------");
            Session.Output("Processing... " + pkg.Name);

            for (var i = 0 ; i < pkg.Diagrams.Count; i++)
            {
                var element as EA.Diagram;
                element = pkg.Diagrams.GetAt(i);

                Session.Output("Analyzing : " + element.Name);

                for (var j = 0; j < element.DiagramLinks.Count; j++)
                {
    var attrib as EA.Connector;
        var diag as EA.DiagramLink;
                    diag = element.DiagramLinks.GetAt(j);
                    attrib = Repository.GetConnectorByID(diag.ConnectorID);
    Session.Output("Connector Type :" + attrib.Type);

    if (attrib.Type == "Association")
    {
attrib.Type = "Realization";
attrib.Update();
Session.Output("- Changed type :" + attrib.Type);
    }

                }
Repository.ReloadDiagram(element.DiagramID);

            }

            Session.Output("----------------------------------------");
            break;
        }

        default:
        {
            // Error message
            Session.Prompt( "This script does not support items of this type.", promptOK );
        }
    }
}

Thanks again for your help.