Creating a Group and How to Fish

Here is a question from Henry Medina on creating groups that might be of interest to a wider audience:

Question:
I have listened to your TV recording several times and was also able to run the Labs project.
Your Labs code shows how to draw beams, columns and braces, but not a group.
How could I create a new instance of an existing group, set its insertion base point, location, rotation angle, and mirror it?
Please show the commands in C#.

Answer:
When addressing a question like this, there are a certain number of steps that I recommend always going through:

  • RevitAPI.chm
  • Getting Started Revit API 2009.doc
  • Revit SDK Samples
  • Revit 2009 API Developer Guide.pdf

Before looking at these, here are some basics about element creation.
Everything that can be created through the Revit API is handled by the Application and Document classes in the Autodesk.Revit.Creation namespace.
Since a group is a database resident object, it is generated by the creation document.
The creation application is used for objects that a memory resident only, such as geometry objects.

Now, looking at the creation document and its methods in RevitAPI.chm or in the Visual Studio object browser, it is easy to find the NewGroup method.
It takes an ElementSet as an argument and returns a newly created Group instance.
It creates a new type of group and returns a new instance of it containing the specified elements. Initially the group will have a generic name, such as Group 1.
This can be modified by changing the name of the group type using something like newGroup.GroupType.Name = newName.
So it actually creates both a GroupType object and an instance of that type.
The GroupType class is derived from Autodesk.Revit.Symbol, and Group from Autodesk.Revit.Element.

The Getting Started document turns up one snippet of information on groups in its question and answer section that may be useful to us later, but does not address our immediate need:

Q: When exporting a model containing groups to an external program, the user receives an error at the end of the export:
“Changes to group “Group 1” are allowed only in group edit mode. Use the Edit Group command to make the change to all instances of the group. You may use the “Ungroup” option to proceed with this change by ungrouping the changed group instances.”

A: Currently the API does not permit changes to members of groups. You can programmatically ungroup, make the change, regroup and then swap the other instances of the old group to the new group to get the same effect.

While this does not help us with our immediate question, it does at least further confirm the
degree of programmatic access that is and is not provided to the grouping functionality.

Searching the Revit SDK Samples for NewGroup turns up no hits. Almost all aspects of the Revit API are demonstrated in the samples, so this is actually quite unusual. I have not addressed this in any of my labs yet either.

The next place to look is the developer guide. This is the most in-depth source of information on Revit programming, because it explains relationships and underlying concepts that are not immediately apparent from the classes, properties and methods presented in the help file nor immediately visible in the sample code. Again, we start by searching for ‘NewGroup’. If no hits were found, I would then search for just ‘group’. This time, ‘NewGroup’ is found, in section 9.4 Group. This section does indeed explain groups very well from the programming point of view. It also includes two code snippets 9-8: Creating a Group and 9-9: Naming a Group.

This answers the first part of your question, on how to create a group.

Regarding the placement of instances, I continued searching in the creation document members and found the PlaceGroup method, which places an instance of a model group into the Revit project, using a location and a group type.

I have not implemented any code testing any of this yet. If you do, please feel free to pass it on.

I hope this helps you learn to fish, and does not just feed you.

Many thanks, Henry, for this illuminating question, and good luck with your further steps!


Comments

