File Open Sample

Description

Demonstrates how to open files using the Open API and OpenUsingContext API. The sample shows how to open a file at the tip (latest version) and how to open historical versions of a file. It uses the Open API to open files at tip and iterate through historical versions, and the OpenUsingContext API to open file using FileOpenContext .

Code Samples

import adsk.core

app = adsk.core.Application.get()
ui  = app.userInterface

def run(context):
    try:
        Open()
        OpenUsingContext()      

    except Exception as e:
        message('Failed:\n{}'.format(e))

# Function to open file using Open API
def Open():
    try:
        data_file = showOpenDialog('Select a File to Open via Open API')
        if data_file:
            # Open the file at tip
            app.documents.open(data_file)

            # Open the historical changes of file
            if data_file.versions.count > 1:
                for idx, df in enumerate(data_file.versions):
                    if idx == 0:  # Skip first index since it's already opened
                        continue
                    app.documents.open(df)

            message("Successfully opened via Open API")
    except Exception as e:
        message('Failed:\n{}'.format(e))

# Function to open file using OpenUsingContext API
def OpenUsingContext():
    try:
        data_file = showOpenDialog('Select a File to Open via OpenUsingContext API')
        if data_file:
            fileOpenCtx = adsk.core.FileOpenContext.create()
            # Open the file at tip
            # Empty timestamp opens the latest version
            app.documents.openUsingContext(data_file, fileOpenCtx)

            if data_file.versions.count > 1:
                # Open the file at historical property commit
                # Get timestamp from GQLAPI
                fileOpenCtx.timestamp = '2026-01-13T05:27:24.657Z'
                app.documents.openUsingContext(data_file, fileOpenCtx)

            message("Successfully opened via OpenUsingContext API")
    except Exception as e:
        message('Failed:\n{}'.format(e))


# Function to show file open dialog and return selected DataFile
def showOpenDialog(title):
    try:
        # Set styles of file dialog.
        fileDlg = ui.createCloudFileDialog()
        fileDlg.isMultiSelectEnabled = False
        fileDlg.title = title
        fileDlg.filter = '*'
        
        # Show file open dialog
        dlgResult = fileDlg.showOpen()
        if dlgResult == adsk.core.DialogResults.DialogOK:
            # Get the selected DataFile
            data_file = fileDlg.dataFile
            if data_file:
                return data_file
            else:
                message("No file selected")
                return None
        else:
            message("Dialog was cancelled")
            return None       

    except Exception as e:
        message('Failed:\n{}'.format(e))
        return None
      
# Utility function to display message in Fusion UI message box and console
def message(msg, displayMessage = True):
    if ui and displayMessage:
        ui.messageBox(msg)
    print(msg)