Accessing the Path of a Revit Family Document from the Family Instance

By Saikat Bhattacharya

When a Revit project (RVT) file contains a family instance and we select the instance and edit the family, it opens up the Family in family editor mode. If users click Save at this point from the Revit User Interface, Revit knows the location of the Family Document from where the Family was loaded initially. Can we extract this file path (location) programmatically?

If you want to programmatically access this path of Family Document from the Family Instance, you can do that by traversing the path from Family Instance –> Family Symbol –> Family –> Family Document. But, the problem that came up with this approach was that after Family element, it only provides access to the Document property which only returns the active document in which the Family is loaded in (or where the Family Instance has been created in) – whereas what we need is to access the Family Document from which the Family was loaded.

After some further playing around, one of the approach that seemed to work was to follow the workflow as followed using Revit UI – which was to call the EditFamily() on the Family element. This provided access to the Family Document and from this Document element, the PathName could be extracted. The Family.Document had taken us to the wrong direction.

The following code snippet illustrates this approach:

using System;
using System.Collections.Generic;
using System.Text;
 
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)
    {
      Document doc = commandData.Application.ActiveUIDocument.Document;
 
      foreach (FamilyInstance famInst in commandData.Application.ActiveUIDocument.Selection.Elements)
      {
        Document famDoc = doc.EditFamily(famInst.Symbol.Family);
        TaskDialog.Show("Family PathName", famDoc.PathName);
      }
      return Result.Succeeded;
    }
  }         
}
 
UPDATE: Thanks Dan for pointing out that we indeed need to close the Family document after the path name has been extracted - otherwise each of the family documents will throw the save dialog and consequently have to close them manually before Revit is closed.

Comments

4 responses to “Accessing the Path of a Revit Family Document from the Family Instance”

  1. Dan Tartaglia Avatar
    Dan Tartaglia

    Hi Saikat,
    I’ve found that you have to close the document after extracting the path. If you do not, you will be asked to save each family when you quit Revit.
    // Close the document
    famDoc.Close(false);

  2. Constantin Gherasim Avatar
    Constantin Gherasim

    Hi Saikat,
    I need to perform an EditFamily() on a family element by selecting it in a legend.
    In other words, I want my code to allow the user to click on a family already inserted in a legend, then edit that family for extracting some information before closing it back.
    Can you help me with this, please?
    Constantin

  3. Kenneth Avatar
    Kenneth

    Hi guys, I am trying to find some info on families and templates. The company has different offices with different servers. When our office works on the server in the head office the time to open files and run program’s is very slow due to poor band width. I want to know if we take their families and templates and copy them over to our office server will they still reference the head office server and if so can I easily repath the families and template to run them on our server. Any help would be great.
    Kenny

  4. Jazza Bogden Avatar
    Jazza Bogden

    if the families are loaded and not saved to a local library it seems an error will arise, can you show how to save family loaded into project first and then go back and attempt to extract file path again please?

Leave a Reply to KennethCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading