Hide Revit group via API

Joe Ye

 

Through Revit UI command, we can hide group by picking it and click the hide command like other elements. Howerver I cannot hide group via API.

Here is the DevelopSharp code I used.
        public void hidegroup()
        {
            Document doc = this.ActiveUIDocument.Document;
            Selection sel = this.ActiveUIDocument.Selection;           
            Reference ref1 = sel.PickObject(ObjectType.Element,"Please pick a group");
            Element elem = doc.GetElement(ref1);
           
            IList<ElementId> lists = new List<ElementId>();
            lists.Add(elem.Id);
           
            Transaction trans = new Transaction(doc);
            trans.Start("hidegroup");
            doc.ActiveView.HideElements(lists);
            trans.Commit ();
          
        }

Launch the command, pick any group,  error occurs. The error message is :
Revit failed to execute hidegroup.
A problem has been detected.

Then how can we hide a group? Any tricks?

 

Solution

Groups cannot be hidden or overridde like ordinary elements. In order to hide a group, we need to hide the element set in that group by HideElements() method. The good aspect is that we can hide part of a group.

Here is the code to hide the whole group.

public void hidegroup()
{
    Document doc = this.ActiveUIDocument.Document;

    Selection sel = this.ActiveUIDocument.Selection;   

    Reference ref1 = sel.PickObject(ObjectType.Element,"Please pick a group");

    Element elem = doc.GetElement(ref1);

    IList<ElementId> lists = new List<ElementId>();

    Group g = elem as Group;

    IList<ElementId> ids = g.GetMemberIds();

    Transaction trans = new Transaction(doc);

    trans.Start("hidegroup");

    this.ActiveUIDocument.ActiveView.HideElements(ids);

    trans.Commit ();

}


Comments

2 responses to “Hide Revit group via API”

  1. Hi Joe,
    Can you please show me how to implement a Revit add-in that can ‘talk’ to an opened dwg file in an AutoCAD session? or an AutoCAD add-in that can drive Revit to do something?
    What I am trying to do is to read dwg lines data, from a specific layer, and send end points coordinate to Revit to create beams.
    Thank you very much.
    Jacob

  2. Hi Jacob,
    why not exchange the data by using a file ?
    No need to drive any program from outside.
    Just create an AutoCAD plugin which exports your points, e.g. into a xml file.
    Also create a Revit plugin which imports that files and creates objects.
    The xml format may be written by yourself.
    Just search for “XML serialization”.
    This xml stuff may be defined in a common class library which will be referenced by both of your addins.
    Best regards,
    Rudolf Honke

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading