Track Override Graphics in View > By Element via Events

Track Override Graphics in View > By Element via Events

p_knight
Participant Participant
523 Views
1 Reply
Message 1 of 2

Track Override Graphics in View > By Element via Events

p_knight
Participant
Participant

I have and IExternalDBApplication that tracks a few key metrics across an office (like sync times, loading families etc).

 

I would like to track when element graphics are overridden in a view. I can't work out how - or if it's possible. I would like the element that has been changed, and the view.

 

Currently I can just get the view, subscribing to the DocumentChanged Event - and I think my method may be crude?

public static void GetChangeType(Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
{
    IList<string> trans = e.GetTransactionNames();
    if (trans[0] == "View Specific Element Graphics")
    {
        ElementGraphics.Overridden(e); //report the view name and number
    }
}

 

When the Event fires overriding the graphics of an element in a view, the only thing in DocumentChangedEventArgs is the view.

Ideally, I'd get the element(s) so I could get the OverrideGraphicSettings object.

 

An option: get all elements and their OverrideGraphicSettings on a ViewActivated Event. Wait for the above transaction then get the OverrideGraphicSettings and compare?! This seems overly complicated and heavy.

Am I missing an efficient way to do this, or is it possibly a problem not worth the effort?

 

0 Likes
524 Views
1 Reply
Reply (1)
Message 2 of 2

p_knight
Participant
Participant

For what it's worth, I've asked ChatGPT in quite a few ways. This is the best I got, which suggests I might get the modified element (which is not in the EventArgs I'm getting):

 

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;

public class ElementGraphicsChangeHandler : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        UIApplication uiapp = commandData.Application;
        UIDocument uidoc = uiapp.ActiveUIDocument;
        Document doc = uidoc.Document;

        // Subscribe to the DocumentChanged event
        doc.Application.DocumentChanged += OnDocumentChanged;

        return Result.Succeeded;
    }

    private void OnDocumentChanged(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
    {
        Document doc = e.GetDocument();

        // Iterate through all added, modified, or deleted elements in the document
        foreach (ElementId modifiedId in e.GetAddedElementIds().Union(e.GetModifiedElementIds()).Union(e.GetDeletedElementIds()))
        {
            Element modifiedElement = doc.GetElement(modifiedId);

            // Check if the modification involves graphic overrides
            if (modifiedElement.IsElementCategoryOverriden(GraphicsCategoryType.LineColor))
            {
                // Get the active view
                View activeView = doc.ActiveView;
                
                // Get the ViewId and ElementId
                ElementId viewId = activeView.Id;
                ElementId elementId = modifiedElement.Id;

                // Do something with the ViewId and ElementId
                Console.WriteLine($"Element {modifiedElement.Name} graphics changed in view {activeView.Name}. ElementId: {elementId}, ViewId: {viewId}");
            }
        }
    }
}

 

 

In this code:

  1. We subscribe to the DocumentChanged event of the Revit application.
  2. In the event handler OnDocumentChanged, we iterate through the added, modified, or deleted elements in the DocumentChangedEventArgs object.
  3. For each modified element, we check if its graphic overrides have changed using the IsElementCategoryOverriden method.
  4. If the graphic overrides have changed, we retrieve the active view (doc.ActiveView) and the IDs of the modified element and the active view.
  5. You can then perform any further actions you need with the ElementId and ViewId.

This approach allows you to capture changes in element graphics made by a user. Remember to handle the event appropriately and unregister it when it's no longer needed to avoid memory leaks or unexpected behavior.

0 Likes