Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Danny F on December 11, 2012, 01:37:10 am

Title: Deep Delete element
Post by: Danny F on December 11, 2012, 01:37:10 am
Hi

How can I react to a delete action on a diagram element (linked)

I want to delete it also from the model (as if the delete action was done on the element in the project browser)

for example deleting a connector on a diagram asks me if I want to delete it from the model or not.



Thanks

Title: Re: Deep Delete element
Post by: qwerty on December 11, 2012, 02:54:07 am
Ctrl-Del

q.
Title: Re: Deep Delete element
Post by: Danny F on December 11, 2012, 05:25:25 pm
Thanks - but I want to react when the user doesn't use Ctrl-Del

Title: Re: Deep Delete element
Post by: Paulus on December 11, 2012, 06:11:48 pm
When you delete a diagram object the event 'EA_OnPreDeleteDiagramObject' fires passing the ID of the element that the diagramobject links to.

First check if you want to delete the element from the model behind the diagramobject, and if so delete it from the model and pass 'truefalse' to EA to prevent EA from trying to delete the (at that time already deleted) diagramobject.

Here's some (vbscript) code that deletes an element (and consequently, its associated diagramobjects) from the model:

Code: [Select]
sub DeleteElementFromModel(elt)            
      dim elements, i
      if elt.ParentID = 0 then
            set elements = GetPackageByID(elt.PackageID).elements
      else
            set elements = GetElementByID(elt.ParentID).elements
      end if

      for i = 0 to elements.Count - 1
            if elements.GetAt(i).ElementID = elt.ElementID then
                  elements.DeleteAt i, false
                  Elements.Refresh()
                  exit sub
            end if
      next
end sub

best regards,

Paulus
Title: Re: Deep Delete element
Post by: Danny F on December 11, 2012, 06:57:24 pm
thanks,  WORKS  :)

This method is not documented in the SDK !  :o (enterprise_architect_sdk.pdf)

I checked the application help (enterprise architect user guide) and there it is included ! in the automation chapter (I'm gonna use that resource now   ::) )

thanks again


gr
Title: Re: Deep Delete element
Post by: Geert Bellekens on December 11, 2012, 07:22:07 pm
Danny,

I didn't even know that pdf existed.
Anyway, you should indeed just use the help file.

Geert
Title: Re: Deep Delete element
Post by: Paulus on December 11, 2012, 08:29:01 pm
ur welcome!

Btw i made a mistake, you should pass FALSE to EA to prevent deletion. Update the previous post for future reference
Title: Re: Deep Delete element
Post by: Eve on December 12, 2012, 08:45:09 am
Please... Don't do that.

I wouldn't be surprised if there aren't more (very good) reasons why this is a bad idea. But I can't imagine a single good reason for doing it. I can see not wanting to train people to use Ctrl delete, but that's not a good reason.

PS. The SDK document was produced for a very short period of time when we tried moving that information from the help file. It is very much out of date, with the information now being back in the help file.
Title: Re: Deep Delete element
Post by: Paulus on December 12, 2012, 10:02:43 am
Hi Simon,

You are of course absolutely right that you need to understand the consequences of what you are doing before scripting something like this and that there aretrade-offs to be considered.

But in as far as  end-users are concerned i think it is not unreasonable to ask for an automatic removal of a model element that (from a user perspective) belongs to only that specific diagram.

For the record the actual script i use includes a 'sanity check' on the element prior to the call to the delete script i provided: only elements that an end-user would consider to be private to the diagram (eg activityinitial, action, ...) are eligible  for removal from the model as well, and even then they are only removed if the element actually is an orphan from the model point of view.

To me preventing cluttering of the model with orphaned elements outweighs the disadvantage of not being able to undo the delete of elements if they can easily be re-introduced.

So no i don't consider this a bad idea at all, as long as you think it through. You mention that there may be additional advantages, could you clarify on that? So far i haven't encountered any. Can it be that the internals of EA may hold a surprise for me?

best regards,

Paulus
Title: Re: Deep Delete element
Post by: Danny F on December 12, 2012, 06:17:22 pm
Thanks very much for the replies and warning !

The use I have for it is very specific for an Add-in
It is not a general 'Ctrl-D' replacement

It concerns an Add-in which has specific diagrams for specific stereotyped elements.
It's only for those elements, which can only be added to specific diagrams, that I want the functionality.

I'm very much a newbie concerning EA Add-in development, any help is very much appreciated  :)
gr

Title: Re: Deep Delete element
Post by: Paulus on December 12, 2012, 07:13:34 pm
Exactly what i'm using it!  :)

But be aware: inexperienced users or others not using your add-in -may- re-use an element in other diagrams anyway (you can always drag it to another diagram from the project explorer).

So be sure to check if the element is used exclusively by a diagram before deleting it.

grts,

Paulus
Title: Re: Deep Delete element
Post by: SvenK on January 09, 2013, 12:54:03 am
Quote
Btw i made a mistake, you should pass FALSE to EA to prevent deletion. Update the previous post for future reference

What is the reason for passing False?
In
http://www.sparxsystems.com/uml_tool_guide/sdk_for_enterprise_architect/collection.htm
it is written that the Boolean in DeletAt() is unused.
Title: Re: Deep Delete element
Post by: qwerty on January 09, 2013, 03:37:15 am
You can pass any value. False == one of any value.

q.
Title: Re: Deep Delete element
Post by: Paulus on January 09, 2013, 05:05:19 am
Hi SvenK,

I wasn't referring to the 2nd argument in 'DeleteAt' but to the return value of the event 'EA_OnPreDeleteDiagramObject'.

You need to pass FALSE to stop EA from deleting the object as well if your code handles the deletion.

Effectively this is (mis)using an event intended to -prevent- the deletion of an ob ject to actually -handle- the deletion ourselves... but it works.

grts,

Paulus