WPF Window inside an Inventor Add-In

<?xml encoding=”UTF-8″>By Adam Nagy

The question could also be: how to use a WPF Window from a Class Library  which is not specific to Inventor programming. However, here is how you can do it.

1) Create an Inventor Add-In – e.g. using the Inventor Add-In template from File >> New >> Project…

WPF1

2) Add a WPF User Control via Project >> Add New Item…

WPF2

3) Change the control to a Window in both the .xaml and .xaml.cs files  

WPF3

4) Add a reference to System.Xaml .NET assembly through Project >> Add Reference… 

5) Modify the Window as you wish – add controls to it, etc

WPF4

6) Add an Inventor command in the StandardAddInServer.Activate function that will show our dialog. If needed you can also use the System.Windows.Interop.WindowInteropHelper for the reason mentioned here: http://adndevblog.typepad.com/manufacturing/2012/06/space-entered-in-a-text-box-of-a-modeless-dialog-causes-command-to-run-again-c.html  

private Inventor.ButtonDefinition m_btnDef;
public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
{
// 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.
var cmdMgr = m_inventorApplication.CommandManager;
m_btnDef = cmdMgr.ControlDefinitions.AddButtonDefinition(
"ShowWpfDialog", "ShowWpfDialog", CommandTypesEnum.kQueryOnlyCmdType);
m_btnDef.OnExecute += ctrlDef_OnExecute;
m_btnDef.AutoAddToGUI();
}
void ctrlDef_OnExecute(NameValueMap Context)
{
var wpfWindow = new InvAddIn.MyWpfWindow();
// Could be a good idea to set the owner for this window
// especially if used as modeless
var helper = new WindowInteropHelper(wpfWindow);
helper.Owner = new IntPtr(m_inventorApplication.MainFrameHWND);
wpfWindow.ShowDialog();
}

The command in action

WPF5

The source code of the sample project: 
https://github.com/adamenagy/Inventor-AddInWithWPF


Comments

2 responses to “WPF Window inside an Inventor Add-In”

  1. Hi Adam,
    Do you have any pointers on getting the XAML window to display correctly? I copied your XAML code into my own addin and despite the addin loading correctly and the window opening when I click the button in the Ribbonbar, all I get is a white window with no buttons, calendar or checkbox.
    Thanks,
    Alex.
    PS. If it helps I will cross-post this to the Autodesk Forums.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading