Implementing IUpdater

Implementing IUpdater

The IUpdater interface requires that the following 5 methods to be implemented:

If a document is modified by an Updater, the document will store the unique Id of the updater. If the user later opens the document and the Updater is not present, Revit will warn the user that the 3rd party updater which previously edited the document is not available unless the Updater is flagged as optional. By default, updaters are non-optional and optional updaters should be used only when necessary.

The following code is a simple example of implementing the IUpdater interface (to change the WallType for newly added walls) and registering the updater in the OnStartup() method. It demonstrates all the key aspects of creating and using an updater.

Code Region 25-1: Example of implementing IUpdater

public class WallUpdaterApplication : Autodesk.Revit.UI.IExternalApplication
{
        public Result OnStartup(Autodesk.Revit.UI.UIControlledApplication application)
        {
                // Register wall updater with Revit
                WallUpdater updater = new WallUpdater(application.ActiveAddInId);
                UpdaterRegistry.RegisterUpdater(updater);
 
                // Change Scope = any Wall element
                ElementClassFilter wallFilter = new ElementClassFilter(typeof(Wall));
 
                // Change type = element addition
                UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), wallFilter, Element.GetChangeTypeElementAddition());
                return Result.Succeeded;
        }
 
        public Result OnShutdown(Autodesk.Revit.UI.UIControlledApplication application)
        {
                WallUpdater updater = new WallUpdater(application.ActiveAddInId);
                UpdaterRegistry.UnregisterUpdater(updater.GetUpdaterId());
                return Result.Succeeded;
        }
}

public class WallUpdater : IUpdater
{
        static AddInId m_appId;
        static UpdaterId m_updaterId;
        WallType m_wallType = null;
        
        // constructor takes the AddInId for the add-in associated with this updater
        public WallUpdater(AddInId id)
        {
                m_appId = id;
                m_updaterId = new UpdaterId(m_appId, new Guid("FBFBF6B2-4C06-42d4-97C1-D1B4EB593EFF"));
        }
 
        public void Execute(UpdaterData data)
        {
                Document doc = data.GetDocument();
 
                // Cache the wall type
                if (m_wallType == null)
                {
                        FilteredElementCollector collector = new FilteredElementCollector(doc);
                        collector.OfClass(typeof(WallType));
                        var wallTypes = from element in collector
                                                        where
                                                                element.Name == "Exterior - Brick on CMU"
                                                        select element;
                        if (wallTypes.Count<Element>() > 0)
                        {
                                m_wallType = wallTypes.Cast<WallType>().ElementAt<WallType>(0);
                        }
                }
        
                if (m_wallType != null)
                {
                        // Change the wall to the cached wall type.
                        foreach (ElementId addedElemId in data.GetAddedElementIds())
                        {
                                Wall wall = doc.GetElement(addedElemId) as Wall;
                                if (wall != null)
                                {
                                        wall.WallType = m_wallType;
                                }
                        }
                }
        }

        public string GetAdditionalInformation()
        {
                return "Wall type updater example: updates all newly created walls to a special wall";
        }
 
        public ChangePriority GetChangePriority()
        {
                return ChangePriority.FloorsRoofsStructuralWalls;
        }
 
        public UpdaterId GetUpdaterId()
        {
                return m_updaterId;
        }
        
        public string GetUpdaterName()
        {
                return "Wall Type Updater";
        }
}