<?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…
2) Add a WPF User Control via Project >> Add New Item…
3) Change the control to a Window in both the .xaml and .xaml.cs files
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
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
The source code of the sample project:
https://github.com/adamenagy/Inventor-AddInWithWPF






Leave a Reply