Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

1 REPLY 1
SOLVED
Reply
Message 1 of 2
rita.aguiar
1245 Views, 1 Reply

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

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.

1 REPLY 1
Message 2 of 2
rita.aguiar
in reply to: rita.aguiar

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

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community