Book a Demo

Author Topic: Adding elements to diagrams  (Read 7938 times)

minna

  • EA User
  • **
  • Posts: 64
  • Karma: +1/-0
    • View Profile
Adding elements to diagrams
« on: March 17, 2014, 09:12:30 pm »
Hi all,

I have classes in Excel which I add to EA using VBA (with the help of a tutorial from Geert: http://bellekens.com/2013/04/30/simple-vba-excel-to-ea-importer-v3/

The classes are added to the package, but are not added to a diagram. I can then go and paste them to a diagram as a 'link'.

Is there any way to paste theses classes programmatically to the selected EA diagram? I am guessing that one would need to add the positions of the classes in the diagram.

Thanks,
Minna

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 elements to diagrams
« Reply #1 on: March 17, 2014, 09:21:12 pm »
From Diagram.cs

Code: [Select]
/// <summary>
/// adds an element to the diagram
/// </summary>
/// <param name="element">the element to add</param>
/// <returns>the new diagramElement</returns>
public UML.Diagrams.DiagramElement addToDiagram(UML.Classes.Kernel.Element element)
{
      UML.Diagrams.DiagramElement diagramElement = null;
      //first check whether this element is not already added to this diagram
      diagramElement = this.diagramObjectWrappers.FirstOrDefault(x=> x.element.Equals(element));
      if (diagramElement == null)
      {
            if (element is EA.ElementWrapper)
            {
                  //first save the diagram to make sure we don't loos any unsaved changes
                  this.model.saveOpenedDiagram(this);
                  global::EA.DiagramObject newDiagramObject = this.wrappedDiagram.DiagramObjects.AddNew("","") as global::EA.DiagramObject;
                  diagramElement = ((Factory)this.model.factory).createDiagramElement(newDiagramObject) ;
                  diagramElement.element = element;
                  //save the element diagramObject
                  ((DiagramObjectWrapper)diagramElement).save();
                  // now refresh to make sure we see the new element on the diagram
                  this.reFresh();
            }

            else if (!(element.owner is UML.Classes.Kernel.Package))
            {
                  diagramElement = this.addToDiagram(element.owner);
            }
      }
      return diagramElement;
}

Geert

minna

  • EA User
  • **
  • Posts: 64
  • Karma: +1/-0
    • View Profile
Re: Adding elements to diagrams
« Reply #2 on: March 18, 2014, 12:28:22 am »
Thanks

minna

  • EA User
  • **
  • Posts: 64
  • Karma: +1/-0
    • View Profile
Re: Adding elements to diagrams
« Reply #3 on: March 18, 2014, 11:11:59 pm »
Hi,

How to I add elements using Excel's VBA? I seems that the code used in C# is a lot different from the code used in VBA.

Thanks!

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 elements to diagrams
« Reply #4 on: March 18, 2014, 11:22:50 pm »
Using the same steps as in C#.
- create a new diagramObject
- set the properties for you new diagramObject, including the reference to the element
- save the diagram object
- refresh the diagram

In the end the EA API is the same for VB(A) and C#, its just the using code that is different.

Geert

minna

  • EA User
  • **
  • Posts: 64
  • Karma: +1/-0
    • View Profile
Re: Adding elements to diagrams
« Reply #5 on: March 19, 2014, 12:35:06 am »
Thanks. I see what you mean.

I am able to complete all the steps you mentioned except for one. What do you mean by adding a reference to the element? When I create the diagramObject, it only allows me to add the name and type.
(diagram.DiagramObjects.AddNew(Name as string, Type as string))

I am saving my diagramObject with aDiagramObject.Update (since there is no aDiagramObject.Save method).  And the only refresh method for diagram is aDiagram.Update.

Attached is my code. It doesn't want to show the new diagramObject. What am I missing?

Code: [Select]
   Dim aDiagram As EA.Diagram
    Dim aDiagramObject As EA.DiagramObject
    
    Set aDiagram = Me.getCurrentDiagram
    
    Set aDiagramObject = aDiagram.DiagramObjects.AddNew("theName", "Class")
    
    aDiagramObject.Update
        
    aDiagram.DiagramObjects.Refresh    
    aDiagram.Update

Thanks.

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 elements to diagrams
« Reply #6 on: March 19, 2014, 12:54:56 am »
How do you expect your diagramObject to be able to show your element if you don't tell it which element it should show?

The example I gave you works completely. You just have to look at the implementations of the operations called.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Adding elements to diagrams
« Reply #7 on: March 19, 2014, 01:32:32 am »
Beware of vampires...


q.