Iupdater Dynamic Model Update Dimensions

Iupdater Dynamic Model Update Dimensions

Mark.Ackerley
Advocate Advocate
1,783 Views
4 Replies
Message 1 of 5

Iupdater Dynamic Model Update Dimensions

Mark.Ackerley
Advocate
Advocate

Hi All,

 

I have managed to write C# which sets Dimension suffix's and colour style depending on the value of the Dimension.

https://forums.autodesk.com/t5/revit-api-forum/code-crit/m-p/9625232#M48334

 

I am now wanting those overrides to update if a dimension value changes.  I have followed this post primarily (the ribbons and buttons are off in a different .CS and the external command is the only bit which is definitely working!)

https://thebuildingcoder.typepad.com/blog/2010/08/structural-dynamic-model-update-sample.html

 

But when I couldn't get it to work, I looked around and tried suggestions in this post...

https://forums.autodesk.com/t5/revit-api-forum/iupdater-get-family-instance-and-update-values/m-p/90...

Also

https://boostyourbim.wordpress.com/2013/07/13/live-link-between-parameters-in-model-detail-families/

 

So I've now hit a bit of a wall!  It runs with no errors, but nothing happens! Any suggestions gratefully received.

 

Cheers,

 

Mark

 

 

using System;
using System.Text;
using System.Linq;

using System.Collections.Generic;

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;


namespace BrickDims
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class UpdateBrickDims : IExternalApplication
    {
        /// <summary>
        /// On shutdown, unregister the updater
        /// </summary>
        /// 

        public Result OnShutdown(UIControlledApplication a)
        {
            BrickDimUpdater updater = new BrickDimUpdater(a.ActiveAddInId);
            UpdaterRegistry.UnregisterUpdater(updater.GetUpdaterId());

            return Result.Succeeded;
        }

        /// <summary>
        /// On start up, add UI buttons, register 
        /// the updater and add triggers
        /// </summary>
        public Result OnStartup(UIControlledApplication a)
        {

            BrickDimUpdater updater = new BrickDimUpdater(a.ActiveAddInId);

            // Register the updater in the singleton 
            // UpdateRegistry class
            UpdaterRegistry.RegisterUpdater(updater);


            // Set the filter
            //ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
            //ElementOwnerViewFilter filterView = new ElementOwnerViewFilter(doc.ActiveView.Id);
            //ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Dimensions);
            //LogicalAndFilter filter = new LogicalAndFilter(filterView, filterClass);
            // Add trigger 
            //UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), filter, Element.GetChangeTypeAny());
            if (!UpdaterRegistry.IsUpdaterRegistered(updater.GetUpdaterId()))
            {
                UpdaterRegistry.RegisterUpdater(updater, true);
                ElementCategoryFilter f = new ElementCategoryFilter(BuiltInCategory.OST_Dimensions);
                UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), f, Element.GetChangeTypeAny());
            }
            return Result.Succeeded;
        }

    }

    public class BrickDimUpdater : IUpdater
    {
        public static bool m_updateActive = false;
        AddInId addinID = null;
        UpdaterId updaterID = null;

        public BrickDimUpdater(AddInId id)
        {
            addinID = id;
            // UpdaterId that is used to register and 
            // unregister updaters and triggers
            updaterID = new UpdaterId(addinID, new Guid(
              "63CDBB88-5CC4-4ac3-AD24-52DD435AAB25"));
            Console.WriteLine("Standard Numeric Format Specifiers3");
        }

        public int caseswitch = 1;

        /// <summary>
        /// Colour Code Dimensions
        /// </summary>
        public void Execute(UpdaterData data)
        {
            TaskDialog.Show("case 2", "Check");
            if (m_updateActive == false) { return; }

            // Get access to document object
            Document doc = data.GetDocument();
            UIDocument uidoc = new UIDocument(doc);

            //using (Transaction t = new Transaction(doc, "Update Dim"))
            //{
             //   t.Start();
            try
            {
                TaskDialog.Show("case 2", "Check");
                switch (caseswitch)
                {
                    case 1:
                        // Loop through all the modified elements
                        ICollection<ElementId> modifiedCollection = data.GetModifiedElementIds();
                        foreach (ElementId elemId in modifiedCollection)
                        {
                        Dimension dim = doc.GetElement(elemId) as Dimension;
                        DimClrChecker.DimClrChngSuffix(dim, uidoc);
                        TaskDialog.Show("case 2", "Check");
                        }
                    caseswitch = 2;
                    break;

                    case 2:
                    TaskDialog.Show("case 2", "SampleUpdater");

                    caseswitch = 1;
                    break;
                }

            }
            catch (Exception ex)
            {
                TaskDialog.Show("Exception", ex.Message);
            }

                //t.Commit();
            //}
        }
        /// <summary>
        /// Set the priority
        /// </summary>
        public ChangePriority GetChangePriority()
        {
            return ChangePriority.Annotations;
        }

        /// <summary>
        /// Return the updater Id
        /// </summary>
        public UpdaterId GetUpdaterId()
        {
            return updaterID;
        }

        /// <summary>
        /// Return the auxiliary string
        /// </summary>
        public string GetAdditionalInformation()
        {
            return "Automatically update dimension overrides";
        }

        /// <summary>
        /// Return the updater name
        /// </summary>
        public string GetUpdaterName()
        {
            return "Dimension Override Updater";
        }
    }

    [Transaction(TransactionMode.ReadOnly)]
    [Regeneration(RegenerationOption.Manual)]
    public class UIDynamicModelUpdateOff : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            BrickDimUpdater.m_updateActive = false;
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.ReadOnly)]
    [Regeneration(RegenerationOption.Manual)]
    public class UIDynamicModelUpdateOn : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            BrickDimUpdater.m_updateActive = true;
            return Result.Succeeded;
        }
    }
}

 

 

 

 

