Finding groups for an object


If you want to find the groups that contain an object, you have to look through the Owners collection. Look for owners of type = “#group”. Note that you can filter the Owners collection by type, but when you filter by “#group”, you’ll also get layers and partitions, because they are types of groups.

import win32com.client
oGroups = win32com.client.Dispatch( "XSI.Collection" )

o = Application.Selection(0)

oOwners = o.Owners.Filter( "#Group" )

for g in oOwners:
	if g.Type == "#Group":
		#Application.LogMessage( ''.join([g.FullName, ' ', g.Type] ) )
		oGroups.Add( g )

Application.LogMessage( oGroups.GetAsText() )

If you had a naming convention for your groups, you could use the third argument to Filter. For example, if all group names started with “grp”, then could do this:

from win32com.client import constants

object = Application.Selection(0)

# Filter by type and then by name
groups = object.Owners.Filter( "#Group", '', "grp*" )
print groups.GetAsText()

# OR filter by family and then by name
groups = object.Owners.Filter( '', constants.siGroupFamily, "grp*" )
print groups.GetAsText()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s