嘿大家,
我正在为 Autodesk Navisworks 开发一个插件:用户在文本框中键入类别类型、属性类型和关键字的名称,然后单击“自定义”按钮,从文件树中搜索和收集匹配项。
以下代码适用于“小”文件树(数百个项目),但是当我启动对整个文件(包含数千个项目)的搜索时,程序冻结并使用所有可用的物理内存。我想知道如何改进我的代码以便它可以处理更大的文件?
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using Autodesk.Navisworks.Api;
using NavisworksApp = Autodesk.Navisworks.Api.Application;
namespace ClashPlugin.Ctr
{
public partial class ClashPlugin : UserControl
{
public ClashPlugin()
{
InitializeComponent();
}
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
Dock = DockStyle.Fill;
}
private static string GetPropValue(DataProperty prop)
{
try
{
return prop.Value.IsDisplayString ? prop.Value.ToDisplayString() : prop.Value.ToString().Split(':')[1];
}
catch (Exception)
{
return "Prop Error";
}
}
private void btCustom_MouseUp(object sender, MouseEventArgs e)
{
//Find all
try
{
var result = new List<ModelItem>();
foreach (var item in NavisworksApp.ActiveDocument.CurrentSelection.SelectedItems)
{
var cat = item.DescendantsAndSelf.Where(i => i.PropertyCategories.FindCategoryByDisplayName(tbCatName.Text) != null);
var pro = cat.Where(m => m.PropertyCategories.FindCategoryByDisplayName(tbCatName.Text).Properties.FindPropertyByDisplayName(tbPropName.Text) != null);
result.AddRange(pro.Where(m => GetPropValue(m.PropertyCategories.FindCategoryByDisplayName(tbCatName.Text).Properties.FindPropertyByDisplayName(tbPropName.Text)).Contains(tbValueName.Text)));
}
NavisworksApp.ActiveDocument.CurrentSelection.Clear();
NavisworksApp.ActiveDocument.CurrentSelection.AddRange(result);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}