Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to programmatically import local Fusion file to Fusion

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
khoa.ho
1753 Views, 2 Replies

How to programmatically import local Fusion file to Fusion

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.

2 REPLIES 2
Message 2 of 3
ekinsb
in reply to: khoa.ho

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
Message 3 of 3
stupadmitry
in reply to: ekinsb

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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report