Prevent accidental deletes with EA-Matic

With EA-Matic you can develop add-ins for Enterprise Architect using the built-in scripting feature of EA.

This example shows how you can prevent deleting elements that are still being used as a type in either an attribute or parameter.

The script uses the EA_OnPreDeleteElement event to first check whether the element is not still used.

If an attribute using this element as type is found then the element is not deleted and the user is informed with a messagebox.

EA-Matic cannot delete attribute

If it isn’t used as attribute but is used as a type in a parameter or the return type of an operation you get the following messagebox:

EA-Matic cannot delete parameter

Usage of Enterprise Architect Add-in Framework

This script also illustrates how you can use the Enterprise Architect Add-in Framework within your script.

This extensive open source framework is the basis for the add-ins EA-Matic and EA Navigator, and offers a much more functional interface to the model then the standard EA API.

This code inializes the model object with the current Repository

set model = CreateObject("TSF.UmlToolingFramework.Wrappers.EA.Model")

It is then further used to get the element based on the ElementID from the Info object

set element = model.getElementWrapperByID(elementID)

The element then gives us access the attributes and parameters using this element by means of

set usingAttributes =  model.toArrayList(element.getUsingAttributes())
set usingParameters = model.toArrayList(element.getUsingParameters())

Note that we need to use model.toArrayList to convert the C# HashSet that cannot be used by VBScript to an ArrayList that can be used.

The code

Download the complete script: EA-Matic Prevent accidental deletes

option explicit
function EA_OnPreDeleteElement(Info)
     'Start by setting false
     EA_OnPreDeleteElement = false
     dim usage
     'Initialize the EAAddinFramework model
     dim model
     set model = CreateObject("TSF.UmlToolingFramework.Wrappers.EA.Model")
     'get the elementID from Info
     dim elementID
     elementID = Info.Get("ElementID")
     'get the element being deleted
     dim element
     set element = model.getElementWrapperByID(elementID)
     'Manual override is triggered by the name. If it starts with DELETED_ then the element may be deleted.
     if Left(,LEN("DELETED_")) = "DELETED_" then
        'OK the element may be deleted
        EA_OnPreDeleteElement = true
        dim usingAttributes
        set usingAttributes =  model.toArrayList(element.getUsingAttributes())
        'Check if the element is used as type in attributes
        if usingAttributes.Count = 0 then
            'Check if the element is used as type in a parameter
            dim usingParameters
            set usingParameters = model.toArrayList(element.getUsingParameters())
            if usingParameters.Count = 0 then
                'OK, no attributes or parameters use this element, it may be deleted
            EA_OnPreDeleteElement = true
                usage = "parameter(s)"
            end if
            usage = "attribute(s)"
        end if
     end if
     if EA_OnPredeleteElement = false then
          'NO the element cannot be deleted
          MsgBox "I'm sorry Dave, I'm afraid I can't do that" & vbNewLine _
          & & " is used as type in " & usage , vbExclamation, "Cannot delete element"
     end if
end function
6 replies
  1. Philipp
    Philipp says:

    How can i use the EA_OnPostNewDiagram that on every new diagram automatically appears the Diagram Notes from the “Common” Toolbox?

    • Geert Bellekens
      Geert Bellekens says:

      You can.
      You’ll first have to create an EA.Element of type “Text” with Ntype = 18
      Then create a new DiagramObject on the diagram and set the ElementID of the diagramObject to this newly created Text element.
      I think that should do it.

  2. Guillaume Finance
    Guillaume Finance says:

    Hi Geert,
    I’m testing the EA_OnPostNewConnector event. When I work on the script, I need to restart EA so that I can see an update to be effective. Saving the script and clicking on refresh scripts doesn’t let me achieve that.

    Is there a workaround to restarting EA?


      • Guillaume Finance
        Guillaume Finance says:

        I’ll give it a try thanks.
        I tried to create a reusable sub routine in a common VBScript that I included in my script but it doesn’t work (the sub works when it’s in the same script). Can you advise if putting e.g. “!INC myCommonLib” at the beginning of the script (followed by ‘ EA-Matic and the function) should work?



Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.