Inserting Groups

Inserting Groups

stever66
Advisor Advisor
1,971 Views
3 Replies
Message 1 of 4

Inserting Groups

stever66
Advisor
Advisor

I'm trying to insert a group by its name.  Does anyone have an example of doing that?  The closest I have found was in the "First Plugin" where a group is inserted by selecting the group.

 

 

 I found this in the API help:

 

public Group PlaceGroup(
	XYZ location,
	GroupType groupType
)

But I'm not sure what it wants for "groupType". 

 

I also found this, but again, I can't get it to run because I'm not sure what it wants for a groupType.

 

public void GetInfo_GroupType(GroupType groupType)
{
    string message = "GroupType";
    //Retrieve a set of all the groups that have this type.
    foreach (Group group in groupType.Groups)
    {
        // Get GroupType group name
        message = "\nThe group type name is : " + groupType.Name;
        //Returns all the members of the group.
        message += "\nThe types and ids of the group members are : ";
        IList<ElementId> groupMembers = group.GetMemberIds();
        foreach (ElementId memberId in groupMembers)
        {
            Element element = group.Document.GetElement(memberId);
            // Get GroupType group element id
            message += "\n\t" + element.GetType().Name + " : " + memberId.IntegerValue;
        }
    }
    TaskDialog.Show("Revit",message);
}

 

0 Likes
Accepted solutions (1)
1,972 Views
3 Replies
Replies (3)
Message 2 of 4

BardiaJahan
Advocate
Advocate

If you want to get the GroupType by its name, you could use a FilteredElementCollector. Try this:

 

GroupType groupType = new FilteredElementCollector(document)
				.OfClass(typeof(GroupType)).OfType<GroupType>()
				.Where(g => g.Name == "ThisGroup").FirstOrDefault();
0 Likes
Message 3 of 4

stever66
Advisor
Advisor

OK, so far I have this (as a sharpmacro):

 

public void PlaceAGroup()
        {
                UIDocument uiDoc = this.ActiveUIDocument;  
                Document doc = this.ActiveUIDocument.Document;
            
            using (Transaction t = new Transaction(doc))
            {
                                
                GroupType groupType = new FilteredElementCollector(doc)
                .OfClass(typeof(GroupType)).OfType<GroupType>()
                .Where(g => g.Name == "Panelboard").FirstOrDefault();
                
                t.Start("Add Group");
                XYZ mypoint = new XYZ(555);
                doc.Create.PlaceGroup(mypoint, groupType);
                t.Commit();
            }
            
        }

 

That does work, but is seems really like a lot of code just to place a group.  Do I really have to use the filteredelement collector if I already know my group name is "Panelboard", or something similar?

 

I've tried things like: 

doc.Create.PlaceGroup(mypoint, "Breaker" );

doc.Create.PlaceGroup(mypoint, groupType.Name = "Breaker" as GroupType);

 

but if there is a way to directly use the string name, I can't find it.

 

 

 

0 Likes
Message 4 of 4

BardiaJahan
Advocate
Advocate
Accepted solution

If you want to retrieve a GroupType with a specific name, I could not think of any other way. However, based on how you are getting that specific name, your code might be shorter. For example if you already have an instance of that GroupType, you could just copy it with ElementTransformUtils.CopyElements

 

Or another thing I would do, if dealing with several GroupTypes is to retrieve all of them at once and make a map of GroupType Name and Ids for future use.

0 Likes