首页 > 编程语言 >AutoCAD C# RIBBOIN自动加载的研究

AutoCAD C# RIBBOIN自动加载的研究

时间:2024-10-13 14:24:16浏览次数:9  
标签:AutoCAD Title C# Windows RIBBOIN var new public ribbonTab

创建自定义类


 public struct AutoCADDllCommand
 {

     public string AssemblyName;

     public string clsName;

     public string MethodName;

     public string Macro;

     public MethodInfo MethodInfo;

 }


 public class MyRibbonTab
 {
     public string Title { get; set; }
     public string ID { get; set; }
     public List<AutoCADDllCommand> Commands { get; set; }

     public bool HasRibbonTab() => RibbonTabs == null ? false : RibbonTabs.Any(c => c.Title == Title && c.Id == ID);
     public static RibbonTabCollection RibbonTabs => ComponentManager.Ribbon?.Tabs;
     public MyRibbonTab(List<AutoCADDllCommand> cmdinfos, string title, string id)
     {
         Title = title;
         ID = id;
         Commands = cmdinfos;
         AddRibbonTab();
         acApp.Core.Application.SystemVariableChanged += (s, e) =>
         {
             if (e.Name.ToUpper() == "WSCURRENT")
             {
                 if (!HasRibbonTab()) AddRibbonTab();
             }
         };

         if (!HasRibbonTab())
         {
             ComponentManager.ItemInitialized -= ComponentManager_ItemInitialized;
             ComponentManager.ItemInitialized += ComponentManager_ItemInitialized;
         }
     }

     private void ComponentManager_ItemInitialized(object sender, RibbonItemEventArgs e)
     {
         if (HasRibbonTab())
         {
             ComponentManager.ItemInitialized -= ComponentManager_ItemInitialized;
             return;
         }
         AddRibbonTab();
     }

     public void AddRibbonTab()
     {
         if (RibbonTabs == null) return;
         RemoveRibbonTab();
         var ribbonTab = new RibbonTab() { Title = Title, Id = ID };
         ribbonTab.IsPanelEnabled = true;
         ribbonTab.Theme = Autodesk.Internal.Windows.TabThemes.Blue;
         var dicts = Commands.GroupBy(x => x.clsName).ToDictionary(c => c.Key, c => c.ToList().OrderBy(m => m.MethodName).ToList());
         foreach (var item in dicts)
         {
             var ribbonPanel = new RibbonPanel() {
                 FloatingOrientation = System.Windows.Controls.Orientation.Vertical,
                 CustomPanelTitleBarBackground = System.Windows.Media.Brushes.BlanchedAlmond
             };
             var rpr = new Autodesk.Windows.RibbonPanelSource() { Title = item.Key, Id = item.Key };
             var rfp = new Autodesk.Windows.RibbonFoldPanel()
             {
                 IsTopJustified = true,
                 ResizeStyle = RibbonItemResizeStyles.ChangeSize| RibbonItemResizeStyles.Collapse,
                 SubPanelResizeStyle = Autodesk.Windows.RibbonFoldPanelResizeStyle.NoResize,
                 DefaultSize = RibbonFoldPanelSize.Medium };
             foreach (var cmd in item.Value)
             {
                 var att = (CommandMethodAttribute)(cmd.MethodInfo.GetCustomAttribute(typeof(CommandMethodAttribute)));
                 var button = new Autodesk.Windows.RibbonButton
                 {
                     Name = att.GlobalName,
                     HelpTopic = "",
                     Orientation = System.Windows.Controls.Orientation.Vertical,
                     ResizeStyle = RibbonItemResizeStyles.ChangeSize,
                     Text = cmd.MethodName,
                     ShowText = true,
                     ShowImage = false,
                     IsVisible = true,
                     CommandHandler = new MyRibbonCommand(),
                     CommandParameter = cmd.Macro,
                     IsEnabled = true,
                     Size = RibbonItemSize.Standard,
                 };
                 //rrp.Items.Add(button);
                 //
                 rfp.Items.Add(button);
             }
             rpr.Items.Add(rfp);
             ribbonPanel.Source = rpr;
             //ribbonPanel.CustomPanelBackground= System.Windows.Media.Brushes.Red;
             ribbonTab.Panels.Add(ribbonPanel);
         }
         //ribbonTab.IsActive = true;
         RibbonTabs.Add(ribbonTab);
     }

     public void RemoveRibbonTab()
     {
         if (HasRibbonTab())
         {
             var ribbonTab = RibbonTabs.FirstOrDefault(c => c.Title == Title && c.Id == ID);
             if (ribbonTab != null)
             {
                 RibbonTabs.Remove(ribbonTab);
             }
         }
     }
     
      public static List<AutoCADDllCommand> GetAssemblyAutoCADCmds( Assembly ass = null)
 {
     var rtn = new List<AutoCADDllCommand>();
     if (ass == null)
         ass = Assembly.GetExecutingAssembly();
     //'提取全部的类型
     var clsColls = ass.GetTypes().Where(a => a.IsClass && a.IsPublic);
     var mis = new List<MethodInfo>();
     clsColls.ForEach(cls =>
                         {
                             var methods = cls.GetMethods().Where
                             (mi => mi.IsPublic &&
                             mi.GetCustomAttribute(typeof(CommandMethodAttribute)) != null);
                             if (methods.Any()) mis.AddRange(methods);
                         }
                     );

     if (!mis.Any()) return null;
     foreach (var mi in mis)
     {
         var att = mi.GetCustomAttribute(typeof(CommandMethodAttribute), true) as CommandMethodAttribute;
         rtn.Add(new AutoCADDllCommand()
         {
             AssemblyName = Path.GetFileNameWithoutExtension(ass.Location),
             clsName = mi.DeclaringType.Name,
             MethodName = mi.Name,
             Macro = $"{(Char)3}{(Char)3}{(Char)95}{att.GlobalName}{(Char)32}",
             MethodInfo = mi
         });
     }
     return rtn.OrderBy(c => c.AssemblyName).ThenBy(c => c.clsName).ThenBy(c => c.MethodName).ToList();

 }
 }

 public class MyRibbonCommand : ICommand
 {
     public event EventHandler CanExecuteChanged;

     public bool CanExecute(object parameter)
     {
         return true;
     }

     public void Execute(object parameter)
     {
         var ctl = parameter as Autodesk.Windows.RibbonCommandItem;
         Application.DocumentManager.MdiActiveDocument.SendStringToExecute(ctl.CommandParameter.ToString().TrimEnd() + " ", true, false, false);
     }
 }

