The IFamilyLoadOptions interface of Revit API can be implemented to do something when a family is being loaded into a model, especially to handle cases like family conflict and parameter overwritten.
It provides two methods to be implemented, the OnFamilyFound () and the OnSharedFamilyFound (). The former will be called when the family has already been there and the latter the shared family is found in the model.
Here is a sample implementation:
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
#endregion
namespace RevitAddinCSProject
{
public class SampleFamilyLoadOptions : IFamilyLoadOptions
{
#region IFamilyLoadOptions Members
public bool OnFamilyFound(bool familyInUse, ref bool overwriteParameterValues)
{
if( !familyInUse )
{
TaskDialog.Show("SampleFamilyLoadOptions", "The family has not been in use and will keep loading.");
overwriteParameterValues = true;
return true;
}
else
{
TaskDialog.Show("SampleFamilyLoadOptions", "The family has been in use but will still be loaded with existing parameters overwritten.");
overwriteParameterValues = true;
return true;
}
}
public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, ref FamilySource source, ref bool overwriteParameterValues)
{
if (!familyInUse)
{
TaskDialog.Show("SampleFamilyLoadOptions", "The shared family has not been in use and will keep loading.");
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
else
{
TaskDialog.Show("SampleFamilyLoadOptions", "The shared family has been in use but will still be loaded from the FamilySource with existing parameters overwritten.");
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
}
#endregion
}
}
If the IFamilyLoadOptions implementation is linked with a LoadFamily() method call like the following:
Transaction trans = new Transaction(CachedDoc, "Test LoadFamily with SampleFamilyLoadOptions");
trans.Start();
Family family;
CachedDoc.LoadFamily("c:/temp/fixed.rfa", new SampleFamilyLoadOptions(), out family);
TaskDialog.Show("Test LoadFamily with SampleFamilyLoadOptions", family.Name);
trans.Commit();
and the code is called by an external command, the following TaskDialog will appear:
After the TaskDialog is dismissed the family will continue to be loaded with existing parameters overwritten as stated.
Family Optional Loader of RevitAddinWizard can help implement the interface with all possible options and combinations automatically in no time.
Hi. I'm trying to use this as a macro in Revit 2013. I'm getting: 'Test.ThisApplication.myFamilyLoadOptions' does not implement interface member 'Autodesk.Revit.DB.IFamilyLoadOptions.OnFamilyFound(bool, out bool)'
error for both public bool's. Any idea on what I might be doing wrong?
Posted by: Michael Coffey | 08/16/2013 at 12:12 PM
Michael, the code sample was for Revit 2012 API and earlier. Revit 2013 API has changed the signatures of the OnFamilyFound members, as discussed in the following post:
Revit .NET 2013: the IFamilyLoadOptions Interface
http://spiderinnet.typepad.com/blog/2012/12/revit-net-2013-the-ifamilyloadoptions-interface.html
It's easy to solve, replacing the 'ref' with 'out' as pointed out.
Posted by: Spiderinnet | 08/16/2013 at 03:20 PM
Thanks. I did find the other post and fixed that, but can't seem to get a family to re-load once they are in. I've read in other places that it might be a bug. Do you know if thats true? Thanks.
Posted by: Michael Coffey | 08/16/2013 at 11:42 PM
Michael, glad to know you figured it out. In terms of the reload issue, it might be a bug; but without seeing it in detail, we are not sure at this moment.
Posted by: Spiderinnet | 08/17/2013 at 02:55 AM