Search code examples
c#revit-apirevit

Revit C# change Family Type


I'm trying to change the family in the project. New family loads successfully but throws and exception Autodesk.Revit.Exceptions.InvalidOperationException: 'This Element cannot have type assigned.' when i try to ChangeTypeId of the old Family to new family GetTypeId();

    public void ReplaceTitleblock (Document cdoc)
    {
        string targetName = "TITLEBLOCK_AHMB";

        Family oldFamily = (from element in new FilteredElementCollector(cdoc).OfClass(typeof(Family))
                                           where element.Name == targetName && element.IsValidObject
                                           select element as Family).FirstOrDefault();

        Family newFamily;

        if (oldFamily != null)
        {
            cdoc.LoadFamily(newTitleblockPath, out newFamily);
            ElementId newElId = newFamily.GetTypeId();
            oldFamily.ChangeTypeId(newElId);
        }

any help on how to do this?

thanks, Luka


Solution

  • I got it to work.

    I'm now iterating through FamilyInstances of the Family and replacing the FamilySymbol with new one

                foreach (FamilyInstance fam in oldFamilyInstancesFilter)
                {
                    FamilySymbol newSymbol = cdoc.GetElement(newFamily.GetFamilySymbolIds().FirstOrDefault()) as FamilySymbol;
                    fam.Symbol = newSymbol;
                }
    

    i know there's only one symbol hence FirstOrDefault() method