Book a Demo

Author Topic: Merge two elements  (Read 4972 times)

gfranz

  • EA User
  • **
  • Posts: 37
  • Karma: +0/-0
    • View Profile
Merge two elements
« on: September 24, 2013, 05:41:52 pm »
Hello

Has someone tried to merge two elements, which actually represent  the same? Let's say there are ClassA and ClassB and each one has relations to other elements in the model. But they are actually the same. What needs to be done in code to link all relations and diagram appearances of ClassB to ClassA without loss of information?

Thanks!

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: Merge two elements
« Reply #1 on: September 24, 2013, 06:57:09 pm »
Hi,

it has to be something like that (a little merge function for excatly this topic  ;D ):


Code: [Select]
private void button26_Click(object sender, EventArgs e)
        {
            //Warning about Merge!
            if (MessageBox.Show("Merge-Function has NO possibility to undo it! The topmost Element will become the master", "Warning! Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.No)
            {
                return;
            }
            //Select the elements
            EA.Collection severals = Repo.GetTreeSelectedElements();
            EA.Element leader = severals.GetAt(0);

            //check for same type
            foreach (EA.Element elem in severals)
            {
                if (elem.Type == leader.Type)
                {
                    //Everything OK ->proceed :-)
                }
                else
                {
                    MessageBox.Show("Selected Elements are not of the same Type", "Failure");
                }
            }

            //move information to leader


            //move the connectors to the leading element
            foreach (EA.Element elementconnectors in severals)
            {
                foreach (EA.Connector con in elementconnectors.Connectors)
                {
                    if (con.ClientID == elementconnectors.ElementID)
                    {
                        con.ClientID = leader.ElementID;
                        con.Update();
                    }
                    if (con.SupplierID == elementconnectors.ElementID)
                    {
                        con.SupplierID = leader.ElementID;
                        con.Update();
                    }
                }
            }
        }

Best regards

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website

gfranz

  • EA User
  • **
  • Posts: 37
  • Karma: +0/-0
    • View Profile
Re: Merge two elements
« Reply #2 on: October 01, 2013, 04:07:30 pm »
Hello,

thank you very much for your reply.

Your code seems to move relations. It does not clean up diagrams and remove the duplicate class? So this must be done manually.

Thanks!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Merge two elements
« Reply #3 on: October 01, 2013, 05:17:26 pm »
Or you improve above code. What you need can be done via API calls.

q.

Stefan Bolleininger

  • EA User
  • **
  • Posts: 308
  • Karma: +0/-0
    • View Profile
Re: Merge two elements
« Reply #4 on: October 01, 2013, 08:03:31 pm »
I don't have the heart to kill elements  ;D ;D

By removing the classes from the model, the diagrams containing it will be cleaned up by themselves :-)

Regards

Stefan
Enterprise Architect in "safetycritical development" like medical device industry. My free Add-in at my Website