CommandManager.ActiveCommand is not set with my command

CommandManager.ActiveCommand is not set with my command

HideoYamada
Advisor Advisor
3,298 Views
20 Replies
Message 1 of 21

CommandManager.ActiveCommand is not set with my command

HideoYamada
Advisor
Advisor

Hello,

 

Please see two pictures below.

 

Built In CommandBuilt In CommandMy CommandMy Command

 

When I execute a built in command, the internal name of the command will be set to ActiveCommand.

But with executing my command that is created by C#, the name is not set.

 

I tried to get a name when OnExecute was just called.

 

public override void ControlEvents_OnExecute(NameValueMap options)
{
    System.Diagnostics.Debug.WriteLine("ActiveCommand : " + InventorApplication.CommandManager.ActiveCommand);

This caused...

 

 

ActiveCommand : AppSelectNorthwestArrowCmd

 

 

How can I set my command name to CommandManager.ActiveCommand?

 

Best regards,

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Accepted solutions (4)
3,299 Views
20 Replies
Replies (20)
Message 2 of 21

chandra.shekar.g
Autodesk Support
Autodesk Support

@HideoYamada,

 

Hoping that suggestions in the below blog link may be helpful.

 

https://modthemachine.typepad.com/my_weblog/2009/03/running-commands-using-the-api.html

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 21

HideoYamada
Advisor
Advisor

Hello chandra,

 

Thank you for your reply.

 


Hoping that suggestions in the below blog link may be helpful.

 

https://modthemachine.typepad.com/my_weblog/2009/03/running-commands-using-the-api.html

 

Unfortunately that was not the answer for me.

 

I've written a simple test code.

 

 

public class StandardAddInServer : Inventor.ApplicationAddInServer
{
    private Inventor.Application m_inventorApplication;
    ButtonDefinition definition; 
    public StandardAddInServer() {}
    public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
    {
        m_inventorApplication = addInSiteObject.Application;
        definition = m_inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition("ActiveCommandTest", "ActiveCommandTest", CommandTypesEnum.kShapeEditCmdType, "MyClientID");
        definition.OnExecute += Definition_OnExecute;
    }
    private void Definition_OnExecute(NameValueMap Context)
    {
        MessageBox.Show("ActiveCommand : " + m_inventorApplication.CommandManager.ActiveCommand);
    }
    public void Deactivate()
    {
        definition.OnExecute -= Definition_OnExecute;
        m_inventorApplication = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    public void ExecuteCommand(int commandID) {}
    public object Automation
    {
        get { return null; }
    }
}

The MessageBox always says 'AppSelectNorthwestArrowCmd'.

It was tested with Inventor 2019 and 2020.

 

Does anyone know what happens (or not)?

 

Best regards,

 

=====

Freeradica

 Hideo Yamada

 

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 4 of 21

chandra.shekar.g
Autodesk Support
Autodesk Support

@HideoYamada,

 

Can you please provide non confidential code and reproducible steps to test the behavior?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 21

HideoYamada
Advisor
Advisor

Hello Chandra,

 

I attached the project archive of the test code.

Open a part document and execute this command, and then you can see the dialog of ActiveCommand.

 

The code and results are the same as described in my last post.

 

Best Regards,

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 6 of 21

chandra.shekar.g
Autodesk Support
Autodesk Support

@HideoYamada,

 

On successful execution of source code, where is "ActiveCommandTest" button created?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 21

HideoYamada
Advisor
Advisor

Hello Chandra,

 

On successful execution of source code, where is "ActiveCommandTest" button created?

No button will be created.

Please assign "ActiveCommandTest" to a key.

ActiveCommandTest.PNG

 

It's better to open a document file before doing the test, because sometime invoking command by the key is ignored when it is in the ZeroDoc environment.

 

Best regards,

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 8 of 21

jjstr8
Collaborator
Collaborator

If you're in the OnExecute handler for your command, you already know what the command is.  What do you need ActiveCommand for?  Just for reference, Inventor reports the ActiveCommand as the default command for my custom commands as well.

0 Likes
Message 9 of 21

HideoYamada
Advisor
Advisor

Hello jjstr8,

 

I want to know other command is already running or not when my command is executed.

Build-in commands sets their names to ActiveCommand and I can do proper processing

(e.g. cancel this execution, terminate the running command)

 

But my commands cannot know other command I made is running or not.

This is the problem.

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 10 of 21

NachoShaw
Advisor
Advisor

What is the window you show that displays the running commands? Is that an inventor tool?

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


0 Likes
Message 11 of 21

HideoYamada
Advisor
Advisor

Hello NachoUK,

 


@NachoShaw wrote:

What is the window you show that displays the running commands? Is that an inventor tool?


 

Only I want to do is knowing another command is running or not.

 

Please see below

CommandIsExecuting.PNG.jpg

If another standard Inventor command is executed in this situation, already running command (Extrude) will be canceled and then new command will be executed.

When my command is executed in this situation, my command runs with another command (Extrude) running. (and this will make Inventor crash)

So I want know command is already running or not.

 

It is said same thing when my command is running first. I think that my commands must tell they are running or not via CommandManager.ActiveCommand.

 

Or, is there a way to execute command as modal?

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 12 of 21

jjstr8
Collaborator
Collaborator

Hideo,

 

As best I can tell, only built-in commands report through ActiveCommand.  In your last post you asked about running a custom command as modal.  In your OnExecute method, use ShowDialog() instead of Show().  If there are cases that this doesn't work for you, I've tested another way for custom commands in a single addin to close each other.  Let me know if you want the details.

 

Thanks,

Jeremy

0 Likes
Message 13 of 21

HideoYamada
Advisor
Advisor

Hello jjstr8,

 

As best I can tell, only built-in commands report through ActiveCommand. 

Ah, It's a bad news for me.

 

In your last post you asked about running a custom command as modal.  In your OnExecute method, use ShowDialog() instead of Show().  If there are cases that this doesn't work for you, I've tested another way for custom commands in a single addin to close each other.  Let me know if you want the details.


Some of my commands have no Window forms. (but FileDialog and UserInteractionEvents)

I can tell to my commands that the others I made are running or not, but still my commands can't know about the add-in which is made by someone else.

I am publishing my commands at App Store. I want to tell to someone's add-in at least about state of my commands.

(To avoid someone's add-in making Inventor crash while my commands are running.)

 

But it seems that there is no way...

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 14 of 21

BrianEkins
Mentor
Mentor
Accepted solution

The reason you're not seeing your command as the active command is that you haven't created a command.  You've created a button definition, added a control to the ribbon, and are listening for the OnExecute event.  This doesn't result in a command being run, but only in notifying you that a button was clicked.  You should see that whatever the active command is when you click the button, remains active.

 

To create a real command you need to do what you're doing but in the OnExecute event you need to create an InteractionEvents object and start it.  This is what actually starts your custom command.  Doing this will terminate the currently running command and you'll now have primary access to all of the user events.  In addition, your command will be returned as the active command.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 15 of 21

HideoYamada
Advisor
Advisor

Hello BrianEkins,


Thank you for replying.


> To create a real command you need to do what you're doing but in the OnExecute event you need to create an InteractionEvents object and start it.

(omission)

> In addition, your command will be returned as the active command.


Please see the first post on this thread.

When an InteractionEvents starts then pre-executed command will be terminated as you said.

However, ActiveCommand is set to 'InteractionEvents' (not my command name).

 

And executing the standard Derived Part command cause opening file dialog and closing pre-exexuted command.
(There is no InteractionEvents!)

 

Is this a lack of API functionality?
Now I think so.

 

Best regards,

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 16 of 21

jjstr8
Collaborator
Collaborator
Accepted solution

Hideo,

 

It looks like you need to set the command name through the InteractionEvents object you created.  I'm not sure where you're doing this in your code, but it can be done in your command's class constructor.

 

public partial class MyCommandForm : Form
{
     private Inventor.Application inventorApp;
     private Inventor.InteractionEvents interactionEvents;

     public MyCommandForm(Inventor.Application app)
     {
           inventorApp = app;
           interactionEvents = inventorApp.CommandManager.CreateInterationEvents();
           interactionEvents.Name = "MyCustomCommand";
           interactionEvents.Start();
     }
     private void MyCommandForm_FormClosing(object sender, FormClosingEventArgs e)
      {
            interactionEvents.Stop();
      }
}

This is just an example.  I don't know if it fits your needs exactly.  Any "start" of another interaction events object will overwrite ActiveCommand (either with the .Name of the new one or "InteractionEvents" if .Name == "").

 

Thanks,

Jeremy

Message 17 of 21

jjstr8
Collaborator
Collaborator

I think what NachoUK is asking is what is the window in your original post with the green title bar? 

0 Likes
Message 18 of 21

HideoYamada
Advisor
Advisor

Hello jjstr8,

 

ActiveCommand could be set as you said. Thank you!

 

=====

Freeradical

 Hideo Yamada

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 19 of 21

HideoYamada
Advisor
Advisor

> I think what NachoUK is asking is what is the window in your original post with the green title bar?

 

Thank you for the advice, jjstr8.

 

The window with the green title bar is the watch window of VBA.

(I should have run the English version, sorry > all)

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 20 of 21

HideoYamada
Advisor
Advisor
Accepted solution

Thanks a lot all of you!! I got a solution.

 

<<Conclusions>>

(1) CommandManager.ActiveCommand can be changed by starting InteractionEvents.

 

(2) InteractionEvents.Start() will stop the command already executed.

 

(3) Every command that will make a document dirty needs to start InteractionEvents, even if the command doesn't require user interaction.

 

 

(3) is important. Because a change of the document when another command is running sometime cause a crash of the command or Inventor.

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes