Book a Demo

Author Topic: Get DiagramObject by ID  (Read 13794 times)

William McKenzie

  • EA User
  • **
  • Posts: 61
  • Karma: +0/-0
    • View Profile
Get DiagramObject by ID
« on: July 14, 2012, 11:05:22 am »
I am trying to implement the EA_OnPostNewDiagramObject method in my addin.  All it passes in the EventProperties is the ID. But I cant for the life of me find a method to get the actual object using the ID. At the time this method is called, it does not even appear to be in the Diagram.DiagramObjects list. Despite being called On 'Post' NewDiagramObject, it seems to be called before the object is actually added to the diagram collection.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Get DiagramObject by ID
« Reply #1 on: July 14, 2012, 07:57:39 pm »
Try diagrams instead of diagramobjects. The first is the diagram and the latter the element instances appearing inside of diagrams.

q.

William McKenzie

  • EA User
  • **
  • Posts: 61
  • Karma: +0/-0
    • View Profile
Re: Get DiagramObject by ID
« Reply #2 on: July 14, 2012, 10:45:06 pm »
It's the latter that I want, though. I want to get hold of new diagram objects on creation and set some of the graphical things that cannot be easily done in a profile.  According to the documentation:

[highlight]EA_OnPostNewDiagramObject notifies Add-Ins that a new object has been created on a diagram. It enables Add-Ins to modify the object upon creation.

This event occurs after a user has dragged a new object from the Project Browser or Resources window onto a diagram. The notification is provided immediately after the object is added to the diagram.

Set Repository.SuppressEADialogs to true to suppress Enterprise Architect from showing its default dialogs.
[/highlight]

However, if I create an empty drawing,  set a breakpoint at this function, and add a single object, when I reach my breakpoint rep.GetCurrentDiagram.Count is still 0.  And there is no Repository.GetDiagramObjectById, like there is for elements and connectors.

So I have an ID for a diagramobject, but no way of getting the underlying object. Which appears to be a problem in general with the API, never mind my event: the only way that I can see to get to a DiagramObject given its ID is to enumerate the Diagram.DiagramObjects collection and try to find it.

I will report as a bug, I guess.

~Bill

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Get DiagramObject by ID
« Reply #3 on: July 15, 2012, 01:38:33 am »
Sorry, not reading well. You issue
Code: [Select]
rep.GetCurrentDiagram.Count But I guess you mean
Code: [Select]
rep.GetCurrentDiagram.DiagramObjects.Count
q.

William McKenzie

  • EA User
  • **
  • Posts: 61
  • Karma: +0/-0
    • View Profile
Re: Get DiagramObject by ID
« Reply #4 on: July 15, 2012, 01:49:05 am »
Sorry, my bad. Yes, that is the point.

~Bill

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Get DiagramObject by ID
« Reply #5 on: July 15, 2012, 01:52:22 am »
Erm. Means that it's solved or that you simply mis-typed the statement in the post?

q.

William McKenzie

  • EA User
  • **
  • Posts: 61
  • Karma: +0/-0
    • View Profile
Re: Get DiagramObject by ID
« Reply #6 on: July 15, 2012, 02:28:44 am »
Still unsolved. The DiagramObject does not show up in the DiagramObjects collection of the diagram until after the callback has returned. Thus, no way to take advantage of the callback. It is notifying me of the creation of something that is not fully created.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Get DiagramObject by ID
« Reply #7 on: July 15, 2012, 05:33:04 am »
I just tried this for myself. Funny, but EA does not fire the EA_OnPostNewDiagramObject but only the Pre event. How did you get EA to fire it? If I was in need I'd submit a bug report, but current I'm not (I have enough open issues).

q.

P.S. this is what my log shows upon creation of  a new element inside a diagram:

2012-07-14 21:29:11 EA_FileOpen
2012-07-14 21:29:20 EA_OnPreNewElement
2012-07-14 21:29:20 EA_OnPreNewDiagramObject
2012-07-14 21:29:22 EA_OnNotifyContextItemModified
2012-07-14 21:29:23 EA_OnPostNewElement
2012-07-14 21:29:25 EA_OnNotifyContextItemModified

