Files
SzmediTools/Szmedi.RvKits/ModelManager/MessageViewModel.cs
2025-09-16 16:06:41 +08:00

95 lines
3.0 KiB
C#

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Nice3point.Revit.Toolkit.External.Handlers;
using System.Linq;
namespace Szmedi.RvKits.ModelManager
{
public partial class MessageViewModel : ObservableObject
{
public MessageViewModel(UIDocument uidoc, List<MessageModel> errorModels, string winTitle = "错误处理")
{
this.uidoc = uidoc;
showElementsSectionBox = new ActionEventHandler();
ErrorModels.AddRange(errorModels);
WinTitle = winTitle;
count = errorModels.Count();
}
[ObservableProperty]
private string footer;
private readonly UIDocument uidoc;
[ObservableProperty]
private List<MessageModel> errorModels = new();
[ObservableProperty]
private string winTitle;
[ObservableProperty]
private int count;
private readonly ActionEventHandler showElementsSectionBox;
[RelayCommand]
private void ShowElement(object obj)
{
if (obj is MessageModel model)
{
var doc = uidoc.Document;
//if (uidoc.ActiveView.IsTemporaryHideIsolateActive())
//{
// uidoc.ActiveView.temporary
//}
if (model.Element.IsValidObject)
{
if (model.Element.IsHidden(uidoc.ActiveView))
{
return;
}
var ids = new List<ElementId> { model.Element.Id };
//uidoc.ActiveView.IsolateElementTemporary(model.Element.ViewId);
showElementsSectionBox.Raise(_ =>
{
if (uidoc.ActiveView is not View3D view3d)
{
view3d = doc.QueryElementsByType<View3D>().FirstElement() as View3D;
}
if (!model.Element.ViewSpecific)
{
uidoc.ActiveView = view3d;
}
doc.Invoke(_ =>
{
if (uidoc.ActiveView is View3D)
{
view3d.CreateSectionBox(ids, true);
}
var box = model.Element.get_BoundingBox(uidoc.ActiveGraphicalView);
var uiView = uidoc.GetOpenUIViews().FirstOrDefault(v => v.ViewId == uidoc.ActiveGraphicalView.Id);
var extendXyz = new XYZ(1, 1, 1);
uiView.ZoomAndCenterRectangle(box.Min - extendXyz, box.Max + extendXyz);
uidoc.Selection.SetElementIds(ids);
});
});
}
else
{
ErrorModels.Remove(model);
}
}
}
}
}