如何通过RevitPythonShell/IronPython访问活动Revit明细表中的字段?

2024-10-16 17:22:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为Revit 2016编写IronPython脚本。首先,我尝试访问活动Revit明细表中的值(作为文本),并将它们加载到变量中。对于非计算值,这种方法非常有效。在

但是,我的一些日程表字段是计算出来的。下面是一个示例计划(此处的所有值均已计算):

Schedule Snippet

Revit API显示了两个方法,分别名为TableView.GetCalculatedValueName()^{},我想使用它们,但似乎并不像宣传的那样有效。在

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

schedule = doc.ActiveView
tableData = schedule.GetTableData()
print(tableData)

tableName = schedule.GetCellText(SectionType.Header,0,0)
qty = schedule.GetCalculatedValueText(SectionType.Body,4,1)
calcValName = schedule.GetCalculatedValueName(SectionType.Body,4,1)
print(tableName)
print("Calculated Qty is: " + qty)
print("Calculated Value Name is: " + calcValName)

运行此代码(在Revit中)将生成以下输出:

^{pr2}$

我想指出的是,使用TableView.GetCellText()实际上对计算值有效,但我真正想在这里使用GetCalculatedValueName()。在


Tags: 方法docbodyscheduleqtyprinttablenamerevit
1条回答
网友
1楼 · 发布于 2024-10-16 17:22:58

我也做了同样的事情,但在c#为Revit 2019。我希望你能理解。在

可以访问明细表数据的值,而无需导出。首先,获取所有的调度表,逐单元读取数据。其次,创建字典并以键、值对的形式存储数据。现在可以根据需要使用明细表数据。我已经在Revit 2019中尝试过了。 这是实现。在

public void getScheduleData(Document doc)
{
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    IList<Element> collection = collector.OfClass(typeof(ViewSchedule)).ToElements();

    foreach (Element e in collection)
    {
        ViewSchedule viewSchedule = e as ViewSchedule;
        TableData table = viewSchedule.GetTableData();
        TableSectionData section = table.GetSectionData(SectionType.Body);
        int nRows = section.NumberOfRows;
        int nColumns = section.NumberOfColumns;

        if (nRows > 1)
        {
            List<List<string>> scheduleData = new List<List<string>>();
            for (int i = 0; i < nRows; i++)
            {
                List<string> rowData = new List<string>();

                for (int j = 0; j < nColumns; j++)
                {
                    rowData.Add(viewSchedule.GetCellText(SectionType.Body, i, j));
                }
                scheduleData.Add(rowData);
            }

            List<string> columnData = scheduleData[0];
            scheduleData.RemoveAt(0);

            DataMapping(columnData, scheduleData);
        }
    }
}

public static void DataMapping(List<string> keyData, List<List<string>>valueData)
{
    List<Dictionary<string, string>> items= new List<Dictionary<string, string>>();

    string prompt = "Key/Value";
    prompt += Environment.NewLine;

    foreach (List<string> list in valueData)
    {
        for (int key=0, value =0 ; key< keyData.Count && value< list.Count; key++,value++)
        {
            Dictionary<string, string> newItem = new Dictionary<string, string>();

            string k = keyData[key];
            string v = list[value];
            newItem.Add(k, v);
            items.Add(newItem);
        }
    }

    foreach (Dictionary<string, string> item in items)
    {
        foreach (KeyValuePair<string, string> kvp in item)
        {
            prompt += "Key: " + kvp.Key + ",Value: " + kvp.Value;
            prompt += Environment.NewLine;
        }
    }

    Autodesk.Revit.UI.TaskDialog.Show("Revit", prompt);
}

相关问题 更多 >