Showing results for 
Search instead for 
Did you mean: 

Context Menu with Addin, how2

Valued Contributor
Valued Contributor

I'm trying to add a contextmenu to SE with my Addin,

but I can't manage it to get worked,

anyone knows it might work?


I used;

Public Class Connect
WithEvents ShortCutMenuEvents As SolidEdgeFramework.ISEShortCutMenuEvents_Event

Private Sub ISolidEdgeAddIn_OnConnection(ByVal Application As Object, ....
ShortCutMenuEvents = SE_App.ShortcutMenuEvents
End Sub

 'This adds the context menu
Private Sub objShortCutMenu_BuildMenu(ByVal EnvCatID As String, ByVal Context As SolidEdgeFramework.ShortCutMenuContextConstants, ByVal pGraphicDispatch As Object, ByRef MenuStrings As System.Array, ByRef CommandIDs As System.Array) Handles ShortCutMenuEvents.BuildMenu
            If LCase(EnvCatID) = LCase(CATID_SeAsm) Then
                If Not pGraphicDispatch Is Nothing Then
                    Dim theMenuStrings() As String = {"FirstCommand", "SecondCommand"}
                    Dim theCommandIDs() As Integer = {1, 2}

                    MenuStrings = theMenuStrings
                    CommandIDs = theCommandIDs
                 End If
            End If
        Catch ex As Exception
        End Try
End Sub
End Class


I think/know that the objShortCutMenu_BuildMenu is wrong, but I can't get event this far!

as the addin stops after

ShortCutMenuEvents = SolidEdgeFramework.ISEShortCutMenuEvents_Event

in the Connection with exception:


"event invocation for COM objects requires event to be attributed with DispIdAttribute"


Posted by: Linus Hechinger
Post date: 6/14/2012 2:23:12 AM


Re: Context Menu with Addin, how2

Valued Contributor
Valued Contributor

I see you are using .NET 4 framework. Must be using Visual Studio 2010. You can probably fix this problem very easily.


Go to your references and select the solid edge framework reference. Right click and pick the properties command.


In the properties window you will see something along the lines of "embed interop types" and it will be set to True. Set that dude to False. Just make sure you deliver your interop assembly with your code assembly.


As for your code in your handler, you are correct, it won't work. I see you are using what I guess is the command IDs you passed Edge in SetAddinInfoEx (you want to use the "Ex" interfaces to support 64 bits). When you call SetAddinInfo(Ex), the input array of command IDs is also an output. Edge will return the command IDs assigned to your commands and controls on the ribbon. These have to be unique across all commands, including add-in commands and hence Edge returns the dynamically allocated command IDs to you specifically so you can use them in short cut menus and the StartCommand API that takes in a command ID. So make sure you use the command IDs Edge returned to you in the event handler. Store them on a per environment basis so you can find them easily in your event handler. That's the only issue I see with your handler.



You can also take another approach. Use the "implements" keyword and implement the event interface directly as is done in the VB .NET add-in template that was uploaded to the Visual Studio on-line gallery. To get the template, run file/new project. On the projects dialog, click the entry on the left side named "Online Templates". When that page loads, there is a "Search installed templates" search box on the upper left side of the UI. Key in "Solid". You should see at least three templates. You'll want the "Solid Edge Add-In Visual Basic" entry that will appear. Double click it and it will install and the IDE will immediately launch the wizard. Just pick a dir and some name and let the template generate the add-in. Then copy the "implements SolidedgeFramework.ISEShortCutMenuEvents" entry and put it in your code.


In the "Utilities" region of the addin.vb source file the template generates you will find a AddOrRemoveEventHandlers function that connects and disconnects the event interface. You can take that and modify it to fit your add-in or just call it with the appropriate flags (it supports four event interfaces currently so you can pass in False for all but the shortcut menu events). Make sure you call to disconnect when you are unloading.


Of course the template also implements the member functions and your code can be moved to the handler the IDE will add when you add the "implements SolidEdgeFramework.ISEShortcutMenuEvents" line to your class. Sometimes the IDE takes a bit of time before it adds the event functions to your class. But once it is there, just copy your above code to the event handler. In any case examine the handler created by the template and you will see that the add-in generated keeps up with the command IDs edge dynamically allocated so that it can put them into the short cut menu.


The shortcut menu events can be implemented by any client and edge doesn't know what code is returning commands and hence returning 1 and 2 is no good as Edge cannot map those to any particular add-in. And of course if you know edge command IDs, you can add them to the menu too.


Posted by: R.D. Holland
Post date: 6/26/2012 3:27:22 PM