Purge one selected group definition only with the RevitAPI

Hallo,

I´m creating a Dynamo script that updates a particular group in a project. The idea is first deleting the group (and its items) then purging it from the model, prior to recreating the same objects down the line. I wish I could erase only this specific group definition, which name is known as an input, instead of the full monthy.

My problem is that I cannot call the group.Name method for all groups, therefore I cannot run the purge operation inside a python node with the RevitAPI. Below my script. As a workaround I just output the groups, then use the Dynamo built in node to get the name and implement a boolean filter down the line. I wish to be more elegant - what am I doing wrong?

############# Bolier Plate start #############

import clr

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

############# Bolier Plate end #############

trigger = IN[0] 
name = IN[1]

# Get the active document
doc = DocumentManager.Instance.CurrentDBDocument

# Collect all group elements
groupCollector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_IOSModelGroups)
groupElements = list(groupCollector)

TransactionManager.Instance.EnsureInTransaction(doc)
test=[]

if trigger > 0: # there was at least one group already
    for group in groupCollector :
        
        test.append(group)
    
        """  this part does not work because some group don´t have name, or it´s not convertible
        
             if group.Name == name :
                    doc.Delete(group.Id)
        """     
TransactionManager.Instance.TransactionTaskDone()

OUT = test

What’s the output from the python node? Your collector may be returning Group Types and Group Instances where the instances (or whatever types are failing) don’t have a Name property. If that’s the case, then you can just filter out the instances and only check the types.

The backup option would be to use a Try/Except loop to ignore the elements without a Name property.

1 Like

The output Is either Group or ElementType. Using the doc(object) method it seems that both have the .Name method.

Can you be more specific about how to identify the instances?

Assuming one of those is the object type you’re looking for and correctly returning a name and the other can be ignored, you would just filter out the type that’s failing. You can use the FilteredElementCollector filter WhereElementIsElementType or WhereElementIsNotElementType to do this.

1 Like

Hi,

maybe you can delete the GroupType directly, this will also remove group instances


import clr
import sys
import System
#import Revit API
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
import Autodesk.Revit.DB as DB

#import transactionManager and DocumentManager (RevitServices is specific to Dynamo)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

#Preparing input from dynamo to revit
groupName = IN[0]

pvp = ParameterValueProvider(ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME))
evaluator = FilterStringEquals()
rule = FilterStringRule(pvp, evaluator, groupName)
filter_g = ElementParameterFilter(rule)
groupTypeId = DB.FilteredElementCollector(doc).OfClass(DB.GroupType).WherePasses(filter_g ).FirstElementId()

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
if groupTypeId != ElementId.InvalidElementId:
    doc.Delete(groupTypeId)

TransactionManager.Instance.TransactionTaskDone()

OUT = groupTypeId
2 Likes

What an elegant solution! Exactly what I was aiming at. Merci Cyril!

1 Like