Search code examples
pythonrevit-apirevitpyrevit

How to reload keynotes using pyRevit


I am attempting to modify the extremely helpful open keynotes button script to create a 'reload keynotes' button.

Currently i am trying to use the Reload Method of the KeyBasedTreeEntryTable class.

kt = DB.KeynoteTable.GetKeynoteTable(revit.doc)
kt_ref = kt.GetExternalFileReference()
path = DB.ModelPathUtils.ConvertModelPathToUserVisiblePath(
    kt_ref.GetAbsolutePath()
    )

reloader = DB.KeyBasedTreeEntryTable.Reload()



if not path:
    forms.alert('No keynote file is assigned.')
else:
    reloader 

This is the error message that i am receiving.

TypeError: Reload() takes exactly 2 arguments (0 given)

Error Message Image

I am stuck here and appreciate any help.


Solution

  • You can use Revit API to reload the keynotes, the method KeyBasedTreeEntryTable.Reload just needs a parameter to store warnings thrown during operation, this parameter can be None to make it easy.

    Also KeyBasedTreeEntryTable should be an instance, the reload method is not static.

    The cool thing is that you don't need to find any KeyBasedTreeEntryTable instance, because the KeynoteTable class inherits from KeyBasedTreeEntryTable, so the Reload method is already available with the kt instance in your script.

    (This operation needs a transaction context too, like in the following examples)

    Simple way

    kt = DB.KeynoteTable.GetKeynoteTable(revit.doc)
    
    t = DB.Transaction(revit.doc)
    t.Start('Keynote Reload')
    try:
        result = kt.Reload(None)
        t.Commit()
    
    except:
        t.RollBack()
    
    forms.alert('Keynote Reloading : {}'.format(result))
    # result can be 'Success', 'ResourceAlreadyCurrent' or 'Failure'
    

    Complete way

    kt = DB.KeynoteTable.GetKeynoteTable(revit.doc)
    
    # create results object
    res = DB.KeyBasedTreeEntriesLoadResults() 
    
    t = DB.Transaction(revit.doc)
    t.Start('Keynote Reload')
    try:
        result = kt.Reload(res) # pass results object
        t.Commit()
    
    except:
        t.RollBack()
    
    # read results
    failures = res.GetFailureMessages()
    syntax_err =  res.GetFileSyntaxErrors()
    entries_err = res.GetKeyBasedTreeEntryErrors()
    # res.GetFileReadErrors() returns files errors, should be already in failures Messages
    
    warnings = ''
    warnings += '\n'.join([message.GetDescriptionText() for message in failures])
    
    if syntax_err:
        warnings += '\n\nSyntax errors in the files :\n'
        warnings += '\n'.join(syntax_err)
    
    if entries_err:
        warnings += '\nEntries with error :\n'
        warnings += '\n'.join([key.GetEntry().Key for key in entries_err])
    
    forms.alert('Keynote Reloading : {}\n{}'.format(result, warnings))