Add-in creating a button

Add-in creating a button

Jef_E
Collaborator Collaborator
5,843 Views
13 Replies
Message 1 of 14

Add-in creating a button

Jef_E
Collaborator
Collaborator

Hi there!

 

I'm trying to create an addin with a button, I earlier created an addin that did some behind the scene's work and no button pressing was required, only a trigger. But now I want to add a button in the ribbon.

 

I tried follow this document from AU thats stored on the modthemachine website (see link)

http://modthemachine.typepad.com/files/VBAtoAddIn.pdf

 

So what I did:

  1. Opened my previous solution, I figured you can have more than one method in a add-in right?
  2. I added the code for creating button.
Namespace EMIA_001
    <ProgIdAttribute("EMIA_001.StandardAddInServer"), _
    GuidAttribute("f3ba742e-959e-4763-a2ea-864d85f4fd2b")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

        ' Inventor application object.
        Private m_inventorApplication As Inventor.Application

        ' Application Events object.
        Private m_AppEvents As ApplicationEvents

        ' ClientID varialble
        Private m_ClientID As String

        ' Main orientation button with event
        Private WithEvents m_MainOrientationButtonDef As ButtonDefinition


#Region "ApplicationAddInServer Members"

        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate

            ' This method is called by Inventor when it loads the AddIn.
            ' The AddInSiteObject provides access to the Inventor Application object.
            ' The FirstTime flag indicates if the AddIn is loaded for the first time.

            ' Initialize AddIn members.
            m_inventorApplication = addInSiteObject.Application

            ' TODO:  Add ApplicationAddInServer.Activate implementation.
            ' e.g. event initialization, command creation etc.

            ' Get the classID for this add-in and save it in a
            ' member variable to use whenever a ClientID is needed
            m_ClientID = addinguid(GetType(StandardAddInServer))

            ' Create the button definition
            Dim controlsdefs As ControlDefinitions
            controlsdefs = m_inventorApplication.CommandManager.ControlDefinitions

            m_MainOrientationButtonDef = controlsdefs.AddButtonDefinition("Main orientation",
                                                                          "emToolMainOrientation",
                                                                          CommandTypesEnum.kQueryOnlyCmdType,
                                                                          m_ClientID,
                                                                          "Place the main orientation on the selected centermark",
                                                                          "Place orientation")

            If firstTime Then
                ' Create a new command bar (toolbar) and make it visible
                Dim commandbars As CommandBars
                commandbars = m_inventorApplication.UserInterfaceManager.CommandBars

                Dim commandbar As CommandBar
                commandbar = commandbars.Add("EM Tools", "emToolsBar", CommandBarTypeEnum.kRegularCommandBar, m_ClientID)
                commandbar.Visible = True

                ' Add the control to the command bar
                commandbar.Controls.AddButton(m_MainOrientationButtonDef)

            End If

            ' Add event handler for save event.
            m_AppEvents = m_inventorApplication.ApplicationEvents
            AddHandler m_AppEvents.OnSaveDocument, AddressOf Me.m_ApplicationEvents_OnSaveDocument

        End Sub

....... some more subs from template

But.. nothing is happening.. I can't see my button 😞 and my addin is not loaded anymore. as I check the addins at runtime.

What am I doing wrong here?

 

Some advice would be helpfull.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
5,844 Views
13 Replies
Replies (13)
Message 2 of 14

adam.nagy
Autodesk Support
Autodesk Support

Hi,

 

Maybe it errors out because you are trying to create old interface. Now (for quite a few releases) you have to create buttons on the Ribbon.

You can have a look at any of the .NET samples in the SDK to see what needs to be done.

 

Or have a look at this: http://adndevblog.typepad.com/manufacturing/2013/07/creating-a-ribbon-item-for-inventor.html

 

If something is not working always debug into the code so that you can see which line is causing issues.

 

Cheers,



Adam Nagy
Autodesk Platform Services
Message 3 of 14

Jef_E
Collaborator
Collaborator

Many thanks @adam.nagy I will defenitily check those samples, didn't thought about that!

 

I also have a second question it's almost in the ally I think so i'll just shoot it here.

 

I saw this application made by @Anonymous as you can see in this post. I don't know if it's a dockable window or just a windows form that is displayed on the right. But here's my question about it: is it also possible to create a dockable window for the inventor application? If yes, do you also have a sample that I can see?

 

