添加项目文件。
This commit is contained in:
147
Szmedi.RvKits/ModelManager/DetailSelectFiltersViewModel.cs
Normal file
147
Szmedi.RvKits/ModelManager/DetailSelectFiltersViewModel.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using Nice3point.Revit.Toolkit.External.Handlers;
|
||||
|
||||
|
||||
namespace Szmedi.RvKits.ModelManager
|
||||
{
|
||||
public partial class DetailSelectFiltersViewModel : ObservableObject
|
||||
{
|
||||
|
||||
[ObservableProperty]
|
||||
ICollectionView filteredList;
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<SelectionElement> items;
|
||||
[ObservableProperty]
|
||||
private string searchProp;
|
||||
private readonly UIApplication uiapp;
|
||||
|
||||
public DetailSelectFiltersViewModel(UIApplication uiapp)
|
||||
{
|
||||
var elemIds = uiapp.ActiveUIDocument.Selection.GetElementIds();
|
||||
Items = [];
|
||||
this.uiapp = uiapp;
|
||||
var doc = uiapp.ActiveUIDocument.Document;
|
||||
foreach (var elementId in elemIds)
|
||||
{
|
||||
//var elem = doc.GetElement(elementId);
|
||||
//var param = elem.GetParameters("ID-100-编号").FirstOrDefault();
|
||||
//if (param != null && param.AsString() == string.Empty)
|
||||
//{
|
||||
// Items.Add(doc.GetElement(elementId));
|
||||
//}
|
||||
var element = doc.GetElement(elementId);
|
||||
if (element.IsValidObject)
|
||||
{
|
||||
Items.Add(new SelectionElement(element));
|
||||
}
|
||||
}
|
||||
FilteredList = CollectionViewSource.GetDefaultView(Items);
|
||||
|
||||
}
|
||||
|
||||
partial void OnSearchPropChanged(string value)
|
||||
{
|
||||
FilteredList.Filter = o =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(SearchProp))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
var item = o as SelectionElement;
|
||||
return item?.Name != null && item.Name.Contains(SearchProp)
|
||||
|| item?.Number != null && item.Number.Contains(SearchProp)
|
||||
|| item?.Comment != null && item.Comment.Contains(SearchProp);
|
||||
};
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void ShowElement(object obj)
|
||||
{
|
||||
if (obj is not SelectionElement selection) return;
|
||||
var model = selection.Element;
|
||||
var uidoc = uiapp.ActiveUIDocument;
|
||||
if (model.IsValidObject)
|
||||
{
|
||||
ZoomElement(model, uidoc);
|
||||
}
|
||||
else
|
||||
{
|
||||
Items.Remove(selection);
|
||||
}
|
||||
}
|
||||
|
||||
private void ZoomElement(Element model, UIDocument uidoc)
|
||||
{
|
||||
var views = uidoc.Document.OfClass<View>().OfType<View>().Where(v => !v.IsTemplate);
|
||||
View targetView = null;
|
||||
if (model.IsVisible(uidoc.ActiveView))
|
||||
{
|
||||
targetView = uidoc.ActiveView;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var v in views)
|
||||
{
|
||||
if (model.IsVisible(v))
|
||||
{
|
||||
targetView = v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (targetView == null)
|
||||
{
|
||||
MessageBox.Show("未找到可见的视图", "错误");
|
||||
return;
|
||||
}
|
||||
uidoc.ActiveView = targetView;
|
||||
|
||||
targetView.ZoomElement(uidoc, model.Id);
|
||||
uidoc.Selection.SetElementIds([model.Id]);
|
||||
}
|
||||
}
|
||||
|
||||
public class SelectionElement(Element element)
|
||||
{
|
||||
public string CategoryName => element.Category?.Name;
|
||||
|
||||
public string Comment => element.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS)?.AsString();
|
||||
public Element Element => element;
|
||||
public string HostName => element is FamilyInstance { Host: not null } instance ? instance.Host.Name : null;
|
||||
public ElementId Id => element.Id;
|
||||
public string LevelName => element.LevelId != ElementId.InvalidElementId ? (doc.GetElement(element.LevelId)?.Name) : null;
|
||||
|
||||
public string MEPSystemName => element is MEPCurve mep ? doc.GetElement(mep.MEPSystem.GetTypeId()).Name : null;
|
||||
public string Name => element.Name;
|
||||
public string Number => element.get_Parameter(BuiltInParameter.DOOR_NUMBER)?.AsString();
|
||||
|
||||
public string Offset =>
|
||||
element is FamilyInstance instance
|
||||
? (instance.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM)?.AsValueString())
|
||||
: null;
|
||||
|
||||
public string Phase => element.CreatedPhaseId == ElementId.InvalidElementId ? null : doc.GetElement(element.CreatedPhaseId).Name;
|
||||
|
||||
public string ReferenceLevel =>
|
||||
element is MEPCurve mep
|
||||
? mep.ReferenceLevel.Name
|
||||
: (element.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM)?.AsValueString());
|
||||
|
||||
public string RoomName => element is FamilyInstance instance && instance.Room != null ? instance.Room.Name : null;
|
||||
|
||||
private readonly Document doc = element.Document;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user