How to save Family to a new file

How to save Family to a new file

petarQ6NCX
Participant Participant
1,805 Views
4 Replies
Message 1 of 5

How to save Family to a new file

petarQ6NCX
Participant
Participant

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 is to save only the loaded Family as a new .rfa document.

To explain better, I will demonstrate how that looks like when using Revit UI:

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

0 Likes
Accepted solutions (1)
1,806 Views
4 Replies
Replies (4)
Message 2 of 5

petarQ6NCX
Participant
Participant
Accepted solution

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();

            var famDoc = document.EditFamily(family);
            ModelPath newPath = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
            SaveAsOptions saveAsOptions = new SaveAsOptions();
         
            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. 🙂 

Message 3 of 5

Mohamed_Arshad
Advisor
Advisor

HI @petarQ6NCX 
  You can Edit the Family, That Method will return you the FamilyDocument. Kindly check the below code for additional reference.

Reference Code:

            //Edit Family which you loaded Will Return you a Family Document
            //Kindly Save the Family Document as .rfa
            Document familyDoc = doc.EditFamily(fam);

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

            familyDoc.SaveAs(newPath, saveAsOptions);

Hope this will Helps 🙂
 


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 4 of 5

Mohamed_Arshad
Advisor
Advisor

oops you've already posted 😊


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 5 of 5

petarQ6NCX
Participant
Participant

Yes, but thanks for the answer anyway! 🙂