调用方法

 var cmds = CMDHelper.GetAssemblyAutoCADCmds(ass);
 var assName= ass.GetName().Name; 
  MyRibbonTab myRibbonTab = new MyRibbonTab(cmds, assName, assName);

效果如下

 

标签:AutoCAD,Title,C#,Windows,RIBBOIN,var,new,public,ribbonTab
From: https://www.cnblogs.com/NanShengBlogs/p/18462261

相关文章

  • c#线程同步&线程安全
    一:线程同步由于现代计算机CPU的多线程技术普及,所有的资源访问都要面临一个问题:在多线程情况下,如何有序访问资源?为此,线程同步技术应运而生。线程同步的技术分三类:1.排它锁:排它锁,顾名思义,独占(资源)、排他(其他线程或者进程)。排它锁每一次只允许一个线程执行特定的活动或者代码。其......
  • P11022 「LAOI-6」Yet Another Graph Coloration Problem
    P11022「LAOI-6」YetAnotherGraphColorationProblem-洛谷|计算机科学教育新生态(luogu.com.cn)关于无解情况,如果这个图有两块连通块,那么不可能同时有黑色白色,假设\(1,2\)连通块,设\(1\)中有黑色,因为\(2\)中点不能到\(1\),所以\(2\)中只能是黑色,又因为\(2\)中......
  • C++STL~~map
    文章目录序列式容器和关联式容器一、map的概念二、map的使用三、map&multimap的差异四、map的练习五、总结序列式容器和关联式容器序列式容器前面已经接触过STL中的部分容器如:string、vector、list、deque、array、forward_list等,这些容器统称为序列式容器,因为逻辑......
  • [LeetCode] 315. 计算右侧小于当前元素的个数
    题目描述:给你一个整数数组 nums ,按要求返回一个新数组 counts 。数组 counts 有该性质: counts[i] 的值是  nums[i] 右侧小于 nums[i] 的元素的数量。题目链接:.-力扣(LeetCode)题目主要思路:其实跟“LCR170.交易逆序对的总数”那道题差不多,就是多了个数组来......
  • iDirect IQ200 REST API
    iDirectIQ200RESTAPI ######################################HTTP基本认证方式(命令行参数中直接输入用户名和密码)获取ModemLED状态信息###请求命令curl-uadmin:P@55w0rd!-isk'https://172.28.170.217/api/1.0/terminal/device/led'#响应头数据HTTP......
  • Mac下使用VSCode开发react native快捷键与终端命令
    一、快捷键在macOS下使用VSCode开发ReactNative时,掌握常用的快捷键可以提高开发效率。以下是一些常用的VSCode快捷键,特别适用于ReactNative开发:1、基础快捷键打开命令面板:Cmd+Shift+P打开VSCode的所有命令面板,可以快速执行命令。打开终端:Ctrl+~......
  • 关于使用plsql操作oracle的一点小技巧和几个常用的查询语句BU
    plsql是什么:就是这个,专门操作oracle的一个工具,好用还免费。创建一个测试表:createtableStudent(Idnumbernotnull,Namevarchar(20),Agenumber,Gradenumber,Gendervarchar(2))里面的varchar2()是oracle自己专门的字符类型,用就行了。光标移到表上,右键选择Descr......
  • 【Azure Cloud Service】使用RESTAPI更新Cloud Service(Extended Support) 中所配置的
    问题描述当根据CloudService(ExtendedSupport)文档更新证书(https://docs.azure.cn/zh-cn/cloud-services-extended-support/certificates-and-key-vault)时,如果遇见旧的证书(如中间证书,根证书)信息保存在KeyVaultSecret中,而更新的时候,只能从KeyVault证书中匹配到服务......
  • 实验1 现代C++编程初体验
    Task1code1.cpp1//现代C++标准库、算法库体验2//本例用到以下内容:3//1.字符串string,动态数组容器类vector、迭代器4//2.算法库:反转元素次序、旋转元素5//3.函数模板、const引用作为形参67#include<iostream>8#include<string>......
  • Nuxt.js 应用中的 kit:compatibility 事件钩子详解
    title:Nuxt.js应用中的kit:compatibility事件钩子详解date:2024/10/11updated:2024/10/11author:cmdragonexcerpt:kit:compatibility是处理浏览器兼容性问题的有效工具。正如本篇文章中所述,合理地利用这一钩子可以提升用户体验,并确保应用在不同环境中都能稳......