Set up Fusion360 Python APIs

Set up Fusion360 Python APIs

Anonymous
Not applicable
5,720 Views
14 Replies
Message 1 of 15

Set up Fusion360 Python APIs

Anonymous
Not applicable

I recently posted a question online on how to convert .sldprt file to .stl and I was advised to use the cloud environment by Fusion360 (answer by @JesusFreke and @BrianEkins ). However, since I am a complete beginner, I have no idea where to start and how to install the Python Libraries (and which libraries to install to make sure I am able to achieve the conversion). Can anyone guide me where to start. 

 

Thank you in advance.

0 Likes
Accepted solutions (2)
5,721 Views
14 Replies
Replies (14)
Message 2 of 15

JesusFreke
Advocate
Advocate
Accepted solution

You don't need to install anything. Fusion comes bundled with its own Python IDE that you can use to create, edit and run scripts. 

 

To get started, you can follow the walkthrough at http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9701BBA7-EC0E-4016-A9C8-964AA4838954

0 Likes
Message 3 of 15

BrianEkins
Mentor
Mentor

Here's a previous response of mine to a similar question that contains some code.

 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/how-to-programmatically-import-local-fusio...

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

Anonymous
Not applicable

Thanks for your response. I will install Fusion360 and get going.

0 Likes
Message 5 of 15

Anonymous
Not applicable

Thanks for help @BrianEkins . I did get a chance to look over the code. However, I still can't seem to figure out the proper flow of code. So I have a single .sldprt file which I want to convert to .stl. I have installed Fusion 360, I have run the first script (Hello Script) as guided by @JesusFreke . However, how to I proceed next. My though process is as follows:

1) Import the file into the script

2) Upload it to the Autodesk cloud

3) Tell Autodesk somehow to convert the uploaded (.sldprt) file to .slt.

4) Download the converted .sldprt file.

 

However, I don't know exactly how to do it (the Python code). I did get a chance to look at your code but I believe it imports an f3d instad of .sldprt. Plus, it assumes that I would have an autodesk 360 project folder (I only have an input .sldprt file).

 

Sorry for my lack of understanding, this is my second day working on Fusion360 and its associated APIs. Can you guide me in the right direction as to how to achieve all the above mentioned 4 steps I wrote about.

 

Thanks.

0 Likes
Message 6 of 15

BrianEkins
Mentor
Mentor
Accepted solution

The workflow in your case will be to upload the SLDPRT file using the Folder.uploadFile method that's demonstrated in the post I referenced earlier.  That uploads the file and begins the translation on the cloud.  The uploadFile method returns a DataFileFuture object.  After the upload, the file is on the queue to be translated but you don't know when that will happen.  The DataFileFuture object lets you check on the status of the translation.  Once it's complete you can access the associated DataFile object that represents the F3D file that was created from the SLDPRT file.  You can then use the Documents.open method to open that DataFile locally in Fusion.  Finally, you can now use the local STL translator in Fusion to export the design as an STL file.

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

Anonymous
Not applicable

Thanks a lot for the detailed explanation. This does makes sense. I will try that out and let you know how that worked out.

0 Likes
Message 8 of 15

Anonymous
Not applicable

Hello @BrianEkins, I tried using the methodology you suggested earlier. However I am running into an error when I try to open the converted .f3d file on my computer locally. I have included an image of error and my code. Is it possible for you to take a look at it and see what the problem is? I am following the exact methodology you explained in the answer to another link.

ErrorA360.JPG

0 Likes
Message 9 of 15

BrianEkins
Mentor
Mentor

I just tested it too and it looks like there is a bug in the Fusion API.  The DataFile object being returned is bad.  Either the underlying data the object represents isn't fully ready or it's just returning a bad object.  This will need to be fixed by the Fusion 360 team.

 

I used a workaround in the code below to use the API to get the DataFile object again and this seems to work, at least in my small testing.  I'm still not sure if the original DataFile returned is bad or not yet ready because getting it again could either be getting a good object or it's just delaying long enough for it to be ready.  Another problem though is that it's displaying the upload dialog, which it shouldn't be.

 

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

        # Upload the Solidworks part.
        proj = app.data.activeProject
        folder = proj.rootFolder
        future = folder.uploadFile('C:\\Temp\\UPPER BASE.SLDPRT')
        
        startTime = time.time()
        while future.uploadState == adsk.core.UploadStates.UploadProcessing:
            time.sleep(1)
            adsk.doEvents()
            if time.time() - startTime > 60:
                ui.messageBox('Aborting.  Translation is taking longer than 2 minutes.')
                return
            
        if future.uploadState == adsk.core.UploadStates.UploadFailed:
            ui.messageBox('Upload failed.')
            return
        elif future.uploadState == adsk.core.UploadStates.UploadFinished:
            # There's a bug that the DataFile return isn't valid.  This gets it
            # again.
            badDataFile = future.dataFile
            files = folder.dataFiles
            file = adsk.core.DataFile.cast(None)
            goodDataFile = None
            for file in files:
                if file.name == badDataFile.name:
                    goodDataFile = file
                    break
                
            doc = app.documents.open(goodDataFile, True)            
            ui.messageBox('Successfully translated the file.')
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 10 of 15

Anonymous
Not applicable

I believe it is returning a bad object. I tried the code you wrote above but I get the same error in line 

doc = app.documents.open(goodDataFile, True).

For me, the upload file dialog pops up at random (1 in 5 times approximately).

Moreover, The loop which has one minute wait time never times out, and the file gets uploaded before one minute. That makes me think a bad object is returned. I am a bit confused as to why it is working on your system.

0 Likes
Message 11 of 15

Anonymous
Not applicable

Wait, your code seems to work! It displays the file in Fusion360 now. Not sure why it gave me an error when I tried it out the first time. I will start working on the last part (Importing a .stl file using the import manager. )

0 Likes
Message 12 of 15

BrianEkins
Mentor
Mentor

I'm not sure how reliable it will be.  The behavior is flaky and there definitely is a bug that needs to be fixed on the Fusion side.

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

Anonymous
Not applicable

You are right. I did eventually accomplish the task, the script inputs a .sldprt file and outputs a .stl file. Currently, I am trying out with different file formats. I just wanted to thank you for all your help and support, without which it wouldn't have been possible.

0 Likes
Message 14 of 15

s.chand6RXFT
Explorer
Explorer

Could you please share your code. I am facing some issue.

Many Thanks

0 Likes
Message 15 of 15

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

In other thread, I used DataFile.isComplete instead of future.uploadState in the waiting loop to check for the upload status of the file.

What I found is that future.uploadState were not updated when the file was ready.

 

Regards,

Jorge