224 lines
6.5 KiB
C#
224 lines
6.5 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using Autodesk.AutoCAD.ApplicationServices;
|
|
using Autodesk.AutoCAD.Runtime;
|
|
using Autodesk.AutoCAD.Windows;
|
|
using Autodesk.Windows;
|
|
using CadParamPluging.UI;
|
|
namespace CadParamPluging
|
|
{
|
|
public class PluginEntry : IExtensionApplication
|
|
{
|
|
private static PaletteSet _palette;
|
|
private static bool _ribbonButtonCreated;
|
|
private static bool _firstShow = true;
|
|
private const string RibbonTabId = "CadParamPlugingTab";
|
|
private const string RibbonPanelId = "CadParamPlugingPanel";
|
|
private const string RibbonButtonId = "CadParamPlugingPanelButton";
|
|
|
|
public void Initialize()
|
|
{
|
|
CreateRibbonButton();
|
|
Application.Idle += OnApplicationIdle;
|
|
}
|
|
|
|
public void Terminate()
|
|
{
|
|
// 可在此释放资源或记录日志
|
|
}
|
|
|
|
private void ShowPalette()
|
|
{
|
|
if (_palette == null)
|
|
{
|
|
_palette = new PaletteSet("参数化出图")
|
|
{
|
|
Style = PaletteSetStyles.ShowCloseButton |
|
|
PaletteSetStyles.ShowAutoHideButton |
|
|
PaletteSetStyles.Snappable,
|
|
DockEnabled = DockSides.Left | DockSides.Right
|
|
};
|
|
|
|
var panel = new ParamDrawingPanel();
|
|
_palette.Add("Main", panel);
|
|
_palette.MinimumSize = new Size(240, 200);
|
|
}
|
|
|
|
_palette.Visible = true;
|
|
|
|
if (_firstShow)
|
|
{
|
|
_firstShow = false;
|
|
Application.Idle += OnDockToRight;
|
|
}
|
|
|
|
_palette.Activate(0);
|
|
}
|
|
|
|
private void OnDockToRight(object sender, EventArgs e)
|
|
{
|
|
Application.Idle -= OnDockToRight;
|
|
if (_palette != null)
|
|
{
|
|
_palette.Dock = DockSides.Right;
|
|
}
|
|
}
|
|
|
|
private void CreateRibbonButton()
|
|
{
|
|
var ribbon = ComponentManager.Ribbon;
|
|
if (ribbon == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_ribbonButtonCreated)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var tab = ribbon.Tabs.FirstOrDefault(t => t.Id == RibbonTabId);
|
|
|
|
if (tab == null)
|
|
{
|
|
tab = ribbon.Tabs.FirstOrDefault(t =>
|
|
string.Equals(t.Title, "插件", StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(t.Title, "Add-Ins", StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(t.Title, "Add-ins", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
if (tab == null)
|
|
{
|
|
tab = new RibbonTab
|
|
{
|
|
Title = "参数化出图",
|
|
Id = RibbonTabId
|
|
};
|
|
ribbon.Tabs.Add(tab);
|
|
}
|
|
|
|
var panel = tab.Panels.FirstOrDefault(p => p.Source != null && p.Source.Id == RibbonPanelId);
|
|
if (panel == null)
|
|
{
|
|
var source = new RibbonPanelSource
|
|
{
|
|
Id = RibbonPanelId,
|
|
Title = "插件"
|
|
};
|
|
panel = new RibbonPanel { Source = source };
|
|
tab.Panels.Add(panel);
|
|
}
|
|
|
|
var existingButton = panel.Source.Items
|
|
.OfType<RibbonButton>()
|
|
.FirstOrDefault(b => b.Id == RibbonButtonId);
|
|
|
|
if (existingButton == null)
|
|
{
|
|
panel.Source.Items.Clear();
|
|
|
|
var button = new RibbonButton
|
|
{
|
|
Id = RibbonButtonId,
|
|
Name = "参数面板",
|
|
Text = "参数面板",
|
|
ShowText = true,
|
|
ShowImage = true,
|
|
Size = RibbonItemSize.Large,
|
|
Orientation = System.Windows.Controls.Orientation.Vertical,
|
|
CommandHandler = new RelayCommand(ShowPalette)
|
|
};
|
|
|
|
var img = LoadImage("param_panel.png");
|
|
if (img != null)
|
|
{
|
|
button.LargeImage = img;
|
|
button.Image = img;
|
|
}
|
|
|
|
var row = new RibbonRowPanel();
|
|
row.Items.Add(button);
|
|
panel.Source.Items.Add(row);
|
|
}
|
|
|
|
tab.IsActive = true;
|
|
_ribbonButtonCreated = true;
|
|
}
|
|
|
|
private void OnApplicationIdle(object sender, EventArgs e)
|
|
{
|
|
if (_ribbonButtonCreated)
|
|
{
|
|
Application.Idle -= OnApplicationIdle;
|
|
return;
|
|
}
|
|
|
|
CreateRibbonButton();
|
|
}
|
|
|
|
private static ImageSource LoadImage(string fileName)
|
|
{
|
|
try
|
|
{
|
|
var asmPath = typeof(PluginEntry).Assembly.Location;
|
|
var dir = Path.GetDirectoryName(asmPath);
|
|
if (string.IsNullOrEmpty(dir))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var fullPath = Path.Combine(dir, fileName);
|
|
if (!File.Exists(fullPath))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var img = new BitmapImage();
|
|
img.BeginInit();
|
|
img.UriSource = new Uri(fullPath, UriKind.Absolute);
|
|
img.CacheOption = BitmapCacheOption.OnLoad;
|
|
img.EndInit();
|
|
img.Freeze();
|
|
return img;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private class RelayCommand : ICommand
|
|
{
|
|
private readonly System.Action _action;
|
|
|
|
public RelayCommand(System.Action action)
|
|
{
|
|
_action = action;
|
|
}
|
|
|
|
public bool CanExecute(object parameter) => true;
|
|
|
|
public void Execute(object parameter)
|
|
{
|
|
_action?.Invoke();
|
|
}
|
|
|
|
public event System.EventHandler CanExecuteChanged
|
|
{
|
|
add { }
|
|
remove { }
|
|
}
|
|
}
|
|
|
|
[CommandMethod("PARAM_PANEL")]
|
|
public void ShowPanelCommand()
|
|
{
|
|
ShowPalette();
|
|
}
|
|
}
|
|
}
|