Book a Demo

Author Topic: Adding a diagram through code  (Read 5905 times)

MalkContent

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Adding a diagram through code
« on: February 27, 2010, 04:28:46 am »
Hi.
I am having some problems adding a diagram to my model by code.
The diagram is added in the "EA_OnPostNewMethod" method and is added as a child of a class element.

This is my code:
Code: [Select]
                               EA.Diagram fledgedDiag = (EA.Diagram)(methparent.Diagrams.AddNew(meth.Name, "MOFSDM::mofsdmDiagram"));
                                fledgedDiag.Update();
                                methparent.Diagrams.Refresh();
                                methparent.Refresh();
                                package.Diagrams.Refresh();
                                package.Elements.Refresh();

methparent is the class element which the method is added to, meth is the new method and package is the package which methparent is located in.

Now to my problem:
It works - but it keeps stating the error "An invalid argument was encountered.".
The code which seems to be responsible for this error is "fledgedDiag.Update()" but the code doesn't work without it.

Did I mess up the order of refreshing/updating or did I forget something?

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 a diagram through code
« Reply #1 on: March 01, 2010, 03:30:45 pm »
Code seems valid to me, the only "out of the ordinary" is the type of the diagram.
Have you tried this code with a "normal" type?

If you have the same error with a normal diagram type then the problem might have something to do with a field that hasn't been filled in yet.
The "update" statement will probably call an operation that takes some parameters. I'm guessing one of them is still at "null", which causes the error.
Try setting as much attributes of the diagram as you can before updating. (start with the parent id or package id)

Geert

PS. You are doing a bit too much refreshes I think. Refresh only updates the collections in memory. The only refresh you should do here is the Element.Diagrams (and maybe the Package.Diagrams) but only if you need those collections later on in the program.
Refresh can't hurt, but its a waist of cpu cycles if nothing has changed to that collection.

MalkContent

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: Adding a diagram through code
« Reply #2 on: March 03, 2010, 03:46:07 am »
hm, good point
I will look into that

MalkContent

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: Adding a diagram through code
« Reply #3 on: March 03, 2010, 05:54:01 am »
nope, still happens
tried it out with an "activity" type

MalkContent

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: Adding a diagram through code
« Reply #4 on: March 03, 2010, 09:11:08 pm »
heres the whole method for you
I don't know whats left to try
The Problem still happens, even when I quote out everything after the diagram.update() (note that the code is fully functional, it just happens that EA crashes from time to time and always prompts the error; when it crashed, >every< changes I wanted to do are done and still there after restarting EA).

Code: [Select]
       public bool EA_OnPostNewMethod(EA.Repository Repository, EA.EventProperties Info)
        {
            EA.Method meth = Repository.GetMethodByID(int.Parse((string)Info.Get(0).Value));
            EA.Element methparent = Repository.GetElementByID(meth.ParentID);
            methparent.Refresh();
            if (methparent.Type.Equals("Class"))
            {
                EA.Package pack = Repository.GetPackageByID(methparent.PackageID);
                foreach (EA.Diagram diag in pack.Diagrams)
                    if(diag.MetaType.Equals("MOF Diagram::MOF Diagram"))
                    {
                        foreach(EA.DiagramObject diagobj in diag.DiagramObjects)
                            if (Repository.GetElementByID(diagobj.ElementID).ElementGUID.Equals(methparent.ElementGUID))
                            {
                                EA.Element fledgedDummy = (EA.Element)(methparent.Elements.AddNew(meth.Name, "Activity"));
                                fledgedDummy.Stereotype = "mofsdmDummyActivity";
                                fledgedDummy.StereotypeEx = "mofsdmDummyActivity";
                                fledgedDummy.Update();
                                EA.Diagram fledgedDiag = (EA.Diagram)(fledgedDummy.Diagrams.AddNew(meth.Name, "MOFSDM::mofsdmDiagram"));
                                fledgedDiag.ShowDetails = 0;
                                fledgedDiag.Update();
                                EA.Element fledgedStart = (EA.Element)fledgedDummy.Elements.AddNew("Start", "StateNode");
                                fledgedStart.Stereotype = "mofsdmStart";
                                fledgedStart.StereotypeEx = "mofsdmStart";
                                fledgedStart.Update();
                                EA.DiagramObject fledgedDObj = (EA.DiagramObject)fledgedDiag.DiagramObjects.AddNew("Start", null);
                                fledgedDObj.ElementID = fledgedStart.ElementID;
                                //fledgedDObj.left = 500;
                                //fledgedDObj.top = 100;
                                fledgedDObj.Update();
                                break;
                            }
                        return true;
                    }
            }
            return false;
        }

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 a diagram through code
« Reply #5 on: March 03, 2010, 09:42:39 pm »
have you tried setting the diagram.PackageID and ParentID before updating?

Geert

MalkContent

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: Adding a diagram through code
« Reply #6 on: March 03, 2010, 10:23:05 pm »
they already are set after using the element.diagrams.addnew(..) method
:/
also: they are set correclty ^^
Diagram.ParentID == ParentActivity.ElementID
Diagram.PackageID == ParenActivity.PackageID

both true

I have no idea what this is - I can add elements all I like without causing any trouble, but as soon as I add an element AND update it - BAM

EDIT:

I have the feeling it has to do with some complications between the Diagram Creation and the Method Creation process, but I am not sure.
When the Error occurs, the Element holding the new Operation does not refresh its Element.Methods collection by itself, the new Operation only appears in the treeview and cant be seen on the DiagramElement OR in the ElementOperationlist when rightclicking(in treeview or on diagram is irrelevant) and choosing "Operations".
Also the operations form is closed when the Error occurs.

Perhaps I am lucky enough to run into a bug again here ^^
Yay for me.
I am using EA v7.1.833
« Last Edit: March 03, 2010, 10:34:25 pm by MalkContent »

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 a diagram through code
« Reply #7 on: March 03, 2010, 10:47:14 pm »
I would try it on 7.5.850 or 8.0 (beta2).
I remember that there were a few bugs when adding (and saving) elements trough the API.

Geert