Disable or enable updater

Disable or enable updater

quocanhxd09
Contributor Contributor
4,070 Views
7 Replies
Message 1 of 8

Disable or enable updater

quocanhxd09
Contributor
Contributor

Hi everyone,

 

I have just write code 1 IExternalApplication  “Updater” and then I run it successful. It will run when Revit start up .

I have 1 ask : after I run it, I want to create 1 IExternalCommand for disable “Updater” and 1 IExternalCommand for enable “Updater”.

 

I try to use method “DisableUpdater” but no successful.

Please help me how to create it.

 

Qa.

 

Thank you !

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Windows.Media.Imaging;
using View = Autodesk.Revit.DB.View;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;
using Document = Autodesk.Revit.DB.Document;

namespace qa
{
    /// <inheritdoc />
    /// <remarks>
    /// This application's main class. The class must be Public.
    /// </remarks>
    public class UpdaterSommaire : IExternalApplication
    {
       
        // Both OnStartup and OnShutdown must be implemented as public method
        public Result OnStartup(UIControlledApplication application)
        {
            ElementId idparaPage = new ElementId(152713);
            ElementId idparaDate = new ElementId(152809);
            ElementId idparaDessine = new ElementId(334940);
            ElementId idparaPageName = new ElementId(335879);
            ElementId idparaIndice = new ElementId(335881);
            ElementId idparaVerifier = new ElementId(334942);

            // Register the ElementUpdater1
            ElementUpdater1 updater1 = new ElementUpdater1(application.ActiveAddInId);
            UpdaterRegistry.RegisterUpdater(updater1);

            ElementCategoryFilter catFilter = new ElementCategoryFilter(BuiltInCategory.OST_GenericAnnotation);
            UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Element.GetChangeTypeParameter(idparaPage));
            UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Element.GetChangeTypeParameter(idparaDate));
            UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Element.GetChangeTypeParameter(idparaDessine));
            UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Element.GetChangeTypeParameter(idparaPageName));
            UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Element.GetChangeTypeParameter(idparaIndice));
            UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Element.GetChangeTypeParameter(idparaVerifier));

            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            // nothing to clean up in this simple case
            // Unregister the ElementUpdater1
            UpdaterRegistry.UnregisterUpdater(new ElementUpdater1(application.ActiveAddInId).GetUpdaterId());
            return Result.Succeeded;
        }

       
    }
    /// <remarks>
    /// The "HelloWorld" external command. The class must be Public.
    /// </remarks>
    [Transaction(TransactionMode.Manual)]
    public class DisableUpater : IExternalCommand
    {
        public static bool enable;
        // The main Execute method (inherited from IExternalCommand) must be public
        public Result Execute(ExternalCommandData revit,
            ref string message, ElementSet elements)
        {
            
            //ElementUpdater1 updater1 = new ElementUpdater1(revit.Application.ActiveAddInId);
            //UpdaterRegistry.DisableUpdater(updater1);
            
            //ElementUpdater1.m_updateActive = false;
            enable = false;
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.Manual)]
    public class EnableUpater : IExternalCommand
    {
        public static bool enable;
        // The main Execute method (inherited from IExternalCommand) must be public
        public Result Execute(ExternalCommandData revit,
            ref string message, ElementSet elements)
        {
            //ElementUpdater1.m_updateActive = true;
            enable = true;
            return Result.Succeeded;
        }
    }


    public class ElementUpdater1 : IUpdater
    {
        //public static bool m_updateActive = true;
        private AddInId mAddinId;
        private UpdaterId mUpdaterId;

        public ElementUpdater1(AddInId id)
        {
            mAddinId = id;
            mUpdaterId = new UpdaterId(mAddinId, new Guid("B2059D9C-799A-48F1-9666-5AC54643D26E"));
        }
        #region IUpdater Members

        public void Execute(UpdaterData data)
        {
            Document doc = data.GetDocument();

            View activeView = doc.ActiveView;

            ICollection<Element> colGene = new FilteredElementCollector(doc, activeView.Id)
                .OfCategory(BuiltInCategory.OST_GenericAnnotation)
                .ToElements();

            string paramName = "Page";
            string paramIndice = "Indice";
            string paramDate = "Date";
            string paramDessine = "Dessine";
            string paramVerifie = "Verifie";


            
            HashSet<FamilyInstance> GenericTilte = new HashSet<FamilyInstance>();
            HashSet<FamilyInstance> lingSommaire = new HashSet<FamilyInstance>();

            foreach (Element element in colGene)
            {
                FamilyInstance familyInstance = element as FamilyInstance;
                if (familyInstance == null) continue;
                if (familyInstance.Name.Contains("A3")) GenericTilte.Add(familyInstance);
                if (familyInstance.Name.Contains("Ligne de Sommaire")) lingSommaire.Add(familyInstance);
            }

            
            foreach (Element ele in GenericTilte)
            {
                string StringPage = ele.LookupParameter(paramName).AsString();
                string stringindice = ele.LookupParameter(paramIndice).AsString();
                string stringdate = ele.LookupParameter(paramDate).AsString();
                string stringdessine = ele.LookupParameter(paramDessine).AsString();
                string stringverifie = ele.LookupParameter(paramVerifie).AsString();


                
                string stringPagename = ele.LookupParameter("PageName").AsString();
                foreach (Element ele2 in lingSommaire)
                {
                    if (ele2.LookupParameter("PageName").AsString() == stringPagename)
                    {
                        ele2.LookupParameter(paramName).Set(StringPage);
                        ele2.LookupParameter(paramIndice).Set(stringindice);
                        ele2.LookupParameter(paramDate).Set(stringdate);
                        ele2.LookupParameter(paramDessine).Set(stringdessine);
                        ele2.LookupParameter(paramVerifie).Set(stringverifie);

                    }
                }
            }


            
        }
        public string GetAdditionalInformation()
        {
            return "ElementUpdater1";
        }
        public ChangePriority GetChangePriority()
        {
            return ChangePriority.Annotations;
        }
        public UpdaterId GetUpdaterId()
        {
            return mUpdaterId;
        }
        public string GetUpdaterName()
        {
            return "ElementUpdater1";
        }
        #endregion
    }

}

 

