Book a Demo

Author Topic: Create a diagram from a UML element  (Read 6672 times)

Henrique Narciso

  • EA User
  • **
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Create a diagram from a UML element
« on: July 23, 2009, 07:21:58 pm »
Hello,

I need some pointers to develop an automatism to do the following.

Select an UML element in the tree browser/ or anywhere else;
create a diagram with the name of the UML element;
Put in the diagram all the related elements up to one level, links included;

Thanks in advance!

smendonc

  • EA User
  • **
  • Posts: 148
  • Karma: +5/-0
  • I love YaBB 1 Gold!
    • View Profile
Re: Create a diagram from a UML element
« Reply #1 on: July 24, 2009, 04:53:23 pm »
You can create an empty diagram, drag the element you want to find the neighbors for onto the diagram.  Right click the element on the diagram and choose Add-->Related Elements from the context menu.  This will allow you to choose related elements to a level of depth you specify (5 max I think), along with other options like constraining relationship types etc.

I'm not sure if this functionality is available via the automation interface, there appears to be an Element.GetRelationSet method that looks promising but I've not used it.

Stan.

Henrique Narciso

  • EA User
  • **
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Re: Create a diagram from a UML element
« Reply #2 on: July 24, 2009, 06:39:13 pm »
Hello,

your description is the one I do manually, I would like to do this automatically.

so perhaps what I am asking is for the commands of:
Create diagram(selected element name);
insert element on diagram(selected element);
and the add related elements(level depth = 1);

Thanks in advance

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Create a diagram from a UML element
« Reply #3 on: July 27, 2009, 10:35:48 am »
Quote
Hello,

your description is the one I do manually, I would like to do this automatically.

so perhaps what I am asking is for the commands of:
Create diagram(selected element name);
insert element on diagram(selected element);
and the add related elements(level depth = 1);

Thanks in advance
Hi Enrique,

A couple of questions before we can fully answer your questions...

1) Where do you want to place the diagram (at package or element level)
2) What are you going to do if you already have a diagram with the same name (and type) in the same place?
3) There is no "Add Related Elements" API call (I think).  But you can iterate the Connectors of the vertex you are placing on the diagram.

We found it to be NOT straight forward but it was possible.

We have designed a small number of "autoupdatable" diagrams (such as what we call a "Context Diagram" - beneath the selected vertexes, add a diagram which identifies the "Contextual Vertex", find a series of related vertexes (according to various filters) and then add a fair bit of metadata to allow an automated process to come along later and keep the diagram up to date.

Some of the stuff we do is a bit weird, but appears necessary for EA to provide the outcomes we needed.  However, we haven't, for example, figured out how to get the little chain-link symbol on the contextual vertex and automatically open the diagram when the contextual vertex is double-clicked in other diagrams...

Paolo
« Last Edit: July 27, 2009, 10:36:26 am by PaoloFCantoni »
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Henrique Narciso

  • EA User
  • **
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Re: Create a diagram from a UML element
« Reply #4 on: August 11, 2009, 09:30:20 pm »
Quote
Hi Enrique,

A couple of questions before we can fully answer your questions...

1) Where do you want to place the diagram (at package or element level)
2) What are you going to do if you already have a diagram with the same name (and type) in the same place?
3) There is no "Add Related Elements" API call (I think).  But you can iterate the Connectors of the vertex you are placing on the diagram.

...

Paolo

hello Paolo,

here are my answers:
1) at the package level
2) ask if the existant should be replaced
3) whatever works is fine :)

Thanks in advance!

Best regards,
Henrique

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Create a diagram from a UML element
« Reply #5 on: August 12, 2009, 12:51:14 pm »
This should help get you started....  However, please check the EA Help system under SDK for Enterprise Architect|Enterprise Architect Object Model|Reference

Code: [Select]
EA.Package EAPackage = (some means to get the package);
//Do we already have the diagram?
EA.Diagram EADiagram = (EA.Diagram)EAPackage.Diagrams.GetByName("<diagram name>");


//Add a new diagram
EADiagram = (EA.Diagram)EAPackage.Diagrams.AddNew("<diagram name>", "<diagram type>");

EA.DiagramObject EADiagramObject = (EA.DiagramObject)EADiagram.DiagramObjects.AddNew("<Coordinates string>", "");
EADiagramObject.ElementID = EARootElement.ElementID;      //Assign it to an object in the model
            
