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.
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:
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") model.initialize(Repository)
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.
Download the complete script: EA-Matic Prevent accidental deletes
option explicit 'EA-Matic 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") model.initialize(Repository) '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(element.name,LEN("DELETED_")) = "DELETED_" then 'OK the element may be deleted EA_OnPreDeleteElement = true else 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 else usage = "parameter(s)" end if else 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 _ & element.name & " is used as type in " & usage , vbExclamation, "Cannot delete element" end if end function