dataFiles.itemById() doesn't return a dataFile when versionId is passed as argument

sharas_s
Contributor

dataFiles.itemById() doesn't return a dataFile when versionId is passed as argument

sharas_s
Contributor
Contributor

Hello everyone,
I am trying to retrieve a specific version of a file. For this, I tried to take specific versions of a file using dataFile.versionId and later in the code I use that versionId string in dataFile.versions.ItemById() . According to documentation that should work and give back dataFile of the specific version, right?

0 Likes
Reply
Accepted solutions (1)
437 Views
3 Replies
Replies (3)

kandennti
Mentor
Mentor

Hi @sharas_s .

 

I tried it too, but it didn't work.

Why don't you try to get the desired version of the DataFile from the versions property?

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9C14590B-1189-4A19-9438-3F6F2C9ABA85 

 

Here is a sample to get the DataFile of the previous version of the active document.

# Fusion360API Python script
import adsk.core, adsk.fusion, traceback

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app :adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface

        # get active doc datafile
        doc = app.activeDocument
        dataFile: adsk.core.DataFile = doc.dataFile

        # get previous version datafile
        previousVersionDataFile = getPreviousVersionDataFile(dataFile)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def getPreviousVersionDataFile(
    dataFile: adsk.core.DataFile) -> adsk.core.DataFile:

    currentVersion = dataFile.versionNumber
    if currentVersion < 2:
        return None
    
    dfs = [df for df in dataFile.versions if df.versionNumber == currentVersion - 1]
    if len(dfs) < 1:
        return None

    return dfs[0]

 

1 Like

BrianEkins
Mentor
Mentor
Accepted solution

I'm also able to reproduce this and it is a bug.  Hopefully, it can be fixed soon.  The workaround that @kandennti provided is a viable option until then.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
2 Likes

sharas_s
Contributor
Contributor

Thanks for the suggestion @kandennti, that was my 1st way of retrieving different versions. Just in my case, the dataFile I am working on has a lot of versions (>1000). And going through versions to take a specific one takes quite some time (>10 min) when there are so many versions.

 

I made a workaround by saving the index of a specific version in dataFile.versions and later retrieving it using dataFile.versions.item(). This works fine for now, just dataFile.versions.itemById() would be more elegant.


Thanks @BrianEkins for confirming that this is a bug.


 

1 Like