0

I am new to Revit API and am working in C#. I want to get the schedule element parameters value using C#. I used the below code to get the view schedule.

var viewSchedule = new FilteredElementCollector(document)
                                    .OfClass(typeof(ViewSchedule))
                                    .FirstOrDefault(e => e.Name == "MyScheduleName") as ViewSchedule;

Schedule Element Data

From the above schedule, I used the below code to get the element data (please refer the above screenshot link) but it taking long time to reflect the output (10 to 15 seconds).

var rowCount = viewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfRows;
            var colCount = viewSchedule.GetTableData().GetSectionData(SectionType.Body).NumberOfColumns;

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    data += viewSchedule.GetCellText(SectionType.Body, i, j);
                }
            }

Please let me know is there any alternate approach to get the schedule data using C#. Thanks in advance.

Philip S
  • 21
  • 1
  • 7
  • What's the purpose of this? I am asking because you can get the "displayed" data using the method that Jeremy suggested below by simply exporting the data to a tabular format. If you want to get access to the underlying elements (remember that a schedule is just a tabular representation of information contained in elements) there are other ways. Are you looking to get a value of specific parameter (column) for all elements in the schedule? – konrad Jul 25 '18 at 17:33

2 Answers2

0

Maybe you can also use ViewSchedule.Export as demonstrated by The Building Coder discussing The Schedule API and Access to Schedule Data.

Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17
0

Yes, you can easily access Schedule data without exporting. Firstly, get all the schedules and read the data cell by cell. Secondly, create dictionary and store data in form of key, value pairs. Now you can use the schedule data as you want. I have tried this in Revit 2019.
Here is the implementation.

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

        String prompt = "ScheduleData :";
        prompt += Environment.NewLine;

        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)
            {
                //valueData.Add(viewSchedule.Name);

                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);
    }
Mah Noor
  • 83
  • 1
  • 11