Search code examples
c#type-conversionrevit-apiupdaters

Struggling with disabling Updater


I've been at this for a while now and have had no luck in getting this to work. I think I'm doing something wrong with the conversions. Can anyone help me out? Basically I just want to disable the updater after it has been implemented into the app.

Below in order is the App Initialization, Utility class, & the Disabler class.

            //App Initiate
            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;
        }

//utility class

{
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.";
        }
    }
}

}

//Disable command {

    [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;
            }
        }
        
    }  

}


Solution

  • Just figured out what I was doing wrong. I added a constructor & fixed the argument in the update function.

    {
    
        [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;
                }
            }
            
        }  
    

    }