To execute the python code to open the fusion 360 file

To execute the python code to open the fusion 360 file

s9anturu
Explorer Explorer
2,828 Views
5 Replies
Message 1 of 6

To execute the python code to open the fusion 360 file

s9anturu
Explorer
Explorer
 
I am Turumella Anil Yasasvi. I am studying master studies and currently working on a project using Fusion 360, The main objective is to trigger the parametertic model through python code.
 
But I am facing the issues while handling it, could you please help me out with the following issues:
 
 
  • The code I have programmed is:
import os
import adsk.core, adsk.fusion, traceback

 
# Create an instance of the Fusion 360 application
app = adsk.core.Application.get()
if not app:
    app = adsk.core.Application.create()
 
# Get the user interface of the application
ui = app.userInterface
 
# Define the file path of the Fusion 360 CAD model to open    (*** REPLACE THE file_path WITH CORRESPONDING FILE PATH ON YOUR COMPUTER)
file_path = 'C:/Users/Anil Yasasvi/Desktop/Parameteric_Model_1 v1.f3d'

# Open the Fusion 360 CAD model
doc = app.documents.open(file_path, True)
 
# Get the root component of the active design
design = adsk.fusion.Design.cast(doc)
print(design)
os.system("pause")
root_comp = design.rootComponent
 
 
# Get the parameters collection of the root component
params = root_comp.modelParameters
print(params.item(1))
os.system("pause")


# Loop through each parameter and print its name and value
for param in params:
    param_name = param.name
    param_value = param.value
    print(param_name + ": " + str(param_value))
 
 
# Close the CAD model and quit Fusion 360
doc.close(False)
app.quit()
 

 

 
 
  • Here in the params it is only taking the model parameters which are unsaved while opening the model outside the Fusion 360(i.e; when it was exported as .f3d file)
  • Is there any other option to change the code to take the values from user parameters other than from modelparameters
  • Also the error I observed while running this add-in was 

    TypeError: Wrong number or type of arguments for overloaded function 'Documents_open'.

 

 
I am also attaching the parameters window for your reference, kindly help me regarding this issue.
 
With Regards
Turumella Anil Yasasvi
0 Likes
Accepted solutions (1)
2,829 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor

Hi @s9anturu .

 

Unfortunately, the create method is not implemented in the Application object.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7209c4c7-bc58-4388-9e20-036410459094 

Basically, I think the Fusion360 API is designed to be used after the software is launched.

 

I have not tried it, but here is a sample of opening a file from a web page.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7209c4c7-bc58-4388-9e20-036410459094 

0 Likes
Message 3 of 6

s9anturu
Explorer
Explorer

Thank you for the reply but both the samples are same, could you please elaborate how it works, it will be really helpful for me,

0 Likes
Message 4 of 6

BrianEkins
Mentor
Mentor
Accepted solution

Here's some code that does mostly what you want. As was said previously, the API can only be used in a running session of Fusion. It only works when using the Python that's available when running Fusion.

 

Regarding parameters, there are model and user parameters. Each component has its own set of model parameters. User parameters are shared across an entire design. Because of that, model parameters are accessed from a Component and user parameters are accessed from a Design. For convenience, there is an allParameters property on the Design that combines the user parameters with all of the model parameters in all components in the design. That's what the code below uses.

 

import adsk.core, adsk.fusion, traceback

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

        # Create an instance of the Fusion 360 application
        app = adsk.core.Application.get()
        
        # Get the user interface of the application
        ui = app.userInterface
        
        # Define the file path of the Fusion 360 CAD model to open 
        file_path = 'C:/Temp/TestPart.f3d'

        # Import the file as a new document.
        impMgr = app.importManager
        f3dImportOptions = impMgr.createFusionArchiveImportOptions(file_path)
        doc = impMgr.importToNewDocument(f3dImportOptions)

        # Get the Design product from the document
        design: adsk.fusion.Design = doc.products.itemByProductType('DesignProductType')
        
        # Get the root component of the design.
        root_comp = design.rootComponent
               
        # Get the parameters collection of the root component
        params = design.allParameters

        # Loop through each parameter and print its name and value
        for param in params:
            param_name = param.name
            param_value = param.value
            print(f'{param.name}: {param.value}')

        # Close the document.
        doc.close(False)        
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) 

 

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

s9anturu
Explorer
Explorer

Thank you for the script, I have implemented into my script, the model gets opened but I am unable to change the parameters through python script and the text command is showing script error, the script below is for your referene. Kindly let me know if the errors

 

#Author-
#Description-
import os
import adsk.core, adsk.fusion, traceback


# Get the application object
app = adsk.core.Application.get()
if app:

    # Define the file path of the Fusion 360 CAD model to open    (*** REPLACE THE file_path WITH CORRESPONDING FILE PATH ON YOUR COMPUTER)
    file_path = 'C:/Users/Anil Yasasvi/Desktop/Fusion_360/Model.f3d'

    # Import the file as a new document.
    impMgr = app.importManager
    f3dImportOptions = impMgr.createFusionArchiveImportOptions(file_path)
    doc = impMgr.importToNewDocument(f3dImportOptions)

    # Get the Design product from the document
    design: adsk.fusion.Design = doc.products.itemByProductType('DesignProductType')
 
    # Get design object
    design = adsk.fusion.Design.cast(app.activeProduct)
    if design:
        rootComp = design.rootComponent
       
        userParams = rootComp.modelParameters
       
        param = userParams.itemByName('Diameter')
       
        if param:
            # Get the current value of the parameter
            value = param.value
           
            # Set the new value of the parameter
            param.value = 30
           
            # Save the changes to the design
            doc.saveAs('Sample demonstrating watching for the save to complete.', '',"Model.f3d",'C:/Users/Anil Yasasvi/Desktop/Fusion_360' )
        else:
            print('Parameter not found')
    else:
        print('No active Fusion design')
else:
    print('No Fusion application')
 
pls help me to rectify the error
0 Likes
Message 6 of 6

kandennti
Mentor
Mentor

Hi @s9anturu .

 

If you are creating 'Diameter' as a user parameter, then do the following

・・・
                # userParams = rootComp.modelParameters
                userParams: adsk.fusion.UserParameters = design.userParameters
・・・

 

Also, the second argument of doc.saveAs should be a dataFolder object, not str.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-7b292219-62fa-4a01-b522-2a1a297edb28 

 

You probably intend to read a local file, modify it, and then export it as a local file.
In such a case, you need to use the ExportManager Object.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-bb9ca64f-0fc0-43a8-a381-076bded65744 

 

0 Likes