using Autodesk.Revit.UI;
using System.Drawing;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using adWin = Autodesk.Windows;
namespace ShrlAlgoToolkit.Revit.Assists
{
public class UIAssist
{
///
/// 创建ToggleButton
///
///
///
///
///
public static adWin.RibbonToggleButton NewAwToggleButton(string showText, Bitmap largeImage, string toolTip = null)
{
return new adWin.RibbonToggleButton()
{
LargeImage = ToBitmapSource(largeImage),
Size = adWin.RibbonItemSize.Large,
ToolTip = toolTip ?? showText,
ShowText = true,
Id = $"ID_{showText}",
Name = $"Tog_{showText}",
Text = showText,
IsCheckable = true,
Orientation = System.Windows.Controls.Orientation.Vertical,
};
}
///
/// 位图转像素集
///
///
///
private static BitmapSource ToBitmapSource(Bitmap bitmap)
{
return bitmap == null
? null
: Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
///
/// 创建按钮
///
///
///
///
///
/// RelayCommand
/// 命令的参数
///
///
public static adWin.RibbonButton NewAwButton(string showText, Bitmap largeImage, Bitmap image, ICommand command, object parameter, string toolTip = default)
{
return new()
{
Image = ToBitmapSource(image),
LargeImage = ToBitmapSource(largeImage),
Size = adWin.RibbonItemSize.Standard,
ShowText = true,
Id = $"ID_{showText}",
Name = $"Btn_{showText}",
ToolTip = toolTip ?? showText,
Text = showText,
IsCheckable = true,
CommandHandler = command,
CommandParameter = parameter,
//Orientation = System.Windows.Controls.Orientation.Vertical
};
}
private static readonly string AddInPath = Assembly.GetExecutingAssembly().Location;
#region 静态方法
///
/// 新建命令按钮
///
///
///
///
///
///
///
///
public static PushButtonData NewPushButtonData(
string text,
Bitmap largeImage = null,
Bitmap image = null,
string toolTip = null,
string longDescription = null
)
where Command : class, IExternalCommand
{
return new(typeof(Command).FullName, text, AddInPath, typeof(Command).FullName)
{
LargeImage = ToBitmapSource(largeImage),
Image = ToBitmapSource(image),
ToolTip = toolTip ?? text,
LongDescription = longDescription
};
}
///
/// 新建可控的命令按钮
///
///
///
///
///
///
///
///
///
public static PushButtonData NewButtonData(
string text,
Bitmap largeBitmap = null,
Bitmap bitmap = null,
string toolTip = null,
string longDescription = null
)
where Command : class, IExternalCommand
where Availablility : class, IExternalCommandAvailability
{
return new(typeof(Command).FullName, text, AddInPath, typeof(Command).FullName)
{
LargeImage = ToBitmapSource(largeBitmap),
Image = ToBitmapSource(bitmap),
ToolTip = toolTip ?? text,
LongDescription = longDescription,
AvailabilityClassName = typeof(Availablility).FullName
};
}
#endregion
}
}