Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: survex on January 20, 2015, 04:22:25 am

Title: How to delete element from diagram?
Post by: survex 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?
Title: Re: How to delete element from diagram?
Post by: qwerty 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
Title: Re: How to delete element from diagram?
Post by: survex on January 20, 2015, 10:24:34 am
That's helped! Thanks! Mention this stuff in your book.
Title: Re: How to delete element from diagram?
Post by: qwerty 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.
Title: Re: How to delete element from diagram?
Post by: jakson 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 )
Title: Re: How to delete element from diagram?
Post by: Geert Bellekens 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
Title: Re: How to delete element from diagram?
Post by: Uffe 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
Title: Re: How to delete element from diagram?
Post by: qwerty 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.