edit: added link



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 4 of 14

Jef_E
Collaborator
Collaborator

Hi @adam.nagy

 

I tried the samples and it gets me to creating a button. But not in the ribbon.. I followed the code in the like you provided, but also it's unloaded Smiley Sad not so fun!

 

        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate

            ' This method is called by Inventor when it loads the AddIn.
            ' The AddInSiteObject provides access to the Inventor Application object.
            ' The FirstTime flag indicates if the AddIn is loaded for the first time.

            ' Initialize AddIn members.
            m_inventorApplication = addInSiteObject.Application

            ' TODO:  Add ApplicationAddInServer.Activate implementation.
            ' e.g. event initialization, command creation etc.

            ' Add event handler for save event.
            m_AppEvents = m_inventorApplication.ApplicationEvents
            AddHandler m_AppEvents.OnSaveDocument, AddressOf Me.m_ApplicationEvents_OnSaveDocument

#Region "Button Creation"

            ' Get the command manager control defenition
            Dim oControlDefinitions As ControlDefinitions
            oControlDefinitions = m_inventorApplication.CommandManager.ControlDefinitions

            ' Set the Command ID
            Dim oCommandID1 As String
            oCommandID1 = "Command_ID_1"

            Try
                ' try get the existing command definition
                oControlDefinition1 = oControlDefinitions.Item(oCommandID1)
            Catch ex As Exception
                ' or create it
                oControlDefinition1 = oControlDefinitions.AddButtonDefinition("Command 1",
                                                                              oCommandID1,
                                                                              CommandTypesEnum.kEditMaskCmdType,
                                                                              Guid.NewGuid().ToString(),
                                                                              "Command 1 description",
                                                                              "Command 1 Tooltip",
                                                                              GetICOResource("Orientation.ico"),
                                                                              GetICOResource("Orientation.ico"))
            End Try

            If (firstTime) Then

                If (m_inventorApplication.UserInterfaceManager.InterfaceStyle = InterfaceStyleEnum.kRibbonInterface) Then
                    ' 1. access the Zero Doc ribbon
                    Dim ribbonPart As Inventor.Ribbon = m_inventorApplication.UserInterfaceManager.Ribbons.Item("ZeroDoc")

                    ' 2. create our custom tab
                    Dim tabSampleBlog As Inventor.RibbonTab = ribbonPart.RibbonTabs.Add("Sample Blog", "TAB_SAMPLE_BLOG", Guid.NewGuid().ToString())

                    ' 3. criar um painel
                    Dim pnlMyCommands As Inventor.RibbonPanel = tabSampleBlog.RibbonPanels.Add("My Command", "PNL_MY_COMMANDS", Guid.NewGuid().ToString())

                    ' 4. colocar o botão no painel
                    pnlMyCommands.CommandControls.AddButton(oControlDefinition1, True)
                End If
            End If

            ' register the method that will be executed
            AddHandler oControlDefinition1.OnExecute, AddressOf Command1Method

#End Region

        End Sub

        Private Sub Command1Method()

            ' ToDo: do your code here

        End Sub

        Private Function GetICOResource(ByVal icoResourceName As String) As Object

            Dim assemblyNet As System.Reflection.Assembly
            assemblyNet = System.Reflection.Assembly.GetExecutingAssembly()

            Dim stream As System.IO.Stream
            stream = assemblyNet.GetManifestResourceStream(icoResourceName)

            Dim ico As System.Drawing.Icon
            ico = New System.Drawing.Icon(stream)

            Return PictureDispConverter.ToIPictureDisp(ico)

        End Function

and also added a class as described in this blogpost.

http://modthemachine.typepad.com/my_weblog/2012/02/bitmaps-without-vb6-icontoipicture.html

 

I'm currently on inventor 2014.



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 5 of 14

Owner2229
Advisor
Advisor

HI, did you also import the icon and set is as "Embeded Resource"? It will fail if the icon is not present.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 6 of 14

Jef_E
Collaborator
Collaborator

Yes I did.

 

But I searched some more, and there is also this blogpost. This sample made me able to create the button as in the sample

 

Private _ctrlDef1 As ButtonDefinition
Private _ctrlDef2 As ButtonDefinition
Private _ctrlDef3 As ButtonDefinition

