Vault Explorer Command Events

Vault 2012 was a very eventful release for the API in the sense that we added a 3 event engines for you to hook to in your programs.  I've already talked about Web Service Command Events, which is the most complex of the 3.  Now let me switch to the most simple type, Vault Explorer Command Events.

Whenever any UI command is invoked in Vault Explorer, it will fire a pre and a post to anyone that subscribes.  The UI command is basically everything in the main menu, toolbars, and context menus.  In your handler, you get passed in the key string for that command.  And that's it.  You don't get the selection context and you don't get any information about what happened during the command.

It told you it was simple.   But don't worry, I can stretch this out to a full article.  Just because a feature is simple doesn't mean it's useless.


Here are a couple of uses to get things started…

Use #1:  Cache clearing

Sometimes I find myself building up a cache in my custom command.  For example, I'll need to read all the file Property Definitions.  I don't want to read these objects each time my command is invoked, so I save the data in memory.  But what if I want to keep my cache up-to-date?  I just need to check for the Refresh or Admin Settings command and clear the cache at that point.

Use #2:  Event combo

Just like in video games, you make use of combos to get the best results.
Web Service Command events are good at knowing what work is being done but only in isolation.  They don't know what overall command is being done.  Vault Explorer Command events are just the opposite.  If you combine the two, you have a fairly complete picture.

Here is the timeline of a Copy Design operation that creates 3 files.  The top blue lines are the Vault Explorer events, the bottom blue lines are the web service events, and the red lines are the web service function calls. 

We can make good use of all these events.  For example, you can send out an email report after a copy design is completed.  Here is how you would do it:

  1. In the Vault Explorer pre event for Copy Design:  Create a new List.
  2. In the DocumentService AddFile post event:  Add the File to your List.
  3. In the Vault Explorer post event for Copy Design:  Use the entries in the List to send out the email report explaining which files were added via Copy Design.

Now you're playing with power!  


Cheat Code: 

You will probably want to know the key string values for the various UI commands.  This information isn't documented anywhere.  But here some code that you can use to build your own dev app that displays the key value each time you invoke a command in Vault explorer.

// A simple dialog with a label to display the commands
public partial class TextDialog : Form
{
    public string LabelText
    {
        set { m_label.Text = value; }
    }

    public TextDialog()
    {
        InitializeComponent();
    }
}

// In your IExtension class

TextDialog
m_commandBox;

public
void OnStartup(IApplication application)
{
    application.CommandBegin += new EventHandler<CommandBeginEventArgs>(application_CommandBegin);
}

void application_CommandBegin(object sender, CommandBeginEventArgs e)
{
    if (m_commandBox == null)
    {
        m_commandBox = new TextDialog();
        m_commandBox.Show();
    }
    m_commandBox.LabelText = e.CommandId;
}

' A simple dialog with a label to display the commands
Public Partial Class TextDialog
      Inherits Form
      Public WriteOnly Property LabelText() As String
            Set
                  m_label.Text = value
            End Set
      End Property

      Public Sub New()
            InitializeComponent()
      End Sub
End Class


' In your IExtension class

Dim m_commandBox As TextDialog

Public Sub OnStartup(application As IApplication)
    AddHandler application.CommandBegin, AddressOf application_CommandBegin
End Sub

Private Sub application_CommandBegin(sender As Object, e As CommandBeginEventArgs)
      If m_commandBox Is Nothing Then
            m_commandBox = New TextDialog()
            m_commandBox.Show()
      End If
      m_commandBox.LabelText = e.CommandId
End Sub

Coming soon:  Web Service Invoke Events (aka. God Mode)


Comments

5 responses to “Vault Explorer Command Events”

  1. Balaji A Avatar
    Balaji A

    What’s that IApplication in OnStartUp() method??

  2. It’s part of the IExtension interface that you use for writing custom commands. OnStartUp runs when Vault Explorer starts up.

  3. Balaji A Avatar
    Balaji A

    Thanks Doug!.. Similar to OnStartUp Event which event will be triggered when we rename a file in vault or any other way to invoke a method while renaming a file in vault.. Thanks in Advance..

  4. Solved the Problem Doug by checking the CommandId Property of each command..
    Regards
    Balaji A

  5. Above Copy Design sample is for old Copy Design. For new Copy Design, which is a modeless dialog, CommandEnd event will be fired once the dialog is shown. However, there is another Autodesk.DataManagement.Client.Framework.Vault.Forms.Library.CopyDesignDialogClosed event which is fired when new copy design dialog is closed. So it could be a replacement of the CommandEnd event here.

Leave a Reply to Doug RedmondCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading