【发布时间】:2021-09-18 15:33:30
【问题描述】:
这段代码执行了很长时间,最终没有实现它的目标——在一个时间表内组合元素的参数。这显然是由于项目中“管件”类别的大量元素而发生的。如何提高速度?是通过使用某些预定类来选择元素吗?
使用 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();
}
【问题讨论】:
-
Document是什么类型? -
显然,这就是我们使用 Revit API 的方式。上面的代码 sn-p 属于“public partial class Form1 : System.Windows.Forms.Form”这个类。这些行来自 Command.cs "Document doc = uidoc.Document; Form1 form = new Form1(doc)"
-
我猜您已经检查过性能下降实际上是在
foreach中,而不是在它上面的几行代码中。可以提高性能的一件事是不要在循环内创建新对象。您可以在循环上方创建parameterValues并在循环中清除它。我发现这样做有时会有所作为,但不能保证偏离轨道。 Transaction 对象也是如此 -
我注意到 Button 不是异步的,这可能意味着您为创建参数而读取的 comboboxX.Text 值不会改变。一定要把它从foreach(管道中的元素管道)循环中取出。
标签: c# performance winforms combobox revit-api