Esc button in add-in

Esc button in add-in

GeorgK
Advisor Advisor
2,101 Views
17 Replies
Message 1 of 18

Esc button in add-in

GeorgK
Advisor
Advisor

Hello together,

 

I have an add-in in which I would like to add an ESC-button. The button should be activated when the button from the program is clicked and should appear when the user clicks the right mouse button (OnContextMenu). Otherwise the button should be hidden.

 

 

If occurrenceList.Count < 1 Then

                While True
                    entity = m_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select a component" & "  " & "Hit Esc to cancel selection")

                    ' If nothing gets selected then we're done
                    If IsNothing(entity) Then Exit While

                    occurrenceList.Add(entity)

                End While

               
            End If

Thank you very much

 

Georg

 

 

 

 

 

 

0 Likes
Accepted solutions (1)
2,102 Views
17 Replies
Replies (17)
Message 2 of 18

GeorgK
Advisor
Advisor

I got the button to the OnContextMenu. But how could I filter it that it shows only the button when the program runs?

 

Dim ESC_Button As String = "ESC_Button"
                    Try
                     
                        oButtonDefinition169_ESC_Menue = DirectCast(oControlDefinitions(ESC_Button), Inventor.ButtonDefinition)
                    Catch
                        oButtonDefinition169_ESC_Menue = oControlDefinitions.AddButtonDefinition("ESC", ESC_Button, Inventor.CommandTypesEnum.kQueryOnlyCmdType, m_ClientID, "ESC", "ESC", _
                         Nothing, Nothing, Inventor.ButtonDisplayEnum.kAlwaysDisplayText)
                    End Try
                   
                    Try
                         Dim oCtrl As Inventor.CommandBarControl = CommandBar.Controls(ESC_Button)
                    Catch

                        CommandBar.Controls.AddButton(oButtonDefinition169_ESC_Menue, 1)
                        CommandBar.Controls(2).GroupBegins = True
                        oButtonDefinition169_ESC_Menue.Enabled = True
                    End Try
0 Likes
Message 3 of 18

GeorgK
Advisor
Advisor

Does nobody know a solution for the problem? Please could someone provide an example. Thanks

0 Likes
Message 4 of 18

Vladimir.Ananyev
Alumni
Alumni

AddButton(...) method returns CommandControl object. 
You may control its visibility via Visible property.


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 18

GeorgK
Advisor
Advisor

Hello Vladimir,

 

thanks for your answer. But I could not show or hide the button. Either the button is displayed or not. Do you have any working example?

 

Georg

0 Likes
Message 6 of 18

Richard.MM
Contributor
Contributor

I do not know if it helps, but I use this method:

  Private Sub Form_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
        Me.KeyPreview = True
    
        If (e.KeyCode = Keys.Enter) Then
    
            MessageBox.Show("Enter key pressed")
            e.SuppressKeyPress = True

        ElseIf (e.KeyCode = Keys.Escape) Then

            MessageBox.Show("Escape key pressed")
            e.SuppressKeyPress = True
        End If

    End Sub

 

0 Likes
Message 7 of 18

GeorgK
Advisor
Advisor

Hello Richard,

 

this works in a form very well. But how could I get it running in an add-in?

 

Georg

0 Likes
Message 8 of 18

Richard.MM
Contributor
Contributor

I do it through the Form, where I use BackgroundWorker.

"Add-in" then I call through bw_DoWork().

 

0 Likes
Message 9 of 18

Vladimir.Ananyev
Alumni
Alumni

You may add the button oButtonDefinition169_ESC_Menue to the linear marking menu only if some conditions are satisfied. 

In the code sample published in the blog “Entity specific context menu“ http://adndevblog.typepad.com/manufacturing/2015/03/entity-specific-context-menu.html  the button is added only if the selected entities are all ordinate dimensions:

Private Sub uie_OnLinearMarkingMenu( _
                ByVal SelectedEntities As ObjectsEnumerator, _
                ByVal SelectionDevice As SelectionDeviceEnum, _
                ByVal LinearMenu As CommandControls, _
                ByVal AdditionalInfo As NameValueMap)
    ' We only add our context menu item if the
    ' selected entities are all ordinate dimensions
    If AreAllOrdinate(SelectedEntities) Then
        Call LinearMenu.AddButton(buttondefinition1)
    End If
