现在好象WPF比较受欢迎了,到处都在说WPF。Inventor自己并没有做到真正地支持WPF,你只能在它的Ribbon界面上增加老式的按钮。但是你可以弹出一个对话框来,在你的对话框上面运用WPF,让你的界面炫一点。 我最近用C#写了个简单的Inventor插件的例子,用来演示如何做到这一点。因为WPF是从.net 3.0开始引入的,VS.net 2005不支持,所以要至少用到VS.net 2008。下面是执行效果: 大家可以在http://barbarahan.download.csdn.net/里面下载到源代码。运行代码之前,先把new.jpg保存到c:/temp目录,否则WPF控件加载会有问题。 StandardAddInServer.cs的代码如下: [c-sharp] view plaincopy
- using System;
- using System.Runtime.InteropServices;
- using Inventor;
- using Microsoft.Win32;
- using System.Windows.Forms;
-
- namespace InvWPFSample
- {
- /// <summary>
- /// This is the primary AddIn Server class that implements the ApplicationAddInServer interface
- /// that all Inventor AddIns are required to implement. The communication between Inventor and
- /// the AddIn is via the methods on this interface.
- /// </summary>
- [GuidAttribute("a512b2b0-ce1f-4453-9072-543130c39afb")]
- public class StandardAddInServer : Inventor.ApplicationAddInServer
- {
-
- // Inventor application object.
- private Inventor.Application m_inventorApplication;
- private Button m_Button;
- //user interface event
- private UserInterfaceEvents m_userInterfaceEvents;
- // ribbon panel
- RibbonPanel m_partRibbonPanel;
- private Inventor.UserInterfaceEventsSink_OnResetCommandBarsEventHandler UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;
- private Inventor.UserInterfaceEventsSink_OnResetEnvironmentsEventHandler UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;
- private Inventor.UserInterfaceEventsSink_OnResetRibbonInterfaceEventHandler UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate;
-
- public StandardAddInServer()
- {
- }
-
- #region ApplicationAddInServer Members
-
- 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;
-
- Button.InventorApplication = m_inventorApplication;
-
- //initialize event delegates
- m_userInterfaceEvents = m_inventorApplication.UserInterfaceManager.UserInterfaceEvents;
-
- UserInterfaceEventsSink_OnResetCommandBarsEventDelegate = new UserInterfaceEventsSink_OnResetCommandBarsEventHandler(UserInterfaceEvents_OnResetCommandBars);
- m_userInterfaceEvents.OnResetCommandBars += UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;
-
- UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate = new UserInterfaceEventsSink_OnResetEnvironmentsEventHandler(UserInterfaceEvents_OnResetEnvironments);
- m_userInterfaceEvents.OnResetEnvironments += UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;
-
- UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate = new UserInterfaceEventsSink_OnResetRibbonInterfaceEventHandler(UserInterfaceEvents_OnResetRibbonInterface);
- m_userInterfaceEvents.OnResetRibbonInterface += UserInterfaceEventsSink_OnResetRibbonInterfaceEventDelegate;
-
- //retrieve the GUID for this class
- GuidAttribute addInCLSID;
- addInCLSID = (GuidAttribute)GuidAttribute.GetCustomAttribute(typeof(StandardAddInServer), typeof(GuidAttribute));
- string addInCLSIDString;
- addInCLSIDString = "{" + addInCLSID.Value + "}";
-
- m_Button = new Button(
- "Test", "InvWPFSample:DrawSlotCmdBtn", CommandTypesEnum.kShapeEditCmdType,
- addInCLSIDString, "Create slot sketch graphics",
- "Draw Slot",null,null, ButtonDisplayEnum.kDisplayTextInLearningMode);
-
- if (firstTime == true)
- {
- //access user interface manager
- UserInterfaceManager userInterfaceManager;
- userInterfaceManager = m_inventorApplication.UserInterfaceManager;
-
- InterfaceStyleEnum interfaceStyle;
- interfaceStyle = userInterfaceManager.InterfaceStyle;
-
- //create the UI for classic interface
- if (interfaceStyle == InterfaceStyleEnum.kRibbonInterface)
- {
- Inventor.Ribbons ribbons;
- ribbons = userInterfaceManager.Ribbons;
-
- Inventor.Ribbon partRibbon;
- partRibbon = ribbons["Part"];
-
- //get the tabls associated with part ribbon
- RibbonTabs ribbonTabs;
- ribbonTabs = partRibbon.RibbonTabs;
-
- RibbonTab partSketchRibbonTab;
- partSketchRibbonTab = ribbonTabs["id_TabModel"];
-
- //create a new panel with the tab
- RibbonPanels ribbonPanels;
- ribbonPanels = partSketchRibbonTab.RibbonPanels;
- m_partRibbonPanel = ribbonPanels.Add("MyPanel", "InvWPFSample:myRibbonPanel", "{D9640F58-514F-4b86-9C44-507DE330C7F5}", "", false);
-
- //add controls to the slot panel
- CommandControls partRibbonPanelCtrls;
- partRibbonPanelCtrls = m_partRibbonPanel.CommandControls;
-
- CommandControl cmdBtnCmdCtrl;
- cmdBtnCmdCtrl = partRibbonPanelCtrls.AddButton(m_Button.ButtonDefinition, false, true, "", false);
- }
- }
-
- }
-
- public void Deactivate()
- {
- // This method is called by Inventor when the AddIn is unloaded.
- // The AddIn will be unloaded either manually by the user or
- // when the Inventor session is terminated
-
- try
- {
- m_userInterfaceEvents.OnResetCommandBars -= UserInterfaceEventsSink_OnResetCommandBarsEventDelegate;
- m_userInterfaceEvents.OnResetEnvironments -= UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate;
-
- UserInterfaceEventsSink_OnResetCommandBarsEventDelegate = null;
- UserInterfaceEventsSink_OnResetEnvironmentsEventDelegate = null;
- m_userInterfaceEvents = null;
- if (m_partRibbonPanel != null)
- {
- m_partRibbonPanel.Delete();
- }
-
- //release inventor Application object
- Marshal.ReleaseComObject(m_inventorApplication);
- m_inventorApplication = null;
-
- GC.WaitForPendingFinalizers();
- GC.Collect();
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- private void UserInterfaceEvents_OnResetCommandBars(ObjectsEnumerator commandBars, NameValueMap context)
- {
- try
- {
-
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
-
- private void UserInterfaceEvents_OnResetEnvironments(ObjectsEnumerator environments, NameValueMap context)
- {
- try
- {
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
-
- private void UserInterfaceEvents_OnResetRibbonInterface(NameValueMap context)
- {
- try
- {
-
- UserInterfaceManager userInterfaceManager;
- userInterfaceManager = m_inventorApplication.UserInterfaceManager;
-
- //get the ribbon associated with part document
- Inventor.Ribbons ribbons;
- ribbons = userInterfaceManager.Ribbons;
-
- Inventor.Ribbon partRibbon;
- partRibbon = ribbons["Part"];
-
- //get the tabls associated with part ribbon
- RibbonTabs ribbonTabs;
- ribbonTabs = partRibbon.RibbonTabs;
-
- RibbonTab partSketchRibbonTab;
- partSketchRibbonTab = ribbonTabs["id_TabModel"];
-
- //create a new panel with the tab
- RibbonPanels ribbonPanels;
- ribbonPanels = partSketchRibbonTab.RibbonPanels;
- m_partRibbonPanel = ribbonPanels.Add("MyPanel", "InvWPFSample:myRibbonPanel", "{F86BD2AF-F06C-4973-9461-CBF9C12F1738}", "", false);
-
- //add controls to the slot panel
- CommandControls partRibbonPanelCtrls;
- partRibbonPanelCtrls = m_partRibbonPanel.CommandControls;
-
- CommandControl cmdBtnCmdCtrl;
- cmdBtnCmdCtrl = partRibbonPanelCtrls.AddButton(m_Button.ButtonDefinition, false, true, "", false);
- }
- catch (Exception e)
- {
- MessageBox.Show(e.ToString());
- }
- }
- public void ExecuteCommand(int commandID)
- {
- // Note:this method is now obsolete, you should use the
- // ControlDefinition functionality for implementing commands.
- }
-
- public object Automation
- {
- // This property is provided to allow the AddIn to expose an API
- // of its own to other programs. Typically, this would be done by
- // implementing the AddIn's API interface in a class and returning
- // that class object through this property.
-
- get
- {
- // TODO: Add ApplicationAddInServer.Automation getter implementation
- return null;
- }
- }
-
- #endregion
-
- #region COM Registration functions
-
- /// <summary>
- /// Registers this class as an Add-In for Autodesk Inventor.
- /// This function is called when the assembly is registered for COM.
- /// </summary>
- [ComRegisterFunctionAttribute()]
- public static void Register(Type t)
- {
- RegistryKey clssRoot = Registry.ClassesRoot;
- RegistryKey clsid = null;
- RegistryKey subKey = null;
-
- try
- {
- clsid = clssRoot.CreateSubKey("CLSID//" + AddInGuid(t));
- clsid.SetValue(null, "InvWPFSample");
- subKey = clsid.CreateSubKey("Implemented Categories//{39AD2B5C-7A29-11D6-8E0A-0010B541CAA8}");
- subKey.Close();
-
- subKey = clsid.CreateSubKey("Settings");
- subKey.SetValue("AddInType", "Standard");
- subKey.SetValue("LoadOnStartUp", "1");
-
- //subKey.SetValue("SupportedSoftwareVersionLessThan", "");
- subKey.SetValue("SupportedSoftwareVersionGreaterThan", "13..");
- //subKey.SetValue("SupportedSoftwareVersionEqualTo", "");
- //subKey.SetValue("SupportedSoftwareVersionNotEqualTo", "");
- //subKey.SetValue("Hidden", "0");
- //subKey.SetValue("UserUnloadable", "1");
- subKey.SetValue("Version", 0);
- subKey.Close();
-
- subKey = clsid.CreateSubKey("Description");
- subKey.SetValue(null, "InvWPFSample");
- }
- catch
- {
- System.Diagnostics.Trace.Assert(false);
- }
- finally
- {
- if (subKey != null) subKey.Close();
- if (clsid != null) clsid.Close();
- if (clssRoot != null) clssRoot.Close();
- }
-
- }
-
- /// <summary>
- /// Unregisters this class as an Add-In for Autodesk Inventor.
- /// This function is called when the assembly is unregistered.
- /// </summary>
- [ComUnregisterFunctionAttribute()]
- public static void Unregister(Type t)
- {
- RegistryKey clssRoot = Registry.ClassesRoot;
- RegistryKey clsid = null;
-
- try
- {
- clssRoot = Microsoft.Win32.Registry.ClassesRoot;
- clsid = clssRoot.OpenSubKey("CLSID//" + AddInGuid(t), true);
- clsid.SetValue(null, "");
- clsid.DeleteSubKeyTree("Implemented Categories//{39AD2B5C-7A29-11D6-8E0A-0010B541CAA8}");
- clsid.DeleteSubKeyTree("Settings");
- clsid.DeleteSubKeyTree("Description");
- }
- catch { }
- finally
- {
- if (clsid != null) clsid.Close();
- if (clssRoot != null) clssRoot.Close();
- }
- }
-
- // This function uses reflection to get the value for the GuidAttribute attached to the class.
- private static String AddInGuid(Type t)
- {
- string guid = "";
-
- try
- {
- Object[] customAttributes = t.GetCustomAttributes(typeof(GuidAttribute), false);
- GuidAttribute guidAttribute = (GuidAttribute)customAttributes[0];
- guid = "{" + guidAttribute.Value.ToString() + "}";
- }
- catch
- {
- }
-
- return guid;
-
- }
-
- #endregion
-
- }
- }
UserControl(WPF)控件的代码如下: [c-sharp] view plaincopy
- <UserControl x:Class="InvWPFSample.UserControl1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Height="305.806" Width="410.123" ToolTip="System.Windows.Controls.StackPanel">
- <Grid Height="317.238">
-
- <Button Margin="32,101,25,0" Name="button1" Height="49" VerticalAlignment="Top" Style="{DynamicResource GlowButton}" mce_Style="{DynamicResource GlowButton}">
- <Button.ToolTip>
- <StackPanel Orientation="Horizontal">
- <Image Source="c:/temp/new.jpg" Margin="3"/>
- <TextBlock>
- <Run FontWeight="Bold">Tooltips</Run>
- <LineBreak/>
- What about tooltips with pictures and formatting!
- </TextBlock>
- </StackPanel>
- </Button.ToolTip>
- <TextBlock>
- Look a <Run FontWeight="Bold">button</Run> with <Run FontStyle="Oblique">formatting! </Run>
- </TextBlock>
- </Button>
-
- </Grid>
- </UserControl>
因为VS.NET 2008不支持往一个ClassLibrary类型的工程中直接添加WPF windows form,所以我使用了一个普通的Windows Form,然后增加一个ElementHost来显示前面提到的WPF控件UserControl1。要做到这一点,就是在Form1的InitializeComponent函数里增加下面几行代码: [c-sharp] view plaincopy
- //show a WPF control in my form
- ElementHost host = new ElementHost();
- host.Dock = DockStyle.Fill;
- UserControl1 uc = new UserControl1(); // This is a WPF UserControl defined in XAML
- host.Child = uc;
- this.Controls.Add(host);
文章来源:http://blog.csdn.net/barbarahan
|