export a schedule

export a schedule

Anonymous
Not applicable
1,329 Views
6 Replies
Message 1 of 7

export a schedule

Anonymous
Not applicable

Hi!

I want to export schedules to excel. User opens a schedule, clicks the button and saves this schedule to a specified folder.

I use following code:

      FilteredElementCollector col  = new FilteredElementCollector( doc ).OfClass( typeof( ViewSchedule ) );
      ViewScheduleExportOptions opt = new ViewScheduleExportOptions();
      SaveFileDialog saveFileDialog1 = new SaveFileDialog();

      saveFileDialog1.Filter = "Excel file(*.xls)|*.xls|CSV file(*.csv)|*.csv|Text file(*.txt)|*.txt";
      saveFileDialog1.FilterIndex = 1;
      saveFileDialog1.RestoreDirectory = true;

              foreach (ViewSchedule vs in col)
              {
                  string path, filename;
                  Autodesk.Revit.DB.View view = uidoc.ActiveView;

                  if (view.ViewType == ViewType.Schedule)
                  {
                      saveFileDialog1.ShowDialog();
                      path = Path.GetDirectoryName(saveFileDialog1.FileName);
                      filename = uidoc.ActiveView.Name;

                      if (saveFileDialog1.FileName != "")
                      {
                          vs.Export(path, vs.Name + _ext, opt);
                          return Result.Succeeded;
                      }
                  }
                  else
                  {
                      filename = "Choose schedule an active view";
                      TaskDialog.Show("Active view is", filename);
                      return Result.Failed;
                  }
              }
 

There is one mistake. I use foreach, so it returns and saves first schedule in my project.

But I need to save the schedule that is in active view.

How can I acheive this?

 

Thanks,

Alexander.

 

0 Likes
1,330 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(ViewSchedule));;
0 Likes
Message 3 of 7

Anonymous
Not applicable

Remy, thank you for your valuable answer.

Unfortunatelly revit doesn't return anything when I enter doc.ActiveView.Id , I cheked it within debugger. The same result occurs when I used BuiltInCategory.

FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(ViewSchedule));

 

Thanks,

Alexander

0 Likes
Message 4 of 7

Anonymous
Not applicable
this is very strange, are you sure you have selected a view ? Because the project browser is also considered as a view.
0 Likes
Message 5 of 7

arnostlobel
Alumni
Alumni

That is strange indeed that ActiveDocument would return a null. There have been some changes over the years in implementation of UIDocument.ActiveView, but I believe we have kept it backward compatible. That means the property (at least the one on UIDocument – which is, by the way, the only right object to get an active view from) should return a valid view regardless of whether the view is graphic (e.g. a Sheet) or non-graphic (e.g. the Browser). Naturally, this only applies to Revit versions where non-graphic views were publicly available (i.e. obtainable via the API); I do not think it was the original way though (like in 2012 and maybe even 2013, I do not recall that detail).

 

The sum it up, In the current version of Revit, UIDocument.ActiveView should return a valid instance of View regardles of the type o fth view, unless the document itself is not the active one in Revit. Naturally, the active view can be only had on the current active document.

 

Note: I recall a fix being made correcting the way Revit would see dockable views. Before the fix was made such views might mess with the ActiveView property. I believe we made that a hot fix in one of 2014 updates.

 

Arnošt Löbel

Autodesk, Revit R&D

Arnošt Löbel
0 Likes
Message 6 of 7

Anonymous
Not applicable

I have selected walls like in Revit API sample:

 

private void CountElements(Autodesk.Revit.DB.Document document)
{
    StringBuilder message = new StringBuilder();
    FilteredElementCollector viewCollector = 
        new FilteredElementCollector(document, document.ActiveView.Id);
    viewCollector.OfCategory(BuiltInCategory.OST_Walls);
    message.AppendLine("Wall category elements within active View: "
       + viewCollector.ToElementIds().Count);

    FilteredElementCollector docCollector = new FilteredElementCollector(document);
    docCollector.OfCategory(BuiltInCategory.OST_Walls);
    message.AppendLine("Wall category elements within document: "
       + docCollector.ToElementIds().Count);

    TaskDialog.Show("Revit", message.ToString());
}

 And in this case program works without problems. ActiveView returned valid value.

 

Regards,

Alexander.

0 Likes
Message 7 of 7

Anonymous
Not applicable

Hi,

 

Can you take your code in this totality ?

 

Thanks.

Xavier.

 
0 Likes