修改命名空间
This commit is contained in:
183
ShrlAlgoToolkit.RevitCore/Assists/OptionsBarAssist.cs
Normal file
183
ShrlAlgoToolkit.RevitCore/Assists/OptionsBarAssist.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using UIFramework;
|
||||
|
||||
namespace ShrlAlgoToolkit.RevitCore.Assists;
|
||||
|
||||
#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 中找到内部工具面板");
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
_panelPresenter.Visibility = Visibility.Visible;
|
||||
//设为0,隐藏内部的Panel
|
||||
InternalToolPanel.Height = 0;
|
||||
return;
|
||||
}
|
||||
//第一次执行时,会创建
|
||||
_panelPresenter = CreateOptionsBar();
|
||||
_panelPresenter.Content = content;
|
||||
|
||||
InternalToolPanel.Height = 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// 关闭、释放
|
||||
/// </summary>
|
||||
public static void CloseOptionsBar()
|
||||
{
|
||||
if (_panelPresenter is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_panelPresenter.Content = null;
|
||||
_panelPresenter.Visibility = Visibility.Collapsed;
|
||||
|
||||
InternalToolPanel.Height = 26;
|
||||
}
|
||||
/// <summary>
|
||||
/// 隐藏
|
||||
/// </summary>
|
||||
public static void HideOptionsBar()
|
||||
{
|
||||
if (_panelPresenter is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//_panelPresenter.Content = null;
|
||||
_panelPresenter.Visibility = Visibility.Collapsed;
|
||||
|
||||
InternalToolPanel.Height = 26;
|
||||
}
|
||||
/// <summary>
|
||||
/// 显示
|
||||
/// </summary>
|
||||
public static void ShowOptionsBar()
|
||||
{
|
||||
if (_panelPresenter is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//_panelPresenter.Content = null;
|
||||
_panelPresenter.Visibility = Visibility.Visible;
|
||||
|
||||
InternalToolPanel.Height = 0;
|
||||
}
|
||||
public static async void CloseOptionsBarAsync(TimeSpan delay)
|
||||
{
|
||||
await Task.Delay(delay);
|
||||
try
|
||||
{
|
||||
CloseOptionsBar();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user