0 Likes
Accepted solutions (2)
4,071 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

Independently of your question:

 

Since you have stored 

 

idparaPage

 

anyway, it would be much more safe and efficient to use that with GetParameter instead of LookupParameter to access the element parameters.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 8

jeremytammik
Autodesk
Autodesk
Accepted solution

To disable the updater, you could simply return straight back from the Execute method if 

 

m_updateActive

 

is false. That is what it was originally intended for, isn't it?

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 4 of 8

quocanhxd09
Contributor
Contributor

thank you , @jeremytammik

 

That's good idea !

I can not  get value "m_updateActive" in other class. It always return value "null"

 

Can you mark in my code, @jeremytammik ?

 

 

 

 

0 Likes
Message 5 of 8

jeremytammik
Autodesk
Autodesk
Accepted solution

Maybe it will help if you make it a static class variable.

 

And it will certainly help if you read up a bit on C# and .NET programming basics.

 

In this case, just search for 'access c# boolean variable from other class':

 

https://duckduckgo.com/?q=access+c%23+boolean+variable+from+other+class

 

Your question has nothing to do with the Revit API, it is very basic C# and .NET beginner stuff.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 6 of 8

quocanhxd09
Contributor
Contributor

yes, you're right. I am not programmer, and I have no experience in C# or Revit API.

I will try repair my code.

 

Thanks you so much ! @jeremytammik

Have a nice day !!!

0 Likes
Message 7 of 8

MarryTookMyCoffe
Collaborator
Collaborator

some time ago I made my registration of ElementUpdater the way that it will had a different icon while it is registered or not.

ofc you don't have to play in changing button icon

            String tabName = "your tab name";
            String ribbonPanelName = "Test";
            String buttonName = "Test Button";

            List<RibbonPanel> ribbonPanels = uiapp.GetRibbonPanels(tabName);
            RibbonPanel ribbonPanel = null;
            if (ribbonPanels.Exists(e => e.Name == ribbonPanelName))
            {
                ribbonPanel = ribbonPanels.Find(e => e.Name == ribbonPanelName);
            }

            if (ribbonPanel != null)
            {
                ElementUpdater updater1 = new ElementUpdater(uiapp.ActiveAddInId);
                foreach (RibbonItem ribbonItem in ribbonPanel.GetItems())
                {
                    if (ribbonItem.ItemType == RibbonItemType.PushButton)
                    {
                        if (ribbonItem.Name == $"{ buttonName }")
                        {
                            if (UpdaterRegistry.IsUpdaterRegistered(updater1.GetUpdaterId()))
                            {

                                UpdaterRegistry.UnregisterUpdater(updater1.GetUpdaterId());

                                (ribbonItem as PushButton).LargeImage = App.BitmapToBitmapImage(Resources.Off);
                            }
                            else
                            {
                                UpdaterRegistry.RegisterUpdater(updater1);
                                ElementCategoryFilter catFilter = new ElementCategoryFilter(Autodesk.Revit.DB.BuiltInCategory.OST_PipeCurves);
                                UpdaterRegistry.AddTrigger(updater1.GetUpdaterId(), catFilter, Element.GetChangeTypeElementAddition());
                                (ribbonItem as PushButton).LargeImage = App.BitmapToBitmapImage(Resources.On);
                            }

                        }
                    }
                }
            }


ElementUpdater class I used:

    public class ElementUpdater : IUpdater
    {
        private AddInId mAddinId;
        private UpdaterId mUpdaterId;


        public ElementUpdater(AddInId id)
        {
            mAddinId = id;
            mUpdaterId = new UpdaterId(mAddinId, new Guid("8a5b141a-cd9e-4c12-9f99-168c3ddb1de2"));
        }

        #region IUpdater Members

        public void Execute(UpdaterData data)
        {
            //code

        }

        public string GetAdditionalInformation()
        {
            return "ElementUpdater";
        }

        public ChangePriority GetChangePriority()
        {
            return Autodesk.Revit.DB.ChangePriority.MEPSystems;
        }

        public UpdaterId GetUpdaterId()
        {
            return mUpdaterId;
        }

        public string GetUpdaterName()
        {
            return "ElementUpdater";
        }

        #endregion
    }

remember to change guid.

 

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes
Message 8 of 8

quocanhxd09
Contributor
Contributor

Hi MarryTookMyCoffe,

 

Thanks for your answer.

 

I will try with your idea.

 

qa.

 

0 Likes