Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

How to programmatically import local Fusion file to Fusion

Anonymous

How to programmatically import local Fusion file to Fusion

Anonymous
Not applicable

Hi,

 

I am looking for the code to import my local Fusion file to Fusion cloud system so I can open it in Fusion. Could you please help?

 

Thank you.

0 Likes
Reply
Accepted solutions (1)
2,216 Views
2 Replies
Replies (2)

ekinsb
Alumni
Alumni
Accepted solution

There are actually two ways to accomplish what you want.  The first is what you suggested of uploading the file to A360 and then opening it from there.  The second is to do the equivalent of the "New Design From File" command and then save it, which will then save it to A360.  Below is some code that demonstrates uploading and then opening.

 

import adsk.core, adsk.fusion, traceback
					
def run(context):
    ui = None 
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        # Find the project by name.
        proj = adsk.core.DataProject.cast(None)
        for checkProj in app.data.dataProjects:
            if checkProj.name == 'Junk':
                proj = checkProj
                break

        # Get the root folder of the project. 
        folder = proj.rootFolder

        # If you need to save it in another folder you can use this
        # to get it by name.           
        folder = folder.dataFolders.itemByName('Samples')

        # Upload the file.  This will start the upload process.
        future = folder.uploadFile('C:\Temp\Pistons.f3d')
        
        # Monitor the upload, waiting for it to finish.
        uploading = True
        while uploading:
            if future.uploadState == adsk.core.UploadStates.UploadFinished:
                # The upload finished, so open the file.
                app.documents.open(future.dataFile)
                
                # Set the flag to break out of the loop.
                uploading = False
            elif future.uploadState == adsk.core.UploadStates.UploadFailed:
                # The upload failed for some reason.
                ui.messageBox('The upload failed.')

                # Set the flag to break out of the loop.
                uploading = False
            
            # Turn control back to Fusion to allow it to process.
            adsk.doEvents()
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

And here's an example that imports and then saves.  I think this is simpler.

 

import adsk.core, adsk.fusion, traceback

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

        # Create a new file by importing the existing file.        
        f3dImportOptions = app.importManager.createFusionArchiveImportOptions('C:\Temp\Pistons.f3d')
        newDoc = app.importManager.importToNewDocument(f3dImportOptions)

        # Find the project to save it to by name.
        proj = adsk.core.DataProject.cast(None)
        for checkProj in app.data.dataProjects:
            if checkProj.name == 'Junk':
                proj = checkProj
                break

        # Get the root folder of the project. 
        folder = proj.rootFolder

        # If you need to save it in another folder you can use this
        # to get it by name.           
        folder = folder.dataFolders.itemByName('Samples')

        newDoc.saveAs('NewPiston', folder, '', '')            
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

 

 

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
2 Likes

Anonymous
Not applicable

1. Is this only possible for  .f3d files?  if I import assembly f3z file I get an error.

 

2. How to get current active folder in datapanel (on the left)? that user select folder or create new one, and then run the script.

 

thanks

0 Likes