Book a Demo

Author Topic: retrive from diagram class,componenet,package  (Read 6052 times)

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
retrive from diagram class,componenet,package
« on: April 29, 2012, 05:21:11 am »
Hi!
I have to retrieve from a diagram that i have in input only elements of the type class, component and package. I made it with the following code:

 public void buildMDG(MDG graph)
        {
            List<Node> elementsCol = null;
            List<Node> packagesCol = null;
            List<Link> connectorsCol = null;
            ArrayList nodeList = new ArrayList();
          
            Node thisNode = null;

            //inizializzazione nodi

            foreach (EA.Package pack in modelRepository.Models)
            {
                foreach (EA.Package inpack in pack.Packages)
                {
                    ArrayList nodespackage = new ArrayList();
                    nodespackage.Add(inpack.Element);

                    foreach (EA.Element elempack in nodespackage)
                    {
                        nodesList.Add(elempack);
                    }
                }
            }
          
  
            for (int i = 0; i < graph.getNodesList().Count; i++)
                nodesList.Add(graph.getNodesList());

            while (nodesList.Count > 0)
            {
                thisNode = nodesList[0] as Node;
                nodesList.RemoveAt(0);

                if (thisNode.GetType().Equals("Package"))
                {
                    packagesCol.Add(thisNode);
                    elementsCol.Add(thisNode);
                }
                else if (thisNode.GetType().Equals("Element"))
                {
                    packagesCol.Add(null);
                    elementsCol.Add(thisNode);
                }

                foreach (EA.Element element in elementsCol)
                {
                    if ((element.Type == "Class") || (element.Type == "Component") || (element.Type == "Package"))
                    {
                        nodesList.Add(element as Node);
                      
                    }
                }

                if (packagesCol != null)
                {
                    foreach (EA.Package package in packagesCol)
                    {
                        nodesList.Add(package as Node);
                    }
                }
            }


but i get all the time the error "object reference not set to an instance of an object" for every value in every "foreach". Can someone help me? i don't get it why i get this error for every value.
Thank you a lot for your time and sorry for the long question.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: retrive from diagram class,componenet,package
« Reply #1 on: April 29, 2012, 11:50:13 pm »
Check whether your EA instance is working right. Just debug print Repository.ConnectionString. Check this also for your other code.

q.

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: retrive from diagram class,componenet,package
« Reply #2 on: April 30, 2012, 12:41:00 am »
Have a look at my answer to the other thread you opened here: http://www.sparxsystems.com/cgi-bin/yabb/YaBB.cgi?num=1335627771

WBR,
Günther
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: retrive from diagram class,componenet,package
« Reply #3 on: April 30, 2012, 03:02:17 pm »
And if you want to retrieve things on a diagram you should loop diagram.DiagramObjects.
Now you are just looping elements in your model.

Geert

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: retrive from diagram class,componenet,package
« Reply #4 on: April 30, 2012, 10:12:59 pm »
Thank you for your help.
Is this any better?
Code: [Select]
public void buildMDG(MDG graph)
        {
            ArrayList diagramList = new ArrayList();
            ArrayList dObject = new ArrayList();
            ArrayList dLink = new ArrayList();

            foreach (EA.Diagram diagram in modelRepository.Models)
            {
                diagramList.Add(diagram);
                foreach (EA.DiagramObject dobj in diagramList)
                {
                    dObject.Add(dobj);
                    foreach (EA.Element elem in dObject)
                    {
                        if ((elem.Type == "Class") || (elem.Type == "Component") || (elem.Type == "Package"))
                        elementList.Add(elem);
                    }
                }

                foreach (EA.DiagramLink dlink in diagramList)
                {
                    dLink.Add(dlink);
                    foreach (EA.Connector conn in dLink)
                    {
                        if ((conn.Type == "Aggregation") || (conn.Type == "Association") || (conn.Type == "Generalization") || (conn.Type == "Realization") || (conn.Type == "Composite") ||
                             (conn.Type == "Depdendency"))
                        {
                            int client = conn.ClientID;
                            if (client != 0)
                             {
                                int supplier = conn.SupplierID;
                                if (supplier != 0)
                                    {
                                    //Arrivato qua i due estremi del connettore fanno parte del mio grafo
                                    //Aggiungo la dipendenza solo se il nodo in cui mi trovo è l'elemento dipendente o se la connessione non è specificata o è bidirezionale
                                    if ((elementList.Contains(client)) && (conn.Direction != "Destination -> Source"))
                                        updateGraph();
                                    else if ((elementList.Contains(supplier) && ((conn.Direction == "Unspecified") || (conn.Direction == "Bi-Directional") || (conn.Direction == "Destination -> Source")))
                                        updateGraph();
                                     connectorList.Add(conn);
                                    }
                               }
                         }
                    }
                }

I have to access the elements and connectors because i have to verify the type of this objects. Because i need only some of them.

Why i get the "InvalidCastException" in each foreach. I look in the diagram of the dependencies and diagram was connected with diagramLink and diagramObject so why i can look into the diagram and took only the diagramLink/diagramObject that i want?
« Last Edit: April 30, 2012, 10:36:51 pm by defi »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: retrive from diagram class,componenet,package
« Reply #5 on: May 01, 2012, 08:33:19 am »
       ArrayList diagramList = new ArrayList();
            ...

            foreach (EA.Diagram diagram in modelRepository.Models)
            {
                diagramList.Add(diagram);
                ...

                foreach (EA.DiagramLink dlink in diagramList)
                {
                    dLink.Add(dlink);

« Last Edit: May 01, 2012, 08:34:00 am by simonm »

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: retrive from diagram class,componenet,package
« Reply #6 on: May 02, 2012, 04:56:22 pm »
sorry, but i don't understand what do you mean with that post.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: retrive from diagram class,componenet,package
« Reply #7 on: May 02, 2012, 05:15:32 pm »
defi,

Seriously, I would first try a basic OO software development course before trying to tackle EA.
Don't try to run before you can walk.

Geert

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: retrive from diagram class,componenet,package
« Reply #8 on: May 03, 2012, 08:52:35 am »
I just highlighted why it was giving you an InvalidCastException.

       ArrayList diagramList = new ArrayList();
            ...

            foreach (EA.Diagram diagram in modelRepository.Models) // 1
            {
                diagramList.Add(diagram); // 2
                ...

                foreach (EA.DiagramLink dlink in diagramList) // 3
                {
                    dLink.Add(dlink);

1. diagram is of type EA.Diagram.
2. diagram is added to diagramList.
3. Everything in diagramList is converted to EA.DiagramLink.
EA.Diagram is not a kind of EA.DiagramLink