Inventor 的原生界面可通过 API 进行深度定制,包括添加功能区按钮、自定义对话框、创建右键菜单等,使开发的插件更符合用户的操作习惯,提升工具的易用性。本章将讲解 Inventor 用户界面(UI)的核心对象模型,以及各类界面元素的开发方法。
7.1 用户界面对象模型概述
Inventor 的用户界面由UserInterfaceManager(用户界面管理器)统一管理,其核心对象层次结构如下:
7.2 自定义命令的创建
在创建界面元素之前,需先创建自定义命令(Command),命令是界面控件的核心逻辑载体。
7.2.1 创建命令对象
using Inventor; using System; using System.Runtime.InteropServices; // 需实现CommandEventHandler接口以响应命令事件 [ComVisible(true)] public class CustomCommand : CommandEventHandler { private Application _inventorApp; private Command _customCommand; public CustomCommand(Application inventorApp) { _inventorApp = inventorApp; CreateCommand(); } // 创建自定义命令 public void CreateCommand() { try { CommandManager cmdManager = _inventorApp.CommandManager; // 创建命令分类(若不存在) CommandCategory cmdCategory = null; try { cmdCategory = cmdManager.CommandCategories["我的自定义命令"]; } catch { cmdCategory = cmdManager.CommandCategories.Add("我的自定义命令"); } // 创建命令 _customCommand = cmdManager.Commands.Add( "MyCustomCommand", // 命令ID "批量建模工具", // 命令名称 "用于批量创建零件模型的工具", // 命令说明 CommandTypesEnum.kShapeEditCommandType, // 命令类型 GetType().GUID.ToString() // 事件处理程序GUID ); // 将命令添加到分类 cmdCategory.Commands.Add(_customCommand); // 注册命令事件 _customCommand.OnExecute += new CommandEventHandler_OnExecuteEventHandler(OnExecute); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建命令失败:" + ex.Message); } } // 命令执行事件 public void OnExecute(Command cmd) { _inventorApp.UserInterfaceManager.MessageBox.Show("批量建模工具已执行!"); // 此处添加命令的核心逻辑 } // 实现CommandEventHandler接口的必需方法 public void OnHelp(Command cmd) { } public void OnSelect(Command cmd, CommandSelectionStateEnum selectionState) { } }注意:自定义命令类需添加[ComVisible(true)]特性,并实现CommandEventHandler接口,否则无法注册事件。
7.2.2 命令的启用与禁用
可通过命令的Enabled属性控制命令的启用状态:
// 启用/禁用命令 public void SetCommandEnabled(bool enabled) { _customCommand.Enabled = enabled; }7.3 功能区(Ribbon)的定制
功能区是 Inventor 的主要界面元素,通过 API 可添加自定义的功能区标签、面板和按钮。
7.3.1 创建功能区标签与面板
// 创建功能区标签和面板 public RibbonPanel CreateRibbonPanel() { try { UserInterfaceManager uiManager = _inventorApp.UserInterfaceManager; // 获取默认的功能区(如零件环境的功能区) Ribbon ribbon = uiManager.Ribbons["零件"]; // 创建功能区标签(若不存在) RibbonTab ribbonTab = null; try { ribbonTab = ribbon.RibbonTabs["我的工具"]; } catch { ribbonTab = ribbon.RibbonTabs.Add("我的工具", "MyToolsTab", GetType().GUID.ToString()); } // 创建功能区面板 RibbonPanel ribbonPanel = ribbonTab.RibbonPanels.Add("批量处理", "BatchProcessPanel", GetType().GUID.ToString()); return ribbonPanel; } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建功能区面板失败:" + ex.Message); return null; } }7.3.2 添加按钮控件到面板
将自定义命令关联到按钮控件,添加到功能区面板中:
// 向面板添加按钮 public void AddButtonToPanel(RibbonPanel panel, Command cmd) { try { // 创建按钮控件 ButtonControl button = panel.CommandControls.AddButton( cmd, // 关联的命令 true, // 是否显示文本 true, // 是否显示图标 "MyButton", // 控件ID CommandControlsAlignmentEnum.kHorizontalAlignment // 对齐方式 ); // 设置按钮图标(可选) // button.Icon = _inventorApp.TransientGraphics.CreateBitmap(@"D:\Icons\MyIcon.bmp"); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("添加按钮失败:" + ex.Message); } }7.3.3 添加其他控件(下拉框、切换按钮)
// 添加下拉控件 public void AddDropDownToPanel(RibbonPanel panel) { try { // 创建下拉控件 DropDownControl dropDown = panel.CommandControls.AddDropDown( "MyDropDown", // 控件ID true, // 是否显示文本 CommandControlsAlignmentEnum.kHorizontalAlignment // 对齐方式 ); // 添加下拉项 dropDown.Items.Add("选项1", "Option1", null); dropDown.Items.Add("选项2", "Option2", null); dropDown.Items.Add("选项3", "Option3", null); // 注册下拉项选择事件 dropDown.OnSelect += new DropDownControl_OnSelectEventHandler(OnDropDownSelect); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("添加下拉控件失败:" + ex.Message); } } // 下拉项选择事件 private void OnDropDownSelect(DropDownControl dropDown, DropDownItem item) { _inventorApp.UserInterfaceManager.MessageBox.Show("选择了:" + item.DisplayName); } // 添加切换按钮 public void AddToggleButtonToPanel(RibbonPanel panel, Command cmd) { try { ToggleButtonControl toggleButton = panel.CommandControls.AddToggleButton( cmd, // 关联的命令 true, // 是否显示文本 true, // 是否显示图标 "MyToggleButton", // 控件ID CommandControlsAlignmentEnum.kHorizontalAlignment // 对齐方式 ); // 设置默认状态 toggleButton.Value = false; } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("添加切换按钮失败:" + ex.Message); } }