Book a Demo

Author Topic: Add existing element to diagram  (Read 7533 times)

michael.jeier

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Add existing element to diagram
« on: March 19, 2009, 12:26:23 am »
Hi,

I'm trying to add an existing use case to a diagram, but unfortunately it seems that one can only add new elements to a diagram:

Code: [Select]
EADiagram.DiagramObjects.AddNew(string Name, string Type);

Is there any way?? Any help on this would be appreciated - thanks!

Regards, Michael

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Add existing element to diagram
« Reply #1 on: March 19, 2009, 11:25:04 am »
The following code can be used to add an existing element to a diagram:

Code: [Select]
Set obj = diagram.DiagramObjects.AddNew("", "")
obj.ElementID = element.ElementID
obj.Update

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Add existing element to diagram
« Reply #2 on: March 19, 2009, 11:13:09 pm »
Michael,

It helps to get your head around the way EA does this. What you are adding to the diagram is not the element itself, but a DiagramObject (i.e. an depiction of the element). This diagram object requires a pointer to an element - that tells EA which element to portray - which is the ElementID you must provide before you call Update().

This way EA can have multiple references to the 'real' element, on several diagrams. These depictions can be added to (and removed from) the model without affecting the 'real' element itself. This also allows EA to maintain the element in isolation from any given depiction. If you change the 'real' element then it is changed everywhere.

HTH, David

PS: The same holds true for connectors. You will find a DiagramLink class in the SDK. Use this class to add a connector to a diagram. Note that EA 'helps' you with connectors, so it is not always necessary to add them directly. But if you need to manipulate the connector on a given diagram, or do something like hiding labels, then you need to work with the DiagramLink class.
« Last Edit: March 19, 2009, 11:15:44 pm by Midnight »
No, you can't have it!

michael.jeier

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Add existing element to diagram
« Reply #3 on: March 24, 2009, 01:58:53 am »
Thank you both for your helpful insights. Unfortunately, the suggest approach won't work, because the field "ElementID" is read only. :(

I tried this:
Code: [Select]
EA.Element obj = (EA.Element) newDiagram.DiagramObjects.AddNew("", "");
obj.ElementID = elmUseCase.ElementID;
obj.Update();

The field Element GUID is also read-only. I'm assuming there must be another way, but how??

Regards, Michael

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Add existing element to diagram
« Reply #4 on: March 24, 2009, 03:10:16 am »
Don't panic Michael,

You've almost got it, but there is still one detail left. You are still casting the result of the first line as an EA.Element. You need to cast it as an EA.DiagramObject.

Once you do that you should be fine. Just to be sure, please post back and close the loop either way.

David
No, you can't have it!

michael.jeier

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Add existing element to diagram
« Reply #5 on: March 24, 2009, 03:43:31 am »
D'oh! Well of course it's a DiagramObject. Thanks a lot!! :) That does the trick.

Btw, can I make a composite out of this use case and diagram? I searched the API, but didn't find anything for this.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Add existing element to diagram
« Reply #6 on: March 24, 2009, 03:57:07 am »
Yes you can, though I don't know all the details from memory. The 'rules' here a somewhat obscure.

