Hello @jeremy_tammik, I meant to be able to see which family has been loaded into the file and save it in an external temp directory, it worked to catch that event(FamilyLoadedIntoDocument) but the problem was I couldn't either save the family while it's being loaded into the Revit file or either to save it after its already loaded into the Revit file, is there a possible way to do it? this is my code so far for more clarification.
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using System.IO;
using Autodesk.Revit.DB.Events;
using System;
using System.Collections.Generic;
using System.Text;
namespace LoadInProjectWatcher
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class Class1 : IExternalApplication
{
private readonly string saveDirectory = @"";
private string doctitle { get; set;}
public Result OnStartup(UIControlledApplication application)
{
application.ControlledApplication.FamilyLoadedIntoDocument += OnFamilyLoading;
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication application)
{
application.ControlledApplication.FamilyLoadedIntoDocument -= OnFamilyLoading;
return Result.Succeeded;
}
private void OnFamilyLoading(object sender, FamilyLoadedIntoDocumentEventArgs e)
{
doctitle = e.FamilyName;
string logFilePath = Path.Combine(saveDirectory, "FamilyLoadLog.txt");
if (!Directory.Exists(saveDirectory))
Directory.CreateDirectory(saveDirectory);
using (StreamWriter sw = File.AppendText(logFilePath))
sw.WriteLine(doctitle);
TaskDialog.Show("Info", $"{doctitle} is loading into the project.");
SaveFamilies(e.Document);
}
public void SaveFamilies(Document doc)
{
if (!Directory.Exists(saveDirectory))
Directory.CreateDirectory(saveDirectory);
FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Family));
StringBuilder errorMessages = new StringBuilder();
foreach (Family family in collector)
{
if (family.Name == doctitle)
{
Document famDoc = null;
try
{
famDoc = doc.EditFamily(family);
if (famDoc != null)
{
string fileName = Path.Combine(saveDirectory, family.Name + ".rfa");
SaveFamilyDocument(famDoc, fileName);
}
}
catch (Exception e)
{
errorMessages.AppendLine($"Failed to save family {family.Name}: {e.Message}");
}
finally
{
if (famDoc != null)
{
famDoc.Close(false);
}
}
}
}
}
private void SaveFamilyDocument(Document famDoc, string fileName)
{
using (Transaction trans = new Transaction(famDoc, "Save Family"))
{
trans.Start();
SaveAsOptions saveAsOptions = new SaveAsOptions();
famDoc.SaveAs(fileName, saveAsOptions);
trans.Commit();
}
}
}
}