Create a Door Schedule and include FromRoom, ToRoom (and some other) properties.

Create a Door Schedule and include FromRoom, ToRoom (and some other) properties.

louisC6AEK
Participant Participant
1,150 Views
3 Replies
Message 1 of 4

Create a Door Schedule and include FromRoom, ToRoom (and some other) properties.

louisC6AEK
Participant
Participant

I'm using this code in my addin to automatically create and export a Door Schedule:
I need more information in my Schedule, so I am adding a few custom parameters to store the values that I need.

 

The 'custom' parameters that I populate are the following:

FromRoom and ToRoom Name, Number and Location

ElementId

UniqueId

TypeMark

 

Question:
How can I include the Room parameter values when I create the schedule?

Code:

                ViewSchedule schedule = new FilteredElementCollector(doc).OfClass(typeof(ViewSchedule)).Where(x => x.Name == "Sample Door Schedule").FirstOrDefault() as ViewSchedule;
                if (schedule == null)
                {
                    Transaction tSchedule = new Transaction(doc, "Create Schedule");
                    tSchedule.Start();

                    //Create an empty view schedule for doors.
                    schedule = ViewSchedule.CreateSchedule(doc, new ElementId(BuiltInCategory.OST_Doors), ElementId.InvalidElementId);
                    schedule.Name = "Sample Door Schedule";
                   

                    //Iterate all the schedulable fields gotten from the doors view schedule.
                    foreach (SchedulableField schedulableField in schedule.Definition.GetSchedulableFields())
                    {
                        //Seeif the FieldType is ScheduleFieldType.Instance.
                        if (schedulableField.FieldType == ScheduleFieldType.Instance)
                        {
                            //Get ParameterId of SchedulableField.
                            ElementId parameterId = schedulableField.ParameterId;

                            //Add a new schedule field to the view schedule by using the SchedulableField as argument of AddField method of Autodesk.Revit.DB.ScheduleDefinition class.
                            ScheduleField field = schedule.Definition.AddField(schedulableField);

                            //See if the parameterId is a BuiltInParameter.
                            if (Enum.IsDefined(typeof(BuiltInParameter), parameterId.IntegerValue))
                            {
                                BuiltInParameter bip = (BuiltInParameter)parameterId.IntegerValue;
                                //Get the StorageType of BuiltInParameter.
                                Autodesk.Revit.DB.StorageType st = doc.get_TypeOfStorage(bip);
                                //if StorageType is String or ElementId, set GridColumnWidth of schedule field to three times of current GridColumnWidth.
                                //And set HorizontalAlignment property to left.
                                if (st == Autodesk.Revit.DB.StorageType.String || st == Autodesk.Revit.DB.StorageType.ElementId)
                                {
                                    field.GridColumnWidth = 3 * field.GridColumnWidth;
                                    field.HorizontalAlignment = ScheduleHorizontalAlignment.Left;
                                }
                                //For other StorageTypes, set HorizontalAlignment property to center.
                                else
                                {
                                    field.HorizontalAlignment = ScheduleHorizontalAlignment.Center;
                                }
                            }

                        }
                    }

                    tSchedule.Commit();
                    tSchedule.Dispose();
                  
                }
                else
                {
                    schedule.RefreshData();
                }
                opt.FieldDelimiter = "\t";
                opt.Title = false;

 

                string path = GlobalVar.folder_application + "\\Schedules";


                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);


                string file = System.IO.Path.GetFileNameWithoutExtension(doc.PathName) + ".csv";

                schedule.Export(path, file, opt);

 

                //UIApplication uiapp = GlobalVar.ThisApplication;
                //UIDocument uidoc = uiapp.ActiveUIDocument;
                //uidoc.ActiveView = schedule;

 

                //Show the schedule in Excel

                GlobalVar.StartDoc(path + "\\" + file);


            }

0 Likes
Accepted solutions (1)
1,151 Views
3 Replies
Replies (3)
Message 2 of 4

Mustafa.Salaheldin
Collaborator
Collaborator
Accepted solution

Please find attached the complete solution for your question

 

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;