Set the Element.SubType property to 8 (that's a Long variable). This works for element types where Composite makes sense. AFAIK there is no 'official' list of these. Use common sense backed up with testing on an EA model by hand.

You will also need to set up a child diagram for your composite element. EA provides a CompositeDiagram property but this is read-only; your element has to already have a designated diagram for this to work. AFAIK EA just chooses the first diagram in the element's Diagrams collection. Since diagrams don't have a position index property - EA uses various names for this feature of several classes, but does not appear to handle it at all for elements and diagrams, among others - you might just have to ensure that the correct diagram is added to the list first. This might involve clearing, refreshing, and then reloading the list. You'll have to test. [And post back here of course, to tell us all what actually happens.]

Read up a little on the Diagram class and you should be OK. Remember to set the ParentID of the child diagrams to the ElementID of the parent element. Don't change the PackageID for this purpose. That attribute will usually point to the package that owns the parent element - or not, if you have a more complex hierarchy.

HTH, David
No, you can't have it!

michael.jeier

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Add existing element to diagram
« Reply #7 on: March 24, 2009, 08:43:32 pm »
Thanks again! I tried to make a composite out of the use case and the diagram, but it didn't quite work.

You actually get a composite when you just do this:
Code: [Select]
elmUseCase.Subtype = 8;

BUT: it is only a composite in so far that when you right-click the use case in a diagram the "Composite" property is checked under "Advanced". It does not open the corresponding diagram when you double-click it and it also doesn't show the little "composite icon" in the use case bubble.

So I also tried to set the parent of the diagram, but this doesn't have any effect.
This is my code so far:
Code: [Select]
// create the use case
EA.Element elmUseCase = (EA.Element)package.Elements.AddNew(strUseCaseName, "Use Case");
 elmUseCase.Update();

// create the use case diagram
 EA.Diagram newDiagram = (EA.Diagram) elmUseCase.Diagrams.AddNew(strUseCaseName, "Use Case");
newDiagram.ShowDetails = 0;
newDiagram.Update();

// add the use case to the diagram
EA.DiagramObject obj = (EA.DiagramObject)newDiagram.DiagramObjects.AddNew("", "");
obj.ElementID = elmUseCase.ElementID;
obj.Update();

// TODO make a composite
elmUseCase.Subtype = 8;
newDiagram.ParentID = elmUseCase.ElementID;
newDiagram.Update();
//elmUseCase.ParentID = newDiagram.DiagramID;
elmUseCase.Update();

Does anyone has an idea what I'm doing wrong?  :-/

Regards, Michael

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Add existing element to diagram
« Reply #8 on: March 24, 2009, 11:14:55 pm »
You seem to be trying to add the parent use case to the child diagram. This seems a bit strange.

Try changing the later diagram object to use the element ID of the child use case.
No, you can't have it!

michael.jeier

  • EA Novice
  • *
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: Add existing element to diagram
« Reply #9 on: April 07, 2009, 03:54:39 am »
Actually, this is exactly what I want. I want a use case, which contains a diagram. This makes up my composite. Then I'm also adding the use case to the underlying diagram. So when I double-click the use case I arrive at the diagram of that use case.
I played around with the code a little more, but it seems that this is a bug in the API as it creates a composite, but it is not shown as composite and doesn't work like a composite (as I mentioned in my post before).
 :(

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Add existing element to diagram
« Reply #10 on: April 07, 2009, 10:49:31 pm »
Follow through all the parts; it will work eventually!

Check the database itself to make sure you are setting all the requisite parts. Try adding a composite use case by hand first, so you will have an example.

You can read an EAP database by making a copy and opening it with MS Access. Use the copy since Access might want to convert the file, which can cause problems for EA later. You can also use a DBMS repository for your project and read it via whatever tools you normally would use for that database engine.

David
No, you can't have it!

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8607
  • Karma: +257/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Add existing element to diagram
« Reply #11 on: May 29, 2009, 12:33:12 pm »
Hi Michael and David (and anyone who can help),

I'm having essentially the same problem as Michael.  I want to create a "Composite" diagram below an element.  I can create the diagram and set the "bits" (as Michael has done).  However, (I presume) like him I don't get:
  • The "chain links" glyph on the bottom of the "composite" element.
  • The navigation to the "Composite" diagram from the parent element (on double-click)
  • element.CompositeDiagram to NOT return NULL
Does anybody know the EXACT sequence of steps to achieve the above?
Why isn't there an API call to "Make Composite".

[update]Oh... I forgot.  element.CompositeDiagram returns NULL even for a manually created "Composite".  And it's broken on build 845.[/update]

TIA,
Paolo
(Who won't utter the "C" word that dare not speak it's name...)
« Last Edit: May 29, 2009, 12:40:13 pm by PaoloFCantoni »
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!