Set Schedule Field Index

Hey,

So I have a schedule (that someone else made) it has hundreds of parameters, every time I make a calculated field, it gets dropped at the end.

Rather than clicking Up for 10 minutes I tried to write a little python script.

There is an insert sort field method, but I couldn’t get it to work… I presume that it must be an ennumerate to have an order? I’m not very experienced with working with those unfortunately… I tried to ‘remove’ and ‘insert’ but they returned not supported?
https://www.revitapidocs.com/2020/8e9b2895-7627-7430-6db4-0ed0e25ffa60.htm

The other idea I had was Remove & Insert methods from the API… Unfortunately Insert requires a schedulable field, which a calculated field is not…

https://www.revitapidocs.com/2020/31528702-22e7-f515-5edc-9356585e3bc1.htm

#thanks All!

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
from Revit.Elements import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.GeometryReferences)

schedule = UnwrapElement(IN[0])
name = IN[1]

definit = schedule.Definition
countParameters = definit.GetFieldCount() 


TransactionManager.Instance.EnsureInTransaction(doc)

for i in range(countParameters):
    field = definit.GetField(i)
    if field.ColumnHeading == name:
        definit.RemoveField(field.FieldId)
        definit.InsertField(field, 0)

TransactionManager.Instance.TransactionTaskDone()

OUT = definit.GetFieldOrder()

image

Re Sort Schedule Columns.dyn (18.6 KB)

Any help would be greatly appreciated…

Thanks,

Mark

Hi Mark.

I think I figured it out as I got it working in a test project I made.

#thanks All!

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
from Revit.Elements import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.GeometryReferences)

schedule = UnwrapElement(IN[0])
name = IN[1]
TransactionManager.Instance.EnsureInTransaction(doc)

definit = schedule.Definition #Gets schedule definitions
field_order = definit.GetFieldOrder() #Gets schedulefieldid in current order
field_names = [definit.GetField(i).ColumnHeading for i in field_order]
ind = field_names.index(name) #Finds the index of named field
field_order_int = [v.IntegerValue for v in field_order] #Converts ordered list to ints
field_order_int.insert(0, field_order_int.pop(ind)) #Reorders so named is at 0 index
new_field_order = [ScheduleFieldId(i) for i in field_order_int] 
definit.SetFieldOrder(new_field_order) #Create and set new field order

TransactionManager.Instance.TransactionTaskDone()
field_order = [definit.GetField(v).ColumnHeading for v in definit.GetFieldOrder()]

OUT = field_order

It was able to send the calculated field to the front. The number used as the index is set in the field_order_int.insert(0, ...) line, where 0 is the number.

Explanation part below:

Going from your progress, I figured if there is a GetFieldOrder, maybe there is a SetFieldOrder and there was. It requires a list of ScheduleFieldIds but we get that from GetFieldOrder so I just need to reorder the GetFieldOrder. GetFieldOrder returns them as an IList so I originally tried to reorder it using the IList commands but got some weird error deep within the Revit config files so I turned it all into a python list of integers, reordered them, then turned the integers back into ScheduleFieldIds and it all worked.

Let me know how it goes.

2 Likes

Thank you so much! :smiley: will go stare at it for a while!

1 Like