Disable Updater with ExternalCommand

Disable Updater with ExternalCommand

Nathan.HilsonN67V6
Advocate Advocate
707 Views
5 Replies
Message 1 of 6

Disable Updater with ExternalCommand

Nathan.HilsonN67V6
Advocate
Advocate

I'm running into a similar issue same as this post. 

 

Solved: Disable or enable updater - Autodesk Community - Revit Products

 

I keep getting the object reference isn't set to an instance when it clearly is unless I'm missing something that's going on internally. Below is the command that I'm using: 

 

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                //UpdaterRegistry.DisableUpdater(new assemblyLockUpdater.GetUpdaterId());
                //UpdaterRegistry.UnregisterUpdater(new AssemblyLockUpdaterId(application.ActiveAddInId).GetUpdaterId());
                //UpdaterRegistry.RemoveAllTriggers(assemblyLockUpdater.GetUpdaterId());
                //UpdaterRegistry.EnableUpdater(assemblyLockUpdater.GetUpdaterId());
                UpdaterRegistry.DisableUpdater(assemblyLockUpdater.GetUpdaterId());
                return Result.Succeeded;
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message); 
                return Result.Failed;
            }
        }

 

This is how I have it registered in the application: 

 

            {
                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.";
                                
            }
0 Likes
Accepted solutions (1)
708 Views
5 Replies
Replies (5)
Message 2 of 6

Kennan.Chen
Advocate
Advocate

I don't see where the assemblyLockUpdater instance get initialized in you external command.

0 Likes
Message 3 of 6

Nathan.HilsonN67V6
Advocate
Advocate

I created that in a separate class. The updater works fine it's just disabling it that I'm having issues with: 

 

 

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

 

0 Likes
Message 4 of 6

Kennan.Chen
Advocate
Advocate
You use it directly in your external command. I don't see any connection between your command and the AssemblyLockUpdaterId class. Can you provide all the code of your external command?
0 Likes
Message 5 of 6

Nathan.HilsonN67V6
Advocate
Advocate

 

I was trying a few things out so it's not currently functional:

{
    [Transaction(TransactionMode.Manual)]
    public class AssemblyUpdaterId : IExternalCommand
    {
        private static UtilsAssemblyLock.AssemblyLockUpdaterId assemblyLockUpdater;
        private static UtilsAssemblyLock utilsAssemblyLock;
        public static bool enable;
        public static void DisableUpdater(UtilsAssemblyLock utilsAssemblyLock)
        {
           var utilsAssemblyLockUpdater = utilsAssemblyLock;
            if (utilsAssemblyLockUpdater != null)
            {
                enable = false;
            }

            enable = false;
            return;
        }
        public static UIControlledApplication application;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                //UpdaterRegistry.DisableUpdater(new assemblyLockUpdater.GetUpdaterId());
                //UpdaterRegistry.UnregisterUpdater(new AssemblyLockUpdaterId(application.ActiveAddInId).GetUpdaterId());
                //UpdaterRegistry.RemoveAllTriggers(assemblyLockUpdater.GetUpdaterId());
                //UpdaterRegistry.EnableUpdater(assemblyLockUpdater.GetUpdaterId());
                UpdaterRegistry.DisableUpdater(assemblyLockUpdater.GetUpdaterId());
                return Result.Succeeded;
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message); 
                return Result.Failed;
            }
        }
    }


}

 

0 Likes
Message 6 of 6

Nathan.HilsonN67V6
Advocate
Advocate
Accepted solution

Figured it out. I added a constructor to the utility class & cleaned up my command class. Here's both.

 

 

//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).GetUpdaterId());
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                            return Result.Failed;
                        }

                        finally
                        {
                            transaction.Commit();
                        }
                      
                    }
                    return Result.Succeeded;
                }
            }
            
        }  
}

 

 

//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 AssemblyLockUpdaterId()
            {
            }

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