Inventor button not working, button created using C#

Inventor button not working, button created using C#

madhukum339
Participant Participant
207 Views
1 Reply
Message 1 of 2

Inventor button not working, button created using C#

madhukum339
Participant
Participant

Hi,

 

I'm using the below C# code to create a custom tab with panel and button in Inventor 2024. but upon clicking the button it's not executing. Please correct the code if needed. Thanks in advance 

 

private void CreateCustomTab(string tabName)
{

UserInterfaceManager uIManager = m_inventorApplication.UserInterfaceManager;
if (uIManager.InterfaceStyle == InterfaceStyleEnum.kRibbonInterface)
{
Ribbon ribbon = uIManager.Ribbons["Assembly"];
RibbonTabs rTabs = ribbon.RibbonTabs;

foreach (RibbonTab tab in rTabs)
{
if (tab.DisplayName.Equals(tabName))
return;
}

rTabs.Add(tabName, "Test", clsid, "", true, false);

RibbonTab rTab = ribbon.RibbonTabs["Test"];

RibbonPanels panels = rTab.RibbonPanels;
foreach (RibbonPanel panel in panels)
{
if (panel.DisplayName != null && panel.DisplayName.Equals("WPF Form"))
{
panel.Delete();
}
}
RibbonPanel newPanel = rTab.RibbonPanels.Add("WPF", "InventorAddinServer.RibbonPanel_" + Guid.NewGuid(), clsid, String.Empty, true);

CommandControls cmdCtrls = newPanel.CommandControls;
ButtonDefinition button1 = m_inventorApplication.CommandManager.ControlDefinitions.AddButtonDefinition("OpenWPF", "InventorAddinServer.OpenWPFButton_" + Guid.NewGuid(), CommandTypesEnum.kShapeEditCmdType);
button1.OnExecute += Button1_OnExecute;
cmdCtrls.AddButton(button1);

button1.Enabled = true;
button1.Execute();

}

}

private void Button1_OnExecute(NameValueMap Context)
{

//trying to open a wpf window on button click
MainWindow mainWindow = new MainWindow(m_inventorApplication);
}

 

I'm using the class Guid as the clsid to create the tab and button. 

madhukum339_1-1738565407444.png

 

0 Likes
208 Views
1 Reply
Reply (1)
Message 2 of 2

jjstr8
Collaborator
Collaborator

You're not showing your window in Button1_OnExecute, just creating the window. Also, delete the "button1.Execute();" line when creating the button, unless you want the window to show when Inventor starts and loads your add-in.

 

private void Button1_OnExecute(NameValueMap Context)
{

//trying to open a wpf window on button click
MainWindow mainWindow = new MainWindow(m_inventorApplication);
mainWindow.Show(); // or mainWindow.ShowDialog()
}

 

0 Likes