努力禁用更新程序

问题描述 投票:0回答:1

我无法让它发挥作用。我认为我在转换方面做错了什么。我只想在将更新程序实施到应用程序中后禁用它。

下面按顺序是应用程序初始化、实用程序类和禁用程序类。

应用程序启动

try
{
    UtilsAssemblyLock.AssemblyLockUpdaterId assemblyLockUpdater =
    new UtilsAssemblyLock.AssemblyLockUpdaterId(application
        .ActiveAddInId);
    UpdaterRegistry.RegisterUpdater(assemblyLockUpdater, true);
    UpdaterRegistry.AddTrigger(
        assemblyLockUpdater.GetUpdaterId(),
        new ElementClassFilter(typeof(FabricationPart)),
        Element.GetChangeTypeParameter(new ElementId(BuiltInParameter.FABRICATION_PART_WEIGHT)));

    UtilsAssemblyLock.assemblyLockedFailureId =
        new FailureDefinitionId(Guid.NewGuid());
    FailureDefinition.CreateFailureDefinition(
        UtilsAssemblyLock.assemblyLockedFailureId,
        FailureSeverity.Error,
        "S.A.I.: \n" + "Cannot edit assembly without permission.");

}
catch (Exception ex3)
{
    MessageBox.Show(ex3.Message);
    return Result.Cancelled;
}

实用类

public class UtilsAssemblyLock
{
    public static FailureDefinitionId assemblyLockedFailureId;

    public class AssemblyLockUpdaterId : IUpdater
    {
        private readonly UpdaterId assemblyLockUpdaterId;
        private FabricationDimensionDefinition _dimensionDefinition;
        public AssemblyLockUpdaterId(AddInId id)
        {
            assemblyLockUpdaterId = new UpdaterId(id, new Guid("7595F412-275B-4F65-A354-7F9CB439283B"));
        }
        public void Execute(UpdaterData data)
        {
            try
            {
                Document doc = data.GetDocument();
                List<ElementId> ids = new List<ElementId>();
                ids.AddRange(data.GetAddedElementIds());
                ids.AddRange(data.GetModifiedElementIds());
                ids.AddRange(data.GetDeletedElementIds());
                foreach (ElementId id in ids)
                {
                    if (doc.GetElement(id) is FabricationPart fabricationPart)
                    {
                        if (!data.IsChangeTriggered(id, Element.GetChangeTypeParameter(new ElementId(BuiltInParameter.FABRICATION_PART_WEIGHT))))
                        {
                            break;
                        }
                        if (!fabricationPart.IsValidObject)
                            continue;
                        #region Elements Are Assembled
                        if (!fabricationPart.AssemblyInstanceId.IsNull())
                        {

                            Console.Beep();
                            AssemblyPassword assemblyPasswordForm = new AssemblyPassword();
                            assemblyPasswordForm.ShowDialog();

                            if (assemblyPasswordForm.assemblyPasswordEntry() == "1234" ||
                                assemblyPasswordForm.assemblyPasswordEntry() == "abcd")
                            {

                            }
                            else 
                            {
                                FailureMessage failureMessage = new FailureMessage(assemblyLockedFailureId);
                                failureMessage.SetFailingElement(id);
                                doc.PostFailure(failureMessage);
                            }
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public UpdaterId GetUpdaterId()
        {
            UpdaterId id = assemblyLockUpdaterId;
            return (id);
        }
        public ChangePriority GetChangePriority()
        {
            return ChangePriority.MEPSystems;
        }
        public string GetUpdaterName()
        {
            return "Fabrication Assembly Updater";
        }
        public string GetAdditionalInformation()
        {
            return "Fabrication Pipe Assembly updater example: checks to make sure Pipe that has been assembled " +
                "is not edited.";
        }
    }
}

禁用命令

{

    [Transaction(TransactionMode.Manual)]
    //[Journaling(JournalingMode.NoCommandData)]
    public class DissableAssemblyLock : IExternalCommand
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            var transaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "Dissable Assembly Lock");
            using (transaction)
            {
                if (transaction.Start() == TransactionStatus.Started)
                {
                    try
                    {
                    UpdaterRegistry.DisableUpdater(new AssemblyLockUpdaterId(commandData.Application.ActiveAddInId));
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                        return Result.Failed;
                    }

                    finally
                    {
                        transaction.Commit();
                    }
                  
                }
                return Result.Succeeded;
            }
        }
    }  
}
c# type-conversion revit-api
1个回答
1
投票

刚刚发现我做错了什么。我添加了一个构造函数并修复了更新函数中的参数。

{

    [Transaction(TransactionMode.Manual)]
    //[Journaling(JournalingMode.NoCommandData)]
    public class DissableAssemblyLock : IExternalCommand
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            var transaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "Dissable Assembly Lock");
            using (transaction)
            {
                if (transaction.Start() == TransactionStatus.Started)
                {
                    try
                    {
                    UpdaterRegistry.DisableUpdater(new AssemblyLockUpdaterId(commandData.Application.ActiveAddInId).GetUpdaterId());
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                        return Result.Failed;
                    }

                    finally
                    {
                        transaction.Commit();
                    }
                  
                }
                return Result.Succeeded;
            }
        }
    }  
}
© www.soinside.com 2019 - 2024. All rights reserved.