IUpdater を実装する

IUpdater を実装する

IUpdater インタフェースには次の 5 つのメソッドを実装する必要があります。

ドキュメントがアップデータによって変更された場合、そのドキュメントはアップデータの一意の ID を格納します。後でユーザがドキュメントを開いたときにアップデータがない場合は、アップデータに省略可能のフラグが設定されていないと、以前にドキュメントを編集したサード パーティのアップデータが使用できないとの警告が表示されます。既定ではアップデータは省略不可となっており、省略可能なアップデータは必要な場合にのみ使用する必要があります。

次のコードは、IUpdater インタフェースを実装し(新たに追加した壁の WallType を変更するため)、アップデータを OnStartup()メソッドに登録する簡単な例です。これは、アップデータの作成と使用に関する主要な側面をすべて表しています。

コード領域 25-1: 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";
        }
}