首页 > 其他分享 >Revit二次开发-Revit与非模态插件窗口中的DataGrid互选

Revit二次开发-Revit与非模态插件窗口中的DataGrid互选

时间:2023-03-20 18:22:55浏览次数:44  
标签:插件 get System public new 二次开发 using Revit

今天在群里看到一位群友有这样的一个需求,需要在插件的非模态窗口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

相关文章

  • Revit二次开发-在Revit选项卡面板添加扩展Tab
    最近在查Revitapi手册的时候,偶然发现了一个好玩的接口。这个接口用来扩展Revit选项卡的Tab,于是我就自定义了一个拓展的tab。 拓展类如下:TabbedDialogExtension这个类......
  • 【Python工具篇】几款Pycharm插件,提升开发效率
    一、安装方法先来说说插件的安装方法,一点都不难。选择顶部菜单栏的PyCharm选项,打开Preferences,点击plugins,在右侧的文本框中输入想要查看的插件名称,在下方就会罗列......
  • 使用npm发布vue插件或组件库
    1.先行再npm官网注册一个自己的npm账号;https://www.npmjs.com/2.准备插件或组件库:1)新建一个文件夹package作为写组件的地方2)package文件夹内使用npminit命令初始化包,获......
  • idea.2022.3.x社区版插件“intellij-spring-assistant”
    idea.2022.3.x社区版插件“intellij-spring-assistant”https://blog.csdn.net/ErickPang/article/details/128794674?spm=1001.2101.3001.6650.2&utm_medium=distribute.......
  • Hexo-Matery主题评论插件
    参考链接LuckLiuutterancHexo-Matery主题评论插件matery主题集成了各种评论模块,例如gitalk、gitment、disqus、livere、valine、waline、Twikoo、utteranc等,但我使......
  • Vue2 开发必备的 VSCode 插件
    10个vue2必备开发插件Vetur:Vue.js开发插件,提供了语法高亮、错误提示、自动补全等功能。Vue2Snippets:Vue.js2代码段,可以快速生成常用的Vue.js代码。ESLint:JavaScript......
  • 开发一个浏览器插件的一些技巧
    此模式的浏览器<wiz_tmp_highlight_tagclass="cm-searching">插件,并不是<wiz_tmp_highlight_tagclass="cm-searching">chrome 以及firefox模式的<wiz_tmp_highlight_ta......
  • vscode的Git History,GitLens — Git supercharged插件
    Gitsupercharged插件GitSupercharged插件是一个可以扩展和增强您的Git操作的VisualStudioCode插件。它提供了一系列实用工具,使得在VSCode中管理和使用Git变得更加高......
  • Vue插件:Vue-resource github搜索示例
     1:安装插件  vue-resourcevue的插件库,在vue1.0年代使用几率很高......
  • 谷歌游览器打包插件迁移新电脑
    前言由于换新电脑不想把盘所有文件全部移动因为有很多确实没什么用的文件当梳理一下自己有用的吧。大部分的都还好说吧,QQ、Wechat聊天记录都直接移动就行。部分软件直......