Yes, you can script this. It will be a little involved, but it can be done.
There's no immediate API support to find what you're looking for so you'll need to go into the database to find all elements that are not shown in any diagrams. Once you've done that, you can delete each such element from its containing package.
Elements are stored in t_object
and diagram objects (the graphical representation of one element in one diagram) in t_diagramobjects
. An entry in t_diagramobjects
has a reference to the diagram (Diagram_ID
) and to the element being displayed (Object_ID
).
(If you're new to EA hacking, yes, they're called Elements in the API and Objects in the database. Just a fact of life.)
So:
- Find all
t_object.Object_ID
which do not occur in t_diagramobjects.Object_ID
.
- Loop through this set and, using
Repository.GetElementByID()
, retrieve each Element
.
- Fetch the element's containing package using
Repository.GetPackageByID(element.PackageID)
.
- Spin through the package's
Elements
collection using GetAt()
in a for loop, find the Element
whose ElementID
matches the one you're after and Delete()
it. Don't forget to Refresh()
the collection afterwards.
There is a method Repository.SQLQuery()
, which allows you to perform a select
query against the database, but you have to parse the result from a single XML string.
It's simpler to use Repository.GetElementsByQuery()
, which returns the elements in a Collection
, but this requires you to predefine the query as an EA search. If you use this, you can skip steps 1 and 2 above.
Of course, you could go straight into the database and simply delete all those t_object
rows which are not referred to from t_diagramobjects
. This is a terribly bad idea which will (I'm pretty sure) leave you with a corrupted database. When you use the API to delete things, EA cleans up all references (so no connectors are left dangling, etc).
And of course, you should only unleash a script like this if you are absolutely sure there are no other elements that aren't shown in diagrams that need to be kept. So a temp project for this is probably a good idea.
Note, finally, that packages are also elements, and if you don't want to lose all your imported source in one fell swoop by deleting the package they're in (because the package itself is typically not shown in a diagram, is it?) you should probably exclude t_object.Object_Type
/ Element.Type
"Package"
.