[Revit 2024] Load rfa file into Rvt project.

[Revit 2024] Load rfa file into Rvt project.

satouak
Participant Participant
1,798 Views
4 Replies
Message 1 of 5

[Revit 2024] Load rfa file into Rvt project.

satouak
Participant
Participant

Hello I would like to load family file (.rfa) into the current project document.
I use this api
document.LoadFamily(filename, out Family family)
But it always return false.

I have do my best to search the info for loading rfa.
What I have done wrong? 

This is the reference Link I have read :
https://jeremytammik.github.io/tbc/a/0250_nested_family.htm



The attachments is my code and the family which I want to load.

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

moturi.magati.george
Autodesk
Autodesk
Accepted solution

Hi @satouak,

 

Here is a sample snippet that you can customize to load your family.

Family family = null;
string libraryPath = "";
uiapp.Application.GetLibraryPaths().TryGetValue("Imperial Library", out libraryPath);

if (String.IsNullOrEmpty(libraryPath))
{
	libraryPath = "c:\\";   // If not have, use a default path.
}
// Allow the user to select a family file.
System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
openFileDialog1.InitialDirectory = libraryPath;
openFileDialog1.Filter = "Family Files (*.rfa)|*.rfa";

if (DialogResult.OK == openFileDialog1.ShowDialog())
{
	using (Transaction transaction = new Transaction(doc, $"Loading {openFileDialog1.FileName}"))
	{
		transaction.Start();
		var result = doc.LoadFamily(openFileDialog1.FileName, out family);
		//Check if the family already exists otherwise load it
		if (family == null)
		{
			FamilySymbol familySymbol = null;
			if (!doc.LoadFamilySymbol(openFileDialog1.FileName, "standard", out familySymbol))
			{
				//show error message here
			}
		}
		transaction.Commit();
	}
}




  Moturi George,     Developer Advocacy and Support,  ADN Open
Message 3 of 5

satouak
Participant
Participant

Thanks for the reply.

I found a solution.

The code which I referenced did not use a transaction...

0 Likes
Message 4 of 5

joseph.he
Contributor
Contributor

Do  you mind sharing your solution, or the snippet you used to fix it? Im having a lot troubling loading families into my project programmatically as well. Thank you!

0 Likes
Message 5 of 5

satouak
Participant
Participant

Sure. Not complete code. But something like this should work.
Notice that I am using c# 11. 

 

 

    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        var uiapp = commandData.Application;
        var uidoc = uiapp.ActiveUIDocument;
        var app = uiapp.Application;
        var doc = uidoc.Document;

        using (Transaction t = new(doc, "TRANSACTION1"))
        {
            t.Start();

            var filepath="...";
            var typeName=""
            doc.LoadFamilySymbol(filepath, typeName, out FamilySymbol familySymbol);
            if (familySymbol == null)
            {                
                return Result.Failed;
            }

            familySymbol.Activate();

            XYZ basePoint=new(0,0,0);
            var familyInstance = _doc.Create.NewFamilyInstance(basePoint,familySymbol, StructuralType.NonStructural);
            t.Commit();
        }


        return Result.Succeeded;
    }

 

0 Likes