今天在群里看到一位群友有这样的一个需求,需要在插件的非模态窗口Datagrid控件中列出当前视图的所有图元,然后在窗口中选中一项的同时
选中Revit中的Element。或者在Revit中选择一个Element,让插件窗口的Datagrid选中该项。这个需求实现的大致思路就是非模态加空闲事件,空闲事件里判断当Selection里新的ElementId是否和以前的ElementId相等。废话不多说,直接代码了,WPF 非mvvm模式
WPF部分
<Window x:Class="RevitTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:RevitTest"
mc:Ignorable="d"
Height="500" Width="300">
<Grid>
<DataGrid Margin="5"
x:Name="datagrid"
SelectionMode="Single"
AutoGenerateColumns="False"
ItemsSource="{Binding}"
SelectionChanged="Datagrid_SelectionChanged"
>
<DataGrid.Columns>
<DataGridTextColumn Header="ElementName"
Binding="{Binding Name}"
Width="*" IsReadOnly="True"/>
<DataGridTextColumn Header="ElementType"
Binding="{Binding Type}"
Width="*" IsReadOnly="True"/>
<DataGridTextColumn Header="Id" Width="*"
Binding="{Binding Id}"
IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace RevitTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public UIDocument Uidoc { get; }
public Document Doc { get; }
public List<ElementModel> ElementList { get; set; }
public MainWindow(ExternalCommandData commandData)
{
InitializeComponent();
Uidoc = commandData.Application.ActiveUIDocument;
Doc = Uidoc.Document;
InitializeData();
}
private void InitializeData()
{
IEnumerable<Element> elements = new FilteredElementCollector(Doc, Uidoc.ActiveView.Id).WherePasses(new ElementIsElementTypeFilter(true)).
Where(e => e.Category.CategoryType == CategoryType.Model);
ElementList = new List<ElementModel>();
foreach (var e in elements)
{
ElementType type = Doc.GetElement(e.GetTypeId()) as ElementType;
ElementList.Add(new ElementModel(e.Name, type?.Name, e.Id.IntegerValue));
}
datagrid.DataContext = ElementList;
}
private void Datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
List<ElementId> elementIds = new List<ElementId>();
foreach (var item in e.AddedItems)
{
if (item is ElementModel elementModel)
{
elementIds.Add(new ElementId(elementModel.Id));
}
}
if (elementIds?.Any() ?? false)
{
Uidoc.Selection.SetElementIds(elementIds);
}
}
}
}
抽象的实体
namespace RevitTest
{
public class ElementModel
{
public string Name { get; set; }
public string Type { get; set; }
public int Id { get; set; }
public ElementModel(string name, string type, int id)
{
Name = name;
Type = type;
Id = id;
}
}
}
IExternalCommand
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Interop;
namespace RevitTest
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
private UIDocument uidoc;
private Document doc;
private ElementId selectedId = null;
private UIApplication uiApp;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
uiApp = commandData.Application;
uidoc = commandData.Application.ActiveUIDocument;
doc = uidoc.Document;
MainWindow window = new MainWindow(commandData);
IntPtr revitPtr = Autodesk.Windows.ComponentManager.ApplicationWindow;
WindowInteropHelper helper = new WindowInteropHelper(window)
{
Owner = revitPtr
};
uiApp.Idling += (sender, args) =>
{
UIApplication uiapp = sender as UIApplication;
UIDocument uidoc = uiapp.ActiveUIDocument;
ElementId newId = uidoc.Selection.GetElementIds().FirstOrDefault();
if (!(uidoc.Selection.GetElementIds().Count > 1) && newId != selectedId)
{
List<ElementModel> elementModels = window.datagrid.DataContext as List<ElementModel>;
window.datagrid.SelectedItem = elementModels.Find(e => e.Id == newId.IntegerValue);
}
};
window.Show();
return Result.Succeeded;
}
}
}
标签:插件,get,System,public,new,二次开发,using,Revit From: https://www.cnblogs.com/youngala/p/17237264.html