Accessing data from a Revit Family without opening the family file

By Saikat Bhattacharya

If you wish to access any information from a Revit Family, be its specific parameter value, category, etc., without opening the family file, you can always load the family and access the information required. However, if have to do this for a number of family files, loading all the families will increase the model file size – that too just for reading a small piece of information. A better approach is to use transactions, load up the family file(s), access the specific family information required (say Family Category) and then roll back the transaction. With rolling back the transaction, it is almost like we never loaded the family to read the information from the Family file. The code below illustrates the complete approach.

using System;

using System.Collections.Generic;

using System.Text;

 

using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.ApplicationServices;

 

namespace Revit.SDK.Samples.HelloRevit.CS

{

 

  [Transaction(TransactionMode.Manual)]

  public class Command : IExternalCommand

  {

 

    public Result Execute(ExternalCommandData commandData,

      ref string message,

      ElementSet elements)

    {

 

      Document doc =

 &#1
60;      commandData.Application.ActiveUIDocument.Document;

 

      Transaction trans = new Transaction(doc, "Open Family");

      trans.Start();

 

      Family fam = null;

 

      doc.LoadFamily(@"C:\Users\xyz\Desktop\Sample.rfa", out fam);

      TaskDialog.Show("FamilyCategoryName", fam.FamilyCategory.Name);

 

      trans.RollBack();

 

      return Result.Succeeded;

    }

  }

}


Comments

4 responses to “Accessing data from a Revit Family without opening the family file”

  1. Hi Saikat. Is it possible to make an inventory of all famalies in the project getting their size? I have a file over 200 mb and I’m trying to compress it.

  2. Using the path of the family I was able to get some sizes but some families don´t have any familypath data. I’m thinking that maybe I can same those families in a folder and then check their sizes. Please some advice.

  3. Hi Rolando
    I thought over your requirement and I dont think there is any direct way to get the information you need regarding the loaded file sizes. Your workflow/workaround might be the most optimal one at this moment.
    Cheers

  4. Ratnadeep Sarkar Avatar
    Ratnadeep Sarkar

    public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
    {
    try
    {
    this.doc = commandData.Application.ActiveUIDocument.Document;
    this.app = commandData.Application.Application;
    this.uiDoc = commandData.Application.ActiveUIDocument;
    Transaction t1 = new Transaction(doc, “LoadFamily”);
    t1.Start();
    Family family;
    this.doc.LoadFamily(@”C:\Revit Families\03_Electrical\01_Cable Tray & Fittings\Chalfant\Cable_Tray-Fitting-Chalfant-Series_5-Horizontal_Cross.rfa”, out family);
    t1.RollBack();
    Document familyDoc = doc.EditFamily(family);
    FamilyManager fManager = familyDoc.FamilyManager;
    List FamilyTypeNames = fManager.Types.Cast().Select(x => x.Name).ToList();
    List fParamNames = fManager.GetParameters().Select(x => x.Definition.Name).ToList();
    return Result.Succeeded;
    }
    catch (Exception ex)
    {
    return Result.Failed;
    }
    }
    I think if I want to get the value of a family parameter then rolling back the transaction would not let us to edit the family in family doc

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading