Events in the Fusion API

Events allow you to receive notifications when specific actions occur within Fusion. Events are a crucial component when creating custom Fusion commands. Through events, your program can know that the user clicked the button associated with your command and interact with the user while your command is running (preview, input validation, selection, execute, etc.).

To implement an event, you need to add a "handler" function to your code and connect that handler to the event. Fusion will call your handler function whenever the related action occurs in Fusion that causes the event to fire. Even though the concept of implementing an event is the same, the actual practice differs from one language to another. Therefore, specifics for setting up events are provided in the topics covering each supported language; Python and C++. In fact, for Python, there is a Fusion library provided to simplify using events. You can read more about it in the topic about the Fusion Python Add-In Template.

One thing to be aware of that can significantly simplify adding support for events is the "Syntax" portion of the help for every event shows example code for the event handler and connecting the handler to the event. You can copy and paste this code from the help, make minor edits to a couple of variable names, and you'll have the event implemented. There are two tabs for Python and one for C++. The reason for the two tabs with Python is that the tab labeled "Python Using fusion360utils" uses a library provided when you create a Python add-in. This library simplifies the implementation of events. The "Python" tab illustrates implementing an event handler without help from an external library.

Event Sample


Some basic concepts apply to all events, regardless of your programming language.

  1. The initial access to an event is through a property on the object that supports the event. For example, the UserInterface object supports the activeSelectionChanged, commandCreated, commandStarting, and several other events. These are accessed through properties of the same name on the UserInterface object. Events are listed in an "Events" section within the help topic of the object, as shown below.

  2. Workspace Events


  3. The object returned by the event property is an object that derives from the Event base class. Any object derived from Event supports the "add" method, which you call to connect the event to your handler. Event objects also support the "remove" method to disconnect a handle, they support the "name" property, which returns the name of the event, and they support the "sender" property, which returns the object that is firing the event. For example, the ApplicationCommandEvent object returned by the commandStarting event will return the UserInterface object as the event's sender.
  4. When implementing the handler, the handler type must match the type of event. For example, in the case of the commandStarting event, the event type is ApplicationCommandEvent, and the handler is ApplicationCommandEventHandler. In addition, all handlers support a single method named "notify". The notify method is called by Fusion when the event occurs and by reacting to the notify method, you can handle the event.
  5. The notify method has a single argument that provides an object derived from EventArgs. This object provides information about the event being fired. All objects derived from EventArgs support the firingEvent property, which returns the event object that is firing the event. In this example of the commandStarting event, it will return the ApplicationCommandEvent object. Most objects derived from EventArgs also support other properties that provide additional information specific to that type of event. For example, in the case of the notify method for the commandStarting event, an ApplicationCommandEventArgs object is returned and supports the commandDefinition, commandId, and isCanceled properties specific to application command events. These provide information about which command is starting and allow you to cancel it.