Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 516

Change selection from a mini contextual toolbar command

$
0
0

By Adam Nagy

It seems that if you change the selection from a command invoked from the mini contextual toolbar then the mini toolbar will disappear. If you start the same command from e.g. the Ribbon then all is fine and the mini toolbar stays visible.

The above gave me the idea that perhaps starting our command asynchronously from the mini toolbar could work - and it seems to :)

We have our command "MyCommand" that changes the selection and we create another command "MyCommand_MiniToolbar" for the mini toolbar that does not do anything apart from starting our real command asynchronously: see difference between synchronous and asynchronous execution.

Here is our VBA class "clsMiniToolbar" that creates our commands and subscribes to the OnContextualMiniToolbar event where we can add our command to the mini toolbar:

Dim WithEvents oEvents As UserInputEvents
Dim WithEvents oBD As ButtonDefinition
Dim WithEvents oBD_MiniToolbar As ButtonDefinition

Private Sub Class_Initialize()
    Dim oCM As CommandManager
    Set oCM = ThisApplication.CommandManager
    Set oEvents = oCM.UserInputEvents
    Call AddCommands
End Sub

Sub AddCommands()
    Dim oCM As CommandManager
    Set oCM = ThisApplication.CommandManager
    
    Dim oCDs As ControlDefinitions
    Set oCDs = oCM.ControlDefinitions
    
    On Error Resume Next
    oCDs("MyCommand").Delete
    oCDs("MyCommand_MiniToolbar").Delete
    On Error GoTo 0
    Dim oImage As Object' Make sure the path is correct
    Set oImage = LoadPicture("C:\temp\32.bmp")

    Set oBD = oCDs.AddButtonDefinition( _
        "MyCommand", "MyCommand", _
        kNonShapeEditCmdType, _"MyClientid", "My Description", _"My Tooltip", oImage, oImage)
    Set oBD_MiniToolbar = oCDs.AddButtonDefinition( _"MyCommand_MiniToolbar", "MyCommand_MiniToolbar", _
        kNonShapeEditCmdType, _"MyClientid", "My Description", _"My Tooltip", oImage, oImage)
End Sub

Private Sub oBD_MiniToolbar_OnExecute(ByVal Context As NameValueMap)
    Dim oCM As CommandManager
    Set oCM = ThisApplication.CommandManager
    
    ' Just run the other command asynchronously
    Call oCM.ControlDefinitions("MyCommand").Execute2(False)
End Sub

Private Sub oBD_OnExecute(ByVal Context As NameValueMap)
    ' Select the edges of a face
    Dim oPD As PartDocument
    Set oPD = ThisApplication.ActiveDocument
    Dim oF As Face
    Set oF = oPD.ComponentDefinition.SurfaceBodies(1).Faces(1)
    Dim oE As Edge
    For Each oE In oF.Edges
        Call oPD.SelectSet.Select(oE)
    Next
End Sub

Private Sub oEvents_OnContextualMiniToolbar( _
ByVal SelectedEntities As ObjectsEnumerator, _
ByVal DisplayedCommands As NameValueMap, _
ByVal AdditionalInfo As NameValueMap)
  ' Add the toolbar command
  Call DisplayedCommands.Add("MyCommand_MiniToolbar", 0)
End Sub

Now we can instantiate our class from a module:

Dim oMT As clsMiniToolbar

Sub AddToMiniToolbar()
    Set oMT = New clsMiniToolbar
End Sub

This way the mini toolbar stays visible after running our command which selects extra edges in the part document:

MiniToolbar

Note: you need a 32x32 pixel bitmap for the command to be able to add it to the mini toolbar. You can simply use the attached 32.bmp - just place it inside C:\temp before running the above code. 


Viewing all articles
Browse latest Browse all 516

Trending Articles