Book a Demo

Author Topic: How to delete element from diagram?  (Read 6111 times)

survex

  • EA User
  • **
  • Posts: 76
  • Karma: +1/-1
    • View Profile
How to delete element from diagram?
« on: January 20, 2015, 04:22:25 am »
I'm trying to clean diagram via scripting, but following snippet doesn't work:

for(var l = 0; l < diagram.DiagramObjects.Count; l++) {
    var diagramObject = diagram.DiagramObjects.GetAt(l);
    diagramObject.Delete(0);
}

Is there any advices?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: How to delete element from diagram?
« Reply #1 on: January 20, 2015, 05:03:22 am »
You will need to
Code: [Select]
diagram.DiagramObjects.DeleteAt(l, 0);and better have the loop run backwards.

q.

P.S. Delete(l) works also, but somehow I like to pass the unused dummy parameter for DeleteAt which is also 2 chars more to type. Makes me feel EA. ;D
« Last Edit: January 20, 2015, 08:55:49 am by qwerty »

survex

  • EA User
  • **
  • Posts: 76
  • Karma: +1/-1
    • View Profile
Re: How to delete element from diagram?
« Reply #2 on: January 20, 2015, 10:24:34 am »
That's helped! Thanks! Mention this stuff in your book.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: How to delete element from diagram?
« Reply #3 on: January 20, 2015, 10:42:47 am »
I thought I did :-? Will have a cross check...

q.

P.S.: Chapter 3.8 on p. 30. I did not mention the reverse loop. I think it's not necessary since EA does not refresh the collection on delete but only on an explicit refresh. However, it's more or less common good practice to remove elements in collections from the end first since you index them from the first. So if you delete it, which is the second then? Number 0 again or index 1? But backwards it's save anyway.
« Last Edit: January 20, 2015, 10:48:12 am by qwerty »

jakson

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Re: How to delete element from diagram?
« Reply #4 on: January 29, 2015, 07:02:34 pm »
Quote
it's not necessary since EA does not refresh the collection on delete but only on an explicit refresh
BTW
How to "refresh" diagram after some modifications?
Is it good way (code below)?

Code: [Select]
dim currentDiagram as EA.Diagram
set currentDiagram = Repository.GetCurrentDiagram()
'
' do sth with diagram elements...
'
Repository.SaveDiagram( currentDiagram.DiagramID )
Repository.ReloadDiagram( currentDiagram.DiagramID )

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13448
  • Karma: +571/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to delete element from diagram?
« Reply #5 on: January 29, 2015, 07:36:30 pm »

Yes, but be careful with the savediagram. Depending on how you edit the diagram that may undo your changes.

Geert

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: How to delete element from diagram?
« Reply #6 on: January 29, 2015, 07:42:16 pm »
Repository.ReloadDiagram() refreshes the diagram if it's open, so that's a good idea -- but of course you have to save it first.

Repository.SaveDiagram() only works if the diagram is open (according to the help file anyway, I haven't verified this myself). You're probably better off calling Diagram.Update() when you're done modifying the diagram.

/Uffe
My theories are always correct, just apply them to the right reality.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: How to delete element from diagram?
« Reply #7 on: January 29, 2015, 09:07:19 pm »
It depends on what you mean with refresh. If you have people working concurrently things are completely different to work on a local EAP. I guess you mean the latter. So if you are a nice guy and just use API calls there is not need for a refresh. The diagram is updated as expected and no need to refresh. If you call save depends. If the user chooses to close without manual save the diagram stays as it has been. So it's a question of your use case whether you want to force a save.

Now for the not so nice case where you change the diagram under the hood. If you force a save it might overwrite your changes you did under the hood and it clobbers your work. So if you are in your garage close the door. When you're done, open it and show the results.

Uffe mentioned diagram.update. That only has effect on the diagram properties (which can also influence the appearance). But it has no influence on the elements added to/removed from the diagram.

Best advice: test it extensively ;)

q.

P.S. Added connectors are not reflected directly. So a Repository.ReloadDiagram is mandatory. This will throw away any previously made changes to the diagram silently. There is no way to find out if a diagram has been changed by the user. So best is to ask the user in a dialog, then force save and finally issue a reload.
« Last Edit: January 29, 2015, 11:03:32 pm by qwerty »