Mã nguồn làm việc với Ribbon trong AutoCAD
Ngoài việc thêm các thành phần, còn có 1 số vấn đề liên quan đến Ribbon như:
- Cần thêm tham chiếu References đến thư viện nào?
- Khi load thì sẽ phải kiểm tra Panel đã có hay chưa.
- Load Ribbon (Panel) vào thời điểm nào? Khi mở Autocad, hay khi gõ lệnh
- Gán lệnh tắt (ShortcutKey) cho nút RibbonButton ntn?
Projects mẫu của AJS trong đây sẽ đề cập gần như đầy đủ hết các vấn đề ở trên.
Bước 1 - Tạo Project mới
Tạo dự án mới với tên AJS-RibbonAddings, hoặc tên bất kỳ do bạn chọn.
Bước 2 - Thêm liên kết References
Thêm thư viện đến AdWindows.dll có trong thư mục cài đặt AutoCAD
Bước 3 - Chỉnh sửa myPlugin.cs
(Copy nội dung sau)
Code:
using Autodesk.AutoCAD.ApplicationServices;
using System;
namespace AJS_RibbonAddings
{
public class AssignCommandCmd : System.Windows.Input.ICommand
{
public string cmd = "";
public AssignCommandCmd(string _cmd)
{
cmd = _cmd.Replace("_.", "").Trim();
if (Left(cmd, 1) == "_") cmd = Mid(cmd, 1, 100);
if (Left(cmd, 1) == ".") cmd = Mid(cmd, 1, 100);
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
Run(cmd);
}
private static void Run(string cmd)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
using (var dl = doc.LockDocument())
{
string esc = "";
string cmds = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMDNAMES");
if (cmds.Length > 0)
{
int cmdNum = cmds.Split(new char[] { '\'' }).Length;
for (int i = 0; i < cmdNum; i++)
esc += '\x03';
}
doc.SendStringToExecute(esc + "_." + cmd.Trim() + " ", true, false, true);
}
}
private string Left(string input, int count)
{
try
{
return input.Substring(0, Math.Min(input.Length, count));
}
catch { }
return "";
}
private static string Mid(string input, int start, int count)
{
return input.Substring(Math.Min(start, input.Length), Math.Min(count, Math.Max(input.Length - start, 0)));
}
}
}
(Copy nội dung sau)
Code:
using System;
using System.Drawing;
using System.IO;
using System.Windows.Media.Imaging;
namespace AJS_RibbonAddings
{
internal static class Extensions
{
public static string Left(this string input, int count)
{
try
{
return input.Substring(0, Math.Min(input.Length, count));
}
catch { }
return "";
}
public static string Mid(this string input, int start, int count)
{
return input.Substring(Math.Min(start, input.Length), Math.Min(count, Math.Max(input.Length - start, 0)));
}
public static BitmapImage ToImageSource(this Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
}
}
(Copy nội dung sau)
Code:
// (C) Copyright 2024 by www.lisp.vn
//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Customization;
//using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Windows;
using System.Diagnostics;
using System.IO;
using RibbonButton = Autodesk.Windows.RibbonButton;
using RibbonControl = Autodesk.Windows.RibbonControl;
using RibbonPanelSource = Autodesk.Windows.RibbonPanelSource;
using RibbonSplitButton = Autodesk.Windows.RibbonSplitButton;
// This line is not mandatory, but improves loading performances
[assembly: ExtensionApplication(typeof(AJS_RibbonAddings.MyPlugin))]
namespace AJS_RibbonAddings
{
public class MyPlugin : IExtensionApplication
{
const string Caption = "AJS Ribbon Addings - www.lisp.vn";
void IExtensionApplication.Initialize()
{
ComponentManager.ItemInitialized += (s, e) => ToggleButton();
}
void IExtensionApplication.Terminate()
{
// Do plug-in application clean up here
}
private static string AddInPath = typeof(MyPlugin).Assembly.Location;
// Button icons directory
private static string FolderPath = Path.GetDirectoryName(AddInPath);
public static void ToggleButton()
{
if (F4 != null) return;
RibbonControl ribbonControl = ComponentManager.Ribbon;
if (ribbonControl == null) return;
var Tab = ribbonControl.FindTab("ID_AJS_By_LISP_VN");
if (Tab == null)
{
Tab = new RibbonTab();
Tab.Title = "AutoLISP Just Simple - www.lisp.vn";
Tab.Id = "ID_AJS_By_LISP_VN";
ribbonControl.Tabs.Add(Tab);
}
if (Tab == null) Tab = ribbonControl.FindTab("ACAD.RBN_00012112");
if (Tab != null)
{
var Panel = ribbonControl.FindPanel("ID_AJS_RibbonAddings_2024", false);
if (Panel == null)
{
Debug.Print("Found: Title=" + Tab.Title + " Id=" + Tab.Id + " UID=" + Tab.UID);
//Panel.AutomationName = "AutoCAD Debugger by AJS";
RibbonPanelSource srcPanel = new RibbonPanelSource();
srcPanel.Title = "Debuger";
//srcPanel.Items.Insert(2, new RibbonSeparator());
F4 = new RibbonButton();
F4.Id = "cmd_F4";
F4.Text = "Copy all";
//F4.Size = RibbonItemSize.Large;
//F4.Orientation = System.Windows.Controls.Orientation.Horizontal;
F4.ShowText = true;
F4.ShowImage = true;
F4.CommandParameter = "^C^C_F4";
F4.CommandHandler = new AssignCommandCmd("F4");
F4.Image = Properties.Resources.ID_ACETALIAS.ToImageSource();
F4.LargeImage = Properties.Resources.ID_ACETALIAS.ToImageSource();
F5 = new RibbonButton();
F5.Id = "cmd_F5";
F5.Text = "ProgressMeter";
//F5.Size = RibbonItemSize.Large;
//F5.Orientation = System.Windows.Controls.Orientation.Vertical;
F5.ShowText = true;
F5.ShowImage = true;
F5.CommandParameter = "^C^C_F5";
F5.CommandHandler = new AssignCommandCmd("F5");
F5.Image = Properties.Resources.ID_ACETALIAS.ToImageSource();
F5.LargeImage = Properties.Resources.ID_ACETALIAS.ToImageSource();
F6 = new RibbonButton();
F6.Id = "cmd_F6";
F6.Text = "COPYHIST";
//F6.Size = RibbonItemSize.Large;
//F6.Orientation = System.Windows.Controls.Orientation.Vertical;
F6.ShowText = true;
F6.ShowImage = true;
F6.CommandParameter = "^C^C_F6";
F6.CommandHandler = new AssignCommandCmd("F6");
F6.Image = Properties.Resources.ID_ACETALIAS.ToImageSource();
F6.LargeImage = Properties.Resources.ID_ACETALIAS.ToImageSource();
var pt = new RibbonSplitButton();
pt.Description = "Copy history and paste into Notepad++";
pt.Text = "Copy history";
pt.ShowText = true;
pt.Size = RibbonItemSize.Large;
//pt.Image = Properties.Resources.ID_ACETALIAS.ToImageSource();
//pt.LargeImage = Properties.Resources.ID_ACETALIAS.ToImageSource();
//pt.Orientation = System.Windows.Controls.Orientation.Vertical;
//pt.ShowImage = true;
pt.IsSplit = true;
pt.Size = RibbonItemSize.Large;
pt.IsSynchronizedWithCurrentItem = true;
pt.Items.Add(F4);
pt.Items.Add(F6);
pt.Items.Add(F6);
srcPanel.Items.Add(pt);
RibbonCombo rcb = new RibbonCombo();
rcb.Description = "Copy history and paste into Notepad++";
rcb.Text = "AJS System";
rcb.ShowText = true;
RibbonButton ribBut01 = new RibbonButton();
ribBut01.Text = "www.lisp.vn";
ribBut01.Id = "Item01";
ribBut01.ShowText = true;
//ribBut01.ShowImage = true;
//_rbNewSymbol.LargeImage = Img.getBitmap(Properties.Resources.New_Symbol_32);
//_rbNewSymbol.Orientation = System.Windows.Controls.Orientation.Vertical;
RibbonButton ribBut02 = new RibbonButton();
ribBut02.Text = "www.tankhanh.com.vn";
ribBut02.Id = "Item02";
ribBut02.ShowText = true;
//ribBut01.ShowImage = true;
rcb.Items.Add(ribBut01);
rcb.Items.Add(ribBut02);
srcPanel.Items.Add(rcb);
Panel = new RibbonPanel();
Panel.Id = "ID_AJS_RibbonAddings_2024";
Panel.Source = srcPanel;
Tab.Panels.Add(Panel);
bool assign_ShortcutKey = true;
if (assign_ShortcutKey)
{
var path = Path.Combine(FolderPath, typeof(MyPlugin).Namespace + ".cuix");
Debug.Print("path=" + path);
string sMainCuiFile = (string)Application.GetSystemVariable("MENUNAME");
sMainCuiFile += ".cuix";
var oCs = new CustomizationSection(sMainCuiFile);
var ps = oCs.PartialCuiFiles;
if (File.Exists(path))
{
if (!ps.Contains(path)) Application.LoadPartialMenu(path.Replace("\\", "/"));
return;
}
var newoCs = new CustomizationSection();
newoCs.MenuGroupName = typeof(MyPlugin).Namespace;
MacroGroup mg = newoCs.MenuGroup.MacroGroups.Count > 0 ? newoCs.MenuGroup.MacroGroups[0] : new MacroGroup("ID_Debuger_MacroGroup", newoCs.MenuGroup);
MenuMacro mcr = new MenuMacro(mg, "Copy All F4", "^C^C_F4", "ID_Debuger_CMD_F4");
var newover = new MenuAccelerator(mcr, "F4", newoCs.MenuGroup);
mcr = new MenuMacro(mg, "ProgressMeter F4", "^C^C_F5", "ID_Debuger_CMD_F5");
newover = new MenuAccelerator(mcr, "F5", newoCs.MenuGroup);
mcr = new MenuMacro(mg, "COPYHIST F6", "^C^C_F6", "ID_Debuger_CMD_F6");
newover = new MenuAccelerator(mcr, "F6", newoCs.MenuGroup);
newoCs.SaveAs(path);
Debug.Print("Saveas " + path);
//ps.Add(path);
Application.LoadPartialMenu(path.Replace("\\", "/"));
}
}
}
else
{
Debug.Print("Tab=null + ID_ADDINSTAB + RBN_00012112", Caption);
foreach (var t in ribbonControl.Tabs)
{
Debug.Print(" tab Title=" + t.Title + " Id=" + t.Id + " Name=" + t.Name + " AutomationName=" + t.AutomationName);
}
}
}
public static RibbonButton F4 = null;
public static RibbonButton F5 = null;
public static RibbonButton F6 = null;
}
}
Đoạn mã myPluggin.cs sẽ khởi tạo ngay khi Ribbon được mở lên. Tuy nhiên, nếu cần khởi tạo Panel bằng lệnh, có thể thêm đoạn mã sau trong myCommands.cs
(Copy nội dung sau)
Code:
// (C) Copyright 2024 by www.lisp.vn
//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AJS_RibbonAddings.MyCommands))]
namespace AJS_RibbonAddings
{
// This class is instantiated by AutoCAD for each document when
// a command is called by the user the first time in the context
// of a given document. In other words, non static data in this class
// is implicitly per-document!
public class MyCommands
{
// Modal Command with localized name
[CommandMethod("AJSRibbonAddings", CommandFlags.Modal)]
public void MyCommand() // This method can have any name
{
// Put your command code here
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed;
if (doc != null)
{
ed = doc.Editor;
ed.WriteMessage("Hello, this is your first command.");
MyPlugin.ToggleButton();
}
}
}
}
Kết quả của đoạn mã
Link tải OpenSource
https://github.com/AutoLISP-Just-Simple/AJS-RibbonAddings---------------------------------------------------------------------------------------------
Mọi thông tin xin liên hệ Fanpage AutoLISP Thật là đơn giản!
Cảm ơn bạn đã theo dõi!
Không có nhận xét nào:
Đăng nhận xét