17 responses to “Creating a Group and How to Fish”

  1. John Balciar Avatar
    John Balciar

    I followed the steps to find the NewGroup method listed in the API documentation. However, the NewGroup method does not exist in the Autodesk.Revit.Creation Namespace or anywhere else that I can find. (I am using the RevitStructure 2010 beta). Have you found a NewGroup method elesewhere in the API?

  2. Hi John,
    I tried to NOT find NewGroup, but was unable to do so in the 2009 documentation :-)
    In the Revit API help file RevitAPI.chm, I open the search tab and type NewGroup.
    It displays a couple of hits, three to be precise, and they all lead to the Document class in the Autodesk.Revit.Creation namespace and its NewGroup method.
    In the Revit 2010 version of the help file I get seven hits, three of which are the same as in the 2009 version. This is in Revit 2010 Beta 1. Please be aware that the 2010 material may be incomplete because it is still under development.
    Cheers, Jeremy.

  3. John Balciar Avatar
    John Balciar

    Thanks Jeremy, I found the method and have it working now, thank you for your time.

  4. Hi John,
    It’s a pleasure, and I’m glad you have it working now.
    Cheers, Jeremy.

  5. Hi Jeremy,
    will u please suggest
    How can i get the model group drawn of model lines from revit application using c#?
    I am using revit archtitecture 2010 with c#
    Regards
    Rocky

  6. Dear Rocky,
    Have a look at the discussion in
    http://thebuildingcoder.typepad.com/blog/2009/06/model-group-shared-parameter.html
    That shows how to determine the model group category. With the category and normal Revit 2010 filtering, retrieving the model groups is easy.
    And remember what I said in my answer to your last comment :-)
    Cheers, Jeremy.

  7. Hi Jeremy,
    Thanks for your useful information and time. the link above help me lot. Actually i have model lines in the model group and I want to get the properties of these model lines in c#. Please help me or suggest something so that i can make step forward in this.
    Regards
    Rocky

  8. Hi Jeremy,
    Have you worked with attached detail groups, by chance? I’ve created code to place a model group in my model, but can’t seem to get an attached detail group to show up. I can find the detail group by filtering the document, and thought it would be a matter of hiding/unhiding the detail group in the proper view as such:
    Public Sub ShowDetailGroupInstances(ByVal GroupName As String)
    Dim ElemSet As New ElementSet
    Dim GroupTypeCollector1 = _
    New FilteredElementCollector(myDoc.Document)
    Dim View As View = myDoc.Document.ActiveView
    GroupTypeCollector1.OfClass(GetType(GroupType))
    Dim GroupElements = From element _
    In GroupTypeCollector1 _
    Where element.Name.ToUpper = GroupName.ToUpper _
    Select element
    For Each ee As Element In GroupElements
    MsgBox(ee.Name)”Just reporting
    ElemSet.Insert(ee)
    Next ee
    View.Unhide(ElemSet)
    End Sub
    Ultimately, I’d like to know what method or property I need to use to control the display of the attached group.
    Any insight would be be appreciated.
    Thanks,
    Ceorl

  9. Dear Ceorl,
    Nope, I have never worked with attached detail groups, neither by chance nor otherwise.
    I don’t completely understand your problem, yet. Are you saying that you have implemented code to insert such a group successfully, and that it remains invisible but can be retrieved using the element collector?
    Cheers, Jeremy.

  10. Chuck Han Avatar
    Chuck Han

    Hey Jeremy,
    In looking through the documentation, I was not able to locate any method(s) that would allow me to programmatically bring in a RVT file as a group, analogous to the user-driven “Insert->Load as Group” command. Am I missing something?
    thanks in advance, Chuck

  11. Dear Chuck,
    Nope, sorry, you are not missing anything. That is currently still a wish list item.
    Cheers, Jeremy.

  12. Hi..Jeremy..
    How can i save group as revit file through API..and load it in Revit 2012
    Thanks in advance..

  13. Dear Sangsen,
    Sorry, I do not believe that this functionality is available through the API.
    Cheers, Jeremy.

  14. Thanks Jeremy

  15. sangsen Avatar
    sangsen

    Hi..Jeremy
    I am trying to place group using API place group() method.But i am not getting snap(Hover) when i place the group.
    Please help me to get the snap using API.
    Thanks,

  16. Dear Sangsen,
    The only access to any snap functionality in the Revit API that I am aware of is the Selection.PickPoint method taking an ObjectSnapTypes argument.
    I hope this helps.
    Cheers, Jeremy.

  17. Hi Jeremy,
    Is there any news on this “Load as Group” feature on the API?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading