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.