Open file that was just created in folder.

Open file that was just created in folder.

ebunn3
Advocate Advocate
995 Views
3 Replies
Message 1 of 4

Open file that was just created in folder.

ebunn3
Advocate
Advocate

Hi,

 

I'm working on code to have the user select a body to make a copy of in the parent directory, which works, and then have it open the file.  Problem is there seems to be a delay necessary to do this?  Is there a way of refreshing the directory first before you try to open the copied file?  See inserted code below.  The code works up until the line:  adsk.core.Application.get().documents.open(targetDatafile).  The line crashes because targetDataFile is None.  The file exists but it doesn't come up in the dataFiles loop. 

 

If I run just the function openDataFile a second time by manually passing in the name it will find the file and open it.

 

 

Eric

#Author-Bunn
#Description-Select Entities on screen and return the filtered selection.  Does one at a time only.

import adsk.core, adsk.fusion, adsk.cam, traceback




def selectEnt(msg: str,filtterStr: str) -> adsk.core.Selection:
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        sel = ui.selectEntity(msg, filtterStr)
        #get the BRep from the selection (Programming Interface  Fusion 360 API User's Manual  Selection Filters)
        if filtterStr == 'Faces':
            face = adsk.fusion.BRepFace.cast(sel.entity)
            return face
        elif  filtterStr == 'Bodies':
            body = adsk.fusion.BRepBody.cast(sel.entity)
            return body
        elif  filtterStr == 'SolidBodies':
            body = adsk.fusion.BRepBody.cast(sel.entity)
            return body
        elif  filtterStr == 'Edges':
            body = adsk.fusion.BRepEdge.cast(sel.entity)
            return body
        else:
            return sel
    except:
        return None

def getDataFolder(app):
    # get active datafile
    actDataFile: adsk.core.DataFile = app.activeDocument.dataFile
    if actDataFile:
        # get the unique data file
        dataFolder: adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
        return dataFolder
    else:
        return None

def SaveCopyAs():
    ui = None
    app = adsk.core.Application.get()
    ui  = app.userInterface
    root = app.activeProduct.rootComponent
    parentName = root.name

    ui.messageBox('Select Body')
    targetBody: adsk.fusion.BRepFace = selectEnt('Select Body','SolidBodies')
    comp = targetBody.parentComponent
    name = comp.name

    #get dataFolder
    dataFolder = getDataFolder(app)
    if not dataFolder:
        ui.messageBox('Save file first.')
    else: 
        newfile = comp.saveCopyAs(parentName + " " + name + "Tray Design",dataFolder,"","")
    return parentName + " " + name + "Tray Design"

def openDataFile(file):
    app = adsk.core.Application.get()
    #Open File
    # get datafile ID
    actId = app.activeDocument.dataFile.id
    # get the unique data file
    dataFolder: adsk.core.DataFolder = app.activeDocument.dataFile.parentFolder
    targetDatafile: adsk.core.DataFile = None
    #this loads an array with all the files in the parent folder.  This could be used to populate a list box for user selection.
    dataFiles = dataFolder.dataFiles.asArray()
    for df in dataFiles:
        if df.id != actId and df.fileExtension == 'f3d':
            print(df.name)
            
    for df in dataFiles:
        if df.id != actId and df.fileExtension == 'f3d' and df.name == file:
            targetDatafile = df
            break
    adsk.core.Application.get().documents.open(targetDatafile) 


copyName = SaveCopyAs()

# file = 'Nesting Example v4 EndCapTray Design'
openDataFile(copyName)
0 Likes
996 Views
3 Replies
Replies (3)
Message 2 of 4

BrianEkins
Mentor
Mentor

This is a problem with the current API.  When you save the file, it immediately returns but because there is still work happening on the cloud to save the file, the file doesn't exist yet.  There's no way to know if the file is available except to keep trying until it no longer fails.  The saveCopyAs method should return a future object instead of a file.  This would allow you to query and ask if the file is available or not and if it is, get the available file. This has come up before and there has been talk of adding support for this but it's not currently available.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 4

sfmissionsf
Explorer
Explorer

Hi, 
I am encountering the same problem. 
I want to check if  the component.saveCopyAs is done so I can link the saved file to my open document.

Could you please share a script that does that.

 

Thanks

0 Likes
Message 4 of 4

BrianEkins
Mentor
Mentor

Some new functionality was added to the API in the January update of this year. The attached script file demonstrates how to use the dataFileComplete event to know when the file has been fully saved on Fusion Team and you can insert it into a document.

 

To use the sample, have any design open, that has been saved, and run the sample. It will create a new design that contains a cylinder, save it in the same folder as the active design and then insert it into the active design.

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