09/27/2012

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

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.

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

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.

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.