1

Using Revit API and without using Revit UI, I want to save a loaded family into a new file.

Let's take a look at the following code:

 using (Transaction transaction = new Transaction(document))
        {
            transaction.Start("Load and Save Family");


            if (document.LoadFamily(familyFullPath, out Family family))
            {
                Console.Writeline("Load successful")
            }

            transaction.Commit();

            ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
            SaveAsOptions saveAsOptions = new SaveAsOptions();
         
            family.Document.SaveAs(newPath, saveAsOptions);
            
        }

The problem with this code is that it saves the default document in which we load the family, because family.Document is returning "the document in which the element resides".

What I want to do is to save only the loaded Family as a new .rfa document.

To explain better, I will demonstrate what it looks like when using Revit UI:

enter image description here

How can I achieve the same thing but purely with Autodesk.Revit.DB?

codeFreak24
  • 189
  • 10

1 Answers1

3

Ok, the solution is to use EditFamily. So the following code will save the family to a new, separate document:

using (Transaction transaction = new Transaction(document))
    {
        transaction.Start("Load and Save Family");


        if (document.LoadFamily(familyFullPath, out Family family))
        {
            Console.Writeline("Load successful")
        }

        transaction.Commit();

        // create new family document by editing family
        var famDoc = document.EditFamily(family);

        ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
        SaveAsOptions saveAsOptions = new SaveAsOptions();

        // save the new document
        famDoc.SaveAs(newPath, saveAsOptions);
        
    }

And for explanation: EditFamily "creates an independent copy of the family for editing", and while we have the independent copy, we can save it.

codeFreak24
  • 189
  • 10