2025年质量好的单柱液压机厂家最新权威推荐排行榜 - 行业平台推荐
2025/12/17 13:20:40
自定义对话框是插件与用户交互的重要方式,可用于输入参数、选择选项等。Inventor 开发中常用的对话框开发方式有两种:Windows Forms 对话框和Inventor 内置对话框。
在 Visual Studio 中添加 “Windows 窗体” 项,设计对话框界面:
using System.Windows.Forms; public partial class BatchModelDialog : Form { // 定义用于传递参数的属性 public int PartCount { get; set; } public double PartLength { get; set; } public string SavePath { get; set; } public BatchModelDialog() { InitializeComponent(); // 设置默认值 numericUpDown_Count.Value = 10; numericUpDown_Length.Value = 100; textBox_SavePath.Text = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop); } // 确定按钮事件 private void button_Ok_Click(object sender, EventArgs e) { PartCount = (int)numericUpDown_Count.Value; PartLength = (double)numericUpDown_Length.Value; SavePath = textBox_SavePath.Text; DialogResult = DialogResult.OK; Close(); } // 取消按钮事件 private void button_Cancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } // 浏览文件夹按钮事件 private void button_Browse_Click(object sender, EventArgs e) { using (FolderBrowserDialog fbd = new FolderBrowserDialog()) { if (fbd.ShowDialog() == DialogResult.OK) { textBox_SavePath.Text = fbd.SelectedPath; } } } }// 命令执行事件中调用对话框 public void OnExecute(Command cmd) { using (BatchModelDialog dialog = new BatchModelDialog()) { if (dialog.ShowDialog() == DialogResult.OK) { // 获取用户输入的参数 int count = dialog.PartCount; double length = dialog.PartLength; string savePath = dialog.SavePath; // 执行批量建模逻辑 _inventorApp.UserInterfaceManager.MessageBox.Show($"将创建{count}个长度为{length}mm的零件,保存到{savePath}"); } } }Inventor 提供了一些内置的对话框(如文件选择对话框、消息框),可直接调用:
// 显示文件选择对话框 public string ShowFileDialog() { try { FileDialog fileDialog = _inventorApp.FileDialogs.FileOpenDialog; fileDialog.Filter = "零件文件 (*.part)|*.part|装配体文件 (*.iam)|*.iam"; fileDialog.ShowDialog(); return fileDialog.FileName; } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("打开文件对话框失败:" + ex.Message); return string.Empty; } }上下文菜单是在模型上右键点击时显示的菜单,可通过 API 添加自定义菜单项。
// 创建上下文菜单 public void CreateContextMenu() { try { UserInterfaceManager uiManager = _inventorApp.UserInterfaceManager; ContextualMenuManager menuManager = uiManager.ContextualMenuManager; // 获取零件环境的上下文菜单(如选择实体时的菜单) ContextualMenu contextMenu = menuManager.ContextualMenus["PartSelect"]; // 添加菜单项 ContextualMenuItem menuItem = contextMenu.Items.Add( "我的自定义菜单项", // 菜单项名称 "MyContextMenuItem", // 菜单项ID true, // 是否启用 true // 是否显示 ); // 注册菜单项点击事件 menuItem.OnExecute += new ContextualMenuItem_OnExecuteEventHandler(OnContextMenuItemExecute); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("创建上下文菜单失败:" + ex.Message); } } // 上下文菜单项点击事件 private void OnContextMenuItemExecute(ContextualMenuItem item) { _inventorApp.UserInterfaceManager.MessageBox.Show("自定义菜单项被点击!"); }在插件退出时,需清理创建的命令、界面元素,避免占用 Inventor 资源:
// 清理界面元素 public void CleanUp() { try { UserInterfaceManager uiManager = _inventorApp.UserInterfaceManager; CommandManager cmdManager = _inventorApp.CommandManager; // 删除功能区标签 Ribbon ribbon = uiManager.Ribbons["零件"]; try { ribbon.RibbonTabs["我的工具"].Delete(); } catch { } // 删除命令分类 try { cmdManager.CommandCategories["我的自定义命令"].Delete(); } catch { } // 删除上下文菜单项 ContextualMenuManager menuManager = uiManager.ContextualMenuManager; ContextualMenu contextMenu = menuManager.ContextualMenus["PartSelect"]; try { contextMenu.Items["MyContextMenuItem"].Delete(); } catch { } _inventorApp.UserInterfaceManager.MessageBox.Show("界面清理完成!"); } catch (Exception ex) { _inventorApp.UserInterfaceManager.MessageBox.Show("界面清理失败:" + ex.Message); } }