BIM World
A Professional BIM Learning Platform


Revit API: How to Verify Families Before Loading Them

Some time ago, I proposed that to maintain consistency in project data, loading families from unknown sources should be prohibited. Only families approved by the administrator and stored in the enterprise family library should be allowed. Therefore, Revit needs to verify the family by checking paths or other relevant information before loading it. Ideally, editing of the family should be restricted, and feedback should be sent to the administrator if the family fails to load. Here, we will first implement a basic self-check function during family loading through secondary development. If possible, we hope to expand this functionality further in the future.

The approach is to utilize the FamilyLoadingIntoDocument event in the Revit API. This event triggers when a family is loaded, allowing us to verify whether the family meets the set criteria. If it does, loading proceeds; otherwise, the family loading transaction is canceled. There are multiple ways to validate the family, but in this example, I check the family’s file path and a specific parameter within the family file. In practical applications, these conditions can be adapted to fit your needs.

Below is the sample code demonstrating this logic:

public class Class1 : IExternalApplication
{
    public Result OnStartup(UIControlledApplication application)
    {
        // Register event handler
        application.ControlledApplication.FamilyLoadingIntoDocument += new EventHandler<FamilyLoadingIntoDocumentEventArgs>(CheckFamily);
        return Result.Succeeded;
    }

    public Result OnShutdown(UIControlledApplication application)
    {
        // Unregister event handler
        application.ControlledApplication.FamilyLoadingIntoDocument -= new EventHandler<FamilyLoadingIntoDocumentEventArgs>(CheckFamily);
        return Result.Succeeded;
    }

    private void CheckFamily(object sender, FamilyLoadingIntoDocumentEventArgs e)
    {
        // Get the storage path of the family
        string familyPath = e.FamilyPath;

        if (familyPath != @"D:Test")
        {
            e.Cancel();
            TaskDialog.Show("Error", "Loading unaudited families is prohibited. Please contact administrator XXX.");
        }
        else
        {
            // Open the family document
            Document familyDoc = e.Document.Application.OpenDocumentFile(familyPath + "\" + e.FamilyName + ".rfa");
            // Create Family Manager
            FamilyManager familyManager = familyDoc.FamilyManager;
            // Get current family type
            FamilyType familyType = familyManager.CurrentType;

            if (familyType == null)
            {
                familyType = familyManager.NewType("Standard");
            }

            // Retrieve the parameter
            FamilyParameter familyParameter = familyManager.get_Parameter("Test Parameters");

            // Validate the parameter
            if (familyParameter == null || familyType.AsString(familyParameter) != "Allow loading")
            {
                if (e.Cancellable)
                {
                    e.Cancel();
                    TaskDialog.Show("Error", "Loading unaudited families is prohibited. Please contact administrator XXX.");
                }
            }

            familyDoc.Close();
        }
    }
}
xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Revit API: How to Verify Families Before Loading Them

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up