c# aveva marine hull design Design Explorer 的自定义

视屏效果:

https://www.bilibili.com/video/BV1AF4m177cP/?vd_source=0b221dbd75584a13ab6cd4551f3a0ec2

首先显示界面如下

左边为改造后的,右边为默认的界面

1# 将内部名称显示改为装配名显示

2# 直接显示了零件是否已经拉装配

这部分代码也很简单

其次改造下元素的右键菜单

封装的方法,没写注释,看不明白可以联系我交流


public class MyDesignExplorerRightClicKAtt : Attribute
{
    public string AllowType { get; set; }
    public string ToolTiString { get; set; }
    public MyDesignExplorerRightClicKAtt(string allowEleType = "", string Caption = "")
    {
        this.AllowType = allowEleType;
        this.ToolTiString = Caption;
    }
}

public class AM界面管理
 {
     static CommandBarManager CommandBarManager =>(ServiceManager.Instance.GetService(typeof(CommandBarManager)) as CommandBarManager);
     static IExplorer Design_Explorer =>(ServiceManager.Instance.GetService(typeof(ExplorerService)) as ExplorerService).GetExplorer("DESIGN EXPLORER");
     static List<MenuTool> MyMenus = new List<MenuTool>();
     static string LastDbeleType = "";
     public static void DesignxplorerLetRightAttributeCustomized(bool OnorOff = true, DbAttribute left = null, DbAttribute right = null)
     {
         try
         {
             var de1 = Design_Explorer;
             var cbm = CommandBarManager;
             var config = de1.Configuration;
             if (OnorOff)
             {
                 config.FontStyle.LeftNameColor = System.Drawing.Color.Gray;
                 config.FontStyle.LeftNameAttribute = left == null ? DbAttributeInstance.ASMBLS : left;
                 config.FontStyle.NameColor = System.Drawing.Color.DarkRed;
                 //config.FontStyle.NameAttribute = DbAttributeInstance.FLNN;
                 config.FontStyle.RightNameAttribute = right == null ? DbAttributeInstance.PRTIDL : right;
                 config.FontStyle.RightNameColor = System.Drawing.Color.Blue;
                 config.ShowLeaves = true;
                 config.FontName = "Arial";
                 //AM界面管理.DesginExplorerShowAssemblyAndParnamFlag = true;
                 #region 增加右键菜单
                 cbm.AllowCustomization = true;
                 var curArr = Assembly.GetExecutingAssembly();
                 var allCls = curArr.GetTypes().Where(t => t.IsPublic && t.IsClass);
                 foreach (var item in allCls)
                 {
                     var curClsMis = item.GetMethods().Where(m =>
                     m.GetCustomAttributes(typeof(MyDesignExplorerRightClicKAtt), true).Any() && m.IsStatic && m.IsPublic);
                     var methodInfos = curClsMis.ToList();
                     if (methodInfos.Any())
                     {
                         if (!cbm.RootTools.Contains(item.Name))
                         {
                             var cbmtMain = cbm.RootTools.AddMenuTool(item.Name, item.Name, null);
                             if (!config.ContextMenu.Tools.Contains(cbmtMain.Key))
                             {
                                 var itm = config.ContextMenu.Tools.AddTool(cbmtMain.Key) as MenuTool;
                                 foreach (var mi in methodInfos)
                                 {
                                     var att = mi.GetCustomAttributes(typeof(MyDesignExplorerRightClicKAtt), true).FirstOrDefault() as MyDesignExplorerRightClicKAtt;
                                     var cmdKey = cbmtMain.Key + "." + mi.Name;
                                     var cbmtSub = cbm.RootTools.AddButtonTool(cmdKey, mi.Name, null);
                                     cbmtSub.ToolClick += (s, e) => { mi.Invoke(null, null); };
                                     var its = itm.Tools.AddTool(cbmtSub.Key);
                                     its.IsFirstInGroup = true;
                                     its.Visible = true;
                                     its.Tag = att.AllowType;
                                     if (att.ToolTiString != "")
                                         its.Tooltip = att.ToolTiString;
                                 }
                                 itm.IsFirstInGroup = true;
                                 itm.Visible = true;
                                 MyMenus.Add(itm);
                             }
                         }
                     }
                 }
                 //config.RightContextMenu.ShowPopup();
                 config.ContextMenu.BeforePopup += ContextMenu_BeforePopup;
                 config.ContextMenu.AfterCloseup += ContextMenu_AfterCloseup;
                 config.ContextMenu.PopupStyle = PopupStyle.Menu;
                 config.ContextMenu.DropDownArrowStyle = DropDownArrowStyle.SegmentedStateButton;
                 //config.ContextMenu.AllowTearaway = true;
                 #endregion
             }
             else
             {
                 config.FontStyle.LeftNameAttribute = null;
                 config.FontStyle.RightNameAttribute = null;
             }
             de1.Configuration = config;
         }
         catch (Exception ex)
         {
             ex.OutPutExcptionMess();
         }
     }

     private static void ContextMenu_AfterCloseup(object sender, EventArgs e)
     {
         try
         {
             foreach (var item in MyMenus)
             {

                 foreach (ITool btl in item.Tools)
                 {
                     btl.Visible = true;
                 }
                 item.Visible = true;
             }
         }
         catch (Exception ex)
         {
             ex.OutPutExcptionMess();
         }
     }

     private static void ContextMenu_BeforePopup(object sender, EventArgs e)
     {
         try
         {
             var activeNode = UIEx.Design_Explorer.SelectedNode as ExplorerTreeNode;
             if (activeNode == null) return;
             var curEleType = activeNode.Element.GetActualType().Name;
             foreach (var item in MyMenus)
             {
                 int counUnVisiable = 0;

                 foreach (ITool btl in item.Tools)
                 {
                     var methodNeedType = btl.Tag.ToString();
                     if (methodNeedType == "")
                     {
                         btl.Visible = true;
                     }
                     else
                     {
                         if (!methodNeedType.Contains(","))
                         {
                             btl.Visible = string.Compare(methodNeedType, curEleType, true) == 0;
                             if (!btl.Visible)
                             {
                                 counUnVisiable++;
                             }
                         }
                         else
                         {
                             var types = methodNeedType.Split(',').ToList().Select(c => c.ToUpper());
                             btl.Visible = types.Contains(curEleType);
                             if (!btl.Visible)
                             {
                                 counUnVisiable++;
                             }
                         }
                     }
                 }
                 if (counUnVisiable == item.Tools.Count)
                     item.Visible = false;
             }
         }
         catch (Exception ex)
         {
             ex.OutPutExcptionMess();
         }
     }
 }

示例方法

 

[MyDesignExplorerRightClicKAtt("")]
public static void 查找占用人员信息()
{
    try
    {
        var cdl = Aveva.Pdms.Graphics.DrawListManager.Instance.CurrentDrawList;
        AmHullEnv env = new AmHullEnv();
        var ce = Aveva.Pdms.Shared.CurrentElement.Element;
        if (!ce.IsOk()) return;
        StringBuilder sb = new StringBuilder();
        List<DbAttribute> attributes = new List<DbAttribute>()
        {
            DbAttributeInstance.CLMID,
             DbAttributeInstance.USERC
        };
        foreach (var item in attributes)
        {
            sb.AppendLine($"{item.ShortName}====>{ce.GetAsString(item)}");
        }
        env.Ui.MessageConfirm(sb.ToString());
    }
    catch (Exception ex)
    {
        ex.OutPutExcptionMess();
    }
}

 

posted @ 2024-02-16 19:24  南胜NanSheng  阅读(171)  评论(0编辑  收藏  举报