Revit API

Reply
Active Contributor
skeletank
Posts: 26
Registered: ‎08-25-2010
Message 1 of 1 (162 Views)

How to Swap Parameters in a Schedule

162 Views, 0 Replies
05-14-2014 01:39 PM

Recently we have been moving to using a shared parameter file that contains parameters for our Rooms. We have started integrating these parameters into our new projects. However, for older projects we were using project parameters for the same purpose. Now I would like to update some of these older projects so that they use the new shared parameters instead of the old project parameters. I have been able to swap out the values correctly on the actual elements in the project but I have not been able to 100% swap out the parameters inside of schedules.

 

I swap the parameters in the schedule by inserting the new parameter at the same index as the old, copying the properties from the old parameter to the new, then deleting the old parameter from the schedule. This all works fine except for that the group headers are lost when new parameter is inserted.

 

BEFORE                                                                                   

 

Before.png

 

AFTER 

After.png

 

This makes sense because this is the exact same thing that happens if I manually delete the old parameter and then add the new parameter using the user interface. However, I don't know how I could capture the old group header data and then recreate them using code. How could I accomplish this? 

 

private void SwapParametersInSchedule(Document document, ElementId categoryId, ElementId existingParameterId, ElementId newParameterId)
{
  FilteredElementCollector schedules = new FilteredElementCollector(document);
  schedules.OfClass(typeof(ViewSchedule));

  foreach (ViewSchedule schedule in schedules.ToElements())
  {
	if (schedule.Definition.CategoryId != categoryId)
	  continue;

	ScheduleField oldField = null;

	foreach (ScheduleFieldId fieldId in schedule.Definition.GetFieldOrder())
	{
	  ScheduleField field = schedule.Definition.GetField(fieldId);

	  if (field.ParameterId != existingParameterId)
		continue;

	  oldField = field;
	}

	if (oldField != null)
	{
	  ScheduleField newField = schedule.Definition.InsertField(oldField.FieldType, newParameterId, oldField.FieldIndex);
	  newField.ColumnHeading = oldField.ColumnHeading;
	  newField.GridColumnWidth = oldField.GridColumnWidth;

	  if (oldField.CanTotal())
		newField.HasTotals = oldField.HasTotals;

	  newField.HeadingOrientation = oldField.HeadingOrientation;
	  newField.HorizontalAlignment = oldField.HorizontalAlignment;
	  newField.IsHidden = oldField.IsHidden;

	  if (oldField.FieldType == ScheduleFieldType.Percentage)
	  {
		newField.PercentageBy = oldField.PercentageBy;
		newField.PercentageOf = oldField.PercentageOf;
	  }

	  newField.SheetColumnWidth = oldField.SheetColumnWidth;

	  if (oldField.CanTotalByAssemblyType())
		newField.TotalByAssemblyType = oldField.TotalByAssemblyType;

	  newField.SetFormatOptions(oldField.GetFormatOptions());
	  newField.SetStyle(oldField.GetStyle());

	  schedule.Definition.RemoveField(oldField.FieldId);
	}
  }
}

 

As an alternative, through the UI, I know that you can easily change the parameter that a schedule field points to by selecting one of the cells and then on the ribbon changing the parameter from a drop drown (see image below).  Is there a way to do this using the API?

 

Change Parameter.png

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.