2024-09-22 11:05:41 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using UIFramework;
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
namespace ShrlAlgoToolkit.Revit.Assists;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
#if REVIT2020||REVIT2025
|
|
|
|
|
|
public static class OptionsBarAssist
|
|
|
|
|
|
{
|
|
|
|
|
|
private static readonly Grid RootGrid;
|
|
|
|
|
|
private static ContentPresenter _panelPresenter;
|
|
|
|
|
|
private static readonly FrameworkElement InternalToolPanel;
|
|
|
|
|
|
|
|
|
|
|
|
static OptionsBarAssist()
|
|
|
|
|
|
{
|
|
|
|
|
|
//找到整个Revit界面的Grid
|
|
|
|
|
|
RootGrid = VisualAssist.FindVisualParent<Grid>(Autodesk.Windows.ComponentManager.Ribbon, "rootGrid");
|
|
|
|
|
|
if (RootGrid is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException("无法在 Revit UI 中找到root Grid");
|
|
|
|
|
|
}
|
|
|
|
|
|
//选项栏所在的Panel,在第二行Index为1,类型是DialogBarControl
|
|
|
|
|
|
InternalToolPanel = VisualAssist.FindVisualChild<DialogBarControl>(RootGrid, string.Empty);
|
|
|
|
|
|
if (InternalToolPanel is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException("无法在 Revit UI 中找到内部工具面板");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-01 10:02:59 +08:00
|
|
|
|
public static TViewModel SetupOptionsBar<TPanel, TViewModel>()
|
|
|
|
|
|
where TViewModel : ObservableObject, new() where TPanel : Panel, new()
|
|
|
|
|
|
{
|
|
|
|
|
|
CloseOptionsBar();
|
|
|
|
|
|
var options = new TViewModel();
|
|
|
|
|
|
var view = new TPanel();
|
|
|
|
|
|
view.DataContext = options;
|
|
|
|
|
|
ShowOptionsBar(view);
|
|
|
|
|
|
return options;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void ShowInternalToolPanel()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (InternalToolPanel is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
InternalToolPanel.Visibility = Visibility.Visible;
|
|
|
|
|
|
InternalToolPanel.Height = 26;
|
|
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static void ShowOptionsBar(FrameworkElement content)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_panelPresenter is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//var actuallWidth = 0.0;
|
|
|
|
|
|
//var controlHost = VisualAssist.FindVisualChild<ControlHost>(InternalToolPanel, string.Empty);
|
|
|
|
|
|
//var size = VisualTreeHelper.GetChildrenCount(controlHost);
|
|
|
|
|
|
//for (int i = 0; i < size; i++)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// var child = VisualTreeHelper.GetChild(controlHost, 0) as FrameworkElement;
|
|
|
|
|
|
// actuallWidth += child.ActualWidth;
|
|
|
|
|
|
//}
|
|
|
|
|
|
_panelPresenter.Content = content;
|
2026-01-01 10:02:59 +08:00
|
|
|
|
_panelPresenter.Visibility = Visibility.Visible;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
//设为0,隐藏内部的Panel
|
2026-01-01 10:02:59 +08:00
|
|
|
|
InternalToolPanel.Height = 0;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
//第一次执行时,会创建
|
|
|
|
|
|
_panelPresenter = CreateOptionsBar();
|
|
|
|
|
|
_panelPresenter.Content = content;
|
|
|
|
|
|
|
2026-01-01 10:02:59 +08:00
|
|
|
|
InternalToolPanel.Height = 0;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|
2026-01-01 10:02:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 关闭、释放
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void CloseOptionsBar()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_panelPresenter is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_panelPresenter.Content = null;
|
|
|
|
|
|
_panelPresenter.Visibility = Visibility.Collapsed;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2026-01-01 10:02:59 +08:00
|
|
|
|
InternalToolPanel.Height = 26;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 隐藏
|
|
|
|
|
|
/// </summary>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static void HideOptionsBar()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_panelPresenter is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-01 10:02:59 +08:00
|
|
|
|
//_panelPresenter.Content = null;
|
|
|
|
|
|
_panelPresenter.Visibility = Visibility.Collapsed;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
InternalToolPanel.Height = 26;
|
|
|
|
|
|
}
|
2026-01-01 10:02:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 显示
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void ShowOptionsBar()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_panelPresenter is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2026-01-01 10:02:59 +08:00
|
|
|
|
//_panelPresenter.Content = null;
|
|
|
|
|
|
_panelPresenter.Visibility = Visibility.Visible;
|
|
|
|
|
|
|
|
|
|
|
|
InternalToolPanel.Height = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static async void CloseOptionsBarAsync(TimeSpan delay)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
|
|
|
|
|
await Task.Delay(delay);
|
2026-01-01 10:02:59 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CloseOptionsBar();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static ContentPresenter CreateOptionsBar()
|
|
|
|
|
|
{
|
|
|
|
|
|
const int panelRow = 1;
|
|
|
|
|
|
//在内部OptionsBar下方插入一行
|
|
|
|
|
|
RootGrid.RowDefinitions.Insert(panelRow, new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
|
|
|
|
|
|
//RootGrid.ColumnDefinitions.Insert(1,new ColumnDefinition { Width=new GridLength(1, GridUnitType.Auto) });
|
|
|
|
|
|
//把下方的所有元素都往下移动一行
|
|
|
|
|
|
foreach (UIElement child in RootGrid.Children)
|
|
|
|
|
|
{
|
|
|
|
|
|
var row = Grid.GetRow(child);
|
|
|
|
|
|
if (row > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Grid.SetRow(child, row + 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
//Grid.SetColumnSpan(child, 2);
|
|
|
|
|
|
}
|
|
|
|
|
|
var panelPresenter = new ContentPresenter();
|
|
|
|
|
|
Grid.SetRow(panelPresenter, panelRow);
|
|
|
|
|
|
//Grid.SetColumn(panelPresenter, panelRow);
|
|
|
|
|
|
RootGrid.Children.Add(panelPresenter);
|
|
|
|
|
|
|
|
|
|
|
|
return panelPresenter;
|
|
|
|
|
|
|
|
|
|
|
|
//var panelPresenter = new ContentPresenter();
|
|
|
|
|
|
//Grid.SetRow(panelPresenter, 1);
|
|
|
|
|
|
//RootGrid.Children.Add(panelPresenter);
|
|
|
|
|
|
//return panelPresenter;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 简单注册并调用
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TV"></typeparam>
|
|
|
|
|
|
/// <typeparam name="TM"></typeparam>
|
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
|
public static void RegisterOptionsBar<TV, TM>(Action<TV, TM> action)
|
|
|
|
|
|
where TV : Panel, new()
|
|
|
|
|
|
where TM : ObservableObject, new()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
TM viewModel = new();
|
|
|
|
|
|
var view = new TV() { DataContext = viewModel };
|
|
|
|
|
|
action(view, viewModel);
|
|
|
|
|
|
ShowOptionsBar(view);
|
|
|
|
|
|
}
|
2026-01-01 10:02:59 +08:00
|
|
|
|
catch (Exception ex)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
2026-01-01 10:02:59 +08:00
|
|
|
|
MessageBox.Show(ex.Message);
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|