如何提高查找元素的速度



这段代码执行的时间非常长,最终无法实现其目标——将元素的参数组合到计划中。这显然是由于类别";"管件";在项目中。如何提高速度?是通过使用某个预定类来选择元素吗?

使用Win Forms GUI。

Document revitDoc { get; set; }
public Form1(Document doc)
{
InitializeComponent();
this.revitDoc = doc;
//Create a list of the parameters you want your user to choose from 
List<string> stringParameters = new List<string>
{
"GP_Description",
"GP_Model",
"GP_Angle"             
};
//Add list to comboboxes on form 
foreach (string parameterName in stringParameters)
{
comboBox1.Items.Insert(0, parameterName);
comboBox2.Items.Insert(0, parameterName);
comboBox3.Items.Insert(0, parameterName);

}
}
private void button1_Click(object sender, EventArgs e)
{
FilteredElementCollector collector = new FilteredElementCollector(revitDoc);
ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_PipeFitting);

IList<Element> ducts = collector.WherePasses(filter).WhereElementIsNotElementType().ToElements();
foreach (Element duct in ducts)
{

Parameter parameter1 = duct.LookupParameter(comboBox1.Text);
Parameter parameter2 = duct.LookupParameter(comboBox2.Text);
Parameter parameter3 = duct.LookupParameter(comboBox3.Text);

List<string> parameterValues = new List<string>();

if (parameter1 != null)
{
string parameterValue1 = parameter1.AsString();

if (parameterValue1 != "") parameterValues.Add(parameterValue1);

}

if (parameter2 != null)
{
string parameterValue2 = parameter2.AsString();

if (parameterValue2 != "") parameterValues.Add(parameterValue2);

}

if (parameter3 != null)
{
string parameterValue3 = parameter3.AsString();

if (parameterValue3 != "") parameterValues.Add(parameterValue3);
}

if (parameterValues.Count > 0) 
{

string newValue = String.Join(" ,", parameterValues);
using (Transaction t = new Transaction(revitDoc, "Set Parameter name"))
{
t.Start();
duct.LookupParameter("Outcome").Set(newValue);
t.Commit();
}

这是未经测试的。最大的项目是交易的位置。我建议用Transaction来包装foreach循环。

public void Process(Document doc, string text1, string text2, string text3)
{
FilteredElementCollector collector =
new FilteredElementCollector(doc)
.OfCategory(BuiltInCategory.OST_PipeFitting)
.WhereElementIsNotElementType();
using (Transaction t = new Transaction(doc))
{
if (t.Start("Set Parameter Names") == TransactionStatus.Started)
{
foreach (Element elem in collector)
{
string newValue = string.Empty;
Parameter outcomeParam = elem.LookupParameter("Outcome");
if (outcomeParam == null)
{
continue;
}
newValue += this.GetParameterValue(elem, text1);
newValue += this.GetParameterValue(elem, text2);
newValue += this.GetParameterValue(elem, text3);
newValue.TrimEnd(',', ' ');
outcomeParam.Set(newValue);
}
t.Commit();
}
}
}
public string GetParameterValue(Element elem, string parameterName)
{
Parameter param = elem.LookupParameter(parameterName);
return param == null ? string.Empty : param.AsString() + ", ";
}

最新更新