0 Likes
Accepted solutions (1)
1,784 Views
4 Replies
Replies (4)
Message 2 of 5

jeremytammik
Autodesk
Autodesk

I see no problems off-hand.

 

RegenerationOption has no effect and can be removed everywhere. Saves you visual complexity, rests you eyes and mind.

  

First you register the updater, then check whether it was registered, and then register it again. That seems like a bit of overkill.

 

I would replace all calls to taskDialog by Console.Log or something else that does not interact with the user. The UI interruption may well disturb the updater transaction handling.

 



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

Message 3 of 5

Mark.Ackerley
Advocate
Advocate

 

Thanks Jeremy 🙂

 

As you can tell, I was starting to get to the desperate 'throw anything and see if it helps' stage!

 

I've stripped it right back but it's still sitting there refusing to work...

 

As an aside, when I use Console.Write("test", "test") in debug mode, I don't get anything displayed in the VS Output (Debug).

 

Here's the slightly modified stripped back code...

 

Cheers,

 

Mark

 

using System;
using System.Text;
using System.Linq;

using System.Collections.Generic;

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.Attributes;


namespace BrickDims
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    public class UpdateBrickDims : IExternalApplication
    {
        /// <summary>
        /// On shutdown, unregister the updater
        /// </summary>
        /// 

        public Result OnShutdown(UIControlledApplication a)
        {
            BrickDimUpdater updater = new BrickDimUpdater(a.ActiveAddInId);
            UpdaterRegistry.UnregisterUpdater(updater.GetUpdaterId());

            return Result.Succeeded;
        }

        /// <summary>
        /// On start up, add UI buttons, register 
        /// the updater and add triggers
        /// </summary>
        public Result OnStartup(UIControlledApplication a)
        {
            // Add the UI buttons on start up
            AddButtons(a);

            BrickDimUpdater updater = new BrickDimUpdater(a.ActiveAddInId);

            // Register the updater in the singleton 
            // UpdateRegistry class
            UpdaterRegistry.RegisterUpdater(updater);


            // Set the filter
            //ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_StructuralFraming);
            //ElementOwnerViewFilter filterView = new ElementOwnerViewFilter(doc.ActiveView.Id);
            //ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Dimensions);
            //LogicalAndFilter filter = new LogicalAndFilter(filterView, filterClass);
            // Add trigger 
            //UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), filter, Element.GetChangeTypeAny());
            if (!UpdaterRegistry.IsUpdaterRegistered(updater.GetUpdaterId()))
            {
                UpdaterRegistry.RegisterUpdater(updater, true);
                ElementCategoryFilter f = new ElementCategoryFilter(BuiltInCategory.OST_Dimensions);
                UpdaterRegistry.AddTrigger(updater.GetUpdaterId(), f, Element.GetChangeTypeAny());
            }
            return Result.Succeeded;
        }

        /// <summary>
        /// Add UI buttons
        /// </summary>
        public void AddButtons(UIControlledApplication a)
        {
            // create a ribbon panel on the Analyze tab
            RibbonPanel panel = a.CreateRibbonPanel(
              Tab.AddIns, "RST Labs");

            AddDmuCommandButtons(panel);
        }


        /// <summary>
        /// Control buttons for the Dynamic Model Update 
        /// </summary>
        public void AddDmuCommandButtons(RibbonPanel panel)
        {
            string path = GetType().Assembly.Location;

            string sDirectory = System.IO.Path.GetDirectoryName(path);

            // create toggle buttons for radio button group 

            ToggleButtonData toggleButtonData3
              = new ToggleButtonData(
                "RSTLabsDMUOff", "Align Off", path,
                "BrickDims.UIDynamicModelUpdateOff");


            ToggleButtonData toggleButtonData4
              = new ToggleButtonData(
                "RSTLabsDMUOn", "Align On", path,
                "BrickDims.UIDynamicModelUpdateOn");

            // make dyn update on/off radio button group 

            RadioButtonGroupData radioBtnGroupData2 =
              new RadioButtonGroupData("RebarAlign");

            RadioButtonGroup radioBtnGroup2
              = panel.AddItem(radioBtnGroupData2)
                as RadioButtonGroup;

            radioBtnGroup2.AddItem(toggleButtonData3);
            radioBtnGroup2.AddItem(toggleButtonData4);
        }
    }

    public class BrickDimUpdater : IUpdater
    {
        public static bool m_updateActive = false;
        AddInId addinID = null;
        UpdaterId updaterID = null;

        public BrickDimUpdater(AddInId id)
        {
            addinID = id;
            // UpdaterId that is used to register and 
            // unregister updaters and triggers
            updaterID = new UpdaterId(addinID, new Guid(
              "63CDBB88-5CC4-4ac3-AD24-52DD435AAB25"));
            Console.WriteLine("Standard Numeric Format Specifiers3");
        }

        public int caseswitch = 1;

        /// <summary>
        /// Colour Code Dimensions
        /// </summary>
        public void Execute(UpdaterData data)
        {
            TaskDialog.Show("case 2", "Check");
            if (m_updateActive == false) { return; }

            // Get access to document object
            Document doc = data.GetDocument();
            UIDocument uidoc = new UIDocument(doc);

            //using (Transaction t = new Transaction(doc, "Update Dim"))
            //{
            //   t.Start();
            try
            {
                TaskDialog.Show("case 2", "Check");
                switch (caseswitch)
                {
                    case 1:
                        // Loop through all the modified elements
                        ICollection<ElementId> modifiedCollection = data.GetModifiedElementIds();
                        foreach (ElementId elemId in modifiedCollection)
                        {
                            Dimension dim = doc.GetElement(elemId) as Dimension;
                            DimClrChecker.DimClrChngSuffix(dim, uidoc);
                        }
                        caseswitch = 2;
                        break;

                    case 2:
                        TaskDialog.Show("case 2", "SampleUpdater");

                        caseswitch = 1;
                        break;
                }

            }
            catch (Exception ex)
            {
                TaskDialog.Show("Exception", ex.Message);
            }

            //t.Commit();
            //}
        }
        /// <summary>
        /// Set the priority
        /// </summary>
        public ChangePriority GetChangePriority()
        {
            return ChangePriority.Annotations;
        }

        /// <summary>
        /// Return the updater Id
        /// </summary>
        public UpdaterId GetUpdaterId()
        {
            return updaterID;
        }

        /// <summary>
        /// Return the auxiliary string
        /// </summary>
        public string GetAdditionalInformation()
        {
            return "Automatically update dimension overrides";
        }

        /// <summary>
        /// Return the updater name
        /// </summary>
        public string GetUpdaterName()
        {
            return "Dimension Override Updater";
        }
    }

    [Transaction(TransactionMode.ReadOnly)]
    [Regeneration(RegenerationOption.Manual)]
    public class UIDynamicModelUpdateOff : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            BrickDimUpdater.m_updateActive = false;
            return Result.Succeeded;
        }
    }

    [Transaction(TransactionMode.ReadOnly)]
    [Regeneration(RegenerationOption.Manual)]
    public class UIDynamicModelUpdateOn : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            BrickDimUpdater.m_updateActive = true;
            return Result.Succeeded;
        }
    }
}

 

0 Likes
Message 4 of 5

jeremytammik
Autodesk
Autodesk
Accepted solution

Well, scrap the Console stuff if it does not work anyway.

  

Personally, I use Debug.Print to log messages on the VS debug console.

  

You still have a taskDialog call in your Execute method.

  

As already pointed out, that may well seriously disrupt behaviour.

  



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

Message 5 of 5

Mark.Ackerley
Advocate
Advocate

Thanks Jeremy,

 

Apologies, that definitely didn't help!  Also, I didn't have all the bits of start up / shut down code in the main external ribbon, so now it works great 🙂

 

Cheers,

 

Mark

0 Likes