CommandBegin: wrong CommandId

CommandBegin: wrong CommandId

dnicolini
Enthusiast Enthusiast
1,738 Views
8 Replies
Message 1 of 9

CommandBegin: wrong CommandId

dnicolini
Enthusiast
Enthusiast

Hi All,

 

I have a problem to obtain the correct commandId inside the Vault CommandBegin Event. It seems that, when we have a lot of custom application loaded, Vault loads only 318 commands. The sender of the event has a list (CommandIds) that contains maximum 318 commands id. In this case when I run a custom command, the event returns the wrong command id. I think that it returns always the first custom application command id.

 

How can I find the correct command id?

 

Thanks,

Dario

0 Likes
Accepted solutions (1)
1,739 Views
8 Replies
Replies (8)
Message 2 of 9

psaarloos
Collaborator
Collaborator

Hi Dario,

 

You should be able to test on the command name in the event. If you create your own CommandItem, the first argument in the constructor is the uniqueId for the command. This is the name you should use in the CommandBegin event and use the CommandBeginEventArgs instead of the sender.

 

void application_CommandBegin(object sender, CommandBeginEventArgs e)
{
      if (e.CommandId == "CopyDesign")
      {
             // DoStuff
      }
}

 

Hope this helps!

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 3 of 9

dnicolini
Enthusiast
Enthusiast

Hi Psaarloos,

 

thank you for your answer.
The posted code works if you want to hook a standard Vault command like CopyDesign. Instead if you want to hook a custom command you got a wrong behavior.
I attached a simple visual studio solution with 2 projects that create 10 buttons on file context menu. I attached also a screencast to show the problem described below.
To replicate the problem you have to:

 

- Create 2 separate folders for the 2 projects inside Vault 2017 Extensions
- Debug project TestCommandIds2
- Insert a break point on line 119 of VaultUI.cs
- Fire any new button (not Test6)
- Read the e.CommandId inside the event

 

In this case you can see that the command id is always like Test6 and not like the command you fired.

If you try to do the same thing, putting all custom procedure into a single folder all it works fine (see CmdIds0.PNG) but this case is impossibile to replicate in a customer with many customizations.

 

I need to get the correct custom command id when I have many custom procedures deployed into separate folders. How can I do that?

 

Thanks,
Dario

0 Likes
Message 4 of 9

psaarloos
Collaborator
Collaborator

Hi Dario,

 

Weird behavior indeed! The CommandBegin and CommandEnd events are great for capturing standard Vault commands. Why can't you simply use the command events you implemented for your custom commands?

 

private void Test9_Execute(object sender, CommandItemEventArgs e)
{
      //DoStuff
}

If you can't use the handlers for your commands, can you point out what you are trying to achieve?

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 5 of 9

dnicolini
Enthusiast
Enthusiast

Hi Psaarloos,

 

thank you for your answer.


My problem is that I need to intercept a specific custom command from another application.
Think about this scenario:

 

- Application A --> adds a file
- Application B --> hooks the AddFile Event

 

I need to disable, from the application B, the add file event when is fired from the custom command placed in application A. This is impossible because in the CommandBegin event I can't get the correct command.

 

Thanks,
Dario

0 Likes
Message 6 of 9

psaarloos
Collaborator
Collaborator
Accepted solution

Hi Dario,

 

I understand your case. One approach could be to set a specific property when adding a file from application A, which you could retrieve in the AddFileEvent of application B. Another approach could be to look at the call stack in the AddFileEvent of application B. Looking at the frames in the call stack I can see MyCommandHandler is in the collection. Could this work for you?

 

 void AddFileEvents_GetRestrictions(object sender, AddFileCommandEventArgs e)
        {
            System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
            System.Diagnostics.StackFrame[] stackFrames = stackTrace.GetFrames();

            System.Diagnostics.StackFrame test = stackFrames.Where(x => x.GetMethod().Name.Equals("MyCommandHandler")).FirstOrDefault();
            if (test != null)
            {
                //Add File Event was triggered while using MyCommandHandler
            }
        }

 

 

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 7 of 9

dnicolini
Enthusiast
Enthusiast

Hi Pim,

 

Thank for your answer!

 

The code you wrote it works inside the AddFileGetRestriction and show me the name of the custom method.
This workaround works until all procedures use different method names. What happens if I have 2 custom procedures that use the same method name? In that case is there a way to find the correct called method?

 

Anyway I think that the CommandBegin event should work in all cases and I think that is a Vault bug.

 

Regards,

Dario

0 Likes
Message 8 of 9

psaarloos
Collaborator
Collaborator

Hi Dario,

 

I think to get this working you would need a unique event handler for each CommandItem, but the events could all call a shared method. I agree with you this should be fixed by Autodesk in the first place, but I hope you can use the workaround for now. Don't forget to post an idea in the IdeaStation to get this fixed. I saw there is another idea around the CommandBegin event posted just a little while ago.

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 9 of 9

dnicolini
Enthusiast
Enthusiast

Hi Pim,

 

thank you. Yes I can use the code to resolve the problem.

I will post that on IdeaStation!

 

Thank you for your time!

Regards,

Dario

0 Likes