End Sub

 

Is this approach applicable in your workflow?


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 10 of 18

GeorgK
Advisor
Advisor

Hello Vladimir,

 

this works. But how could I add the button when the user clicks a button on the ribbon bar? I could not use the filter version because I select parts and assemblies in an assembly or subassembly.

 

Georg

 

 

0 Likes
Message 11 of 18

Vladimir.Ananyev
Alumni
Alumni

Hi Georg,

do you mean that the user clicks your button or any button available in the ribbon?

If this is your own button then its handler may set some global variable/parameter that could be checked when you populate the context menu.

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 12 of 18

GeorgK
Advisor
Advisor

Hello Vladimir,

 

I tried that but it does not work. The user should click on the button from the add-in. Either the button appears every time or not. Do you have a sample for it?

 

Thanks

 

Georg

0 Likes
Message 13 of 18

GeorgK
Advisor
Advisor

Does nobody knows a solution?

0 Likes
Message 14 of 18

ekinsb
Alumni
Alumni

Hi Georg,

 

I read back through this thread but it's still unclear to me what exactly it is that you want to accomplish.  Can you list step-by-step what the user action is and what you would like the behavior to be?


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 15 of 18

GeorgK
Advisor
Advisor

Hello Brian,

 

in part, drawing or assembly environment I would like to add an escape button to my addin. The escape button should only appear in the contextmenu when the user starts the program.  The escape button should be only visible when the program is running. Otherwise the button should be hidden.

 

User clicks the button on the ribbon bar (program is executed) and escape button is actived on right mouse button. When the program is finished the escape button does not appear any more on the rigt mouse click.

 

Escape.png

 

I hope it is much clearer now what I tried to get.

 

Thanks

 

Georg

0 Likes
Message 16 of 18

ekinsb
Alumni
Alumni

Hi Georg,

 

Thank you for this latest description, I believe I understand what you want to do and it should be possible.  There's one thing that needs to be defined that will affect how you do this.  That is, what is the lifetime of your "command".  The typical Inventor command has a short lifetime and only one command can run at a time.  Completing a command will go back to the default state of Inventor or running another command will terminate the current command.  You can get this kind of behavior by using the CommandManager.CreateInteractionEvents method.

 

Using an InteractionEvents will be the simplest approach.  When the user runs your command you'll get notified through the OnExecute event of the ButtonDefinition and can create and start an InteractionEvents object.  When you start the InteractionEvents object you can also connect to the OnLinearMarkingMenu or OnRadialMarkingMenu events to be able to edit the context menu when it appears.  You can also listen for the OnTerminate event associated with the InteractionEvents object.  This event is fired when your InteractionEvents object is stopped, which means your command is no longer running.  In this even you can disconnect from the OnLinearMarkingMenu or OnRadialMarkingMenu events.

 

If your command isn't a standard command and the user starts it and while it's running they can go back to Inventor and continue to do other work, then you can't use an InteractionEvents object.  In this case the lifetime of your "command" is defined by something else; probably until a dialog is dismissed.  In this case you can still do something similar.  When you display the dialog you can connect to the OnLinearMarkingMenu or OnRadialMarkingMenu events and when the dialog is dismissed you can disconnect from the OnLinearMarkingMenu or OnRadialMarkingMenu events.

 

Hopefully this helps.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 17 of 18

GeorgK
Advisor
Advisor

Hello Brian,

 

Thank you very much for your explanation. Do you have a working example with the CommandManager.CreateInteractionEvents and a OnLinearMarkingMenu or OnRadialMarkingMenu?  The menu entry is showing always or never.

I tried that but could not get it running. Maybe I missed something.

 

Georg

 

 

0 Likes
Message 18 of 18

ekinsb
Alumni
Alumni
Accepted solution

Hi Georg,

 

Attached is a sample add-in that demonstrates what I described.  It creates a new command in the Tools menu and when the command is run it displays a custom dialog.  While the command is running the context menu is completely altered.  It removes the radial menu completely and removes all of the commands from the linear menu and adds one custom command.  Of course you don't have to go to the extents this command does but this demonstrates that it's possible.  While the command is running you'll have this modified context menu.  When the command is terminated, either by clicking the Cancel button on the dialog, pressing the Esc button, or running another command, the context menu will go back to normal.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog