using Autodesk.Navisworks.Api; using Autodesk.Navisworks.Api.Plugins; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using NavisApplication = Autodesk.Navisworks.Api.Application; namespace NavisworksTransport { [PluginAttribute("Basic", "Tian", ToolTip = "Transport Plugin", DisplayName = "Transport Plugin")] [AddInPlugin(AddInLocation.AddIn)] // 将插件显示在Navisworks的"附加模块"选项卡中 public class Main : AddInPlugin { public override int Execute(params string[] parameters) { try { // 显示类别选择对话框 ShowCategorySelectionDialog(); } catch (Exception ex) { MessageBox.Show($"插件执行出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } return 0; } /// /// 显示类别选择对话框 /// private void ShowCategorySelectionDialog() { // 获取当前选中的模型项数量 int selectedCount = NavisApplication.ActiveDocument.CurrentSelection.SelectedItems.Count; // 创建对话框 Form dialog = new Form { Text = "物流路径规划插件控制面板", Size = new Size(350, 550), StartPosition = FormStartPosition.CenterParent, FormBorderStyle = FormBorderStyle.FixedDialog, MaximizeBox = false, MinimizeBox = false }; // 创建主面板 Panel mainPanel = new Panel { Dock = DockStyle.Fill, Padding = new Padding(20) }; dialog.Controls.Add(mainPanel); // 添加选中项目数量标签 Label countLabel = new Label { Text = $"当前选中: {selectedCount} 个模型项", Font = new Font("微软雅黑", 10), ForeColor = selectedCount > 0 ? System.Drawing.Color.Blue : System.Drawing.Color.Red, AutoSize = true, Location = new Point(0, 0) }; mainPanel.Controls.Add(countLabel); // 如果没有选中项目,显示提示 if (selectedCount == 0) { Label hintLabel = new Label { Text = "请先在Navisworks中选择要设置属性的模型项", Font = new Font("微软雅黑", 9), ForeColor = System.Drawing.Color.Gray, AutoSize = false, Size = new Size(290, 40), Location = new Point(0, 35) }; mainPanel.Controls.Add(hintLabel); } // 创建类别设置GroupBox GroupBox categoryGroupBox = new GroupBox { Text = "类别设置", Location = new Point(0, selectedCount > 0 ? 40 : 80), Size = new Size(290, 310), Font = new Font("微软雅黑", 9, FontStyle.Bold) }; mainPanel.Controls.Add(categoryGroupBox); // 创建类别按钮 CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.门, 0); CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.电梯, 1); CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.楼梯, 2); CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.通道, 3); CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.障碍物, 4); CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.装卸区, 5); CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.停车位, 6); CreateCategoryButton(categoryGroupBox, CategoryAttributeManager.LogisticsElementType.检查点, 7); // 创建可见性控制GroupBox GroupBox visibilityGroupBox = new GroupBox { Text = "可见性控制", Location = new Point(0, categoryGroupBox.Bottom + 20), Size = new Size(290, 120), Font = new Font("微软雅黑", 9, FontStyle.Bold) }; mainPanel.Controls.Add(visibilityGroupBox); // 创建可见性控制按钮和状态标签 CreateVisibilityControls(visibilityGroupBox); // 创建关闭按钮 Button closeButton = new Button { Text = "关闭", Size = new Size(80, 30), Location = new Point(210, visibilityGroupBox.Bottom + 20), DialogResult = DialogResult.OK }; mainPanel.Controls.Add(closeButton); // 显示对话框 dialog.ShowDialog(); } /// /// 创建类别按钮 /// /// 父容器 /// 元素类型 /// 按钮索引 private void CreateCategoryButton(GroupBox parent, CategoryAttributeManager.LogisticsElementType elementType, int index) { Button button = new Button { Text = $"设为{elementType}", Size = new Size(200, 35), Location = new Point(45, 30 + index * 35), Font = new Font("微软雅黑", 10), UseVisualStyleBackColor = true }; // 添加点击事件 button.Click += (sender, e) => { try { // 获取当前选中的模型项 ModelItemCollection selectedItems = NavisApplication.ActiveDocument.CurrentSelection.SelectedItems; if (selectedItems.Count == 0) { MessageBox.Show("请先选择要设置属性的模型项", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // 执行属性添加操作 int successCount = CategoryAttributeManager.AddLogisticsAttributes( selectedItems, elementType, elementType != CategoryAttributeManager.LogisticsElementType.障碍物, // 障碍物默认不可通行 5, // 默认优先级 "标准", // 默认车辆尺寸 10.0 // 默认速度限制 ); // 显示结果 if (successCount > 0) { MessageBox.Show($"成功为 {successCount} 个模型项设置了物流属性", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("未能为任何模型项设置属性,请检查选择的模型项", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { MessageBox.Show($"设置属性时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }; parent.Controls.Add(button); } /// /// 创建可见性控制界面 /// /// 父容器 private void CreateVisibilityControls(GroupBox parent) { // 只显示物流分类项目复选框 CheckBox logisticsOnlyCheckBox = new CheckBox { Text = "只显示物流分类项目", Size = new Size(200, 25), Location = new Point(20, 30), Font = new Font("微软雅黑", 10), Checked = false, UseVisualStyleBackColor = true }; // 状态标签 Label statusLabel = new Label { Text = "状态: 显示所有项目", Location = new Point(20, 70), Size = new Size(250, 20), Font = new Font("微软雅黑", 9), ForeColor = System.Drawing.Color.DarkGreen }; // 复选框事件处理 logisticsOnlyCheckBox.CheckedChanged += (sender, e) => { try { logisticsOnlyCheckBox.Enabled = false; statusLabel.Text = "状态: 正在处理..."; statusLabel.ForeColor = System.Drawing.Color.Orange; VisibilityOperationResult result; if (logisticsOnlyCheckBox.Checked) { // 勾选时:隐藏非物流分类项目 result = VisibilityManager.HideNonLogisticsItems(); if (result.Success) { statusLabel.Text = $"状态: 已隐藏 {result.HiddenCount} 个非物流项目"; statusLabel.ForeColor = System.Drawing.Color.DarkGreen; } else { statusLabel.Text = "状态: 隐藏操作失败"; statusLabel.ForeColor = System.Drawing.Color.Red; logisticsOnlyCheckBox.Checked = false; // 回滚状态 } } else { // 取消勾选时:显示所有项目 result = VisibilityManager.ShowAllItems(); if (result.Success) { statusLabel.Text = "状态: 显示所有项目"; statusLabel.ForeColor = System.Drawing.Color.DarkGreen; } else { statusLabel.Text = "状态: 显示操作失败"; statusLabel.ForeColor = System.Drawing.Color.Red; logisticsOnlyCheckBox.Checked = true; // 回滚状态 } } // 如果操作失败,显示错误信息 if (!result.Success) { MessageBox.Show(result.Message, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { statusLabel.Text = "状态: 发生错误"; statusLabel.ForeColor = System.Drawing.Color.Red; logisticsOnlyCheckBox.Checked = !logisticsOnlyCheckBox.Checked; // 回滚状态 MessageBox.Show($"操作出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { logisticsOnlyCheckBox.Enabled = true; } }; // 添加控件到父容器 parent.Controls.Add(logisticsOnlyCheckBox); parent.Controls.Add(statusLabel); } } }