Setting Visibility of Linked Files using Revit API

By Saikat Bhattacharya

Attempting to hide/unhide linked Revit files using Document.ActiveView.SetVisibility(), as shown the code below throws the following exception:

image

Here is the code which throws the above given exception:

FilteredElementCollector collector =
    new FilteredElementCollector(doc);

Element linkedFile =
    collector.OfCategory(BuiltInCategory.OST_RvtLinks).FirstElement();

if (linkedFile != null)
{
    //read its category and set the visbility
    Category category = linkedFile.Category;
    doc.ActiveView.setVisibility(
        category,
        !category.get_Visible(doc.ActiveView));
}

How can we use the Revit API to control the visibility of Linked files, specifically hiding and un-hiding the Linked files?

To hide/unhide linked files, we can use the Document.Element.HideElements() (or the UnHideElements, as the case might be). The following self explanatory code snippet shows the usage of these two APIs.

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Revit.SDK.Samples.HelloRevit.CS
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiApp = commandData.Application;
            Document doc = uiApp.ActiveUIDocument.Document;

            //find the linked files
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            ICollection elementIdSet =
                collector
                .OfCategory(BuiltInCategory.OST_RvtLinks)
                .OfClass(typeof(RevitLinkInstance))
                .ToElementIds();

            using (Transaction trans =
                new Transaction(doc, "LinkedFileVisibility"))
            {
                trans.Start();

                foreach (ElementId linkedFileId in elementIdSet)
                {
                    if (linkedFileId != null)
                    {
                        if (doc.GetElement(linkedFileId).IsHidden(doc.ActiveView))
                        {
                            if (doc.GetElement(linkedFileId)
                                   .CanBeHidden(doc.ActiveView))
                            {
                                doc.ActiveView.UnhideElements(elementIdSet);
                            }
                        }
                        else
                        {
                            doc.ActiveView.HideElements(elementIdSet);
                        }
                    }
                }
                trans.Commit();
            }
            return Result.Succeeded;
        }
    }
}

Comments

5 responses to “Setting Visibility of Linked Files using Revit API”

  1. Dear Saikat:
    Is possible hide section views in viewplans:
    I use the following:
    private void ocultarSecciones(Document doc, IList coleccionsecciones)
    {
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    collector.OfClass(typeof(ViewPlan));
    using (Transaction txxxx = new Transaction(doc))
    {
    txxxx.Start(“Ocultar secciones”);
    foreach (ViewPlan v in collector)
    {
    if (v != null && !v.IsTemplate)
    {
    // Ocultamos las secciones
    v.HideElements(coleccionsecciones);
    }
    }
    txxxx.Commit();
    }
    }

  2. Hi Pachi
    I created a blogpost to provide the code for your requirement:
    http://adndevblog.typepad.com/aec/2013/02/hiding-sections-in-viewplans-using-revit-api.html
    hope this helps
    cheers

  3. sachin Avatar
    sachin

    Hello Saikat:
    I am getting problem to get a real time view(like in case inserting door on a wall,after selecting the door, i am not getting the door view of moving along with the mouse cursor only getting cursor and if click on wall directly it will inserting.) before placing family inserting.
    my code is like this:
    public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
    ……….
    try
    {
    trans.Start();
    Family family = null;
    FamilySymbol symbol = null;
    if (doc.LoadFamily(@h4, out family))
    {
    foreach (FamilySymbol s in family.Symbols)
    {
    symbol = s;
    }
    View view = uidoc.ActiveView;
    Reference r = sel.PickObject(ObjectType.PointOnElement, “Select”);
    Element elem = doc.GetElement(r);
    Wall wall = elem as Wall;
    XYZ globalPoint = r.GlobalPoint;
    XYZ levelPoint = new XYZ(10, 10, 1);
    Wall wall = doc.GetElement(r) as Wall;
    FamilyInstance instance = doc.Create.NewFamilyInstance(levelPoint, symbol, wall,level,StructuralType.NonStructural);
    }
    trans.Commit();
    }
    catch (Exception)
    {
    }
    return Result.Succeeded;
    }

  4. sachin Avatar
    sachin

    and also getting problem in highlight the elements pls help me: my code is like this:
    FilteredElementCollector c = new FilteredElementCollector(doc).OfClass(typeof(Family));
    FilteredElementCollector familyCollector11 = new FilteredElementCollector(doc);
    familyCollector.OfClass(typeof(FamilySymbol));
    List GlobalStrings22 = new List();
    Family family2 = null;
    FamilySymbol symbol4 = null;
    SelElementSet selElements = uidoc.Selection.Elements;
    SelElementSet selSet = SelElementSet.Create();
    foreach (Family f in c)
    {
    family2 = f;
    foreach (FamilySymbol s in family2.Symbols)
    {
    symbol4 = s;
    string ss = s.Name.ToString();
    Element ele = s as Element;
    foreach (Material material in s.Materials)
    {
    if (material.Name.ToString() == Class1.GlobalVar22.ToString())
    { ElementId id = s.Id as ElementId;
    Element e = doc.GetElement(id);
    selSet.Add(e);
    GlobalStrings22.Add(material.Name.ToString());
    }
    } break;
    }
    }
    uidoc.Selection.Elements = selSet;
    uidoc.RefreshActiveView();

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading