How to handle the firing of a Revit command with CommandEventArgs C#

How to handle the firing of a Revit command with CommandEventArgs C#

rita.aguiar
Advocate Advocate
1,634 Views
1 Reply
Message 1 of 2

How to handle the firing of a Revit command with CommandEventArgs C#

rita.aguiar
Advocate
Advocate

I would like to be able to register which commands are being used within the Revit aplication  C#.

To do so, I am trying with the CommandEventArgs.


I know that it is possible to register when the RevitCommandId is the same as e.g. "ID_OBJECTS_WALL" by using the ExecutedEventArgs:

 

 

AddInCommandBinding importBindingID_OBJECTS_WALL = application.CreateAddInCommandBinding(RevitCommandId.LookupCommandId("ID_OBJECTS_WALL"));
                importBindingID_WINDOW_CLOSE_HIDDEN.Executed += new EventHandler
                    <Autodesk.Revit.UI.Events.ExecutedEventArgs>((sender, arg) => application_Command(sender, arg, "ID_OBJECTS_WALL"));

 

 

However, I would like to register ANY command that the user fires and thus, it's not very feasible to use the ExecutedEventArgs with AddInCommandBinding for all Revit commands (+1200).

 

To do so, I am trying with the CommandEventArgs:

 

 

UIControlledApplication.CreateAddInCommandBinding(RevitCommandId) += new EventHandler
<Autodesk.Revit.UI.Events.CommandEventArgs>(application_Command);

 

But I am not sure which namespaces and classes to use to successfully handle this event, I'm not sure if I should use the UIControlledApplication, I'm not sure if I should use the CreateAddInCommandBinding. I have been looking at the Revit Api Docs but I couldn't figure it out.

 

Is it possible to register ANY command with the CommandEventArgs or the ExecutedEventArgs?

How can I achieve that?

 

Any help would be appretiated.

0 Likes
Accepted solutions (1)
1,635 Views
1 Reply
Reply (1)
Message 2 of 2

rita.aguiar
Advocate
Advocate
Accepted solution

Unfortunately it is not possible to register any command in Revit.

I ended up using this method:

 

 

        public void application_DocumentChanged(Object sender, DocumentChangedEventArgs args_changed)
        {
            // get document from event args_changed.
            Document doc = args_changed.GetDocument();

            // dump the element information
            ICollection<ElementId> addedElem = args_changed.GetAddedElementIds();
            foreach (ElementId id in addedElem)
            {
                DumpEventArgs(id, doc, "Added");
            }

            ICollection<ElementId> deletedElem = args_changed.GetDeletedElementIds();
            foreach (ElementId id in deletedElem)
            {
                DumpEventArgs(id, doc, "Deleted");
            }

            ICollection<ElementId> modifiedElem = args_changed.GetModifiedElementIds();
            foreach (ElementId id in modifiedElem)
            {
                DumpEventArgs(id, doc, "Modified");
            }
                                  
        }

 

And I also used

 

elem.Category.Name

to identify which element in being added or modified.

 

to retrieve this:

        private void DumpEventArgs(ElementId id, Document doc, string changeType)
        {
            Element elem = doc.GetElement(id);

            if (elem == null)
            {
                // this branch is for deleted element due to the deleted element cannot be retrieve from the document.

                MessageBox.Show($"The user {Environment.UserName} edited a document: {changeType} of unknown category");
            }

            else
            {
                MessageBox.Show($"The user {Environment.UserName} edited a document: {changeType} {elem.Category.Name}");
            }
        }

 

0 Likes