Author Topic: Add Diagram Note To Diagram programmatically - how to?  (Read 2711 times)

Ribeye

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Add Diagram Note To Diagram programmatically - how to?
« on: December 07, 2023, 02:57:10 pm »
I need my plugin to apply a stereotype to new diagrams. For that it needs to add a Diagram Note element to the diagram.

To add a Diagram Note element I am following
* Add Text element to a Diagram: the Scripting EA book, section "Adding special objects" to diagrams.
* Make a text element a Diagram Note: https://sparxsystems.com/forums/smf/index.php?topic=37907.0

My resulting code is:

        public static void addDiagramNoteToDiagram(Diagram diagram, Package myPackage, Repository repository)
        {
            Element text = myPackage.Elements.AddNew("", "Text");
            String vbCrLf = ((char)13).ToString() + ((char)10).ToString();
            String nnn = "Name: " + diagram.Name + vbCrLf + "Author: " + diagram.Author + vbCrLf + "Version: " + diagram.Version + vbCrLf + "Created: " + diagram.CreatedDate + vbCrLf + "Updated: " + diagram.ModifiedDate;
            text.Notes = nnn;
            text.Subtype = 18;
            text.Update();
            string pos = "l=200;r=300;t=-20;b=-40";
            dynamic dia_obj = diagram.DiagramObjects.AddNew(pos, "");
            diagram.Update();
            dia_obj.ElementID = text.ElementID;
            dia_obj.Update();
            repository.ReloadDiagram(diagram.DiagramID);
        }
       
The text Element is created fine.

but nothing is added to diagram.DiagramObjects by the line

   dynamic dia_obj = diagram.DiagramObjects.AddNew(pos, "");

When walking through in debug mode, diagram.DiagramObjects.count remains at 0 at all times.

When I open the diagram the note element is showing, which I dont get.

But my code which gets the Diagram Note does not find it.

        // get the diagram element which will hold the Stereotype
        public Element getMyDiagramsElement(Repository repository)
        {
                Collection diagramObjects = myElement.DiagramObjects; // myElement is a Diagram property of the non static class
                foreach (DiagramObject o in diagramObjects)
                {
                    int eid = o.ElementID;
                    Element element = repository.GetElementByID(eid);
                    if (element.MetaType.Contains("Text"))
                    {
                        return element;
                    }
                }
                return null;
            }

        }

When the Diagram Note is added manually, through the front end, this code works fine.

Why does getMyDiagramsElement() work for manually added Diagram Notes and not for the addDiagramNoteToDiagram() method way?

Note: If I re-run the code it looks like the Diagram Note is there? So I tried adding
       repository.SaveDiagram(diagram.DiagramID);
at various places, as suggested by Scripting EA, but could not make it make a difference.

« Last Edit: December 07, 2023, 03:54:46 pm by Ribeye »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Add Diagram Note To Diagram programmatically - how to?
« Reply #1 on: December 07, 2023, 09:19:08 pm »
Hmm, what exctly is your issue? (sorry, I just got out of bed, but I cant see it.)

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13241
  • Karma: +554/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add Diagram Note To Diagram programmatically - how to?
« Reply #2 on: December 08, 2023, 05:38:16 am »
have you tried removing the diagram.update()

That statement shouldn't be there anyway.

Geert

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13241
  • Karma: +554/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Add Diagram Note To Diagram programmatically - how to?
« Reply #3 on: December 08, 2023, 05:40:00 am »
If you update the diagramObjects collection of your diagram, you won't see any new elements untill you do a refresh() on the collection.

Geert