PPS: When dragging an existing element onto a diagram:

2012-07-14 21:34:58 EA_OnNotifyContextItemModified
2012-07-14 21:35:04 EA_OnPreNewDiagramObject
2012-07-14 21:35:06 EA_OnPostNewDiagramObject

EAUI at its best! And definitely a bug.

PPPS: And yes. Nothing is passed to EA_OnPostNewDiagramObject. You should report a bug (or two).

I have also tried "Add related elements" which does not fire any event at all.
« Last Edit: July 15, 2012, 06:15:18 am by qwerty »

William McKenzie

  • EA User
  • **
  • Posts: 61
  • Karma: +0/-0
    • View Profile
Re: Get DiagramObject by ID
« Reply #8 on: July 15, 2012, 08:32:32 am »
I actually was just trying to drag from the toolbox. I'll have to  put together a complete experiment, I guess.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Get DiagramObject by ID
« Reply #9 on: July 15, 2012, 09:30:15 am »
I didn't try that. However, the event is not triggered correctly in many cases. And if, it does not deliver the promised results.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get DiagramObject by ID
« Reply #10 on: July 16, 2012, 03:17:41 pm »
Hmm, which ID is EA passing to you in that event?
I think it's the ElementID rather then the DiagramObjectID

Geert

cendito

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Get DiagramObject by ID
« Reply #11 on: July 16, 2012, 05:35:40 pm »
Get current diagram with Repository.GetCurrentDiagram().

Iterate through curDiagram.DiagramObjects checking for curDiagObj.ElementID equals the parameter value.



qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Get DiagramObject by ID
« Reply #12 on: July 16, 2012, 07:40:50 pm »
For me the OnPostNewDiagramObject received NIL. So I know that something was created, but not what. Anyway it's not triggered in all cases. Bug, bugger, EA.

q.
« Last Edit: July 16, 2012, 07:41:44 pm by qwerty »

William McKenzie

  • EA User
  • **
  • Posts: 61
  • Karma: +0/-0
    • View Profile
Re: Get DiagramObject by ID
« Reply #13 on: July 16, 2012, 10:13:51 pm »
First, yes it is the ElementID, not the DiagramObjectID that is passed to the method. This in itself seems a little backwards to me, since it forces you to do some unnecessary de-referencing to find the DiagramObject in question.

I've tried it all 3 ways now (drag from project, drag from toolbox, right-click on diagram surface and add new). For me, the callback is always invoked, but the state of the diagram is different depending on how I create the new object. I'm doing all of this on an empty drawing.

If I drag an existing object from the project browser, it can at least find it in the diagram; curDiag.SelectedObjects has one thing in it (which is the one I want). So I don't actually have to iterate through the DiagramObjects list to find it. I think it is also the (Count-1)th element in DiagramObjects list.

In the other two case, however, there is nothing in curDiagram.DiagramObjects and nothing in curDiagram.SelectedObjects. They both have a count of 0; it's as if the thing has not yet been created.

To make it worse, it appears that even in the one case where you can get hold of the DiagramObject, you cannot make any changes to its appearance without first saving the diagram. I suspect this is because the API works directly against the database, and not the in-memory model of the diagram.

~Bill

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Get DiagramObject by ID
« Reply #14 on: July 16, 2012, 10:25:05 pm »
This is a real problem. EA keeps changes to the diagram in memory. So when you fiddle around via automation you're getting into the twilight zone. The best is to save before manipulating. But when you are in an event handler that does not work (I remember something where Sparx notes that diagram changes were not permitted in an event handler, but can't recall where). But however you turn it, this is extremely fishy. Myself I would not touch it with a hay fork ... except to proof what a cool hacker I am shipping around all those cliffs.

q.
« Last Edit: July 16, 2012, 10:26:44 pm by qwerty »