Pre-Delete Elements: Know What You're About to Delete

Hi all,

The question: how to know which elements would be deleted if I deleted a certain object in my model?
For instance, if I deleted a wall, can I get a list of its hosted objects which would also get deleted?

I’ve been looking for a Python version of this workflow for some time, so though I’d share.

Credit to @Deyan_Nenov for his macro version (I’ve just more or less transcribed it to Python) and to @Thomas_Mahon for various tips. Original macro can be found here: https://twitter.com/didonenov/status/837293919159390208

The definition can be found here:

Element - Pre-Delete.dyn (8.1 KB)

Hopefully someone else finds it helpful!

Ollie

11 Likes

Hey @oliver.green, I went ahead and changed the topic to Share since this is a solution and not a question.
Good work though!

2 Likes

Update: The Revit 2019 API now contains a method which will return the ElementIDs of dependent elements.

Posted under section 2.3 of this document:

As such, if you’re in 2019 you’ll be wanting to use this method instead of the Dynamo script posted above.

Thanks

Ollie

1 Like

They actually added it in Revit 2018.1 update! :sunglasses:

http://www.revitapidocs.com/2018.1/56e875d3-014b-a996-69c3-e6ed9b885f5c.htm

3 Likes

for Revit 2017 i still have to use temporary transaction, but i need to separate model elements from non-model elements (i.e. view specific), how to apply WhereElementIsViewIndependent() directly to existing elements list instead of entire Revit doc? maybe i still have to use logical filter etc

besides, how to use GetDependentElements() in Python? seems ElementFilter is needed as argument

You can enter None (i.e. null) as an argument to return all kinds of dependent elements.

Here’s an example of a selection filter I’ve used before in IronPython:

class WallSelectionFilter(ISelectionFilter):
    def __init__(self, class_name):
    self.class_name = class_name
    def AllowElement(self, element):
        if isinstance(element, self.class_name):
            return True

input_selected_walls = [ ]
input_selected_walls = uidoc.Selection.PickElementsByRectangle(WallSelectionFilter(Autodesk.Revit.DB.Wall), 'Please drag-select across any curtain walls whose elements you wish to obtain.')

This was a new class which had an AllowElement method. Its constructor took a class name as an argument. The AllowElement method tested whether the elements being passed to it (in the FilteredElementCollector) were of the named class or not.

You can write the logic for the SelectionFilter’s AllowElement yourself, so it will only return the dependent elements of the kind you require. I hope that makes sense!

thanks Oliver, seems None cannot be used here so ElementFilter needs to be constructed, what’s the Python way of ElementFilter ef = new ElementFilter()?

Without wanting to read hundreds of lines of Python, I’d suggest undertaking the filtering outside of the original Python script I posted.

Use a node to inspect each returned element’s OwnerView property, for instance using Element.OwnerView. Only elements that are view-specific will have an owner view. You can then filter those which are view-specific from those which aren’t.

thanks Oliver, good workaround but after quick test, seems element’s OwnerView property won’t 100% reflect if it’s view specific, for instance, dimension’s OwnerView property will return null

Dimensions inherit from Revit’s Element class. This means they’ll have both an OwnerViewId property and a ViewSpecific property.

Finding a node which interrogates either of these should enable you to perform the filtering you need.
I’d recommend trying to find a node which can reliably return a value for either of these properties and using that to filter.

right, for this particular project model, some dimensions may be corrupted which can be selected by Id but cannot be displayed and don’t have OwnerView, normal dimensions do have OwnerView, thanks again Oliver.

1 Like

that’s the “corrupted” dimension


that’s the normal dimension

i’m not quite sure if i can delete the “corrupted” dimension, maybe it’s not “corrupted” at all and don’t have OwnerView for some reasons? in fact, i cannot delete it manually via Id