We are working on a project where we use Autodesk's Forge (or Platform Service), more specifically, the Design Automation where we run scripts on our files in the cloud. This means that the Revit.UI is not available.
With that said, we found a way to load a Revit Family file with it's "Type Catalog" and that works nicely. However, the next step would be to update the default Family which is placed inside the opened document with the family we load with document.LoadFamily()
function.
We tried with the following code:
private static int LoadAndUpdateFamily(Document document)
{
var count = 0;
var familyFullPath = (path);
var docFamilyManager = document.FamilyManager;
FilteredElementCollector windows = new FilteredElementCollector(document);
windows.OfClass(typeof(FamilySymbol))
.OfCategory(BuiltInCategory.OST_GenericModel);
IList<Element> windowTypes = windows.OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsElementType().ToElements();
for (int i = 0; i < windowTypes.Count; i++)
{
Console.WriteLine("Window types name = " + windowTypes[i].Name);
}
// this is where we get the default FS which needs to be updated
FamilySymbol defaultFamSymbol = windowTypes.ElementAt(0) as FamilySymbol;
Console.WriteLine("defaultFamSymbol NAME = " + defaultFamSymbol.Name);
defaultFamSymbol.Activate();
using (Transaction transaction = new Transaction(document))
{
transaction.Start("Load Family");
if (document.LoadFamily(familyFullPath, out Family family))
{
var symbolIds = family.GetFamilySymbolIds();
count = symbolIds.Count;
Console.WriteLine($"{family.Name}: {symbolIds.Count}");
var symbols = symbolIds.Select(document.GetElement).OfType<FamilySymbol>();
foreach (var symbol in symbols)
{
Console.WriteLine($"[{symbol.Id}]: {symbol.Name}");
symbol.Activate();
// --- we try to update the default family symbol with the new one here
defaultFamSymbol = symbol;
// --- we print the defaulFamSymbol's name here and it has the new symbol's name, however in the final file nothing is changed.
Console.WriteLine("defaultFamSymbol NAME = " + defaultFamSymbol.Name);
document.Regenerate();
}
} // end of Load family
transaction.Commit();
}
return count;
} // -- end of LoadFamilyCount function
The whole usage of this function is:
var documentTest = data.RevitApp.OpenDocumentFile(path);
Document docT = documentTest;
var loads = LoadAndUpdateFamily(docT);
Console.WriteLine("Loads count = " + loads );
Console.WriteLine("Loads size = " + docT.FamilyManager.Types.Size);
ModelPath pathT = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rfa");
docT.SaveAs(pathT, new SaveAsOptions());
Does anyone has an idea what's wrong with this approach? It seems like the original Family always stays the same, and we couldn't find a way to update it with the loaded one..