Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Swap Parameters in a Schedule

0 REPLIES 0
Reply
Message 1 of 1
skeletank
491 Views, 0 Replies

How to Swap Parameters in a Schedule

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

0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community