Search code examples
c#revit

Getting null when loading or creating new family instances


I've been having issues with the following snippet of code when working with my addin:

doc.LoadFamilySymbol(@"[filepath here]", "[filename here]", out FamilySymbol symbol);
doc.Create.NewFamilyInstance(mid, symbol, d, GetLevelInformation(d), StructuralType.NonStructural);

As far as I can tell while debugging, the FamilySymbol variable symbol is null after I pass the first line. mid, d, and GetLevelInformation all work and contain the expected values, so I suspect the problem lies within the first line.


Solution

  • Figured I'd update this thread for those with similar problems since I did end up solving my issue. The answer was that I was attempting to load a Family using the Name parameter while what I was really looking for was a FamilyName parameter.

    The fix for this was changing x.Name to x.FamilyName as seen below:

    FilteredElementCollector collector = new FilteredElementCollector(doc);
                FamilySymbol symbol = collector.OfClass(typeof(FamilySymbol))
                    .WhereElementIsElementType()
                    .Cast<FamilySymbol>()
                    .First(x => x.FamilyName == "[Family Name]");
    

    There is a difference! Thanks for the responses, Jeremy.