Public Sub CreateRibbons()

            Dim ctrlDefs As ObjectCollection
            ctrlDefs = m_inventorApplication.TransientObjects.CreateObjectCollection()

            _ctrlDef1 = m_inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
                "Button 1",
                "MenuDemo.Button1",
                CommandTypesEnum.kEditMaskCmdType,
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9",
                "Button 1",
                "Button 1",
                PictureDispConverter.ToIPictureDisp(My.Resources.One_16x16),
                PictureDispConverter.ToIPictureDisp(My.Resources.One_32x32),
                ButtonDisplayEnum.kDisplayTextInLearningMode)

            _ctrlDef2 = m_inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
                "Button 2",
                "MenuDemo.Button2",
                CommandTypesEnum.kEditMaskCmdType,
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9",
                "Button 2",
                "Button 2",
                PictureDispConverter.ToIPictureDisp(My.Resources.Two_16x16),
                PictureDispConverter.ToIPictureDisp(My.Resources.Two_32x32),
                ButtonDisplayEnum.kDisplayTextInLearningMode)

            _ctrlDef3 = m_inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
                "Button 3",
                "MenuDemo.Button3",
                CommandTypesEnum.kEditMaskCmdType,
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9",
                "Button 3",
                "Button 3",
                PictureDispConverter.ToIPictureDisp(My.Resources.Three_16x16),
                PictureDispConverter.ToIPictureDisp(My.Resources.Three_32x32),
                ButtonDisplayEnum.kDisplayTextInLearningMode)

            AddHandler _ctrlDef1.OnExecute, AddressOf Button1_Clicked
            AddHandler _ctrlDef2.OnExecute, AddressOf Button2_Clicked
            AddHandler _ctrlDef3.OnExecute, AddressOf Button3_Clicked

            ctrlDefs.Add(_ctrlDef1)
            ctrlDefs.Add(_ctrlDef2)
            ctrlDefs.Add(_ctrlDef3)

            Dim ribbon As Ribbon
            ribbon = m_inventorApplication.UserInterfaceManager.Ribbons.Item("ZeroDoc")

            Dim tab As RibbonTab
            tab = ribbon.RibbonTabs.Item("id_TabTools")

            Dim panel1 As RibbonPanel
            panel1 = tab.RibbonPanels.Add(
                "Demo Panel 1",
                "DemoPanelIntName1",
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9")

            panel1.CommandControls.AddButtonPopup(
                ctrlDefs,
                True,
                True)

            Dim panel2 As RibbonPanel
            panel2 = tab.RibbonPanels.Add(
               "Demo Panel 2",
               "DemoPanelIntName2",
               "c157f443-1f84-4017-bb3f-0d4d9ef5eec9")

            panel2.CommandControls.AddTogglePopup(
                _ctrlDef1,
                ctrlDefs,
                True,
                True)

        End Sub

        Private Sub Button1_Clicked(ByVal context As NameValueMap)
            MsgBox("Button 1 was clicked, great!")
        End Sub

        Private Sub Button2_Clicked(ByVal context As NameValueMap)
            MsgBox("Button 2 was clicked, awesome!!")
        End Sub

        Private Sub Button3_Clicked(ByVal context As NameValueMap)
            MsgBox("Button 3 was clicked, amazing!!!")
        End Sub

These buttons appear to me when I build and open inventor. Cool! Now this is not what I as result ofcourse so I modified it a little to represent more my needs, but I have a question about this.

 

        ' Button variable
        Private oButton1 As ButtonDefinition

        Public Sub CreateRibbon()

            ' Create a button
            oButton1 = m_inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
                "Button 1",
                "MenuDemo.Button1",
                CommandTypesEnum.kEditMaskCmdType,
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9",
                "Button 1",
                "Button 1",
                PictureDispConverter.ToIPictureDisp(My.Resources.Orientation),
                PictureDispConverter.ToIPictureDisp(My.Resources.Orientation),
                ButtonDisplayEnum.kDisplayTextInLearningMode)

            ' Add an event handler for the button.
            AddHandler oButton1.OnExecute, AddressOf Button1_Clicked

            ' Specify the ribbon that will be used for the button
            Dim ribbon As Ribbon
            ribbon = m_inventorApplication.UserInterfaceManager.Ribbons.Item("Drawing")

            ' Add a new RibbonTab
            Dim tab As RibbonTab
            tab = ribbon.RibbonTabs.Add(
                "Ellimetal",
                "EllimetalTabIntName",
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9")

            Dim panel1 As RibbonPanel
            panel1 = tab.RibbonPanels.Add(
                "Demo Panel 1",
                "DemoPanelIntName1",
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9")

            panel1.CommandControls.AddButton(oButton1)

        End Sub

What is the purpose of the clientID when using following methods?

 

  • ControlDefinitions.AddButtonDefinition ( optional )
  • RibbonTabs.Add
  • RibbonPanels.Add

 



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 7 of 14

Anonymous
Not applicable
Read up on vb, MDI. Get the Inventor handle and open it within the MDI. Inventor will "dock" within your application.
Wayne
0 Likes
Message 8 of 14

Jef_E
Collaborator
Collaborator

Hi @Anonymous thanks, I will defenitly do that.

 

Other question. I added a button that launches a new windows form. This works but, when I try to enter text into the textbox on the form it goes directly to inventor and not into the form.

 

I added a screenshot to show the behaviour; i find the very odd. You can see I typed test into the textbox and its showing on the left bottom in Inventor.

 

Code button:

            ' Create a button for creating a new project.
            btn_NewProject = m_inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition(
                "New Project",
                "NewProjectIntName",
                CommandTypesEnum.kEditMaskCmdType,
                "c157f443-1f84-4017-bb3f-0d4d9ef5eec9",
                "Adds main orientation to drawing",
                "Select a centermark to place the main orientation", ,
                PictureDispConverter.ToIPictureDisp(My.Resources.rocket_32x32),
                ButtonDisplayEnum.kDisplayTextInLearningMode)

            ' TODO: Add handler sub
            ' Add an event handler for the button.
            AddHandler btn_NewProject.OnExecute, AddressOf NewProject_Clicked

            ' Add the button to the current panel.
            oRibbonPanel.CommandControls.AddButton(btn_NewProject, True)

 

Code for showing the form:

        Private Sub NewProject_Clicked()

            Dim oWindow As New Form_NewProject
            oWindow.Show()

        End Sub

What should can I do to fix this?

 

AddIn Show form input.png



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 9 of 14

Owner2229
Advisor
Advisor

Hi, try this:

 

Private Sub NewProject_Clicked()
    Dim oWindow As New Form_NewProject
    oWindow.ShowDialog()
End Sub
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 10 of 14

Jef_E
Collaborator
Collaborator

@Owner2229 wrote:

Hi, try this:

 

Private Sub NewProject_Clicked()
    Dim oWindow As New Form_NewProject
    oWindow.ShowDialog()
End Sub

Okay, thanks. but.. why? I don't understand why this works and just show doesn't?

 

In this post I found it seemed to be working?

https://forums.autodesk.com/t5/inventor-customization/addins-showing-a-form-from-a-ribbon-button-cli...



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 11 of 14

Owner2229
Advisor
Advisor

Hi,

".Show" will show the form as non-modal, which means:

1) The code below this command will execute right after

2) You can use the application (Inventor) while the form is shown

 

".ShowDialog" will show the form as modal, which means:

1) The code below this command will wait till the form is closed

2) The form takes the application's focus, so you can't use it and all inputs and commands are going to the form

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 12 of 14

Jef_E
Collaborator
Collaborator

@Owner2229 wrote:

Hi,

".Show" will show the form as non-modal, which means:

1) The code below this command will execute right after

2) You can use the application (Inventor) while the form is shown

 

".ShowDialog" will show the form as modal, which means:

1) The code below this command will wait till the form is closed

2) The form takes the application's focus, so you can't use it and all inputs and commands are going to the form


Is there any way I can use ".show" and be able to add text into the forms textbox?



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 13 of 14

Owner2229
Advisor
Advisor

Hi, you can try to look at the form's setting.

Look for this:

Behavior > AutoValidate > EnablePreventFocusChange

Behavior > ImeMode > NoControl

Focus > CausesValidation > True

 

In attachment you have picture of Properties of one of my forms, try to set up yours like this.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 14 of 14

adam.nagy
Autodesk Support
Autodesk Support

Hi,

 

Have a look at this:

http://adndevblog.typepad.com/manufacturing/2012/06/space-entered-in-a-text-box-of-a-modeless-dialog...

 

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes