Book a Demo

Author Topic: Changing an interaction diagram element via scripting  (Read 8581 times)

kjourdan

  • EA User
  • **
  • Posts: 71
  • Karma: +0/-0
    • View Profile
Changing an interaction diagram element via scripting
« on: April 04, 2018, 03:18:16 am »
I want to replace an element A on an interaction diagram with another element A' via scripting.  I am able to identify element A on the diagram through DiagramObjects. With the identified element A, I have its element ID.  I also have identified element A' and have its element ID.  Through the scripting, I am able to change the diagram links to now connect to element A'. 

If I change the element ID referenced by the diagram object to use the ID for A', the diagram still shows the old element A but the links are removed.  If I drop A' on the diagram, it appears with the links. 
   var diagramObjPtr = diagram.DiagramObjects;
   var diagramObj as EA.DiagramObject
   diagramObj = diagramObjPtr.GetAt(obj_index)
   diagramObj.ElementID = newObj.ElementID
   diagramObj.Update()

I do not want to remove the object and add it; I would like to just change the associated object on the diagram.

Any  idea what I am doing wrong such that the old element is still visible on the diagram and the new element is not.

Thanks

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #1 on: April 04, 2018, 05:39:27 am »
Space for guessing... Does the new element already have relations to elements on the diagram? Have you refreshed the diagram (you need to save it after such a change and issue Repository.ReloadDiagram).

q.

kjourdan

  • EA User
  • **
  • Posts: 71
  • Karma: +0/-0
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #2 on: April 04, 2018, 07:40:21 am »
At the end of the script, I also have logic to update, save and reload the diagram but still the problem exists.
   diagram.Update()
   Repository.SaveDiagram(diagram.DiagramID)
   Repository.ReloadDiagram(diagram.DiagramID)

Prior to running the script, the old element has two diagram links on the diagram.  After execution, the old element has no links but still exists on the diagram.  When I manually add the element to the diagram, it shows up with the two links; these links did not exist prior to execution of the script.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #3 on: April 04, 2018, 08:20:14 am »
I'll look into that tomorrow...

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: Changing an interaction diagram element via scripting
« Reply #4 on: April 04, 2018, 01:28:18 pm »
At the end of the script, I also have logic to update, save and reload the diagram but still the problem exists.
   diagram.Update()
   Repository.SaveDiagram(diagram.DiagramID)
   Repository.ReloadDiagram(diagram.DiagramID)

Prior to running the script, the old element has two diagram links on the diagram.  After execution, the old element has no links but still exists on the diagram.  When I manually add the element to the diagram, it shows up with the two links; these links did not exist prior to execution of the script.
No need to save the diagram I think.
Since you are seeing no links on the diagram, your code probably did what it was supposed to.
And if you can drag the "old" element back on the diagram then you are sure that it is not on there anymore. EA won't allow you to add an element to a diagram twice.

Geert

kjourdan

  • EA User
  • **
  • Posts: 71
  • Karma: +0/-0
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #5 on: April 04, 2018, 08:44:28 pm »
Hi Geert.  After the script runs, the old element still exists on the diagram instead of the new one.  Manipulating the diagram links to point to the new element works but changing the diagram element itself to use the new element ID does not result in the new element appearing on the diagram; the old element still exists and I must manually remove the old element and add the new one.  The script is intended to replace all occurrences of element A on diagrams with element A'

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an interaction diagram element via scripting
« Reply #6 on: April 04, 2018, 10:22:20 pm »
Have you tried running the script and then opening the diagram?

Just to rule out the possibility that the diagram is showing a "cached" version of the truth.
If you change the ElementID of the diagramObject and then update it, then it is changed and it should become apparent when opening the diagram.

If you are still seeing the old element on the diagram instead of the new then you should carefully debug the code to see what is going wrong.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #7 on: April 05, 2018, 07:38:07 am »
Again, sorry for the Perl, but this one worked as expected:

Code: [Select]
my $dia = $rep->GetCurrentDiagram();
my $oldGuid = "{5931B85C-6540-4cad-B4F9-133BF8AE23CD}";
my $newGuid = "{55688F78-8A2F-439b-98A9-E0A2746F1BA5}";
my $e = $rep->GetElementByGUID($newGuid);
$dia->DiagramObjects->Delete(2);
$dia->Update();
my $do = $dia->DiagramObjects->AddNew("l=300;r=400;t=-400;b=-470", "");
$do->{ElementID} = $e->ElementID;
$do->Update();
$rep->ReloadDiagram($dia->DiagramID);

q.

kjourdan

  • EA User
  • **
  • Posts: 71
  • Karma: +0/-0
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #8 on: April 05, 2018, 09:30:39 pm »
Hi Geert. I ran the scripts with the diagram closed and it makes no difference. When I run the script the 1st time, it finds element A with 2 links.  The links to element A are changed successfully to element A'. Changing the diagram object to use element A' does not appear to work.  When the script is run a second time, it finds element A again but there are no links that need to be updated; changing diagram object to element A' again does not work.

Hi qwerty.  I was trying to avoid removing the old element because of the positional information, associated links, etc.  I was hoping to simply change the element ID such that the original diagram would remain intact with the new element placed where the old one did.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Changing an interaction diagram element via scripting
« Reply #9 on: April 06, 2018, 01:13:42 am »
Ha,

It seems like I ran into a similar issue in the past.
Workaround I used was to edit directly in the database:

Code: [Select]
'set the element of the diagramObject to the new action
updateDiagramObjectSQL = "update t_diagramobjects set object_id = "& callingActivity.ElementID &" where Diagram_ID = " & diagramObject.DiagramID & " and Object_ID = " & element.ElementID
Repository.Execute updateDiagramObjectSQL

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #10 on: April 06, 2018, 08:09:48 am »
What Geert did might be a solution. However, you can query the old position and place the new one at this location. And eventually move the old one aside. It might be an issue to replace a diagram object in line. But waiting for Sparx to fix that issue is probably not a good idea any more.

q.

kjourdan

  • EA User
  • **
  • Posts: 71
  • Karma: +0/-0
    • View Profile
Re: Changing an interaction diagram element via scripting
« Reply #11 on: April 07, 2018, 02:42:41 am »
Thanks Geert and qwerty.