//Add elements related to an existing element
for (short i = 0; i < EARootElement.Connectors.Count; i++)      //Pick a "Root" element to find the "Related Elements" for
{
      EA.Connector Connector = (EA.Connector)EARootElement.Connectors.GetAt(i);
      if (Connector.SupplierID != EARootElement.ElementID)
      {
            EA.DiagramObject EADiagramObject = (EA.DiagramObject)EADiagram.DiagramObjects.AddNew("<Coordinates string>", "");
            EADiagramObject.ElementID = Connector.SupplierID;
            //Do some stuff
            EADiagramObject.Update();
      }

      if (Connector.ClientID != EARootElement.ElementID)
      {
            EA.DiagramObject EADiagramObject = (EA.DiagramObject)EADiagram.DiagramObjects.AddNew("<Coordinates string>", "");
            EADiagramObject.ElementID = Connector.ClientID;
            //Do some stuff
            EADiagramObject.Update();
      }      
}

EADiagram.Update();      //ALWAYS update after making changes



HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Henrique Narciso

  • EA User
  • **
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Re: Create a diagram from a UML element
« Reply #6 on: September 02, 2009, 12:58:55 am »
Hello,

so far my code is as goes...
I'm having trouble adding the element to the diagram, the diagram is created, but the creation of the elements inside the diagram is giving me some trouble.


        Object obj = null;
        ObjectType o = Repository.GetTreeSelectedItem(out obj);
        if (o == EA.ObjectType.otElement)
        {
            EA.Element e = (EA.Element)obj;
            EA.Package EAPackage = Repository.GetTreeSelectedPackage();

            EA.Diagram EADiagram = (EA.Diagram)EAPackage.Diagrams.AddNew(e.Name, "Maintenance");

            EA.DiagramObject EADiagramObject = (EA.DiagramObject)EADiagram.DiagramObjects.AddNew(e.Name, "change");
            EADiagramObject.ElementID = e.ElementID;      //Assign it to an object in the model
            EADiagramObject.Update();
            EADiagram.Update();      


«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Create a diagram from a UML element
« Reply #7 on: September 02, 2009, 07:00:05 am »
Hi Henrique,

You're very close, but not quite there. It looks like you are not saving the diagram before adding stuff to it. You should be calling...

 EADiagram.Update();

...before attempting to add the diagram object. My guess is that your code is throwing an exception at that point.

HTH, David

PS: You can get away without calling Update again at the end, since you've not changed the actual properties of the diagram itself since you created it. But if you will need to retrieve a reference to any newly-created diagram objects (via GetAt or such) then you should call EADiagram.DiagramObjects.Refresh() once you've finished adding stuff. This same pattern holds true for collections throughout EA.
No, you can't have it!

Henrique Narciso

  • EA User
  • **
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Re: Create a diagram from a UML element
« Reply #8 on: September 02, 2009, 08:01:20 pm »
thank you midnight!

I got it working!

Had some trouble with the layout, so I leave it here so it can be usefull to others.

Once the diagram is done...

String diagGuid = EADiagram.DiagramGUID;
EA.Project proj = Repository.GetProjectInterface();
proj.LayoutDiagramEx(diagGuid,1,4,20,20,false);
« Last Edit: September 02, 2009, 08:24:30 pm by hnarciso »

Henrique Narciso

  • EA User
  • **
  • Posts: 86
  • Karma: +0/-0
    • View Profile
Re: Create a diagram from a UML element
« Reply #9 on: September 03, 2009, 01:17:59 am »
once again.

the code seems to work fine for the first created diagram, when I try to create the second I get an error in the "does diagram exist?" validation.

Object EADiagramexist = EAPackage.Diagrams.GetByName(e.Name);
if (EADiagramexist != null) {

On the first line a Index out of bounds comes up.


«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Create a diagram from a UML element
« Reply #10 on: September 03, 2009, 11:44:34 pm »
See my earlier post and check the later caution.

Call Refresh() on the Diagrams collection of the owning Package (or Diagram, or whatever) object before you try to retrieve the diagram reference. Until you call Refresh() on an EA collection the internal item list is not reloaded.

David
No, you can't have it!