Some background:
I am building a dynamo script to edit Model groups. It works fine, by ungroup->edit->regroup.
But to get nested groups to work i need a recursive function for grouping and ungrouping. Thats why i am struggling with creating groups in python.
import System
import sys
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
clr.AddReference('System')
from System.Collections.Generic import List
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
#Preparing input from dynamo to revit
elements = UnwrapElement(IN[0])
myCollection = List[ElementId]()
#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
for i in elements:
myCollection.Add(i.Id)
grouping = doc.Create.NewGroup(myCollection)
TransactionManager.Instance.TransactionTaskDone()
OUT = grouping
Yup, that’s about it. If you already have a list of Ids you can feed that directly to the collection creation but otherwise you would iterate through to get the Id like you did.