//using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace Toolbox
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class DoorSchedule : IExternalCommand
    {
        #region Cached Variables

        private static ExternalCommandData _cachedCmdData;

        public static UIApplication CachedUiApp
        {
            get
            {
                return _cachedCmdData.Application;
            }
        }

        public static RvtApplication CachedApp
        {
            get
            {
                return CachedUiApp.Application;
            }
        }

        public static RvtDocument CachedDoc
        {
            get
            {
                return CachedUiApp.ActiveUIDocument.Document;
            }
        }

        #endregion

        #region IExternalCommand Members         

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                //TODO: add your code below.
                ViewSchedule schedule = new FilteredElementCollector(CachedDoc).OfClass(typeof(ViewSchedule)).Where(x => x.Name == "Sample Door Schedule").FirstOrDefault() as ViewSchedule;
                if (schedule == null)
                {
                    Transaction tSchedule = new Transaction(CachedDoc, "Create Schedule");
                    tSchedule.Start();

                    //Create an empty view schedule for doors.
                    schedule = ViewSchedule.CreateSchedule(CachedDoc, new ElementId(BuiltInCategory.OST_Doors), ElementId.InvalidElementId);
                    schedule.Name = "Sample Door Schedule";


                    //Iterate all the schedulable fields gotten from the doors view schedule.
                    foreach (SchedulableField schedulableField in schedule.Definition.GetSchedulableFields())
                    {
                        //See if the FieldType is ScheduleFieldType.Instance.
                        if (schedulableField.FieldType == ScheduleFieldType.FromRoom || schedulableField.FieldType == ScheduleFieldType.ToRoom)
                        {
                            //Get ParameterId of SchedulableField.
                            ElementId parameterId = schedulableField.ParameterId;

                            //Add a new schedule field to the view schedule by using the SchedulableField as argument of AddField method of Autodesk.Revit.DB.ScheduleDefinition class.
                            ScheduleField field = schedule.Definition.AddField(schedulableField);

                            //See if the parameterId is a BuiltInParameter.
                            if (Enum.IsDefined(typeof(BuiltInParameter), parameterId.IntegerValue))
                            {
                                BuiltInParameter bip = (BuiltInParameter)parameterId.IntegerValue;
                                //Get the StorageType of BuiltInParameter.
                                Autodesk.Revit.DB.StorageType st = CachedDoc.get_TypeOfStorage(bip);
                                //if StorageType is String or ElementId, set GridColumnWidth of schedule field to three times of current GridColumnWidth.
                                //And set HorizontalAlignment property to left.
                                if (st == Autodesk.Revit.DB.StorageType.String || st == Autodesk.Revit.DB.StorageType.ElementId)
                                {
                                    field.GridColumnWidth = 3 * field.GridColumnWidth;
                                    field.HorizontalAlignment = ScheduleHorizontalAlignment.Left;
                                }
                                //For other StorageTypes, set HorizontalAlignment property to center.
                                else
                                {
                                    field.HorizontalAlignment = ScheduleHorizontalAlignment.Center;
                                }
                            }

                        }
                    }

                    tSchedule.Commit();
                    tSchedule.Dispose();

                }
                else
                {
                    schedule.RefreshData();
                }

                ViewScheduleExportOptions opt = new ViewScheduleExportOptions();
                opt.FieldDelimiter = "\t";
                opt.Title = false;

                string path = "D:\\Schedules";


                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);


                string file = System.IO.Path.GetFileNameWithoutExtension(CachedDoc.PathName) + ".csv";

                schedule.Export(path, file, opt);

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }
        /// 

        /// Add fields to view schedule.
        /// 

        /// List of view schedule.
        public void AddFieldToSchedule(IList schedules)
        {
            IList<SchedulableField> schedulableFields = null;

            foreach (ViewSchedule vs in schedules)
            {
                //Get all schedulable fields from view schedule definition. 
                schedulableFields = vs.Definition.GetSchedulableFields();

                foreach (SchedulableField sf in schedulableFields)
                {
                    bool fieldAlreadyAdded = false; //Get all schedule field ids IList 

                    List<ScheduleFieldId> ids = vs.Definition.GetFieldOrder().ToList();

                    foreach (ScheduleFieldId id in ids)
                    {
                        //If the GetSchedulableField() method of gotten schedule field returns same schedulable field,
                        // it means the field is already added to the view schedule. 
                        if (vs.Definition.GetField(id).GetSchedulableField() == sf)
                        {
                            fieldAlreadyAdded = true;
                            break;
                        }
                    } //If schedulable field doesn't exist in view schedule, add it. 

                    if (fieldAlreadyAdded == false)
                    {
                        vs.Definition.AddField(sf);
                    }
                }
            }
        }
        #endregion
    }
}

If this reply satisfies your need please don't forget to mark it as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 3 of 4

tim.aikinCSDW8
Observer
Observer

footnote: the using statement Autodesk.Revit.Utility

for revit 2018 +  try Autodesk.Revit.DB.Visual

0 Likes
Message 4 of 4

BenoitE&A
Collaborator
Collaborator

E


Benoit FAVRE
CEO of etudes & automates
www.etudesetautomates.com/
0 Likes